From 7e1591d87c7c3608b35ab6b1bd0b6acfd0d6ce64 Mon Sep 17 00:00:00 2001 From: Navid Shaikh Date: Tue, 21 Jan 2020 20:20:59 +0530 Subject: [PATCH] Add --pull-secrets flag Fixes #616 - Add --pull-secrets flag for service create/update operations - Setting empty string to flag clears the pull secrets - List ImagePullSecrets for service in `service describe` default output - Run e2e tests against latets serving v0.12.0 (ImagePullSecrets introduced in v0.11.1 release) --- docs/cmd/kn_service_create.md | 1 + docs/cmd/kn_service_update.md | 1 + pkg/kn/commands/service/configuration_edit_flags.go | 10 ++++++++++ pkg/kn/commands/service/describe.go | 3 +++ pkg/serving/config_changes.go | 12 ++++++++++++ pkg/serving/config_changes_test.go | 11 +++++++++++ 6 files changed, 38 insertions(+) diff --git a/docs/cmd/kn_service_create.md b/docs/cmd/kn_service_create.md index 44ab350a27..5b793c3d39 100644 --- a/docs/cmd/kn_service_create.md +++ b/docs/cmd/kn_service_create.md @@ -62,6 +62,7 @@ kn service create NAME --image IMAGE [flags] -n, --namespace string Specify the namespace to operate in. --no-lock-to-digest do not keep the running image for the service constant when not explicitly specifying the image. (--no-lock-to-digest pulls the image tag afresh with each new revision) -p, --port int32 The port where application listens on. + --pull-secrets string Image pull secrets to set. Empty image pull secrets will result to clear the pull secrets. --requests-cpu string The requested CPU (e.g., 250m). --requests-memory string The requested memory (e.g., 64Mi). --revision-name string The revision name to set. Must start with the service name and a dash as a prefix. Empty revision name will result in the server generating a name for the revision. Accepts golang templates, allowing {{.Service}} for the service name, {{.Generation}} for the generation, and {{.Random [n]}} for n random consonants. (default "{{.Service}}-{{.Random 5}}-{{.Generation}}") diff --git a/docs/cmd/kn_service_update.md b/docs/cmd/kn_service_update.md index 35f40c0d5a..c26ecccfa3 100644 --- a/docs/cmd/kn_service_update.md +++ b/docs/cmd/kn_service_update.md @@ -57,6 +57,7 @@ kn service update NAME [flags] -n, --namespace string Specify the namespace to operate in. --no-lock-to-digest do not keep the running image for the service constant when not explicitly specifying the image. (--no-lock-to-digest pulls the image tag afresh with each new revision) -p, --port int32 The port where application listens on. + --pull-secrets string Image pull secrets to set. Empty image pull secrets will result to clear the pull secrets. --requests-cpu string The requested CPU (e.g., 250m). --requests-memory string The requested memory (e.g., 64Mi). --revision-name string The revision name to set. Must start with the service name and a dash as a prefix. Empty revision name will result in the server generating a name for the revision. Accepts golang templates, allowing {{.Service}} for the service name, {{.Generation}} for the generation, and {{.Random [n]}} for n random consonants. (default "{{.Service}}-{{.Random 5}}-{{.Generation}}") diff --git a/pkg/kn/commands/service/configuration_edit_flags.go b/pkg/kn/commands/service/configuration_edit_flags.go index 3aa6ea439d..cdd87f5d08 100644 --- a/pkg/kn/commands/service/configuration_edit_flags.go +++ b/pkg/kn/commands/service/configuration_edit_flags.go @@ -47,6 +47,7 @@ type ConfigurationEditFlags struct { NamePrefix string RevisionName string ServiceAccountName string + ImagePullSecrets string Annotations []string // Preferences about how to do the action. @@ -148,6 +149,11 @@ func (p *ConfigurationEditFlags) addSharedFlags(command *cobra.Command) { "any number of times to set multiple annotations. "+ "To unset, specify the annotation name followed by a \"-\" (e.g., name-).") p.markFlagMakesRevision("annotation") + command.Flags().StringVar(&p.ImagePullSecrets, + "pull-secrets", + "", + "Image pull secrets to set. Empty image pull secrets will result to clear the pull secrets.") + p.markFlagMakesRevision("pull-secrets") } // AddUpdateFlags adds the flags specific to update. @@ -347,6 +353,10 @@ func (p *ConfigurationEditFlags) Apply( } } + if cmd.Flags().Changed("pull-secrets") { + servinglib.UpdateImagePullSecrets(template, p.ImagePullSecrets) + } + return nil } diff --git a/pkg/kn/commands/service/describe.go b/pkg/kn/commands/service/describe.go index c942f1463d..23a6c7b0a3 100644 --- a/pkg/kn/commands/service/describe.go +++ b/pkg/kn/commands/service/describe.go @@ -171,6 +171,9 @@ func writeService(dw printers.PrefixWriter, service *v1alpha1.Service) { if (service.Spec.Template != nil) && (service.Spec.Template.Spec.ServiceAccountName != "") { dw.WriteAttribute("ServiceAccount", service.Spec.Template.Spec.ServiceAccountName) } + if service.Spec.Template != nil && service.Spec.Template.Spec.ImagePullSecrets != nil { + dw.WriteAttribute("ImagePullSecrets", service.Spec.Template.Spec.ImagePullSecrets[0].Name) + } } // Write out revisions associated with this service. By default only active diff --git a/pkg/serving/config_changes.go b/pkg/serving/config_changes.go index 53f064b945..86b9a3a8d5 100644 --- a/pkg/serving/config_changes.go +++ b/pkg/serving/config_changes.go @@ -407,6 +407,18 @@ func UpdateServiceAccountName(template *servingv1alpha1.RevisionTemplateSpec, se return nil } +// UpdateImagePullSecrets updates the image pull secrets used for the corresponding knative service +func UpdateImagePullSecrets(template *servingv1alpha1.RevisionTemplateSpec, pullsecrets string) { + pullsecrets = strings.TrimSpace(pullsecrets) + if pullsecrets == "" { + template.Spec.ImagePullSecrets = nil + } else { + template.Spec.ImagePullSecrets = []corev1.LocalObjectReference{{ + Name: pullsecrets, + }} + } +} + // GenerateVolumeName generates a volume name with respect to a given path string. // Current implementation basically sanitizes the path string by changing "/" into "." // To reduce any chance of duplication, a checksum part generated from the path string is appended to the sanitized string. diff --git a/pkg/serving/config_changes_test.go b/pkg/serving/config_changes_test.go index a431bf3b16..46fa666a78 100644 --- a/pkg/serving/config_changes_test.go +++ b/pkg/serving/config_changes_test.go @@ -562,6 +562,17 @@ func TestUpdateServiceAccountName(t *testing.T) { assert.Equal(t, template.Spec.ServiceAccountName, "") } +func TestUpdateImagePullSecrets(t *testing.T) { + template, _ := getV1alpha1RevisionTemplateWithOldFields() + template.Spec.ImagePullSecrets = nil + + UpdateImagePullSecrets(template, "quay") + assert.Equal(t, template.Spec.ImagePullSecrets[0].Name, "quay") + + UpdateImagePullSecrets(template, " ") + assert.Check(t, template.Spec.ImagePullSecrets == nil) +} + func TestUpdateAnnotationsNew(t *testing.T) { service, template, _ := getV1alpha1Service()