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 changes for --scale-init support #990

Merged
merged 9 commits into from
Sep 15, 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
1 change: 1 addition & 0 deletions docs/cmd/kn_service_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ kn service create NAME --image IMAGE
--requests-memory string DEPRECATED: please use --request instead. 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}}")
--scale int Minimum and maximum number of replicas.
--scale-init int Initial scale for a service
--scale-max int Maximum number of replicas.
--scale-min int Minimum number of replicas.
--service-account string Service account name to set. An empty argument ("") clears the service account. The referenced service account must exist in the service's namespace.
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 @@ -70,6 +70,7 @@ kn service update NAME
--requests-memory string DEPRECATED: please use --request instead. 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}}")
--scale int Minimum and maximum number of replicas.
--scale-init int Initial scale for a service
hemanrnjn marked this conversation as resolved.
Show resolved Hide resolved
--scale-max int Maximum number of replicas.
--scale-min int Minimum number of replicas.
--service-account string Service account name to set. An empty argument ("") clears the service account. The referenced service account must exist in the service's namespace.
Expand Down
27 changes: 27 additions & 0 deletions pkg/kn/commands/service/configuration_edit_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package service

import (
"fmt"
"strconv"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,6 +50,7 @@ type ConfigurationEditFlags struct {
RevisionName string
Annotations []string
ClusterLocal bool
ScaleInit int

// Preferences about how to do the action.
LockToDigest bool
Expand Down Expand Up @@ -149,6 +151,9 @@ func (p *ConfigurationEditFlags) addSharedFlags(command *cobra.Command) {
"any number of times to set multiple annotations. "+
"To unset, specify the annotation name followed by a \"-\" (e.g., name-).")
p.markFlagMakesRevision("annotation")

command.Flags().IntVar(&p.ScaleInit, "scale-init", 0, "Initial scale for a service")
hemanrnjn marked this conversation as resolved.
Show resolved Hide resolved
p.markFlagMakesRevision("scale-init")
}

// AddUpdateFlags adds the flags specific to update.
Expand Down Expand Up @@ -427,6 +432,28 @@ func (p *ConfigurationEditFlags) Apply(
servinglib.UpdateUser(template, p.PodSpecFlags.User)
}

if cmd.Flags().Changed("scale-init") {
contains := func(s []string, e string) bool {
for _, a := range s {
if strings.Contains(a, e) {
hemanrnjn marked this conversation as resolved.
Show resolved Hide resolved
return true
}
}
return false
}

if cmd.Flags().Changed("annotation") && contains(p.Annotations, "autoscaling.knative.dev/initialScale") {
return fmt.Errorf("only one of the --scale-init or --annotation autoscaling.knative.dev/initialScale can be specified")
hemanrnjn marked this conversation as resolved.
Show resolved Hide resolved
hemanrnjn marked this conversation as resolved.
Show resolved Hide resolved
}
annotationsMap := make(map[string]string)
annotationsMap["autoscaling.knative.dev/initialScale"] = strconv.Itoa(p.ScaleInit)
hemanrnjn marked this conversation as resolved.
Show resolved Hide resolved
hemanrnjn marked this conversation as resolved.
Show resolved Hide resolved

err = servinglib.UpdateAnnotations(service, template, annotationsMap, []string{})
if err != nil {
return err
}
}

return nil
}

Expand Down
33 changes: 33 additions & 0 deletions pkg/kn/commands/service/create_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,39 @@ func getService(name string) *servingv1.Service {
return service
}

func TestServiceCreateWithInitScaleAsOption(t *testing.T) {
client := knclient.NewMockKnServiceClient(t)

r := client.Recorder()

// Check for existing service --> no
r.GetService("foo", nil, errors.NewNotFound(servingv1.Resource("service"), "foo"))
// Create service (don't validate given service --> "Any()" arg is allowed)
r.CreateService(mock.Any(), nil)
// Wait for service to become ready
r.WaitForService("foo", mock.Any(), wait.NoopMessageCallback(), nil, time.Second)
// Get for showing the URL
r.GetService("foo", getServiceWithUrl("foo", "http://foo.example.com"), nil)

output, err := executeServiceCommand(client, "create", "foo", "--image", "gcr.io/foo/bar:baz", "--scale-init", "0")
assert.NilError(t, err)
assert.Assert(t, util.ContainsAll(output, "created", "foo", "default"))

r.Validate()
}

func TestServiceCreateWithBothAnnotationAndInitScaleAsOption(t *testing.T) {
client := knclient.NewMockKnServiceClient(t)

r := client.Recorder()

output, err := executeServiceCommand(client, "create", "foo", "--image", "gcr.io/foo/bar:baz", "--annotation", "autoscaling.knative.dev/initialScale=0", "--scale-init", "0")
assert.Assert(t, err != nil)
assert.Assert(t, util.ContainsAll(output, "only one of the", "--scale-init", "--annotation", "autoscaling.knative.dev/initialScale", "can be specified"))

r.Validate()
}

func getServiceWithUrl(name string, urlName string) *servingv1.Service {
service := servingv1.Service{}
url, _ := apis.ParseURL(urlName)
Expand Down