diff --git a/pkg/kn/commands/service/configuration_edit_flags.go b/pkg/kn/commands/service/configuration_edit_flags.go index 0464f8e84f..0c443e0c5e 100644 --- a/pkg/kn/commands/service/configuration_edit_flags.go +++ b/pkg/kn/commands/service/configuration_edit_flags.go @@ -500,11 +500,13 @@ func (p *ConfigurationEditFlags) Apply( if len(knconfig.GlobalConfig.Profile(profileName).Annotations) > 0 { annotations := knconfig.GlobalConfig.Profile(profileName).Annotations profileAnnotations := make(util.StringMap) - profileAnnotations.Merge(annotations) + for _, value := range annotations { + profileAnnotations[value.Name] = value.Value + } if deleteProfile { var annotationsToRemove []string - for key := range annotations { - annotationsToRemove = append(annotationsToRemove, key) + for _, value := range annotations { + annotationsToRemove = append(annotationsToRemove, value.Name) } err = servinglib.UpdateRevisionTemplateAnnotations(template, map[string]string{}, annotationsToRemove) } else { diff --git a/pkg/kn/commands/service/configuration_edit_flags_test.go b/pkg/kn/commands/service/configuration_edit_flags_test.go index 93feffdd84..0c05df43e5 100644 --- a/pkg/kn/commands/service/configuration_edit_flags_test.go +++ b/pkg/kn/commands/service/configuration_edit_flags_test.go @@ -15,10 +15,15 @@ package service import ( + "os" + "path/filepath" "testing" + "github.com/spf13/viper" "gotest.tools/v3/assert" + "knative.dev/client/pkg/kn/commands" + "knative.dev/client/pkg/kn/config" "knative.dev/client/pkg/util" "knative.dev/serving/pkg/apis/autoscaling" ) @@ -76,15 +81,92 @@ func TestScaleActivation(t *testing.T) { assert.Equal(t, svc.Spec.Template.Annotations[autoscaling.ActivationScaleKey], "2") } +func TestApplyProfileFlag(t *testing.T) { + var editFlags ConfigurationEditFlags + knParams := &commands.KnParams{} + cmd, _, _ := commands.CreateTestKnCommand(NewServiceCreateCommand(knParams), knParams) + configYaml := ` +profiles: + istio: + labels: + - name: environment + value: "test" + annotations: + - name: sidecar.istio.io/inject + value: "true" + - name: sidecar.istio.io/rewriteAppHTTPProbers + value: "true" + - name: serving.knative.openshift.io/enablePassthrough + value: "true" +` + _, cleanup := setupConfig(t, configYaml) + defer cleanup() + + editFlags.AddCreateFlags(cmd) + + err := config.BootstrapConfig() + assert.NilError(t, err) + + svc := createTestService("test-svc", []string{"test-svc-00001"}, goodConditions()) + cmd.SetArgs([]string{"--profile", "istio"}) + cmd.Execute() + editFlags.Apply(&svc, nil, cmd) + assert.Equal(t, svc.Spec.Template.Annotations["sidecar.istio.io/inject"], "true") + assert.Equal(t, svc.Spec.Template.Annotations["sidecar.istio.io/rewriteAppHTTPProbers"], "true") + assert.Equal(t, svc.Spec.Template.Annotations["serving.knative.openshift.io/enablePassthrough"], "true") +} + func TestApplyProfileFlagError(t *testing.T) { var editFlags ConfigurationEditFlags knParams := &commands.KnParams{} cmd, _, _ := commands.CreateTestKnCommand(NewServiceCreateCommand(knParams), knParams) + configYaml := ` +profiles: + istio: + labels: + - name: environment + value: "test" + annotations: + - name: sidecar.istio.io/inject + value: "true" +` + _, cleanup := setupConfig(t, configYaml) + defer cleanup() editFlags.AddCreateFlags(cmd) - svc := createTestService("test-svc", []string{"test-svc-00001", "test-svc-00002"}, goodConditions()) + + err := config.BootstrapConfig() + assert.NilError(t, err) + + svc := createTestService("test-svc", []string{"test-svc-00001"}, goodConditions()) cmd.SetArgs([]string{"--profile", "invalidprofile"}) cmd.Execute() - err := editFlags.Apply(&svc, nil, cmd) + err = editFlags.Apply(&svc, nil, cmd) assert.Assert(t, util.ContainsAll(err.Error(), "profile", "invalidprofile")) } + +func setupConfig(t *testing.T, configContent string) (string, func()) { + tmpDir := t.TempDir() + + // Avoid to be fooled by the things in the the real homedir + oldHome := os.Getenv("HOME") + os.Setenv("HOME", tmpDir) + + // Save old args + backupArgs := os.Args + + // WriteCache out a temporary configContent file + var cfgFile string + if configContent != "" { + cfgFile = filepath.Join(tmpDir, "config.yaml") + os.Args = []string{"kn", "--config", cfgFile} + err := os.WriteFile(cfgFile, []byte(configContent), 0644) + assert.NilError(t, err) + } + return cfgFile, func() { + // Cleanup everything + os.Setenv("HOME", oldHome) + os.Args = backupArgs + viper.Reset() + } +} diff --git a/pkg/kn/config/config_test.go b/pkg/kn/config/config_test.go index 057a052ff6..006642dda0 100644 --- a/pkg/kn/config/config_test.go +++ b/pkg/kn/config/config_test.go @@ -31,11 +31,15 @@ plugins: profiles: istio: labels: - environment: test + - name: environment + value: "test" annotations: - sidecar.istio.io/inject: "true" - sidecar.istio.io/rewriteAppHTTPProbers: "true" - serving.knative.openshift.io/enablePassthrough: "true" + - name: sidecar.istio.io/inject + value: "true" + - name: sidecar.istio.io/rewriteAppHTTPProbers + value: "true" + - name: serving.knative.openshift.io/enablePassthrough + value: "true" eventing: sink-mappings: - prefix: service @@ -59,10 +63,27 @@ eventing: assert.Equal(t, GlobalConfig.PluginsDir(), "/tmp") assert.Equal(t, GlobalConfig.LookupPluginsInPath(), true) assert.Equal(t, len(GlobalConfig.SinkMappings()), 1) - assert.Equal(t, len(GlobalConfig.Profile("istio").Annotations), 3) - assert.Equal(t, GlobalConfig.Profile("istio").Annotations["sidecar.istio.io/inject"], "true") + assert.DeepEqual(t, GlobalConfig.Profile("istio").Annotations, []NamedValue{ + { + Name: "sidecar.istio.io/inject", + Value: "true", + }, + { + Name: "sidecar.istio.io/rewriteAppHTTPProbers", + Value: "true", + }, + { + Name: "serving.knative.openshift.io/enablePassthrough", + Value: "true", + }, + }) assert.Equal(t, len(GlobalConfig.Profile("istio").Labels), 1) - assert.Equal(t, GlobalConfig.Profile("istio").Labels["environment"], "test") + assert.DeepEqual(t, GlobalConfig.Profile("istio").Labels, []NamedValue{ + { + Name: "environment", + Value: "test", + }, + }) assert.DeepEqual(t, (GlobalConfig.SinkMappings())[0], SinkMapping{ Prefix: "service", Resource: "services", diff --git a/pkg/kn/config/types.go b/pkg/kn/config/types.go index 4aa7c322d5..f9ea9881d7 100644 --- a/pkg/kn/config/types.go +++ b/pkg/kn/config/types.go @@ -74,9 +74,14 @@ type ChannelTypeMapping struct { Version string } +type NamedValue struct { + Name string `yaml:"name"` + Value string `yaml:"value"` +} + type Profile struct { - Annotations map[string]string `yaml:"annotations"` - Labels map[string]string `yaml:"labels"` + Annotations []NamedValue `yaml:"annotations"` + Labels []NamedValue `yaml:"annotations"` } // config Keys for looking up in viper