From 0ac405d4b5d97a88d4af0e50071c9b4be6ea3a29 Mon Sep 17 00:00:00 2001 From: Gunjan Vyas Date: Wed, 13 Apr 2022 21:44:09 +0530 Subject: [PATCH] Add timeout option to service create (#1643) * Add timeout option to service create * Changed unit tests to reflect the default timeout value * Fixed timeout help message * Added unit test for --timeout flag * Moved --timeout flag to shared flags --- docs/cmd/kn_service_apply.md | 1 + docs/cmd/kn_service_create.md | 1 + docs/cmd/kn_service_update.md | 1 + pkg/kn/commands/service/configuration_edit_flags.go | 12 +++++++++++- pkg/kn/commands/service/create_mock_test.go | 1 + pkg/kn/commands/service/create_test.go | 11 +++++++++++ 6 files changed, 26 insertions(+), 1 deletion(-) diff --git a/docs/cmd/kn_service_apply.md b/docs/cmd/kn_service_apply.md index 978cc4ec0..341b94557 100644 --- a/docs/cmd/kn_service_apply.md +++ b/docs/cmd/kn_service_apply.md @@ -68,6 +68,7 @@ kn service apply s0 --filename my-svc.yml --scale-utilization int Percentage of concurrent requests utilization before scaling up. (default 70) --scale-window string Duration to look back for making auto-scaling decisions. The service is scaled to zero if no request was received in during that time. (eg: 10s) --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. + --timeout int Duration in seconds that the request routing layer will wait for a request delivered to a container to begin replying (default 300) --user int The user ID to run the container (e.g., 1001). --volume stringArray Add a volume from a ConfigMap (prefix cm: or config-map:) or a Secret (prefix secret: or sc:). Example: --volume myvolume=cm:myconfigmap or --volume myvolume=secret:mysecret. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --volume myvolume-. --wait Wait for 'service apply' operation to be completed. (default true) diff --git a/docs/cmd/kn_service_create.md b/docs/cmd/kn_service_create.md index 1a0c8ca91..9d7e6968c 100644 --- a/docs/cmd/kn_service_create.md +++ b/docs/cmd/kn_service_create.md @@ -95,6 +95,7 @@ kn service create NAME --image IMAGE --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. --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. --target string Work on local directory instead of a remote cluster (experimental) + --timeout int Duration in seconds that the request routing layer will wait for a request delivered to a container to begin replying (default 300) --user int The user ID to run the container (e.g., 1001). --volume stringArray Add a volume from a ConfigMap (prefix cm: or config-map:) or a Secret (prefix secret: or sc:). Example: --volume myvolume=cm:myconfigmap or --volume myvolume=secret:mysecret. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --volume myvolume-. --wait Wait for 'service create' operation to be completed. (default true) diff --git a/docs/cmd/kn_service_update.md b/docs/cmd/kn_service_update.md index 8c37dee85..9be0a55d1 100644 --- a/docs/cmd/kn_service_update.md +++ b/docs/cmd/kn_service_update.md @@ -82,6 +82,7 @@ kn service update NAME --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. --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. --target string Work on local directory instead of a remote cluster (experimental) + --timeout int Duration in seconds that the request routing layer will wait for a request delivered to a container to begin replying (default 300) --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 specified multiple times. --user int The user ID to run the container (e.g., 1001). diff --git a/pkg/kn/commands/service/configuration_edit_flags.go b/pkg/kn/commands/service/configuration_edit_flags.go index f66e98368..17b13e08f 100644 --- a/pkg/kn/commands/service/configuration_edit_flags.go +++ b/pkg/kn/commands/service/configuration_edit_flags.go @@ -21,8 +21,8 @@ import ( "strings" "github.com/spf13/cobra" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "knative.dev/serving/pkg/apis/config" knflags "knative.dev/client/pkg/kn/flags" servinglib "knative.dev/client/pkg/serving" @@ -54,6 +54,7 @@ type ConfigurationEditFlags struct { AnnotationsRevision []string ClusterLocal bool ScaleInit int + TimeoutSeconds int64 // Preferences about how to do the action. LockToDigest bool @@ -170,6 +171,11 @@ func (p *ConfigurationEditFlags) addSharedFlags(command *cobra.Command) { command.Flags().IntVar(&p.ScaleInit, "scale-init", 0, "Initial number of replicas with which a service starts. Can be 0 or a positive integer.") p.markFlagMakesRevision("scale-init") + + command.Flags().Int64Var(&p.TimeoutSeconds, "timeout", config.DefaultRevisionTimeoutSeconds, + "Duration in seconds that the request routing layer will wait for a request delivered to a "+""+ + "container to begin replying") + p.markFlagMakesRevision("timeout") } // AddUpdateFlags adds the flags specific to update. @@ -450,6 +456,10 @@ func (p *ConfigurationEditFlags) Apply( } } + if cmd.Flags().Changed("timeout") { + service.Spec.Template.Spec.TimeoutSeconds = &p.TimeoutSeconds + } + return nil } diff --git a/pkg/kn/commands/service/create_mock_test.go b/pkg/kn/commands/service/create_mock_test.go index 290fb91ed..67a83a6f2 100644 --- a/pkg/kn/commands/service/create_mock_test.go +++ b/pkg/kn/commands/service/create_mock_test.go @@ -478,6 +478,7 @@ func getService(name string) *servingv1.Service { Requests: corev1.ResourceList{}, }, }} + return service } diff --git a/pkg/kn/commands/service/create_test.go b/pkg/kn/commands/service/create_test.go index 1a9388519..e1e2a1e2d 100644 --- a/pkg/kn/commands/service/create_test.go +++ b/pkg/kn/commands/service/create_test.go @@ -193,6 +193,17 @@ func TestServiceCreateCommand(t *testing.T) { assert.DeepEqual(t, template.Spec.Containers[0].Command, []string{"sh", "/app/start.sh"}) } +func TestServiceCreateTimeout(t *testing.T) { + action, created, _, err := fakeServiceCreate([]string{ + "service", "create", "foo", "--image", "gcr.io/foo/bar:baz", "--timeout", "2"}, false) + assert.NilError(t, err) + assert.Assert(t, action.Matches("create", "services")) + + timeoutSeconds := *created.Spec.Template.Spec.TimeoutSeconds + assert.NilError(t, err) + assert.DeepEqual(t, int64(2), timeoutSeconds) +} + func TestServiceCreateArg(t *testing.T) { action, created, _, err := fakeServiceCreate([]string{ "service", "create", "foo", "--image", "gcr.io/foo/bar:baz",