Add --service-account-name flag (#401)

* 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
This commit is contained in:
Ingwon Song 2019-09-23 04:45:13 -07:00 committed by Knative Prow Robot
parent 43f8386b82
commit 14ec594e56
7 changed files with 55 additions and 1 deletions

View File

@ -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)

View File

@ -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)
```

View File

@ -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.

View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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 {

View File

@ -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) {