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

feat: Add skaffold inspect command for adding config dependencies #9072

Merged
merged 10 commits into from
Sep 12, 2023
2 changes: 1 addition & 1 deletion cmd/skaffold/app/cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewCmdInspect() *cobra.Command {
WithPersistentFlagAdder(cmdInspectFlags).
Hidden().
WithCommands(cmdModules(), cmdProfiles(), cmdBuildEnv(), cmdTests(), cmdNamespaces(),
cmdJobManifestPaths(), cmdExecutionModes())
cmdJobManifestPaths(), cmdExecutionModes(), cmdConfigDependencies())
}

func cmdInspectFlags(f *pflag.FlagSet) {
Expand Down
64 changes: 64 additions & 0 deletions cmd/skaffold/app/cmd/inspect_config_dependencies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2023 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"
"errors"
"io"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/inspect"
configDependencies "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/inspect/configDependencies"
olog "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output/log"
)

func cmdConfigDependencies() *cobra.Command {
return NewCmd("config-dependencies").
WithDescription("Interact with skaffold config dependency definitions.").
WithCommands(cmdConfigDependenciesAdd())
}

func cmdConfigDependenciesAdd() *cobra.Command {
return NewCmd("add").
WithDescription("Add config dependencies").
WithExample("Add config dependency defined in `dependency.json`.", "inspect config-dependencies add dependency.json -f skaffold.yaml").
WithFlagAdder(cmdConfigDependenciesAddFlags).
WithArgs(func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
errMsg := "`config-dependencies add` requires exactly one file path argument"
olog.Entry(context.TODO()).Errorf(errMsg)
return errors.New(errMsg)
}
return nil
}, addConfigDependencies)
}

func cmdConfigDependenciesAddFlags(f *pflag.FlagSet) {
f.StringSliceVarP(&inspectFlags.modules, "module", "m", nil, "Names of modules to filter target action by.")
}

func addConfigDependencies(ctx context.Context, out io.Writer, args []string) error {
return configDependencies.AddConfigDependencies(ctx, out, inspect.Options{
Filename: inspectFlags.filename,
OutFormat: inspectFlags.outFormat,
RemoteCacheDir: inspectFlags.remoteCacheDir,
Modules: inspectFlags.modules,
}, args[0])
}
133 changes: 133 additions & 0 deletions pkg/skaffold/inspect/configDependencies/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
Copyright 2023 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"
"encoding/json"
"io"
"os"

"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/inspect"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/util"
)

type configDependencyList struct {
Dependencies []configDependencyEntry
}

type configDependencyEntry struct {
Names []string `json:"configs,omitempty"`
Path string `json:"path,omitempty"`
Git *git `json:"git,omitempty"`
GoogleCloudStorage *googleCloudStorage `json:"googleCloudStorage,omitempty"`
ActiveProfiles []activeProfile `json:"activeProfiles,omitempty"`
}

type git struct {
Repo string `json:"repo"`
Path string `json:"path,omitempty"`
Ref string `json:"ref,omitempty"`
Sync bool `json:"sync,omitempty"`
}

type googleCloudStorage struct {
Source string `json:"source"`
Path string `json:"path,omitempty"`
Sync bool `json:"sync,omitempty"`
}

type activeProfile struct {
Name string `json:"name"`
ActivatedBy []string `json:"activatedBy,omitempty"`
}

func AddConfigDependencies(ctx context.Context, out io.Writer, opts inspect.Options, inputFile string) error {
formatter := inspect.OutputFormatter(out, opts.OutFormat)

jsonFile, err := os.Open(inputFile)
if err != nil {
formatter.WriteErr(err)
return err
}
defer jsonFile.Close()
fileBytes, err := io.ReadAll(jsonFile)
if err != nil {
formatter.WriteErr(err)
return err
}
var cds configDependencyList
if err := json.Unmarshal(fileBytes, &cds); err != nil {
formatter.WriteErr(err)
return err
}
cfgDependencies := convertToLatestConfigDependencies(cds)

cfgs, err := inspect.GetConfigSet(ctx, config.SkaffoldOptions{
ConfigurationFile: opts.Filename,
RemoteCacheDir: opts.RemoteCacheDir,
ConfigurationFilter: opts.Modules,
SkipConfigDefaults: true,
MakePathsAbsolute: util.Ptr(false),
})
if err != nil {
formatter.WriteErr(err)
return err
}

for _, cfg := range cfgs {
cfg.Dependencies = append(cfg.Dependencies, cfgDependencies...)
}
return inspect.MarshalConfigSet(cfgs)
}

func convertToLatestConfigDependencies(cfgDepList configDependencyList) []latest.ConfigDependency {
var res []latest.ConfigDependency
for _, d := range cfgDepList.Dependencies {
var cd latest.ConfigDependency
cd.Names = d.Names
cd.Path = d.Path
if d.Git != nil {
cd.GitRepo = &latest.GitInfo{
Repo: d.Git.Repo,
Path: d.Git.Path,
Ref: d.Git.Ref,
Sync: &d.Git.Sync,
}
}
if d.GoogleCloudStorage != nil {
cd.GoogleCloudStorage = &latest.GoogleCloudStorageInfo{
Source: d.GoogleCloudStorage.Source,
Path: d.GoogleCloudStorage.Path,
Sync: &d.GoogleCloudStorage.Sync,
}
}
var profileDep []latest.ProfileDependency
for _, ap := range d.ActiveProfiles {
profileDep = append(profileDep, latest.ProfileDependency{
Name: ap.Name,
ActivatedBy: ap.ActivatedBy,
})
}
cd.ActiveProfiles = profileDep

res = append(res, cd)
}
return res
}
Loading