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

feat: Add support to combine service create --filename with other options #937

Merged
merged 8 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion docs/cmd/kn_service_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ kn service create NAME --image IMAGE
--concurrency-utilization int Percentage of concurrent requests utilization before scaling up. (default 70)
-e, --env stringArray Environment variable to set. NAME=value; you may provide this flag any number of times to set multiple environment variables. To unset, specify the environment variable name followed by a "-" (e.g., NAME-).
--env-from stringArray Add environment variables from a ConfigMap (prefix cm: or config-map:) or a Secret (prefix secret:). Example: --env-from cm:myconfigmap or --env-from secret:mysecret. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --env-from cm:myconfigmap-.
-f, --filename string Create a service from file.
-f, --filename string Create a service from file. The created service can be further modified by combining with other options.Example: -f /path/to/file --env NAME=value will also add environment variable.
Copy link
Contributor

Choose a reason for hiding this comment

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

"options.Example:" => "options, e.g.," OR "options. For example:"

Copy link
Contributor

Choose a reason for hiding this comment

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

"also add environment variable" --> "also add an environment variable"

--force Create service forcefully, replaces existing service if any.
-h, --help help for create
--image string Image to run.
Expand Down
5 changes: 4 additions & 1 deletion pkg/kn/commands/service/configuration_edit_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,11 @@ func (p *ConfigurationEditFlags) AddCreateFlags(command *cobra.Command) {
p.addSharedFlags(command)
command.Flags().BoolVar(&p.ForceCreate, "force", false,
"Create service forcefully, replaces existing service if any.")
command.Flags().StringVarP(&p.Filename, "filename", "f", "", "Create a service from file.")
command.Flags().StringVarP(&p.Filename, "filename", "f", "", "Create a service from file. "+
"The created service can be further modified by combining with other options."+
"Example: -f /path/to/file --env NAME=value will also add environment variable.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above. Use same resolution

command.MarkFlagFilename("filename")
p.markFlagMakesRevision("filename")
}

// Apply mutates the given service according to the flags in the command.
Expand Down
5 changes: 2 additions & 3 deletions pkg/kn/commands/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,11 @@ func constructServiceFromFile(cmd *cobra.Command, editFlags ConfigurationEditFla
// Set namespace in case it's specified as --namespace
service.ObjectMeta.Namespace = namespace

// We need to generate revision to have --force replace working
revName, err := servinglib.GenerateRevisionName(editFlags.RevisionName, &service)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why has this been removed ?

Copy link
Contributor Author

@dsimansk dsimansk Jul 21, 2020

Choose a reason for hiding this comment

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

It's superseded by combination of markFlagMakesRevision and editFlags.Apply, per my suggestion/proposal from previous PR.

https://github.com/knative/client/pull/937/files#diff-74b189b0b506d256ede4322475d950f5R285

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, and I think it makes sense.

// Apply options provided from cmdline
err = editFlags.Apply(&service, nil, cmd)
if err != nil {
return nil, err
}
Comment on lines 308 to 310
Copy link
Contributor

Choose a reason for hiding this comment

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

I could comment these lines and all tests are still passing. So this tells me no test for errors when editFlags.Apply(...)

Copy link
Contributor

Choose a reason for hiding this comment

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

But isn't editFlags.Apply() already covered ? Ideally, there is a dedicated unit test only for this method, and should not be tested indirectly within tests that test that intend to test other stuff.

Copy link
Contributor

Choose a reason for hiding this comment

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

If I look at the overall test suite I see, that editFlags.Apply() is already tested to a high degree. Looking at that method though it looks like that it deserves some refactoring, it is far too large.

service.Spec.Template.Name = revName

return &service, nil
}
62 changes: 62 additions & 0 deletions pkg/kn/commands/service/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,3 +973,65 @@ func TestServiceCreateInvalidDataYAML(t *testing.T) {
_, _, _, err = fakeServiceCreate([]string{"service", "create", "foo", "--filename", tempFile}, false)
assert.Assert(t, util.ContainsAll(err.Error(), "found", "tab", "violates", "indentation"))
}

func TestServiceCreateFromYAMLWithOverride(t *testing.T) {
tempDir, err := ioutil.TempDir("", "kn-file")
defer os.RemoveAll(tempDir)
assert.NilError(t, err)

tempFile := filepath.Join(tempDir, "service.yaml")
err = ioutil.WriteFile(tempFile, []byte(serviceYAML), os.FileMode(0666))
assert.NilError(t, err)
// Merge env vars
expectedEnvVars := map[string]string{
"TARGET": "Go Sample v1",
"FOO": "BAR"}
action, created, _, err := fakeServiceCreate([]string{
"service", "create", "foo", "--filename", tempFile, "--env", "FOO=BAR"}, false)
assert.NilError(t, err)
assert.Assert(t, action.Matches("create", "services"))
assert.Equal(t, created.Name, "foo")

actualEnvVar, err := servinglib.EnvToMap(created.Spec.Template.Spec.GetContainer().Env)
assert.NilError(t, err)
assert.DeepEqual(t, actualEnvVar, expectedEnvVars)

// Override env vars
expectedEnvVars = map[string]string{
"TARGET": "FOOBAR",
"FOO": "BAR"}
action, created, _, err = fakeServiceCreate([]string{
"service", "create", "foo", "--filename", tempFile, "--env", "TARGET=FOOBAR", "--env", "FOO=BAR"}, false)
assert.NilError(t, err)
assert.Assert(t, action.Matches("create", "services"))
assert.Equal(t, created.Name, "foo")

actualEnvVar, err = servinglib.EnvToMap(created.Spec.Template.Spec.GetContainer().Env)
assert.NilError(t, err)
assert.DeepEqual(t, actualEnvVar, expectedEnvVars)

// Remove existing env vars
expectedEnvVars = map[string]string{
"FOO": "BAR"}
action, created, _, err = fakeServiceCreate([]string{
"service", "create", "foo", "--filename", tempFile, "--env", "TARGET-", "--env", "FOO=BAR"}, false)
assert.NilError(t, err)
assert.Assert(t, action.Matches("create", "services"))
assert.Equal(t, created.Name, "foo")

actualEnvVar, err = servinglib.EnvToMap(created.Spec.Template.Spec.GetContainer().Env)
assert.NilError(t, err)
assert.DeepEqual(t, actualEnvVar, expectedEnvVars)

// Multiple edit flags
expectedAnnotations := map[string]string{
"foo": "bar"}
action, created, _, err = fakeServiceCreate([]string{"service", "create", "foo", "--filename", tempFile,
"--service-account", "foo", "--cmd", "/foo/bar", "-a", "foo=bar"}, false)
assert.NilError(t, err)
assert.Assert(t, action.Matches("create", "services"))
assert.Equal(t, created.Name, "foo")
assert.DeepEqual(t, created.Spec.Template.Spec.GetContainer().Command, []string{"/foo/bar"})
assert.Equal(t, created.Spec.Template.Spec.ServiceAccountName, "foo")
assert.DeepEqual(t, created.ObjectMeta.Annotations, expectedAnnotations)
}