From 81d40a2f51fb6aac1084e69415256b87abdb641f Mon Sep 17 00:00:00 2001 From: zzxwill Date: Wed, 4 Nov 2020 11:39:05 +0800 Subject: [PATCH] Wrongly rebased newly merged requests from comments, fixed it. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit co-authored-by: 天元 --- .../templates/defwithtemplate/autoscale.yaml | 29 ++++----- docs/README.md | 2 +- .../references/traits/autoscaling.md | 0 docs/developers/set-autoscale.md | 65 +++++++++++++++++++ docs/developers/set-autoscaling.md | 3 - hack/vela-templates/cue/autoscale.cue | 10 +-- pkg/commands/status.go | 2 +- .../autoscaler/autoscaler_controller.go | 9 ++- 8 files changed, 90 insertions(+), 30 deletions(-) delete mode 100644 docs/developers/references/traits/autoscaling.md create mode 100644 docs/developers/set-autoscale.md delete mode 100644 docs/developers/set-autoscaling.md diff --git a/charts/vela-core/templates/defwithtemplate/autoscale.yaml b/charts/vela-core/templates/defwithtemplate/autoscale.yaml index 1d4653132..a444f1c5b 100644 --- a/charts/vela-core/templates/defwithtemplate/autoscale.yaml +++ b/charts/vela-core/templates/defwithtemplate/autoscale.yaml @@ -16,14 +16,14 @@ spec: definitionRef: name: autoscalers.standard.oam.dev extension: - template: |- + template: | output: { apiVersion: "standard.oam.dev/v1alpha1" kind: "Autoscaler" spec: { minReplicas: parameter.minReplicas maxReplicas: parameter.maxReplicas - triggers: [...{ + triggers: [{ name: parameter.name type: parameter.type condition: { @@ -33,18 +33,17 @@ spec: replicas: parameter.replicas timezone: parameter.timezone } - }] + }, ...] } } - parameter: { - minReplicas: *1 | int - maxReplicas: *4 | int - name: *"" | string - type: *"cron" | string - startAt: *"00:01" | string - duration: *"" | string - days: *"Monday, Tuesday" | string - replicas: *"2" | string - timezone: *"Asia/Shanghai" | string - } - + parameter: { + minReplicas: *1 | int + maxReplicas: *4 | int + name: *"" | string + type: *"cron" | string + startAt: string + duration: string + days: string + replicas: *"2" | string + timezone: *"Asia/Shanghai" | string + } diff --git a/docs/README.md b/docs/README.md index 2e8dfc659..7f9839869 100644 --- a/docs/README.md +++ b/docs/README.md @@ -12,7 +12,7 @@ Learn and use KubeVela with tutorials and user stories. - [Setting Up Deployment Environment](developers/config-enviroments.md) - [Deploying Application](developers/deploy-app.md) - [Setting Routes](developers/set-route.md) -- [Setting Auto-scaling Policy](developers/set-autoscaling.md) +- [Setting Auto-scaling Policy](developers/set-autoscale.md) - [Setting Rollout Strategy](developers/set-rollout.md) - [Monitoring Application](developers/set-metrics.md) - [Using Appfile](developers/devex/appfile.md) diff --git a/docs/developers/references/traits/autoscaling.md b/docs/developers/references/traits/autoscaling.md deleted file mode 100644 index e69de29bb..000000000 diff --git a/docs/developers/set-autoscale.md b/docs/developers/set-autoscale.md new file mode 100644 index 000000000..74c636507 --- /dev/null +++ b/docs/developers/set-autoscale.md @@ -0,0 +1,65 @@ +# Automatically scale workload by cron or resource utilization + +## Prerequisites + - [ ] [KEDA v2.0 Beta](https://keda.sh/blog/keda-2.0-beta/) + + KEDA will be automatically deployed during vela installation, so just run the following command. + ```shell + $ vela install + ``` + +## Scale an application + +- Deploy an application + + Run the following command to deploy application `helloworld`. + + ``` + $ vela svc deploy frontend -t webservice -a helloworld --image nginx:1.9.2 --port 80 + App helloworld deployed + ``` + + Check the replicas of Deployment `frontend` which is deployed by workload webservice `helloworld` and there is one replica. + + (TODO: The command below needs to be replaced with `vela show` to check the replicas.) + ``` + $ kubectl get deploy frontend + NAME READY UP-TO-DATE AVAILABLE AGE + frontend 1/1 1 1 2m52s + ``` + +- Scale the application by `cron` + ``` + $ vela autoscale helloworld --svc frontend --minReplicas 1 --maxReplicas 4 --replicas 2 --name cron-test --startAt 21:00 --duration 2h --days "Monday, Tuesday" + Adding autoscale for app frontend + ⠋ Deploying ... + ✅ Application Deployed Successfully! + - Name: frontend + Type: webservice + HEALTHY Ready: 1/1 + Last Deployment: + Created at: 2020-11-03 20:53:50 +0800 CST + Updated at: 2020-11-03T21:01:20+08:00 + Traits: + - autoscale: + maxReplicas=4 + minReplicas=1 + replicas=2 + startAt=21:00 + timezone=Asia/Shanghai + days=Monday, Tuesday + name=cron-test + type=cron + duration=2h + ``` + + The time is `21:07` which is in the active period of the trait which started at `21:00` and the duration is two hours. + Check the replicas of Deployment `frontend` again, it has been scaled to 2. + ``` + $ kubectl get deploy + NAME READY UP-TO-DATE AVAILABLE AGE + frontend 2/2 2 2 8m42s + ``` + + Wait after the period ends, the replicas will be one eventually. + diff --git a/docs/developers/set-autoscaling.md b/docs/developers/set-autoscaling.md deleted file mode 100644 index 95989eadf..000000000 --- a/docs/developers/set-autoscaling.md +++ /dev/null @@ -1,3 +0,0 @@ -# Setting Auto-scaling Policy - -> TODO \ No newline at end of file diff --git a/hack/vela-templates/cue/autoscale.cue b/hack/vela-templates/cue/autoscale.cue index 2f5e4319a..625a715f7 100644 --- a/hack/vela-templates/cue/autoscale.cue +++ b/hack/vela-templates/cue/autoscale.cue @@ -4,7 +4,7 @@ output: { spec: { minReplicas: parameter.minReplicas maxReplicas: parameter.maxReplicas - triggers: [...{ + triggers: [{ name: parameter.name type: parameter.type condition: { @@ -14,7 +14,7 @@ output: { replicas: parameter.replicas timezone: parameter.timezone } - }] + }, ...] } } parameter: { @@ -22,9 +22,9 @@ parameter: { maxReplicas: *4 | int name: *"" | string type: *"cron" | string - startAt: *"00:01" | string - duration: *"" | string - days: *"Monday, Tuesday" | string + startAt: string + duration: string + days: string replicas: *"2" | string timezone: *"Asia/Shanghai" | string } diff --git a/pkg/commands/status.go b/pkg/commands/status.go index 6d1542135..4ddc481c0 100644 --- a/pkg/commands/status.go +++ b/pkg/commands/status.go @@ -196,7 +196,7 @@ func printComponentStatus(ctx context.Context, c client.Client, ioStreams cmduti ioStreams.Infof(" Updated at: %v\n", app.UpdateTime.Format(time.RFC3339)) // workload Must found - ioStreams.Infof(" Routes:\n") + ioStreams.Infof(" Traits:\n") workloadStatus, _ := getWorkloadStatusFromAppConfig(appConfig, compName) for _, tr := range workloadStatus.Traits { traitType, traitInfo, err := traitCheckLoop(ctx, c, tr.Reference, compName, appConfig, app, 60*time.Second) diff --git a/pkg/controller/v1alpha1/autoscaler/autoscaler_controller.go b/pkg/controller/v1alpha1/autoscaler/autoscaler_controller.go index b1b91c5f4..30fb28149 100644 --- a/pkg/controller/v1alpha1/autoscaler/autoscaler_controller.go +++ b/pkg/controller/v1alpha1/autoscaler/autoscaler_controller.go @@ -21,21 +21,19 @@ import ( "fmt" "time" - "github.com/crossplane/oam-kubernetes-runtime/pkg/oam/discoverymapper" - cpv1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1" "github.com/crossplane/crossplane-runtime/pkg/event" + "github.com/crossplane/oam-kubernetes-runtime/pkg/oam/discoverymapper" "github.com/crossplane/oam-kubernetes-runtime/pkg/oam/util" oamutil "github.com/crossplane/oam-kubernetes-runtime/pkg/oam/util" "github.com/go-logr/logr" + "github.com/oam-dev/kubevela/api/v1alpha1" + "github.com/oam-dev/kubevela/pkg/controller/common" "github.com/pkg/errors" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" - - "github.com/oam-dev/kubevela/api/v1alpha1" - "github.com/oam-dev/kubevela/pkg/controller/common" ) const ( @@ -69,6 +67,7 @@ func (r *AutoscalerReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) ctx := context.Background() var scaler v1alpha1.Autoscaler if err := r.Get(ctx, req.NamespacedName, &scaler); err != nil { + log.Error(err, "Failed to get trait", "traitName", scaler.Name) return ReconcileWaitResult, client.IgnoreNotFound(err) } log.Info("Retrieved trait Autoscaler", "APIVersion", scaler.APIVersion, "Kind", scaler.Kind)