Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --service-account-name flag #401

Merged
merged 4 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, it's a bit nit-picky, but using a --service-account without arg during a kn service create is a no-op and it won't clear the service account (as there is one). I know that this option is shared, but maybe we can use still different help messages for create & update ?

happy to get this PR merged now, but maybe we can adjust this in a later PR.

--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 @@ -233,6 +233,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, vars 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 @@ -415,6 +415,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