diff --git a/docs/content/en/docs/how-tos/profiles/_index.md b/docs/content/en/docs/how-tos/profiles/_index.md index 52f693632f2..c31b17def2b 100644 --- a/docs/content/en/docs/how-tos/profiles/_index.md +++ b/docs/content/en/docs/how-tos/profiles/_index.md @@ -47,7 +47,7 @@ You can activate a profile two ways: CLI flag or skaffold.yaml activations. **Activations in skaffold.yaml**: You can auto-activate a profile based on -* kubecontext +* kubecontext (could be either a string or a regexp: prefixing with `!` will negate the match) * environment variable value * skaffold command (dev/run/build/deploy) diff --git a/pkg/skaffold/schema/profiles.go b/pkg/skaffold/schema/profiles.go index 811be1872bc..aef809ff051 100644 --- a/pkg/skaffold/schema/profiles.go +++ b/pkg/skaffold/schema/profiles.go @@ -20,6 +20,7 @@ import ( "fmt" "os" "reflect" + re "regexp" "strings" cfg "github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" @@ -122,9 +123,23 @@ func isKubeContext(kubeContext string) (bool, error) { func satisfies(expected, actual string) bool { if strings.HasPrefix(expected, "!") { - return actual != expected[1:] + notExpected := expected[1:] + + return !matches(notExpected, actual) + } + + return matches(expected, actual) +} + +func matches(expected, actual string) bool { + 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 actual == expected } - return actual == expected + + return matcher.MatchString(actual) } func applyProfile(config *latest.SkaffoldConfig, profile latest.Profile) error { diff --git a/pkg/skaffold/schema/profiles_test.go b/pkg/skaffold/schema/profiles_test.go index 5b9b3c1531c..6fcda05571b 100644 --- a/pkg/skaffold/schema/profiles_test.go +++ b/pkg/skaffold/schema/profiles_test.go @@ -377,8 +377,10 @@ func TestActivatedProfiles(t *testing.T) { {Name: "activated", Activation: []latest.Activation{{KubeContext: "prod-context"}}}, {Name: "not-activated", Activation: []latest.Activation{{KubeContext: "dev-context"}}}, {Name: "also-activated", Activation: []latest.Activation{{KubeContext: "!dev-context"}}}, + {Name: "activated-regexp", Activation: []latest.Activation{{KubeContext: "prod-.*"}}}, + {Name: "not-activated-regexp", Activation: []latest.Activation{{KubeContext: "dev-.*"}}}, }, - expected: []string{"activated", "also-activated"}, + expected: []string{"activated", "also-activated", "activated-regexp"}, }, { description: "AND between activation criteria", opts: &cfg.SkaffoldOptions{