Skip to content

Commit

Permalink
feat: add new 'skaffold inspect executionModes' command (#8651)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-prindle authored Apr 10, 2023
1 parent fb2a196 commit 8e23d94
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cmd/skaffold/app/cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func NewCmdInspect() *cobra.Command {
WithDescription("Helper commands for Cloud Code IDEs to interact with and modify skaffold configuration files.").
WithPersistentFlagAdder(cmdInspectFlags).
Hidden().
WithCommands(cmdModules(), cmdProfiles(), cmdBuildEnv(), cmdTests(), cmdNamespaces(), cmdJobManifestPaths())
WithCommands(cmdModules(), cmdProfiles(), cmdBuildEnv(), cmdTests(), cmdNamespaces(),
cmdJobManifestPaths(), cmdExecutionModes())
}

func cmdInspectFlags(f *pflag.FlagSet) {
Expand Down
55 changes: 55 additions & 0 deletions cmd/skaffold/app/cmd/inspect_execution_modes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
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/GoogleContainerTools/skaffold/v2/pkg/skaffold/inspect"
executionModes "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/inspect/executionModes"
)

func cmdExecutionModes() *cobra.Command {
return NewCmd("executionModes").
WithDescription("View skaffold executionMode information defined in the specified skaffold configuration").
WithCommands(cmdExecutionModesList())
}

func cmdExecutionModesList() *cobra.Command {
return NewCmd("list").
WithExample("Get list of executionModes", "inspect executionModes list --format json").
WithExample("Get list of executionModes targeting a specific configuration", "inspect executionModes list --profile local --format json").
WithDescription("Print the list of executionModes that would be run for a given configuration (default skaffold configuration, specific module, specific profile, etc).").
WithArgs(func(cmd *cobra.Command, args []string) error {
// skaffold inspect executionModes list take in an optional list of customActions as well
return nil
}, listExecutionModes)
}

func listExecutionModes(ctx context.Context, out io.Writer, args []string) error {
return executionModes.PrintExecutionModesList(ctx, out, inspect.Options{
Filename: inspectFlags.filename,
RepoCacheDir: inspectFlags.repoCacheDir,
OutFormat: inspectFlags.outFormat,
Modules: inspectFlags.modules,
Profiles: inspectFlags.profiles,
PropagateProfiles: inspectFlags.propagateProfiles,
}, args)
}
63 changes: 63 additions & 0 deletions pkg/skaffold/inspect/executionModes/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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/v2/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/inspect"
)

type executionModeList struct {
VerifyExecutionModes map[string]string `json:"verifyExecutionModes"`
CustomActionsExecutionModes map[string]string `json:"customActionsExecutionModes"`
}

func PrintExecutionModesList(ctx context.Context, out io.Writer, opts inspect.Options, customActions []string) error {
formatter := inspect.OutputFormatter(out, opts.OutFormat)
cfgs, err := inspect.GetConfigSet(ctx, config.SkaffoldOptions{
ConfigurationFile: opts.Filename,
ConfigurationFilter: opts.Modules,
RepoCacheDir: opts.RepoCacheDir,
Profiles: opts.Profiles,
PropagateProfiles: opts.PropagateProfiles,
})
if err != nil {
formatter.WriteErr(err)
return err
}

l := &executionModeList{
VerifyExecutionModes: map[string]string{},
CustomActionsExecutionModes: map[string]string{},
}

for _, c := range cfgs {
for _, tc := range c.Verify {
if tc.ExecutionMode.KubernetesClusterExecutionMode != nil {
l.VerifyExecutionModes[tc.Name] = "kubernetesCluster"
} else {
l.VerifyExecutionModes[tc.Name] = "local"
}
}
}
// TODO(#8572) add similar logic for customAction schema fields when they are complete

return formatter.Write(l)
}
146 changes: 146 additions & 0 deletions pkg/skaffold/inspect/executionModes/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
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/v2/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/inspect"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/parser"
sErrors "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/errors"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/util/stringslice"
"github.com/GoogleContainerTools/skaffold/v2/testutil"
)

func TestPrintExecutionModesList(t *testing.T) {
tests := []struct {
description string
profiles []string
module []string
customActions []string
err error
expected string
}{
{
description: "print all executionModes where no executionMode is set in the verify config",
expected: "{\"verifyExecutionModes\":{},\"customActionsExecutionModes\":{}}" + "\n",
module: []string{"cfg-without-executionModes"},
},
{
description: "print all executionModes where one executionMode is set in the verify and customAction config via a profile but no customAction arg",
expected: "{\"verifyExecutionModes\":{\"foo\":\"kubernetesCluster\"},\"customActionsExecutionModes\":{}}" + "\n",
profiles: []string{"has-verify-executionMode"},
module: []string{"cfg-without-executionModes"},
},
{
description: "print all executionModes where one executionMode is set in the verify config via a module",
expected: "{\"verifyExecutionModes\":{\"bar\":\"kubernetesCluster\"},\"customActionsExecutionModes\":{}}" + "\n",
module: []string{"cfg-with-executionModes"},
},
{
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":"INSPECT_UNKNOWN_ERR","errorMessage":"some error occurred"}` + "\n",
},
}

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
configSet := parser.SkaffoldConfigSet{
&parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{
Metadata: latest.Metadata{Name: "cfg-without-executionModes"},
Pipeline: latest.Pipeline{},
Profiles: []latest.Profile{
{Name: "has-verify-executionMode",
Pipeline: latest.Pipeline{
Verify: []*latest.VerifyTestCase{
{
Name: "foo",
Container: latest.VerifyContainer{
Name: "foo",
Image: "foo",
},
ExecutionMode: latest.VerifyExecutionModeConfig{
VerifyExecutionModeType: latest.VerifyExecutionModeType{
KubernetesClusterExecutionMode: &latest.KubernetesClusterVerifier{},
},
},
},
},
},
}},
}, SourceFile: "path/to/cfg-without-executionModes"},

&parser.SkaffoldConfigEntry{SkaffoldConfig: &latest.SkaffoldConfig{
Metadata: latest.Metadata{Name: "cfg-with-executionModes"},
Pipeline: latest.Pipeline{
Verify: []*latest.VerifyTestCase{
{
Name: "bar",
Container: latest.VerifyContainer{
Name: "bar",
Image: "bar",
},
ExecutionMode: latest.VerifyExecutionModeConfig{
VerifyExecutionModeType: latest.VerifyExecutionModeType{
KubernetesClusterExecutionMode: &latest.KubernetesClusterVerifier{},
},
},
},
},
},
}, SourceFile: "path/to/cfg-with-default-namespace"},
}
t.Override(&inspect.GetConfigSet, func(_ context.Context, opts config.SkaffoldOptions) (parser.SkaffoldConfigSet, error) {
// mock profile activation
var set parser.SkaffoldConfigSet
for _, c := range configSet {
if len(opts.ConfigurationFilter) > 0 && !stringslice.Contains(opts.ConfigurationFilter, c.Metadata.Name) {
continue
}
for _, pName := range opts.Profiles {
for _, profile := range c.Profiles {
if profile.Name != pName {
continue
}
c.Verify = profile.Verify
c.CustomActions = profile.CustomActions
}
}
set = append(set, c)
}
return set, test.err
})
var buf bytes.Buffer
err := PrintExecutionModesList(context.Background(), &buf, inspect.Options{
OutFormat: "json", Modules: test.module, Profiles: test.profiles}, test.customActions)
t.CheckError(test.err != nil, err)
t.CheckDeepEqual(test.expected, buf.String())
})
}
}

0 comments on commit 8e23d94

Please sign in to comment.