Skip to content

allow specifying regex in global config kubecontext #4076

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/skaffold/app/cmd/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)

const (
Expand Down Expand Up @@ -147,7 +148,7 @@ func writeConfig(cfg *config.ContextConfig) error {
} else {
found := false
for i, contextCfg := range fullConfig.ContextConfigs {
if contextCfg.Kubecontext == kubecontext {
if util.RegexEqual(contextCfg.Kubecontext, kubecontext) {
fullConfig.ContextConfigs[i] = cfg
found = true
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/skaffold/app/cmd/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)

func resolveKubectlContext() {
Expand Down Expand Up @@ -74,7 +75,7 @@ func getConfigForKubectx() (*config.ContextConfig, error) {
return cfg.Global, nil
}
for _, contextCfg := range cfg.ContextConfigs {
if contextCfg.Kubecontext == kubecontext {
if util.RegexEqual(contextCfg.Kubecontext, kubecontext) {
return contextCfg, nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/docs/design/global-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ featureId: global_config

---

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.
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.*`

The options are:

Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func getConfigForKubeContextWithGlobalDefaults(cfg *GlobalConfig, kubeContext st

var mergedConfig ContextConfig
for _, contextCfg := range cfg.ContextConfigs {
if contextCfg.Kubecontext == kubeContext {
if util.RegexEqual(contextCfg.Kubecontext, kubeContext) {
logrus.Debugf("found config for context %q", kubeContext)
mergedConfig = *contextCfg
}
Expand Down
34 changes: 5 additions & 29 deletions pkg/skaffold/schema/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"os"
"reflect"
re "regexp"
"strings"

yamlpatch "github.com/krishicks/yaml-patch"
Expand All @@ -31,6 +30,7 @@ import (
kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util"
skutil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/yamltags"
)

Expand Down Expand Up @@ -154,15 +154,15 @@ func isEnv(env string) (bool, error) {
return envValue == "", nil
}

return satisfies(value, envValue), nil
return skutil.RegexEqual(value, envValue), nil
}

func isCommand(command string, opts cfg.SkaffoldOptions) bool {
if command == "" {
return true
}

return satisfies(command, opts.Command)
return skutil.RegexEqual(command, opts.Command)
}

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

// cli flag takes precedence
if opts.KubeContext != "" {
return satisfies(kubeContext, opts.KubeContext), nil
return skutil.RegexEqual(kubeContext, opts.KubeContext), nil
}

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

return satisfies(kubeContext, currentKubeConfig.CurrentContext), nil
}

func satisfies(expected, actual string) bool {
if strings.HasPrefix(expected, "!") {
notExpected := expected[1:]

return !matches(notExpected, actual)
}

return matches(expected, actual)
}

func matches(expected, actual string) bool {
if actual == expected {
return true
}

matcher, err := re.Compile(expected)
if err != nil {
logrus.Infof("profile activation criteria '%s' is not a valid regexp, falling back to string", expected)
return false
}

return matcher.MatchString(actual)
return skutil.RegexEqual(kubeContext, currentKubeConfig.CurrentContext), nil
}

func applyProfile(config *latest.SkaffoldConfig, profile latest.Profile) error {
Expand Down
50 changes: 50 additions & 0 deletions pkg/skaffold/util/regex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2020 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 util

import (
re "regexp"
"strings"

"github.com/sirupsen/logrus"
)

// RegexEqual matches the string 'actual' against a regex compiled from 'expected'
// If 'expected' is not a valid regex, string comparison is used as fallback
func RegexEqual(expected, actual string) bool {
if strings.HasPrefix(expected, "!") {
notExpected := expected[1:]

return !regexMatch(notExpected, actual)
}

return regexMatch(expected, actual)
}

func regexMatch(expected, actual string) bool {
if actual == expected {
return true
}

matcher, err := re.Compile(expected)
if err != nil {
logrus.Infof("context activation criteria '%s' is not a valid regexp, falling back to string", expected)
return false
}

return matcher.MatchString(actual)
}
37 changes: 37 additions & 0 deletions pkg/skaffold/util/regex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2020 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 util

import (
"testing"

"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestKubectxEqual(t *testing.T) {
ctxRe := ".*-i.*-am.*-test.*"
ctxRePos := "wohoo-i-am-test-or"
ctxReNeg := "test-am-i"
testutil.CheckDeepEqual(t, true, RegexEqual(ctxRe, ctxRePos))
testutil.CheckDeepEqual(t, false, RegexEqual(ctxRe, ctxReNeg))

ctxStr := "^s^"
ctxStrPos := "^s^"
ctxStrNeg := "test-am-i"
testutil.CheckDeepEqual(t, true, RegexEqual(ctxStr, ctxStrPos))
testutil.CheckDeepEqual(t, false, RegexEqual(ctxStr, ctxStrNeg))
}