Skip to content

Commit

Permalink
Add --service-account-name flag (#401)
Browse files Browse the repository at this point in the history
* Add --service-account-name flag

* Change the flag --service-account-name to --service-account, and change its default value from "-" to an empty string. In addition, CHANGELOG.adoc is changed.

* Update docs with ./hack/build.sh
  • Loading branch information
igsong authored and knative-prow-robot committed Sep 23, 2019
1 parent 43f8386 commit 14ec594
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_service_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_service_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion pkg/kn/commands/service/configuration_edit_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type ConfigurationEditFlags struct {
Labels []string
NamePrefix string
RevisionName string
ServiceAccountName string

// Preferences about how to do the action.
LockToDigest bool
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down
21 changes: 21 additions & 0 deletions pkg/kn/commands/service/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 7 additions & 0 deletions pkg/serving/config_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions pkg/serving/config_changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 14ec594

Please sign in to comment.