diff --git a/cmd/skaffold/app/cmd/init.go b/cmd/skaffold/app/cmd/init.go index 0f78ea0db38..a3f517efc73 100644 --- a/cmd/skaffold/app/cmd/init.go +++ b/cmd/skaffold/app/cmd/init.go @@ -36,6 +36,7 @@ var ( analyze bool enableJibInit bool enableBuildpacksInit bool + enableNewInitFormat bool buildpacksBuilder string ) @@ -56,6 +57,8 @@ func NewCmdInit() *cobra.Command { f.StringArrayVarP(&cliArtifacts, "artifact", "a", nil, "'='-delimited Dockerfile/image pair, or JSON string, to generate build artifact\n(example: --artifact='{\"builder\":\"Docker\",\"payload\":{\"path\":\"/web/Dockerfile.web\"},\"image\":\"gcr.io/web-project/image\"}')") f.StringArrayVarP(&cliKubernetesManifests, "kubernetes-manifest", "k", nil, "a path or a glob pattern to kubernetes manifests (can be non-existent) to be added to the kubectl deployer (overrides detection of kubernetes manifests). Repeat the flag for multiple entries. E.g.: skaffold init -k pod.yaml -k k8s/*.yml") f.BoolVar(&analyze, "analyze", false, "Print all discoverable Dockerfiles and images in JSON format to stdout") + f.BoolVar(&enableNewInitFormat, "XXenableNewInitFormat", false, "") + f.MarkHidden("XXenableNewInitFormat") f.BoolVar(&enableJibInit, "XXenableJibInit", false, "") f.MarkHidden("XXenableJibInit") f.BoolVar(&enableBuildpacksInit, "XXenableBuildpacksInit", false, "") @@ -77,6 +80,7 @@ func doInit(ctx context.Context, out io.Writer) error { Analyze: analyze, EnableJibInit: enableJibInit, EnableBuildpacksInit: enableBuildpacksInit, + EnableNewInitFormat: enableNewInitFormat || enableBuildpacksInit || enableJibInit, BuildpacksBuilder: buildpacksBuilder, Opts: opts, }) diff --git a/cmd/skaffold/app/cmd/init_test.go b/cmd/skaffold/app/cmd/init_test.go index 8eafeb09278..153e30ca467 100644 --- a/cmd/skaffold/app/cmd/init_test.go +++ b/cmd/skaffold/app/cmd/init_test.go @@ -51,10 +51,12 @@ func TestFlagsToConfigVersion(t *testing.T) { Analyze: false, EnableJibInit: false, EnableBuildpacksInit: false, + EnableNewInitFormat: false, BuildpacksBuilder: "heroku/buildpacks", Opts: opts, }, }, + { name: "no error + non-default values for flags mapped to config values", args: []string{ @@ -70,6 +72,7 @@ func TestFlagsToConfigVersion(t *testing.T) { "--analyze", "--XXenableJibInit", "--XXenableBuildpacksInit", + "--XXenableNewInitFormat", "--XXdefaultBuildpacksBuilder", "buildpacks/builder", }, expectedConfig: initializer.Config{ @@ -82,10 +85,54 @@ func TestFlagsToConfigVersion(t *testing.T) { Analyze: true, EnableJibInit: true, EnableBuildpacksInit: true, + EnableNewInitFormat: true, BuildpacksBuilder: "buildpacks/builder", Opts: opts, }, }, + + { + name: "enableJibInit implies enableNewInitFormat", + args: []string{ + "init", + "--XXenableJibInit", + }, + expectedConfig: initializer.Config{ + ComposeFile: "", + CliArtifacts: nil, + CliKubernetesManifests: nil, + SkipBuild: false, + SkipDeploy: false, + Force: false, + Analyze: false, + EnableJibInit: true, + EnableBuildpacksInit: false, + EnableNewInitFormat: true, + BuildpacksBuilder: "heroku/buildpacks", + Opts: opts, + }, + }, + { + name: "enableBuildpackInit implies enableNewInitFormat", + args: []string{ + "init", + "--XXenableBuildpacksInit", + }, + expectedConfig: initializer.Config{ + ComposeFile: "", + CliArtifacts: nil, + CliKubernetesManifests: nil, + SkipBuild: false, + SkipDeploy: false, + Force: false, + Analyze: false, + EnableJibInit: false, + EnableBuildpacksInit: true, + EnableNewInitFormat: true, + BuildpacksBuilder: "heroku/buildpacks", + Opts: opts, + }, + }, } for _, test := range tests { testutil.Run(t, test.name, func(t *testutil.T) { diff --git a/pkg/skaffold/initializer/init.go b/pkg/skaffold/initializer/init.go index f4228131c39..da45b1fdcaa 100644 --- a/pkg/skaffold/initializer/init.go +++ b/pkg/skaffold/initializer/init.go @@ -61,6 +61,7 @@ type Config struct { Analyze bool EnableJibInit bool // TODO: Remove this parameter EnableBuildpacksInit bool + EnableNewInitFormat bool BuildpacksBuilder string Opts config.SkaffoldOptions } @@ -109,8 +110,8 @@ func DoInit(ctx context.Context, out io.Writer, c Config) error { if c.Analyze { // TODO: Remove backwards compatibility block - if !c.EnableJibInit && !c.EnableBuildpacksInit { - return printAnalyzeJSONNoJib(out, c.SkipBuild, pairs, unresolvedBuilderConfigs, unresolvedImages) + if !c.EnableNewInitFormat { + return printAnalyzeOldFormat(out, c.SkipBuild, pairs, unresolvedBuilderConfigs, unresolvedImages) } return printAnalyzeJSON(out, c.SkipBuild, pairs, unresolvedBuilderConfigs, unresolvedImages) diff --git a/pkg/skaffold/initializer/init_test.go b/pkg/skaffold/initializer/init_test.go index e64ee1c0046..4a374cd2482 100644 --- a/pkg/skaffold/initializer/init_test.go +++ b/pkg/skaffold/initializer/init_test.go @@ -162,8 +162,8 @@ func TestDoInitAnalyze(t *testing.T) { name: "analyze microservices new format", dir: "testdata/init/microservices", config: Config{ - Analyze: true, - EnableJibInit: true, + Analyze: true, + EnableNewInitFormat: true, }, expectedOut: strip(`{ "builders":[ diff --git a/pkg/skaffold/initializer/json.go b/pkg/skaffold/initializer/json.go index dff11d548c8..4c80eca7d2f 100644 --- a/pkg/skaffold/initializer/json.go +++ b/pkg/skaffold/initializer/json.go @@ -25,7 +25,7 @@ import ( "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" ) -func printAnalyzeJSONNoJib(out io.Writer, skipBuild bool, pairs []builderImagePair, unresolvedBuilders []InitBuilder, unresolvedImages []string) error { +func printAnalyzeOldFormat(out io.Writer, skipBuild bool, pairs []builderImagePair, unresolvedBuilders []InitBuilder, unresolvedImages []string) error { if !skipBuild && len(unresolvedBuilders) == 0 { return errors.New("one or more valid Dockerfiles must be present to build images with skaffold; please provide at least one Dockerfile and try again, or run `skaffold init --skip-build`") } diff --git a/pkg/skaffold/initializer/json_test.go b/pkg/skaffold/initializer/json_test.go index 04401ddec78..fbb65f3440d 100644 --- a/pkg/skaffold/initializer/json_test.go +++ b/pkg/skaffold/initializer/json_test.go @@ -111,7 +111,7 @@ func TestPrintAnalyzeJSONNoJib(t *testing.T) { testutil.Run(t, test.description, func(t *testutil.T) { var out bytes.Buffer - err := printAnalyzeJSONNoJib(&out, test.skipBuild, test.pairs, test.builders, test.images) + err := printAnalyzeOldFormat(&out, test.skipBuild, test.pairs, test.builders, test.images) t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, out.String()) })