-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
skaffold inspect build-env
command (#5792)
* add `skaffold inspect build-env` command * fix typo
- Loading branch information
1 parent
b1fa526
commit 3bd827a
Showing
6 changed files
with
245 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright 2021 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/inspect" | ||
) | ||
|
||
func cmdBuildEnv() *cobra.Command { | ||
return NewCmd("build-env"). | ||
WithDescription("Interact with skaffold build environment definitions."). | ||
WithPersistentFlagAdder(cmdBuildEnvFlags). | ||
WithCommands(cmdBuildEnvList()) | ||
} | ||
|
||
func cmdBuildEnvList() *cobra.Command { | ||
return NewCmd("list"). | ||
WithExample("Get list of target build environments with activated profiles p1 and p2", "inspect build-env list -p p1,p2 --format json"). | ||
WithDescription("Print the list of active build environments."). | ||
WithFlagAdder(cmdBuildEnvListFlags). | ||
NoArgs(listBuildEnv) | ||
} | ||
|
||
func listBuildEnv(ctx context.Context, out io.Writer) error { | ||
return inspect.PrintBuildEnvsList(ctx, out, inspect.Options{Filename: inspectFlags.fileName, OutFormat: inspectFlags.outFormat, Modules: inspectFlags.modules, BuildEnvOptions: inspect.BuildEnvOptions{Profiles: inspectFlags.profiles}}) | ||
} | ||
|
||
func cmdBuildEnvFlags(f *pflag.FlagSet) { | ||
f.StringSliceVarP(&inspectFlags.modules, "module", "m", nil, "Names of modules to filter target action by.") | ||
} | ||
|
||
func cmdBuildEnvListFlags(f *pflag.FlagSet) { | ||
f.StringSliceVarP(&inspectFlags.profiles, "profile", "p", nil, `Profile names to activate`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright 2021 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package inspect | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" | ||
) | ||
|
||
type buildEnvList struct { | ||
BuildEnvs []buildEnvEntry `json:"build_envs"` | ||
} | ||
|
||
type buildEnvEntry struct { | ||
Type string `json:"type"` | ||
Path string `json:"path"` | ||
Module string `json:"module,omitempty"` | ||
} | ||
|
||
func PrintBuildEnvsList(ctx context.Context, out io.Writer, opts Options) error { | ||
formatter := getOutputFormatter(out, opts.OutFormat) | ||
cfgs, err := getConfigSetFunc(config.SkaffoldOptions{ConfigurationFile: opts.Filename, Profiles: opts.Profiles}) | ||
if err != nil { | ||
return formatter.WriteErr(err) | ||
} | ||
|
||
l := &buildEnvList{BuildEnvs: []buildEnvEntry{}} | ||
for _, c := range cfgs { | ||
if len(opts.Modules) > 0 && !util.StrSliceContains(opts.Modules, c.Metadata.Name) { | ||
continue | ||
} | ||
buildEnv := GetBuildEnv(&c.Build.BuildType) | ||
l.BuildEnvs = append(l.BuildEnvs, buildEnvEntry{Type: string(buildEnv), Path: c.SourceFile, Module: c.Metadata.Name}) | ||
} | ||
return formatter.Write(l) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
Copyright 2021 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package inspect | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/parser" | ||
sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/errors" | ||
v1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1" | ||
"github.com/GoogleContainerTools/skaffold/testutil" | ||
) | ||
|
||
func TestPrintBuildEnvsList(t *testing.T) { | ||
tests := []struct { | ||
description string | ||
profiles []string | ||
module []string | ||
err error | ||
expected string | ||
}{ | ||
{ | ||
description: "print all build environments", | ||
expected: `{"build_envs":[` + | ||
`{"type":"local","path":"path/to/cfg1","module":"cfg1"},` + | ||
`{"type":"googleCloudBuild","path":"path/to/cfg2","module":"cfg2"}` + | ||
"]}\n", | ||
}, | ||
{ | ||
description: "print all build environments for one module", | ||
expected: `{"build_envs":[` + | ||
`{"type":"local","path":"path/to/cfg1","module":"cfg1"}` + | ||
"]}\n", | ||
module: []string{"cfg1"}, | ||
}, | ||
{ | ||
description: "print all build environments for two activated profiles", | ||
|
||
expected: `{"build_envs":[` + | ||
`{"type":"cluster","path":"path/to/cfg1","module":"cfg1"},` + | ||
`{"type":"local","path":"path/to/cfg2","module":"cfg2"}` + | ||
"]}\n", | ||
profiles: []string{"local", "cluster"}, | ||
}, | ||
{ | ||
description: "print all build environments for one module and an activated profile", | ||
|
||
expected: `{"build_envs":[` + | ||
`{"type":"cluster","path":"path/to/cfg1","module":"cfg1"}` + | ||
"]}\n", | ||
module: []string{"cfg1"}, | ||
profiles: []string{"cluster"}, | ||
}, | ||
{ | ||
description: "actionable error", | ||
err: sErrors.MainConfigFileNotFoundErr("path/to/skaffold.yaml", fmt.Errorf("failed to read file : %q", "skaffold.yaml")), | ||
expected: `{"errorCode":"CONFIG_FILE_NOT_FOUND_ERR","errorMessage":"unable to find configuration file \"path/to/skaffold.yaml\": failed to read file : \"skaffold.yaml\". Check that the specified configuration file exists at \"path/to/skaffold.yaml\"."}` + "\n", | ||
}, | ||
{ | ||
description: "generic error", | ||
err: errors.New("some error occurred"), | ||
expected: `{"errorCode":"UNKNOWN_ERROR","errorMessage":"some error occurred"}` + "\n", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
testutil.Run(t, test.description, func(t *testutil.T) { | ||
configSet := parser.SkaffoldConfigSet{ | ||
&parser.SkaffoldConfigEntry{SkaffoldConfig: &v1.SkaffoldConfig{ | ||
Metadata: v1.Metadata{Name: "cfg1"}, | ||
Pipeline: v1.Pipeline{Build: v1.BuildConfig{BuildType: v1.BuildType{LocalBuild: &v1.LocalBuild{}}}}, | ||
Profiles: []v1.Profile{ | ||
{Name: "cluster", Pipeline: v1.Pipeline{Build: v1.BuildConfig{BuildType: v1.BuildType{Cluster: &v1.ClusterDetails{}}}}}, | ||
}}, SourceFile: "path/to/cfg1"}, | ||
&parser.SkaffoldConfigEntry{SkaffoldConfig: &v1.SkaffoldConfig{ | ||
Metadata: v1.Metadata{Name: "cfg2"}, | ||
Pipeline: v1.Pipeline{Build: v1.BuildConfig{BuildType: v1.BuildType{GoogleCloudBuild: &v1.GoogleCloudBuild{}}}}, | ||
Profiles: []v1.Profile{ | ||
{Name: "local", Pipeline: v1.Pipeline{Build: v1.BuildConfig{BuildType: v1.BuildType{LocalBuild: &v1.LocalBuild{}}}}}, | ||
}}, SourceFile: "path/to/cfg2"}, | ||
} | ||
t.Override(&getConfigSetFunc, func(config.SkaffoldOptions) (parser.SkaffoldConfigSet, error) { | ||
// mock profile activation | ||
for _, c := range configSet { | ||
for _, pName := range test.profiles { | ||
for _, profile := range c.Profiles { | ||
if profile.Name != pName { | ||
continue | ||
} | ||
c.Build.BuildType = profile.Build.BuildType | ||
} | ||
} | ||
} | ||
return configSet, test.err | ||
}) | ||
var buf bytes.Buffer | ||
err := PrintBuildEnvsList(context.Background(), &buf, Options{OutFormat: "json", Modules: test.module, BuildEnvOptions: BuildEnvOptions{Profiles: test.profiles}}) | ||
t.CheckNoError(err) | ||
t.CheckDeepEqual(test.expected, buf.String()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters