diff --git a/pkg/kn/commands/service/configuration_edit_flags.go b/pkg/kn/commands/service/configuration_edit_flags.go index 17c9798aff..4da31dbecd 100644 --- a/pkg/kn/commands/service/configuration_edit_flags.go +++ b/pkg/kn/commands/service/configuration_edit_flags.go @@ -24,6 +24,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/serving/pkg/apis/config" + knconfig "knative.dev/client/pkg/kn/config" knflags "knative.dev/client/pkg/kn/flags" servinglib "knative.dev/client/pkg/serving" "knative.dev/client/pkg/util" @@ -57,6 +58,7 @@ type ConfigurationEditFlags struct { ClusterLocal bool ScaleInit int TimeoutSeconds int64 + Profile string // Preferences about how to do the action. LockToDigest bool @@ -187,6 +189,11 @@ func (p *ConfigurationEditFlags) addSharedFlags(command *cobra.Command) { "Duration in seconds that the request routing layer will wait for a request delivered to a "+""+ "container to begin replying") 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. "+ + "To unset, specify the profile name followed by a \"-\" (e.g., name-).") + p.markFlagMakesRevision("profile") } // AddUpdateFlags adds the flags specific to update. @@ -479,6 +486,16 @@ func (p *ConfigurationEditFlags) Apply( service.Spec.Template.Spec.TimeoutSeconds = &p.TimeoutSeconds } + if cmd.Flags().Changed("profile") { + if len(knconfig.GlobalConfig.Profile(p.Profile).Annotations) > 0 { + return fmt.Errorf("profile doesn't exist %s", p.Profile) + } else if len(knconfig.GlobalConfig.Profile(p.Profile).Annotations) == 0 { + return fmt.Errorf("profile %s doesn't contain any annotations.", p.Profile) + } else { + return fmt.Errorf("profile %s doesn't exist.", p.Profile) + } + } + return nil } diff --git a/pkg/kn/commands/service/configuration_edit_flags_test.go b/pkg/kn/commands/service/configuration_edit_flags_test.go index 853407e863..b258ba71ec 100644 --- a/pkg/kn/commands/service/configuration_edit_flags_test.go +++ b/pkg/kn/commands/service/configuration_edit_flags_test.go @@ -75,3 +75,29 @@ func TestScaleActivation(t *testing.T) { assert.NilError(t, err) assert.Equal(t, svc.Spec.Template.Annotations[autoscaling.ActivationScaleKey], "2") } + +func TestApplyProfileFlagError(t *testing.T) { + var editFlags ConfigurationEditFlags + knParams := &commands.KnParams{} + cmd, _, _ := commands.CreateTestKnCommand(NewServiceCreateCommand(knParams), knParams) + + editFlags.AddCreateFlags(cmd) + svc := createTestService("test-svc", []string{"test-svc-00001", "test-svc-00002"}, goodConditions()) + cmd.SetArgs([]string{"--profile", "invalidprofile"}) + cmd.Execute() + err := editFlags.Apply(&svc, nil, cmd) + assert.Assert(t, util.ContainsAll(err.Error(), "profile", "invalidprofile")) +} + +func TestApplyProfileFlagAnnotationError(t *testing.T) { + var editFlags ConfigurationEditFlags + knParams := &commands.KnParams{} + cmd, _, _ := commands.CreateTestKnCommand(NewServiceCreateCommand(knParams), knParams) + + editFlags.AddCreateFlags(cmd) + svc := createTestService("test-svc", []string{"test-svc-00001", "test-svc-00002"}, goodConditions()) + cmd.SetArgs([]string{"--profile", "istio"}) + cmd.Execute() + err := editFlags.Apply(&svc, nil, cmd) + assert.Assert(t, util.ContainsAll(err.Error(), "profile", "istio", "doesn't contain any annotations")) +} diff --git a/pkg/kn/commands/service/create.go b/pkg/kn/commands/service/create.go index d3d60561c4..26fa1679f2 100644 --- a/pkg/kn/commands/service/create.go +++ b/pkg/kn/commands/service/create.go @@ -80,7 +80,10 @@ var create_example = ` # Create the service in offline mode instead of kubernetes cluster (Beta) kn service create gitopstest -n test-ns --image knativesamples/helloworld --target=/user/knfiles kn service create gitopstest --image knativesamples/helloworld --target=/user/knfiles/test.yaml - kn service create gitopstest --image knativesamples/helloworld --target=/user/knfiles/test.json` + 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` func NewServiceCreateCommand(p *commands.KnParams) *cobra.Command { var editFlags ConfigurationEditFlags diff --git a/pkg/kn/config/types.go b/pkg/kn/config/types.go index e0997c164a..9ba92ea133 100644 --- a/pkg/kn/config/types.go +++ b/pkg/kn/config/types.go @@ -78,8 +78,6 @@ type Profile struct { Annotations map[string]string `yaml:"annotations"` } -type Profiles map[string]Profile - // config Keys for looking up in viper const ( keyFeaturesContextSharing = "features.context-sharing"