Skip to content

Commit db6631e

Browse files
authored
Merge pull request #4076 from DanielSel/feature/global-kubecontext-regex
allow specifying regex in global config kubecontext
2 parents 0a33ef4 + 5d3e0f3 commit db6631e

File tree

7 files changed

+98
-33
lines changed

7 files changed

+98
-33
lines changed

cmd/skaffold/app/cmd/config/set.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strings"
2626

2727
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
28+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
2829
)
2930

3031
const (
@@ -147,7 +148,7 @@ func writeConfig(cfg *config.ContextConfig) error {
147148
} else {
148149
found := false
149150
for i, contextCfg := range fullConfig.ContextConfigs {
150-
if contextCfg.Kubecontext == kubecontext {
151+
if util.RegexEqual(contextCfg.Kubecontext, kubecontext) {
151152
fullConfig.ContextConfigs[i] = cfg
152153
found = true
153154
}

cmd/skaffold/app/cmd/config/util.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
2525
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
26+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
2627
)
2728

2829
func resolveKubectlContext() {
@@ -74,7 +75,7 @@ func getConfigForKubectx() (*config.ContextConfig, error) {
7475
return cfg.Global, nil
7576
}
7677
for _, contextCfg := range cfg.ContextConfigs {
77-
if contextCfg.Kubecontext == kubecontext {
78+
if util.RegexEqual(contextCfg.Kubecontext, kubecontext) {
7879
return contextCfg, nil
7980
}
8081
}

docs/content/en/docs/design/global-config.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ featureId: global_config
66

77
---
88

9-
Some context specific settings can be configured in a global configuration file, which defaults to `~/.skaffold/config`. Options can be configured globally or for specific Kubernetes contexts.
9+
Some context specific settings can be configured in a global configuration file, which defaults to `~/.skaffold/config`. Options can be configured globally or for specific Kubernetes contexts. Context name matching supports regex, e.g.: `.*-cluster.*-regex.*-test.*`
1010

1111
The options are:
1212

pkg/skaffold/config/util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func getConfigForKubeContextWithGlobalDefaults(cfg *GlobalConfig, kubeContext st
132132

133133
var mergedConfig ContextConfig
134134
for _, contextCfg := range cfg.ContextConfigs {
135-
if contextCfg.Kubecontext == kubeContext {
135+
if util.RegexEqual(contextCfg.Kubecontext, kubeContext) {
136136
logrus.Debugf("found config for context %q", kubeContext)
137137
mergedConfig = *contextCfg
138138
}

pkg/skaffold/schema/profiles.go

+5-29
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121
"os"
2222
"reflect"
23-
re "regexp"
2423
"strings"
2524

2625
yamlpatch "github.com/krishicks/yaml-patch"
@@ -31,6 +30,7 @@ import (
3130
kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
3231
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
3332
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util"
33+
skutil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
3434
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/yamltags"
3535
)
3636

@@ -154,15 +154,15 @@ func isEnv(env string) (bool, error) {
154154
return envValue == "", nil
155155
}
156156

157-
return satisfies(value, envValue), nil
157+
return skutil.RegexEqual(value, envValue), nil
158158
}
159159

160160
func isCommand(command string, opts cfg.SkaffoldOptions) bool {
161161
if command == "" {
162162
return true
163163
}
164164

165-
return satisfies(command, opts.Command)
165+
return skutil.RegexEqual(command, opts.Command)
166166
}
167167

168168
func isKubeContext(kubeContext string, opts cfg.SkaffoldOptions) (bool, error) {
@@ -172,39 +172,15 @@ func isKubeContext(kubeContext string, opts cfg.SkaffoldOptions) (bool, error) {
172172

173173
// cli flag takes precedence
174174
if opts.KubeContext != "" {
175-
return satisfies(kubeContext, opts.KubeContext), nil
175+
return skutil.RegexEqual(kubeContext, opts.KubeContext), nil
176176
}
177177

178178
currentKubeConfig, err := kubectx.CurrentConfig()
179179
if err != nil {
180180
return false, fmt.Errorf("getting current cluster context: %w", err)
181181
}
182182

183-
return satisfies(kubeContext, currentKubeConfig.CurrentContext), nil
184-
}
185-
186-
func satisfies(expected, actual string) bool {
187-
if strings.HasPrefix(expected, "!") {
188-
notExpected := expected[1:]
189-
190-
return !matches(notExpected, actual)
191-
}
192-
193-
return matches(expected, actual)
194-
}
195-
196-
func matches(expected, actual string) bool {
197-
if actual == expected {
198-
return true
199-
}
200-
201-
matcher, err := re.Compile(expected)
202-
if err != nil {
203-
logrus.Infof("profile activation criteria '%s' is not a valid regexp, falling back to string", expected)
204-
return false
205-
}
206-
207-
return matcher.MatchString(actual)
183+
return skutil.RegexEqual(kubeContext, currentKubeConfig.CurrentContext), nil
208184
}
209185

210186
func applyProfile(config *latest.SkaffoldConfig, profile latest.Profile) error {

pkg/skaffold/util/regex.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2020 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
re "regexp"
21+
"strings"
22+
23+
"github.com/sirupsen/logrus"
24+
)
25+
26+
// RegexEqual matches the string 'actual' against a regex compiled from 'expected'
27+
// If 'expected' is not a valid regex, string comparison is used as fallback
28+
func RegexEqual(expected, actual string) bool {
29+
if strings.HasPrefix(expected, "!") {
30+
notExpected := expected[1:]
31+
32+
return !regexMatch(notExpected, actual)
33+
}
34+
35+
return regexMatch(expected, actual)
36+
}
37+
38+
func regexMatch(expected, actual string) bool {
39+
if actual == expected {
40+
return true
41+
}
42+
43+
matcher, err := re.Compile(expected)
44+
if err != nil {
45+
logrus.Infof("context activation criteria '%s' is not a valid regexp, falling back to string", expected)
46+
return false
47+
}
48+
49+
return matcher.MatchString(actual)
50+
}

pkg/skaffold/util/regex_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2020 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"testing"
21+
22+
"github.com/GoogleContainerTools/skaffold/testutil"
23+
)
24+
25+
func TestKubectxEqual(t *testing.T) {
26+
ctxRe := ".*-i.*-am.*-test.*"
27+
ctxRePos := "wohoo-i-am-test-or"
28+
ctxReNeg := "test-am-i"
29+
testutil.CheckDeepEqual(t, true, RegexEqual(ctxRe, ctxRePos))
30+
testutil.CheckDeepEqual(t, false, RegexEqual(ctxRe, ctxReNeg))
31+
32+
ctxStr := "^s^"
33+
ctxStrPos := "^s^"
34+
ctxStrNeg := "test-am-i"
35+
testutil.CheckDeepEqual(t, true, RegexEqual(ctxStr, ctxStrPos))
36+
testutil.CheckDeepEqual(t, false, RegexEqual(ctxStr, ctxStrNeg))
37+
}

0 commit comments

Comments
 (0)