Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore on deploy #283

Merged
merged 7 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

services:
localstack:
image: localstack/localstack
image: localstack/localstack:1.4.0
env:
SERVICES: lambda,sts,s3
DEFAULT_REGION: ap-northeast-1
Expand Down
1 change: 1 addition & 0 deletions cmd/lambroll/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func _main() int {
AliasToLatest: deploy.Flag("alias-to-latest", "set alias to unpublished $LATEST version").Default("false").Bool(),
SkipArchive: deploy.Flag("skip-archive", "skip to create zip archive. requires Code.S3Bucket and Code.S3Key in function definition").Default("false").Bool(),
KeepVersions: deploy.Flag("keep-versions", "Number of latest versions to keep. Older versions will be deleted. (Optional value: default 0).").Default("0").Int(),
Ignore: deploy.Flag("ignore", `ignore function elements. jq queries joined with ",". for example, ".Foo, .Bar"`).String(),
}

rollback := kingpin.Command("rollback", "rollback function")
Expand Down
28 changes: 20 additions & 8 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"log"
"os"

"github.com/aereal/jsondiff"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/itchyny/gojq"
"github.com/pkg/errors"
)

Expand All @@ -26,6 +28,7 @@ type DeployOption struct {
DryRun *bool
SkipArchive *bool
KeepVersions *int
Ignore *string
}

func (opt DeployOption) label() string {
Expand Down Expand Up @@ -98,6 +101,22 @@ func (app *App) Deploy(opt DeployOption) error {
}
fillDefaultValues(fn)

if ignore := aws.StringValue(opt.Ignore); ignore != "" {
q, err := gojq.Parse(ignore)
if err != nil {
return errors.Wrap(err, "failed to parse ignore query")
}
q = jsondiff.WithUpdate(q)
fnAny, _ := marshalAny(fn)
fnAny, err = jsondiff.ModifyValue(q, fnAny)
if err != nil {
return errors.Wrap(err, "failed to modify function")
}
src, _ := json.Marshal(fnAny)
fn = &Function{}
unmarshalJSON(src, &fn, *opt.FunctionFilePath)
}

if err := app.prepareFunctionCodeForDeploy(opt, fn); err != nil {
return errors.Wrap(err, "failed to prepare function code for deploy")
}
Expand All @@ -107,6 +126,7 @@ func (app *App) Deploy(opt DeployOption) error {
DeadLetterConfig: fn.DeadLetterConfig,
Description: fn.Description,
EphemeralStorage: fn.EphemeralStorage,
Environment: fn.Environment,
FunctionName: fn.FunctionName,
FileSystemConfigs: fn.FileSystemConfigs,
Handler: fn.Handler,
Expand All @@ -121,14 +141,6 @@ func (app *App) Deploy(opt DeployOption) error {
ImageConfig: fn.ImageConfig,
SnapStart: fn.SnapStart,
}
if env := fn.Environment; env == nil || env.Variables == nil {
confIn.Environment = &lambda.Environment{
Variables: map[string]*string{}, // set empty variables explicitly
}
} else {
confIn.Environment = env
}

log.Printf("[debug]\n%s", confIn.String())

var newerVersion string
Expand Down
8 changes: 5 additions & 3 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/fatih/color"
"github.com/itchyny/gojq"
"github.com/kylelemons/godebug/diff"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -58,6 +57,7 @@ func (app *App) Diff(opt DiffOption) error {
packageType = *res.Configuration.PackageType
}
latestFunc := newFunctionFrom(latest, code, tags)
fillDefaultValues(latestFunc)

opts := []jsondiff.Option{}
if ignore := aws.StringValue(opt.Ignore); ignore != "" {
Expand Down Expand Up @@ -98,10 +98,12 @@ func (app *App) Diff(opt DiffOption) error {
}
newCodeSha256 := base64.StdEncoding.EncodeToString(h.Sum(nil))
prefix := "CodeSha256: "
if ds := diff.Diff(prefix+currentCodeSha256, prefix+newCodeSha256); ds != "" {
if currentCodeSha256 != newCodeSha256 {
fmt.Println(color.RedString("--- " + app.functionArn(name)))
fmt.Println(color.GreenString("+++ " + "--src=" + *opt.Src))
fmt.Println(coloredDiff(ds))
fmt.Println("@@ @@")
fmt.Println(color.RedString("-" + prefix + currentCodeSha256))
fmt.Println(color.GreenString("+" + prefix + newCodeSha256))
}
}

Expand Down
2 changes: 1 addition & 1 deletion function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestLoadFunction(t *testing.T) {
t.Errorf("unexpected SecurityGroupIds %v", fn.VpcConfig.SecurityGroupIds)
}
arch := fn.Architectures
if len(arch) != 2 || *arch[0] != "x86_64" || *arch[1] != "arm64" {
if len(arch) != 1 || *arch[0] != "x86_64" {
t.Errorf("unexpected Architectures %v", fn.Architectures)
}
if *fn.EphemeralStorage.Size != 1024 {
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/Songmu/prompter v0.5.0
github.com/aereal/jsondiff v0.2.3
github.com/aereal/jsondiff v0.2.4-0.20221231034809-2e797076aee1
github.com/alecthomas/kingpin v2.2.6+incompatible
github.com/aws/aws-sdk-go v1.44.147
github.com/fatih/color v1.13.0
Expand All @@ -15,7 +15,6 @@ require (
github.com/hashicorp/go-envparse v0.0.0-20200406174449-d9cfd743a15e
github.com/itchyny/gojq v0.12.8
github.com/kayac/go-config v0.6.0
github.com/kylelemons/godebug v1.1.0
github.com/mattn/go-isatty v0.0.14
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/errors v0.9.1
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Songmu/prompter v0.5.0 h1:uf60xlFItY5nW+rlLJ2XIUfaUReo4gUEeftuUeHpio8=
github.com/Songmu/prompter v0.5.0/go.mod h1:S4Eg25l60kPlnfB2ttFVpvBKYw7RKJexzB3gzpAansY=
github.com/aereal/jsondiff v0.2.3 h1:OldK7tcBQudPQm/wIIUVW7dErBcLEJHR23uo+hwNOXU=
github.com/aereal/jsondiff v0.2.3/go.mod h1:a5oayUivaiGHWcodFYZmy0km36ZbSfYtjuVMfKUQdbM=
github.com/aereal/jsondiff v0.2.4-0.20221231034809-2e797076aee1 h1:PrNsETLYKHeiniM9hCru9topK9bOiPjxl5xdYKLQoNo=
github.com/aereal/jsondiff v0.2.4-0.20221231034809-2e797076aee1/go.mod h1:a5oayUivaiGHWcodFYZmy0km36ZbSfYtjuVMfKUQdbM=
github.com/alecthomas/go-thrift v0.0.0-20170109061633-7914173639b2/go.mod h1:CxCgO+NdpMdi9SsTlGbc0W+/UNxO3I0AabOEJZ3w61w=
github.com/alecthomas/kingpin v2.2.6+incompatible h1:5svnBTFgJjZvGKyYBtMB0+m5wvrbUHiqye8wRJMlnYI=
github.com/alecthomas/kingpin v2.2.6+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE=
Expand Down Expand Up @@ -245,8 +245,6 @@ github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is=
github.com/lestrrat-go/strftime v1.0.1/go.mod h1:E1nN3pCbtMSu1yjSVeyuRFVm/U0xoR76fd03sz+Qz4g=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
Expand Down
7 changes: 6 additions & 1 deletion lambroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,17 @@ func fillDefaultValues(fn *Function) {
if fn.Description == nil {
fn.Description = aws.String("")
}
if fn.Environment == nil || fn.Environment.Variables == nil {
fn.Environment = &lambda.Environment{
Variables: make(map[string]*string),
}
}
if fn.MemorySize == nil {
fn.MemorySize = aws.Int64(128)
}
if fn.TracingConfig == nil {
fn.TracingConfig = &lambda.TracingConfig{
Mode: aws.String("PassThrough"),
Mode: aws.String(lambda.TracingModePassThrough),
}
}
if fn.EphemeralStorage == nil {
Expand Down
5 changes: 2 additions & 3 deletions test/function.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"Architectures": [
"x86_64",
"arm64"
"x86_64"
],
"Description": "hello function",
"EphemeralStorage": {
Expand All @@ -24,7 +23,7 @@
"Handler": "index.js",
"MemorySize": 128,
"Role": "{{ tfstate `data.aws_iam_role.lambda.arn` }}",
"Runtime": "nodejs12.x",
"Runtime": "nodejs16.x",
"Timeout": 5,
"TracingConfig": {
"Mode": "PassThrough"
Expand Down
1 change: 0 additions & 1 deletion test/function.jsonnet
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
Architectures: [
'x86_64',
'arm64',
],
Description: std.extVar('Description'),
EphemeralStorage: {
Expand Down