-
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.
feat: Add skaffold inspect command for adding config dependencies (#9072
) * feat: add `skaffold inspect config-dependencies add` command that allows adding config dependencies to the configs in a skaffold file * feat: add `skaffold inspect config-dependencies add` command that allows adding config dependencies to the configs in a skaffold file * fix: repo cache dir is now remote cache dir in skaffold opts * moved config dependency injection to skaffold inspect modules add * fix error message when no file is provided to skaffold inspect modules add * fix another error message typo * move functionality back to inspect config-dependencies add * fix comment formatting
- Loading branch information
Showing
4 changed files
with
434 additions
and
1 deletion.
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,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]) | ||
} |
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,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 | ||
} |
Oops, something went wrong.