-
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 add googleCloudBuild
command (#5824)
* add `skaffold inspect build-env add googleCloudBuild` command * fix function desc * improve tests
- v2.13.2
- v2.13.1
- v2.13.0
- v2.12.0
- v2.11.1
- v2.11.0
- v2.10.1
- v2.10.0
- v2.9.0
- v2.8.0
- v2.7.1
- v2.7.0
- v2.6.4
- v2.6.3
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.1
- v2.5.0
- v2.4.1
- v2.4.0
- v2.3.8
- v2.3.7
- v2.3.6
- v2.3.5
- v2.3.4
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.18
- v2.0.17
- v2.0.16
- v2.0.15
- v2.0.14
- v2.0.13
- v2.0.12
- v2.0.11
- v2.0.10
- v2.0.9
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v2.0.0-beta3
- v2.0.0-beta2
- v2.0.0-beta1
- v1.39.18
- v1.39.17
- v1.39.16
- v1.39.15
- v1.39.14
- v1.39.13
- v1.39.12
- v1.39.11
- v1.39.10
- v1.39.9
- v1.39.8
- v1.39.7
- v1.39.6
- v1.39.5
- v1.39.4
- v1.39.3
- v1.39.2
- v1.39.1
- v1.39.0
- v1.38.0
- v1.37.7
- v1.37.6
- v1.37.5
- v1.37.4
- v1.37.3
- v1.37.2
- v1.37.1
- v1.37.0
- v1.36.1
- v1.36.0
- v1.35.3
- v1.35.2
- v1.35.1
- v1.35.1-aplha
- v1.35.0
- v1.35.0-aplha
- v1.35.0-alpha
- v1.34.1
- v1.34.0
- v1.33.1
- v1.33.0
- v1.32.0
- v1.31.0
- v1.30.0
- v1.29.0
- v1.28.1
- v1.28.0
- v1.27.0
- v1.26.1
- v1.26.0
- v1.25.0
- v1.24.2
- v1.24.1
- test-v0.0.0
- test
1 parent
ffb7d2b
commit d9b397e
Showing
24 changed files
with
1,071 additions
and
251 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,113 @@ | ||
/* | ||
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/inspect" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/parser" | ||
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" | ||
) | ||
|
||
func AddGcbBuildEnv(ctx context.Context, out io.Writer, opts inspect.Options) error { | ||
formatter := inspect.OutputFormatter(out, opts.OutFormat) | ||
cfgs, err := inspect.ConfigSetFunc(config.SkaffoldOptions{ConfigurationFile: opts.Filename, ConfigurationFilter: opts.Modules, SkipConfigDefaults: true, MakePathsAbsolute: util.BoolPtr(false)}) | ||
if err != nil { | ||
return formatter.WriteErr(err) | ||
} | ||
if opts.Profile == "" { | ||
// empty profile flag implies that the new build env needs to be added to the default pipeline. | ||
// for these cases, don't add the new env definition to any configs imported as dependencies. | ||
cfgs = cfgs.SelectRootConfigs() | ||
for _, cfg := range cfgs { | ||
if cfg.Build.GoogleCloudBuild != nil && (*cfg.Build.GoogleCloudBuild != latestV1.GoogleCloudBuild{}) { | ||
return formatter.WriteErr(inspect.BuildEnvAlreadyExists(inspect.BuildEnvs.GoogleCloudBuild, cfg.SourceFile, "")) | ||
} | ||
cfg.Build.GoogleCloudBuild = constructGcbDefinition(cfg.Build.GoogleCloudBuild, opts.BuildEnvOptions) | ||
cfg.Build.LocalBuild = nil | ||
cfg.Build.Cluster = nil | ||
} | ||
} else { | ||
for _, cfg := range cfgs { | ||
index := -1 | ||
for i := range cfg.Profiles { | ||
if cfg.Profiles[i].Name == opts.Profile { | ||
index = i | ||
break | ||
} | ||
} | ||
if index < 0 { | ||
index = len(cfg.Profiles) | ||
cfg.Profiles = append(cfg.Profiles, latestV1.Profile{Name: opts.Profile}) | ||
} | ||
if cfg.Profiles[index].Build.GoogleCloudBuild != nil && (*cfg.Profiles[index].Build.GoogleCloudBuild != latestV1.GoogleCloudBuild{}) { | ||
return formatter.WriteErr(inspect.BuildEnvAlreadyExists(inspect.BuildEnvs.GoogleCloudBuild, cfg.SourceFile, opts.Profile)) | ||
} | ||
cfg.Profiles[index].Build.GoogleCloudBuild = constructGcbDefinition(cfg.Profiles[index].Build.GoogleCloudBuild, opts.BuildEnvOptions) | ||
cfg.Profiles[index].Build.LocalBuild = nil | ||
cfg.Profiles[index].Build.Cluster = nil | ||
|
||
addProfileActivationStanza(cfg, opts.Profile) | ||
} | ||
} | ||
return inspect.MarshalConfigSet(cfgs) | ||
} | ||
|
||
func constructGcbDefinition(existing *latestV1.GoogleCloudBuild, opts inspect.BuildEnvOptions) *latestV1.GoogleCloudBuild { | ||
var b latestV1.GoogleCloudBuild | ||
if existing != nil { | ||
b = *existing | ||
} | ||
if opts.Concurrency >= 0 { | ||
b.Concurrency = opts.Concurrency | ||
} | ||
if opts.DiskSizeGb > 0 { | ||
b.DiskSizeGb = opts.DiskSizeGb | ||
} | ||
if opts.MachineType != "" { | ||
b.MachineType = opts.MachineType | ||
} | ||
if opts.ProjectID != "" { | ||
b.ProjectID = opts.ProjectID | ||
} | ||
if opts.Timeout != "" { | ||
b.Timeout = opts.Timeout | ||
} | ||
return &b | ||
} | ||
|
||
func addProfileActivationStanza(cfg *parser.SkaffoldConfigEntry, profileName string) { | ||
for i := range cfg.Dependencies { | ||
if cfg.Dependencies[i].GitRepo != nil { | ||
// setup profile activation stanza only for local config dependencies | ||
continue | ||
} | ||
for j := range cfg.Dependencies[i].ActiveProfiles { | ||
if cfg.Dependencies[i].ActiveProfiles[j].Name == profileName { | ||
if !util.StrSliceContains(cfg.Dependencies[i].ActiveProfiles[j].ActivatedBy, profileName) { | ||
cfg.Dependencies[i].ActiveProfiles[j].ActivatedBy = append(cfg.Dependencies[i].ActiveProfiles[j].ActivatedBy, profileName) | ||
} | ||
return | ||
} | ||
} | ||
cfg.Dependencies[i].ActiveProfiles = append(cfg.Dependencies[i].ActiveProfiles, latestV1.ProfileDependency{Name: profileName, ActivatedBy: []string{profileName}}) | ||
} | ||
} |
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,383 @@ | ||
/* | ||
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/inspect" | ||
"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/pkg/skaffold/util" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/yaml" | ||
"github.com/GoogleContainerTools/skaffold/testutil" | ||
) | ||
|
||
func TestAddGcbBuildEnv(t *testing.T) { | ||
tests := []struct { | ||
description string | ||
profile string | ||
modules []string | ||
buildEnvOpts inspect.BuildEnvOptions | ||
expectedConfigs []string | ||
err error | ||
expectedErrMsg string | ||
}{ | ||
{ | ||
description: "add to default pipeline", | ||
buildEnvOpts: inspect.BuildEnvOptions{ProjectID: "project1", DiskSizeGb: 2, MachineType: "machine1", Timeout: "128", Concurrency: 2}, | ||
expectedConfigs: []string{ | ||
`apiVersion: "" | ||
kind: "" | ||
metadata: | ||
name: cfg1_0 | ||
build: | ||
googleCloudBuild: | ||
projectId: project1 | ||
diskSizeGb: 2 | ||
machineType: machine1 | ||
timeout: "128" | ||
concurrency: 2 | ||
profiles: | ||
- name: p1 | ||
build: | ||
cluster: {} | ||
--- | ||
apiVersion: "" | ||
kind: "" | ||
metadata: | ||
name: cfg1_1 | ||
requires: | ||
- path: path/to/cfg2 | ||
build: | ||
googleCloudBuild: | ||
projectId: project1 | ||
diskSizeGb: 2 | ||
machineType: machine1 | ||
timeout: "128" | ||
concurrency: 2 | ||
profiles: | ||
- name: p1 | ||
build: | ||
cluster: {} | ||
`, ``, | ||
}, | ||
}, | ||
{ | ||
description: "add to existing profile", | ||
buildEnvOpts: inspect.BuildEnvOptions{ProjectID: "project1", DiskSizeGb: 2, MachineType: "machine1", Timeout: "128", Concurrency: 2, Profile: "p1"}, | ||
expectedConfigs: []string{ | ||
`apiVersion: "" | ||
kind: "" | ||
metadata: | ||
name: cfg1_0 | ||
build: | ||
local: {} | ||
profiles: | ||
- name: p1 | ||
build: | ||
googleCloudBuild: | ||
projectId: project1 | ||
diskSizeGb: 2 | ||
machineType: machine1 | ||
timeout: "128" | ||
concurrency: 2 | ||
--- | ||
apiVersion: "" | ||
kind: "" | ||
metadata: | ||
name: cfg1_1 | ||
requires: | ||
- path: path/to/cfg2 | ||
activeProfiles: | ||
- name: p1 | ||
activatedBy: | ||
- p1 | ||
build: | ||
local: {} | ||
profiles: | ||
- name: p1 | ||
build: | ||
googleCloudBuild: | ||
projectId: project1 | ||
diskSizeGb: 2 | ||
machineType: machine1 | ||
timeout: "128" | ||
concurrency: 2 | ||
`, `apiVersion: "" | ||
kind: "" | ||
metadata: | ||
name: cfg2 | ||
build: | ||
googleCloudBuild: {} | ||
profiles: | ||
- name: p1 | ||
build: | ||
googleCloudBuild: | ||
projectId: project1 | ||
diskSizeGb: 2 | ||
machineType: machine1 | ||
timeout: "128" | ||
concurrency: 2 | ||
`, | ||
}, | ||
}, | ||
{ | ||
description: "add to new profile", | ||
buildEnvOpts: inspect.BuildEnvOptions{ProjectID: "project1", DiskSizeGb: 2, MachineType: "machine1", Timeout: "128", Concurrency: 2, Profile: "p2"}, | ||
expectedConfigs: []string{ | ||
`apiVersion: "" | ||
kind: "" | ||
metadata: | ||
name: cfg1_0 | ||
build: | ||
local: {} | ||
profiles: | ||
- name: p1 | ||
build: | ||
cluster: {} | ||
- name: p2 | ||
build: | ||
googleCloudBuild: | ||
projectId: project1 | ||
diskSizeGb: 2 | ||
machineType: machine1 | ||
timeout: "128" | ||
concurrency: 2 | ||
--- | ||
apiVersion: "" | ||
kind: "" | ||
metadata: | ||
name: cfg1_1 | ||
requires: | ||
- path: path/to/cfg2 | ||
activeProfiles: | ||
- name: p2 | ||
activatedBy: | ||
- p2 | ||
build: | ||
local: {} | ||
profiles: | ||
- name: p1 | ||
build: | ||
cluster: {} | ||
- name: p2 | ||
build: | ||
googleCloudBuild: | ||
projectId: project1 | ||
diskSizeGb: 2 | ||
machineType: machine1 | ||
timeout: "128" | ||
concurrency: 2 | ||
`, `apiVersion: "" | ||
kind: "" | ||
metadata: | ||
name: cfg2 | ||
build: | ||
googleCloudBuild: {} | ||
profiles: | ||
- name: p1 | ||
build: | ||
local: {} | ||
- name: p2 | ||
build: | ||
googleCloudBuild: | ||
projectId: project1 | ||
diskSizeGb: 2 | ||
machineType: machine1 | ||
timeout: "128" | ||
concurrency: 2 | ||
`, | ||
}, | ||
}, | ||
{ | ||
description: "add to new profile in selected modules", | ||
modules: []string{"cfg1_1"}, | ||
buildEnvOpts: inspect.BuildEnvOptions{ProjectID: "project1", DiskSizeGb: 2, MachineType: "machine1", Timeout: "128", Concurrency: 2, Profile: "p2"}, | ||
expectedConfigs: []string{ | ||
`apiVersion: "" | ||
kind: "" | ||
metadata: | ||
name: cfg1_0 | ||
build: | ||
local: {} | ||
profiles: | ||
- name: p1 | ||
build: | ||
cluster: {} | ||
--- | ||
apiVersion: "" | ||
kind: "" | ||
metadata: | ||
name: cfg1_1 | ||
requires: | ||
- path: path/to/cfg2 | ||
activeProfiles: | ||
- name: p2 | ||
activatedBy: | ||
- p2 | ||
build: | ||
local: {} | ||
profiles: | ||
- name: p1 | ||
build: | ||
cluster: {} | ||
- name: p2 | ||
build: | ||
googleCloudBuild: | ||
projectId: project1 | ||
diskSizeGb: 2 | ||
machineType: machine1 | ||
timeout: "128" | ||
concurrency: 2 | ||
`, `apiVersion: "" | ||
kind: "" | ||
metadata: | ||
name: cfg2 | ||
build: | ||
googleCloudBuild: {} | ||
profiles: | ||
- name: p1 | ||
build: | ||
local: {} | ||
- name: p2 | ||
build: | ||
googleCloudBuild: | ||
projectId: project1 | ||
diskSizeGb: 2 | ||
machineType: machine1 | ||
timeout: "128" | ||
concurrency: 2 | ||
`, "", | ||
}, | ||
}, | ||
{ | ||
description: "add to new profile in nested module", | ||
modules: []string{"cfg2"}, | ||
buildEnvOpts: inspect.BuildEnvOptions{ProjectID: "project1", DiskSizeGb: 2, MachineType: "machine1", Timeout: "128", Concurrency: 2, Profile: "p2"}, | ||
expectedConfigs: []string{"", | ||
`apiVersion: "" | ||
kind: "" | ||
metadata: | ||
name: cfg2 | ||
build: | ||
googleCloudBuild: {} | ||
profiles: | ||
- name: p1 | ||
build: | ||
local: {} | ||
- name: p2 | ||
build: | ||
googleCloudBuild: | ||
projectId: project1 | ||
diskSizeGb: 2 | ||
machineType: machine1 | ||
timeout: "128" | ||
concurrency: 2 | ||
`, | ||
}, | ||
}, | ||
{ | ||
description: "actionable error", | ||
err: sErrors.MainConfigFileNotFoundErr("path/to/skaffold.yaml", fmt.Errorf("failed to read file : %q", "skaffold.yaml")), | ||
expectedErrMsg: `{"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"), | ||
expectedErrMsg: `{"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: &v1.SkaffoldConfig{ | ||
Metadata: v1.Metadata{Name: "cfg1_0"}, | ||
Pipeline: v1.Pipeline{Build: v1.BuildConfig{BuildType: v1.BuildType{LocalBuild: &v1.LocalBuild{}}}}, | ||
Profiles: []v1.Profile{ | ||
{Name: "p1", Pipeline: v1.Pipeline{Build: v1.BuildConfig{BuildType: v1.BuildType{Cluster: &v1.ClusterDetails{}}}}}, | ||
}}, SourceFile: "path/to/cfg1", IsRootConfig: true, SourceIndex: 0}, | ||
&parser.SkaffoldConfigEntry{SkaffoldConfig: &v1.SkaffoldConfig{ | ||
Metadata: v1.Metadata{Name: "cfg1_1"}, | ||
Dependencies: []v1.ConfigDependency{{Path: "path/to/cfg2"}}, | ||
Pipeline: v1.Pipeline{Build: v1.BuildConfig{BuildType: v1.BuildType{LocalBuild: &v1.LocalBuild{}}}}, | ||
Profiles: []v1.Profile{ | ||
{Name: "p1", Pipeline: v1.Pipeline{Build: v1.BuildConfig{BuildType: v1.BuildType{Cluster: &v1.ClusterDetails{}}}}}, | ||
}}, SourceFile: "path/to/cfg1", IsRootConfig: true, SourceIndex: 1}, | ||
&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: "p1", Pipeline: v1.Pipeline{Build: v1.BuildConfig{BuildType: v1.BuildType{LocalBuild: &v1.LocalBuild{}}}}}, | ||
}}, SourceFile: "path/to/cfg2", SourceIndex: 0}, | ||
} | ||
t.Override(&inspect.ConfigSetFunc, func(opts config.SkaffoldOptions) (parser.SkaffoldConfigSet, error) { | ||
if test.err != nil { | ||
return nil, test.err | ||
} | ||
var sets parser.SkaffoldConfigSet | ||
if len(opts.ConfigurationFilter) == 0 || util.StrSliceContains(opts.ConfigurationFilter, "cfg2") || util.StrSliceContains(opts.ConfigurationFilter, "cfg1_1") { | ||
sets = append(sets, configSet[2]) | ||
} | ||
if len(opts.ConfigurationFilter) == 0 || util.StrSliceContains(opts.ConfigurationFilter, "cfg1_0") { | ||
sets = append(sets, configSet[0]) | ||
} | ||
if len(opts.ConfigurationFilter) == 0 || util.StrSliceContains(opts.ConfigurationFilter, "cfg1_1") { | ||
sets = append(sets, configSet[1]) | ||
} | ||
return sets, nil | ||
}) | ||
t.Override(&inspect.ReadFileFunc, func(filename string) ([]byte, error) { | ||
if filename == "path/to/cfg1" { | ||
return yaml.MarshalWithSeparator([]*v1.SkaffoldConfig{configSet[0].SkaffoldConfig, configSet[1].SkaffoldConfig}) | ||
} else if filename == "path/to/cfg2" { | ||
return yaml.MarshalWithSeparator([]*v1.SkaffoldConfig{configSet[2].SkaffoldConfig}) | ||
} | ||
t.FailNow() | ||
return nil, nil | ||
}) | ||
var actualCfg1, actualCfg2 string | ||
t.Override(&inspect.WriteFileFunc, func(filename string, data []byte) error { | ||
switch filename { | ||
case "path/to/cfg1": | ||
actualCfg1 = string(data) | ||
case "path/to/cfg2": | ||
actualCfg2 = string(data) | ||
default: | ||
t.FailNow() | ||
} | ||
return nil | ||
}) | ||
|
||
var buf bytes.Buffer | ||
err := AddGcbBuildEnv(context.Background(), &buf, inspect.Options{OutFormat: "json", Modules: test.modules, BuildEnvOptions: test.buildEnvOpts}) | ||
t.CheckNoError(err) | ||
if test.err == nil { | ||
t.CheckDeepEqual(test.expectedConfigs[0], actualCfg1) | ||
t.CheckDeepEqual(test.expectedConfigs[1], actualCfg2) | ||
} else { | ||
t.CheckDeepEqual(test.expectedErrMsg, 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
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,45 @@ | ||
/* | ||
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 ( | ||
"fmt" | ||
|
||
sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/errors" | ||
"github.com/GoogleContainerTools/skaffold/proto/v1" | ||
) | ||
|
||
// BuildEnvAlreadyExists specifies that there's an existing build environment definition for the same type. | ||
func BuildEnvAlreadyExists(b BuildEnv, filename string, profile string) error { | ||
var msg string | ||
if profile == "" { | ||
msg = fmt.Sprintf("trying to create a %q build environment definition that already exists, in file %s", b, filename) | ||
} else { | ||
msg = fmt.Sprintf("trying to create a %q build environment definition that already exists, in profile %q in file %s", b, profile, filename) | ||
} | ||
return sErrors.NewError(fmt.Errorf(msg), | ||
proto.ActionableErr{ | ||
Message: msg, | ||
ErrCode: proto.StatusCode_INSPECT_BUILD_ENV_ALREADY_EXISTS_ERR, | ||
Suggestions: []*proto.Suggestion{ | ||
{ | ||
SuggestionCode: proto.SuggestionCode_INSPECT_DEDUP_NEW_BUILD_ENV, | ||
Action: "Use the `modify` command instead of the `add` command to overwrite fields for an already existing build environment type. Otherwise pass the `--profile` flag with a unique name to create the new build environment definition in a new profile instead", | ||
}, | ||
}, | ||
}) | ||
} |
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,89 @@ | ||
/* | ||
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" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
|
||
yamlv3 "gopkg.in/yaml.v3" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/parser" | ||
sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/errors" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/yaml" | ||
) | ||
|
||
var ( | ||
ReadFileFunc = util.ReadConfiguration | ||
WriteFileFunc = func(filename string, data []byte) error { | ||
return ioutil.WriteFile(filename, data, 0644) | ||
} | ||
) | ||
|
||
// MarshalConfigSet marshals out the slice of skaffold configs into the respective source `skaffold.yaml` files. | ||
// It ensures that the unmodified configs are copied over as-is in their original positions in the file. | ||
func MarshalConfigSet(cfgs parser.SkaffoldConfigSet) error { | ||
m := make(map[string]parser.SkaffoldConfigSet) | ||
for _, cfg := range cfgs { | ||
m[cfg.SourceFile] = append(m[cfg.SourceFile], cfg) | ||
} | ||
for file, set := range m { | ||
if err := marshalConfigSetForFile(file, set); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func marshalConfigSetForFile(filename string, cfgs parser.SkaffoldConfigSet) error { | ||
buf, err := ReadFileFunc(filename) | ||
if err != nil { | ||
return sErrors.ConfigParsingError(err) | ||
} | ||
in := bytes.NewReader(buf) | ||
decoder := yamlv3.NewDecoder(in) | ||
decoder.KnownFields(true) | ||
var sl []interface{} | ||
for { | ||
var parsed yamlv3.Node | ||
err := decoder.Decode(&parsed) | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
return fmt.Errorf("unable to parse YAML: %w", err) | ||
} | ||
// parsed content is a document so the `Content` slice has exactly one element | ||
sl = append(sl, parsed.Content[0]) | ||
} | ||
|
||
for i, cfg := range cfgs { | ||
sl[cfg.SourceIndex] = cfgs[i].SkaffoldConfig | ||
} | ||
|
||
newCfgs, err := yaml.MarshalWithSeparator(sl) | ||
if err != nil { | ||
return fmt.Errorf("marshaling new configs: %w", err) | ||
} | ||
if err := WriteFileFunc(filename, newCfgs); err != nil { | ||
return fmt.Errorf("writing config file: %w", err) | ||
} | ||
return nil | ||
} |
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
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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Oops, something went wrong.
Oops, something went wrong.