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 --scale-activation flag to match new Serving option #1729

Merged
merged 4 commits into from
Sep 5, 2022
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
1 change: 1 addition & 0 deletions docs/cmd/kn_service_apply.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ kn service apply s0 --filename my-svc.yml
--request strings The resource requirement requests for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource request, append "-" to the resource name, e.g. '--request cpu-'.
--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 (e.g. {{.Service}}-{{.Random 5}}-{{.Generation}})
--scale string Set the Minimum and Maximum number of replicas. You can use this flag to set both to a single value, or set a range with min/max values, or set either min or max values without specifying the other. Example: --scale 5 (scale-min = 5, scale-max = 5) or --scale 1..5 (scale-min = 1, scale-max = 5) or --scale 1.. (scale-min = 1, scale-max = unchanged) or --scale ..5 (scale-min = unchanged, scale-max = 5)
--scale-activation int Minimum non-zero value that a service should scale to.
--scale-init int Initial number of replicas with which a service starts. Can be 0 or a positive integer.
--scale-max int Maximum number of replicas.
--scale-metric string Set the name of the metric the PodAutoscaler should scale on. Example: --scale-metric rps (to scale on rps) or --scale-metric concurrency (to scale on concurrency). The default metric is concurrency.
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 @@ -90,6 +90,7 @@ kn service create NAME --image IMAGE
--request strings The resource requirement requests for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource request, append "-" to the resource name, e.g. '--request cpu-'.
--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 (e.g. {{.Service}}-{{.Random 5}}-{{.Generation}})
--scale string Set the Minimum and Maximum number of replicas. You can use this flag to set both to a single value, or set a range with min/max values, or set either min or max values without specifying the other. Example: --scale 5 (scale-min = 5, scale-max = 5) or --scale 1..5 (scale-min = 1, scale-max = 5) or --scale 1.. (scale-min = 1, scale-max = unchanged) or --scale ..5 (scale-min = unchanged, scale-max = 5)
--scale-activation int Minimum non-zero value that a service should scale to.
--scale-init int Initial number of replicas with which a service starts. Can be 0 or a positive integer.
--scale-max int Maximum number of replicas.
--scale-metric string Set the name of the metric the PodAutoscaler should scale on. Example: --scale-metric rps (to scale on rps) or --scale-metric concurrency (to scale on concurrency). The default metric is concurrency.
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 @@ -77,6 +77,7 @@ kn service update NAME
--request strings The resource requirement requests for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource request, append "-" to the resource name, e.g. '--request cpu-'.
--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 (e.g. {{.Service}}-{{.Random 5}}-{{.Generation}})
--scale string Set the Minimum and Maximum number of replicas. You can use this flag to set both to a single value, or set a range with min/max values, or set either min or max values without specifying the other. Example: --scale 5 (scale-min = 5, scale-max = 5) or --scale 1..5 (scale-min = 1, scale-max = 5) or --scale 1.. (scale-min = 1, scale-max = unchanged) or --scale ..5 (scale-min = unchanged, scale-max = 5)
--scale-activation int Minimum non-zero value that a service should scale to.
--scale-init int Initial number of replicas with which a service starts. Can be 0 or a positive integer.
--scale-max int Maximum number of replicas.
--scale-metric string Set the name of the metric the PodAutoscaler should scale on. Example: --scale-metric rps (to scale on rps) or --scale-metric concurrency (to scale on concurrency). The default metric is concurrency.
Expand Down
8 changes: 8 additions & 0 deletions pkg/kn/commands/service/configuration_edit_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type ConfigurationEditFlags struct {
Scale string
MinScale int
MaxScale int
ScaleActivation int
ScaleTarget int
ScaleMetric string
ConcurrencyLimit int
Expand Down Expand Up @@ -94,6 +95,9 @@ func (p *ConfigurationEditFlags) addSharedFlags(command *cobra.Command) {
command.Flags().IntVar(&p.MaxScale, "scale-max", 0, "Maximum number of replicas.")
p.markFlagMakesRevision("scale-max")

command.Flags().IntVar(&p.ScaleActivation, "scale-activation", 0, "Minimum non-zero value that a service should scale to.")
p.markFlagMakesRevision("scale-activation")

command.Flags().StringVar(&p.ScaleMetric, "scale-metric", "", "Set the name of the metric the PodAutoscaler should scale on. "+
"Example: --scale-metric rps (to scale on rps) or --scale-metric concurrency (to scale on concurrency). The default metric is concurrency.")
p.markFlagMakesRevision("scale-metric")
Expand Down Expand Up @@ -358,6 +362,10 @@ func (p *ConfigurationEditFlags) Apply(
servinglib.UpdateScaleMetric(template, p.ScaleMetric)
}

if cmd.Flags().Changed("scale-activation") {
servinglib.UpdateScaleActivation(template, p.ScaleActivation)
}

if cmd.Flags().Changed("concurrency-limit") {
err = servinglib.UpdateConcurrencyLimit(template, int64(p.ConcurrencyLimit))
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions pkg/kn/commands/service/configuration_edit_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"gotest.tools/v3/assert"
"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/util"
"knative.dev/serving/pkg/apis/autoscaling"
)

func TestApplyPullPolicyFlag(t *testing.T) {
Expand Down Expand Up @@ -60,3 +61,17 @@ func TestScaleMetric(t *testing.T) {
err := editFlags.Apply(&svc, nil, cmd)
assert.NilError(t, err)
}

func TestScaleActivation(t *testing.T) {
var editFlags ConfigurationEditFlags
knParams := &commands.KnParams{}
cmd, _, _ := commands.CreateTestKnCommand(NewServiceCreateCommand(knParams), knParams)

editFlags.AddCreateFlags(cmd)
svc := createTestService("test-svc", []string{"test-svc-00001"}, goodConditions())
cmd.SetArgs([]string{"--scale-activation", "2"})
cmd.Execute()
err := editFlags.Apply(&svc, nil, cmd)
assert.NilError(t, err)
assert.Equal(t, svc.Spec.Template.Annotations[autoscaling.ActivationScaleKey], "2")
}
5 changes: 5 additions & 0 deletions pkg/serving/config_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ func UpdateScaleTarget(template *servingv1.RevisionTemplateSpec, target int) err
return UpdateRevisionTemplateAnnotation(template, autoscaling.TargetAnnotationKey, strconv.Itoa(target))
}

//UpdateScaleActivation updates the scale activation annotation
func UpdateScaleActivation(template *servingv1.RevisionTemplateSpec, activation int) error {
return UpdateRevisionTemplateAnnotation(template, autoscaling.ActivationScaleKey, strconv.Itoa(activation))
}

// UpdateScaleUtilization updates container target utilization percentage annotation
func UpdateScaleUtilization(template *servingv1.RevisionTemplateSpec, target int) error {
return UpdateRevisionTemplateAnnotation(template, autoscaling.TargetUtilizationPercentageKey, strconv.Itoa(target))
Expand Down
13 changes: 13 additions & 0 deletions test/e2e/service_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ func TestServiceOptions(t *testing.T) {
test.ServiceDelete(r, "svc17")
_, err = kubectl.Run("delete", "-n", it.Namespace(), "configmap", "test-cm2")
assert.NilError(t, err)

t.Log("create and validate a service with scale activation")
serviceCreateWithOptions(r, "svc18", "--scale-activation", "2")
validateServiceActivation(r, "svc18", 2)
}

func serviceCreateWithOptions(r *test.KnRunResultCollector, serviceName string, options ...string) {
Expand Down Expand Up @@ -384,3 +388,12 @@ func validateServiceEnvVariables(r *test.KnRunResultCollector, serviceName strin
}
}
}

func validateServiceActivation(r *test.KnRunResultCollector, serviceName string, activation int) {
out := r.KnTest().Kn().Run("service", "describe", serviceName, "-o", "json")
r.AssertNoError(out)
var svc servingv1.Service
json.Unmarshal([]byte(out.Stdout), &svc)
activationStr := strconv.Itoa(activation)
assert.Check(r.T(), svc.Spec.Template.Annotations[autoscaling.ActivationScaleKey] == activationStr)
}