diff --git a/docs/cmd/kn_service_apply.md b/docs/cmd/kn_service_apply.md index 4d1963739a..4c927e05d6 100644 --- a/docs/cmd/kn_service_apply.md +++ b/docs/cmd/kn_service_apply.md @@ -60,7 +60,7 @@ kn service apply s0 --filename my-svc.yml --probe-liveness-opts string Add common options to liveness probe. Common opts (comma separated, case insensitive): InitialDelaySeconds=, FailureThreshold=, SuccessThreshold=, PeriodSeconds=, TimeoutSeconds= --probe-readiness string Add readiness probe to Service deployment. Supported probe types are HTTGet, Exec and TCPSocket. Format: [http,https]:host:port:path, exec:cmd[,cmd,...], tcp:host:port. --probe-readiness-opts string Add common options to readiness probe. Common opts (comma separated, case insensitive): InitialDelaySeconds=, FailureThreshold=, SuccessThreshold=, PeriodSeconds=, TimeoutSeconds= - --profile string The profile name to set. This will add the annotations related to profile to the service. To unset, specify the profile name followed by a "-" (e.g., name-). + --profile string The profile name is from the config definition. Related annotations and labels will be added to the service.To unset, specify the profile name followed by a "-" (e.g., name-). --pull-policy string Image pull policy. Valid values (case insensitive): Always | Never | IfNotPresent --pull-secret string Image pull secret to set. An empty argument ("") clears the pull secret. The referenced secret must exist in the service's namespace. --request strings The resource requirement requests for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource request, append "-" to the resource name, e.g. '--request cpu-'. diff --git a/docs/cmd/kn_service_create.md b/docs/cmd/kn_service_create.md index bdc2f49207..b2f9130460 100644 --- a/docs/cmd/kn_service_create.md +++ b/docs/cmd/kn_service_create.md @@ -51,7 +51,7 @@ kn service create NAME --image IMAGE kn service create gitopstest --image knativesamples/helloworld --target=/user/knfiles/test.json # Create a service with profile - kn service create isoto --image knativesamples/helloworld --profile istio + kn service create profiletest --image knativesamples/helloworld --profile istio ``` ### Options @@ -88,7 +88,7 @@ kn service create NAME --image IMAGE --probe-liveness-opts string Add common options to liveness probe. Common opts (comma separated, case insensitive): InitialDelaySeconds=, FailureThreshold=, SuccessThreshold=, PeriodSeconds=, TimeoutSeconds= --probe-readiness string Add readiness probe to Service deployment. Supported probe types are HTTGet, Exec and TCPSocket. Format: [http,https]:host:port:path, exec:cmd[,cmd,...], tcp:host:port. --probe-readiness-opts string Add common options to readiness probe. Common opts (comma separated, case insensitive): InitialDelaySeconds=, FailureThreshold=, SuccessThreshold=, PeriodSeconds=, TimeoutSeconds= - --profile string The profile name to set. This will add the annotations related to profile to the service. To unset, specify the profile name followed by a "-" (e.g., name-). + --profile string The profile name is from the config definition. Related annotations and labels will be added to the service.To unset, specify the profile name followed by a "-" (e.g., name-). --pull-policy string Image pull policy. Valid values (case insensitive): Always | Never | IfNotPresent --pull-secret string Image pull secret to set. An empty argument ("") clears the pull secret. The referenced secret must exist in the service's namespace. --request strings The resource requirement requests for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource request, append "-" to the resource name, e.g. '--request cpu-'. diff --git a/docs/cmd/kn_service_update.md b/docs/cmd/kn_service_update.md index ea515f185b..a809147cb7 100644 --- a/docs/cmd/kn_service_update.md +++ b/docs/cmd/kn_service_update.md @@ -72,7 +72,7 @@ kn service update NAME --probe-liveness-opts string Add common options to liveness probe. Common opts (comma separated, case insensitive): InitialDelaySeconds=, FailureThreshold=, SuccessThreshold=, PeriodSeconds=, TimeoutSeconds= --probe-readiness string Add readiness probe to Service deployment. Supported probe types are HTTGet, Exec and TCPSocket. Format: [http,https]:host:port:path, exec:cmd[,cmd,...], tcp:host:port. --probe-readiness-opts string Add common options to readiness probe. Common opts (comma separated, case insensitive): InitialDelaySeconds=, FailureThreshold=, SuccessThreshold=, PeriodSeconds=, TimeoutSeconds= - --profile string The profile name to set. This will add the annotations related to profile to the service. To unset, specify the profile name followed by a "-" (e.g., name-). + --profile string The profile name is from the config definition. Related annotations and labels will be added to the service.To unset, specify the profile name followed by a "-" (e.g., name-). --pull-policy string Image pull policy. Valid values (case insensitive): Always | Never | IfNotPresent --pull-secret string Image pull secret to set. An empty argument ("") clears the pull secret. The referenced secret must exist in the service's namespace. --request strings The resource requirement requests for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource request, append "-" to the resource name, e.g. '--request cpu-'. diff --git a/pkg/kn/commands/service/configuration_edit_flags.go b/pkg/kn/commands/service/configuration_edit_flags.go index 0c443e0c5e..63a3263c6a 100644 --- a/pkg/kn/commands/service/configuration_edit_flags.go +++ b/pkg/kn/commands/service/configuration_edit_flags.go @@ -191,7 +191,7 @@ func (p *ConfigurationEditFlags) addSharedFlags(command *cobra.Command) { p.markFlagMakesRevision("timeout") command.Flags().StringVar(&p.Profile, "profile", "", - "The profile name to set. This will add the annotations related to profile to the service. "+ + "The profile name is from the config definition. Related annotations and labels will be added to the service."+ "To unset, specify the profile name followed by a \"-\" (e.g., name-).") p.markFlagMakesRevision("profile") } @@ -497,20 +497,38 @@ func (p *ConfigurationEditFlags) Apply( deleteProfile = false } - if len(knconfig.GlobalConfig.Profile(profileName).Annotations) > 0 { + if len(knconfig.GlobalConfig.Profile(profileName).Annotations) > 0 || len(knconfig.GlobalConfig.Profile(profileName).Labels) > 0 { annotations := knconfig.GlobalConfig.Profile(profileName).Annotations + labels := knconfig.GlobalConfig.Profile(profileName).Labels + profileAnnotations := make(util.StringMap) for _, value := range annotations { profileAnnotations[value.Name] = value.Value } + + profileLabels := make(util.StringMap) + for _, value := range labels { + profileLabels[value.Name] = value.Value + } + if deleteProfile { var annotationsToRemove []string for _, value := range annotations { annotationsToRemove = append(annotationsToRemove, value.Name) } + + var labelsToRemove []string + for _, value := range labels { + labelsToRemove = append(labelsToRemove, value.Name) + } + err = servinglib.UpdateRevisionTemplateAnnotations(template, map[string]string{}, annotationsToRemove) + updatedLabels := servinglib.UpdateLabels(service.ObjectMeta.Labels, map[string]string{}, labelsToRemove) + service.ObjectMeta.Labels = updatedLabels // In case service.ObjectMeta.Labels was nil } else { err = servinglib.UpdateRevisionTemplateAnnotations(template, profileAnnotations, []string{}) + updatedLabels := servinglib.UpdateLabels(service.ObjectMeta.Labels, profileLabels, []string{}) + service.ObjectMeta.Labels = updatedLabels // In case service.ObjectMeta.Labels was nil } if err != nil { diff --git a/pkg/kn/commands/service/configuration_edit_flags_test.go b/pkg/kn/commands/service/configuration_edit_flags_test.go index 0c05df43e5..32d80a8b91 100644 --- a/pkg/kn/commands/service/configuration_edit_flags_test.go +++ b/pkg/kn/commands/service/configuration_edit_flags_test.go @@ -114,6 +114,52 @@ profiles: 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") + assert.Equal(t, svc.ObjectMeta.Labels["environment"], "test") +} + +func TestDeleteProfileFlag(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, len(svc.Spec.Template.Annotations), 4) + + assert.Equal(t, svc.ObjectMeta.Labels["environment"], "test") + + cmd.SetArgs([]string{"--profile", "istio-"}) + cmd.Execute() + editFlags.Apply(&svc, nil, cmd) + + assert.Equal(t, svc.Spec.Template.Annotations["sidecar.istio.io/inject"], "") + assert.Equal(t, len(svc.Spec.Template.Annotations), 1) + + assert.Equal(t, svc.ObjectMeta.Labels["environment"], "") } func TestApplyProfileFlagError(t *testing.T) { diff --git a/pkg/kn/commands/service/create.go b/pkg/kn/commands/service/create.go index 26fa1679f2..24bf476ff9 100644 --- a/pkg/kn/commands/service/create.go +++ b/pkg/kn/commands/service/create.go @@ -83,7 +83,7 @@ var create_example = ` kn service create gitopstest --image knativesamples/helloworld --target=/user/knfiles/test.json # Create a service with profile - kn service create isoto --image knativesamples/helloworld --profile istio` + kn service create profiletest --image knativesamples/helloworld --profile istio` func NewServiceCreateCommand(p *commands.KnParams) *cobra.Command { var editFlags ConfigurationEditFlags diff --git a/pkg/kn/config/config.go b/pkg/kn/config/config.go index e0daafa865..3bf47b424a 100644 --- a/pkg/kn/config/config.go +++ b/pkg/kn/config/config.go @@ -65,6 +65,7 @@ type config struct { // channelTypeMappings is a list of channel type mapping channelTypeMappings []ChannelTypeMapping + // profiles is a map of profiles from the config file and built-in profiles profiles map[string]Profile } diff --git a/pkg/kn/config/config_test.go b/pkg/kn/config/config_test.go index 006642dda0..97b63dc7cb 100644 --- a/pkg/kn/config/config_test.go +++ b/pkg/kn/config/config_test.go @@ -63,6 +63,7 @@ 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.DeepEqual(t, GlobalConfig.Profile("istio").Annotations, []NamedValue{ { Name: "sidecar.istio.io/inject", diff --git a/pkg/kn/config/types.go b/pkg/kn/config/types.go index f9ea9881d7..ea3472aa20 100644 --- a/pkg/kn/config/types.go +++ b/pkg/kn/config/types.go @@ -74,14 +74,16 @@ type ChannelTypeMapping struct { Version string } +// NamedValue is the struct of name and values in the Profile struct type NamedValue struct { Name string `yaml:"name"` Value string `yaml:"value"` } +// Profile is the struct of profile config in kn config type Profile struct { Annotations []NamedValue `yaml:"annotations"` - Labels []NamedValue `yaml:"annotations"` + Labels []NamedValue `yaml:"labels"` } // config Keys for looking up in viper