diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 051621617..8a55b4bc8 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -58,6 +58,10 @@ | Add documentation for traffic splitting and tagging targets | https://github.com/knative/client/pull/331[#331] +| 🎁 +| Add --service-account flag +| https://github.com/knative/client/pull/401[#401] + |=== ## v0.2.0 (2019-07-10) diff --git a/docs/cmd/kn_service_create.md b/docs/cmd/kn_service_create.md index 39defc7bc..7af15a9ce 100644 --- a/docs/cmd/kn_service_create.md +++ b/docs/cmd/kn_service_create.md @@ -58,6 +58,7 @@ kn service create NAME --image IMAGE [flags] --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}}") + --service-account string Service account name to set. Empty service account name will result to clear the service account. --wait-timeout int Seconds to wait before giving up on waiting for service to be ready. (default 60) ``` diff --git a/docs/cmd/kn_service_update.md b/docs/cmd/kn_service_update.md index d448d8ab9..425062a70 100644 --- a/docs/cmd/kn_service_update.md +++ b/docs/cmd/kn_service_update.md @@ -56,6 +56,7 @@ kn service update NAME [flags] --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}}") + --service-account string Service account name to set. Empty service account name will result to clear the service account. --tag strings Set tag (format: --tag revisionRef=tagName) where revisionRef can be a revision or '@latest' string representing latest ready revision. This flag can be specified multiple times. --traffic strings Set traffic distribution (format: --traffic revisionRef=percent) where revisionRef can be a revision or a tag or '@latest' string representing latest ready revision. This flag can be given multiple times with percent summing up to 100%. --untag strings Untag revision (format: --untag tagName). This flag can be spcified multiple times. diff --git a/pkg/kn/commands/service/configuration_edit_flags.go b/pkg/kn/commands/service/configuration_edit_flags.go index 97630d0f9..d3ae45680 100644 --- a/pkg/kn/commands/service/configuration_edit_flags.go +++ b/pkg/kn/commands/service/configuration_edit_flags.go @@ -40,6 +40,7 @@ type ConfigurationEditFlags struct { Labels []string NamePrefix string RevisionName string + ServiceAccountName string // Preferences about how to do the action. LockToDigest bool @@ -107,7 +108,8 @@ func (p *ConfigurationEditFlags) addSharedFlags(command *cobra.Command) { "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)") // Don't mark as changing the revision. - + command.Flags().StringVar(&p.ServiceAccountName, "service-account", "", "Service account name to set. Empty service account name will result to clear the service account.") + p.markFlagMakesRevision("service-account") } // AddUpdateFlags adds the flags specific to update. @@ -254,6 +256,13 @@ func (p *ConfigurationEditFlags) Apply( } } + if cmd.Flags().Changed("service-account") { + err = servinglib.UpdateServiceAccountName(template, p.ServiceAccountName) + if err != nil { + return err + } + } + return nil } diff --git a/pkg/kn/commands/service/create_test.go b/pkg/kn/commands/service/create_test.go index 10cdc2080..0917472aa 100644 --- a/pkg/kn/commands/service/create_test.go +++ b/pkg/kn/commands/service/create_test.go @@ -472,3 +472,24 @@ func TestServiceCreateEnvForce(t *testing.T) { t.Fatalf("wrong output: %s", output) } } + +func TestServiceCreateWithServiceAccountName(t *testing.T) { + action, created, _, err := fakeServiceCreate([]string{ + "service", "create", "foo", "--image", "gcr.io/foo/bar:baz", + "--service-account", "foo-bar-account", + "--async"}, false, false) + + if err != nil { + t.Fatal(err) + } else if !action.Matches("create", "services") { + t.Fatalf("Bad action %v", action) + } + + template, err := servinglib.RevisionTemplateOfService(created) + + if err != nil { + t.Fatal(err) + } else if template.Spec.ServiceAccountName != "foo-bar-account" { + t.Fatalf("wrong service account name:%v", template.Spec.ServiceAccountName) + } +} diff --git a/pkg/serving/config_changes.go b/pkg/serving/config_changes.go index bf9b6269a..ac9e0c449 100644 --- a/pkg/serving/config_changes.go +++ b/pkg/serving/config_changes.go @@ -245,6 +245,13 @@ func UpdateLabels(service *servingv1alpha1.Service, template *servingv1alpha1.Re return nil } +// UpdateServiceAccountName updates the service account name used for the corresponding knative service +func UpdateServiceAccountName(template *servingv1alpha1.RevisionTemplateSpec, serviceAccountName string) error { + serviceAccountName = strings.TrimSpace(serviceAccountName) + template.Spec.ServiceAccountName = serviceAccountName + return nil +} + // ======================================================================================= func updateEnvVarsFromMap(env []corev1.EnvVar, toUpdate map[string]string) []corev1.EnvVar { diff --git a/pkg/serving/config_changes_test.go b/pkg/serving/config_changes_test.go index a36385435..3f42e395f 100644 --- a/pkg/serving/config_changes_test.go +++ b/pkg/serving/config_changes_test.go @@ -408,6 +408,17 @@ func TestUpdateLabelsRemoveExisting(t *testing.T) { assert.DeepEqual(t, expected, actual) } +func TestUpdateServiceAccountName(t *testing.T) { + template, _ := getV1alpha1RevisionTemplateWithOldFields() + template.Spec.ServiceAccountName = "" + + UpdateServiceAccountName(template, "foo-bar") + assert.Equal(t, template.Spec.ServiceAccountName, "foo-bar") + + UpdateServiceAccountName(template, "") + assert.Equal(t, template.Spec.ServiceAccountName, "") +} + // ========================================================================================================= func getV1alpha1RevisionTemplateWithOldFields() (*servingv1alpha1.RevisionTemplateSpec, *corev1.Container) {