Skip to content

Commit

Permalink
Added test cases for delete profile
Browse files Browse the repository at this point in the history
  • Loading branch information
sharmaansh21 authored and Anshul Sharma committed Dec 7, 2023
1 parent a4a2ac6 commit c3034c3
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/cmd/kn_service_apply.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<int_value>, FailureThreshold=<int_value>, SuccessThreshold=<int_value>, PeriodSeconds=<int_value>, TimeoutSeconds=<int_value>
--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=<int_value>, FailureThreshold=<int_value>, SuccessThreshold=<int_value>, PeriodSeconds=<int_value>, TimeoutSeconds=<int_value>
--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-'.
Expand Down
4 changes: 2 additions & 2 deletions docs/cmd/kn_service_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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=<int_value>, FailureThreshold=<int_value>, SuccessThreshold=<int_value>, PeriodSeconds=<int_value>, TimeoutSeconds=<int_value>
--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=<int_value>, FailureThreshold=<int_value>, SuccessThreshold=<int_value>, PeriodSeconds=<int_value>, TimeoutSeconds=<int_value>
--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-'.
Expand Down
2 changes: 1 addition & 1 deletion docs/cmd/kn_service_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<int_value>, FailureThreshold=<int_value>, SuccessThreshold=<int_value>, PeriodSeconds=<int_value>, TimeoutSeconds=<int_value>
--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=<int_value>, FailureThreshold=<int_value>, SuccessThreshold=<int_value>, PeriodSeconds=<int_value>, TimeoutSeconds=<int_value>
--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-'.
Expand Down
22 changes: 20 additions & 2 deletions pkg/kn/commands/service/configuration_edit_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down
46 changes: 46 additions & 0 deletions pkg/kn/commands/service/configuration_edit_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/kn/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions pkg/kn/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion pkg/kn/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c3034c3

Please sign in to comment.