Skip to content

Commit

Permalink
hpa add default behavior (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaByte875 authored Mar 26, 2024
1 parent e0d3147 commit 2666240
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 67 deletions.
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ else
GOBIN=$(shell go env GOBIN)
endif

all: build
all: generate manifests build-operator

##@ General

Expand All @@ -48,8 +48,10 @@ help: ## Display this help.
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(GOBIN)/controller-gen $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases

generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(GOBIN)/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
generate: controller-gen defaulter-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(GOBIN)/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./apis/..."
mkdir -p tmp.gen && $(GOBIN)/defaulter-gen --go-header-file "hack/boilerplate.go.txt" --input-dirs "./apis/autoscaling/v1alpha1" --output-base "./tmp.gen/"
cp -f tmp.gen/github.com/vesoft-inc/nebula-operator/apis/autoscaling/v1alpha1/zz_generated.defaults.go apis/autoscaling/v1alpha1/ && rm -rf ./tmp.gen

check: fmt vet lint ## Run check against code.

Expand Down Expand Up @@ -156,7 +158,7 @@ undeploy: kustomize ## Undeploy controller from the K8s cluster specified in ~/.

##@ Tools

tools: golangci-lint controller-gen kustomize ginkgo kind ## Download all go tools locally if necessary.
tools: golangci-lint controller-gen defaulter-gen kustomize ginkgo kind ## Download all go tools locally if necessary.

golangci-lint:
@[ -f $(GOBIN)/golangci-lint ] || { \
Expand All @@ -167,6 +169,9 @@ golangci-lint:
controller-gen:
$(call go-get-tool,$(GOBIN)/controller-gen,sigs.k8s.io/controller-tools/cmd/[email protected])

defaulter-gen:
$(call go-get-tool,$(GOBIN)/default-gen,k8s.io/code-generator/cmd/[email protected])

kustomize:
$(call go-get-tool,$(GOBIN)/kustomize,sigs.k8s.io/kustomize/kustomize/[email protected])

Expand Down
26 changes: 26 additions & 0 deletions apis/autoscaling/scheme/scheme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package scheme

import (
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"

appsv1alpha1 "github.com/vesoft-inc/nebula-operator/apis/apps/v1alpha1"
"github.com/vesoft-inc/nebula-operator/apis/autoscaling/v1alpha1"
)

var (
Scheme = runtime.NewScheme()
)

func init() {
AddToScheme(Scheme)
}

func AddToScheme(scheme *runtime.Scheme) {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(v1alpha1.AddToScheme(clientgoscheme.Scheme))
utilruntime.Must(v1alpha1.AddToScheme(scheme))
utilruntime.Must(appsv1alpha1.AddToScheme(clientgoscheme.Scheme))
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
}
136 changes: 136 additions & 0 deletions apis/autoscaling/v1alpha1/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
Copyright 2023 Vesoft Inc.
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
)

const DefaultCPUUtilization = 80

var (
// These constants repeat previous HPA behavior
scaleUpLimitPercent int32 = 100
scaleUpLimitMinimumPods int32 = 4
scaleUpPeriod int32 = 15
scaleUpStabilizationSeconds int32
maxPolicy = autoscalingv2.MaxChangePolicySelect
defaultHPAScaleUpRules = autoscalingv2.HPAScalingRules{
StabilizationWindowSeconds: &scaleUpStabilizationSeconds,
SelectPolicy: &maxPolicy,
Policies: []autoscalingv2.HPAScalingPolicy{
{
Type: autoscalingv2.PodsScalingPolicy,
Value: scaleUpLimitMinimumPods,
PeriodSeconds: scaleUpPeriod,
},
{
Type: autoscalingv2.PercentScalingPolicy,
Value: scaleUpLimitPercent,
PeriodSeconds: scaleUpPeriod,
},
},
}
scaleDownPeriod int32 = 15
scaleDownStabilizationSeconds int32 = 300
// Currently we can set the downscaleStabilizationWindow from the command line
// So we can not rewrite the command line option from here
scaleDownLimitPercent int32 = 100
defaultHPAScaleDownRules = autoscalingv2.HPAScalingRules{
StabilizationWindowSeconds: &scaleDownStabilizationSeconds,
SelectPolicy: &maxPolicy,
Policies: []autoscalingv2.HPAScalingPolicy{
{
Type: autoscalingv2.PercentScalingPolicy,
Value: scaleDownLimitPercent,
PeriodSeconds: scaleDownPeriod,
},
},
}
)

func addDefaultingFuncs(scheme *runtime.Scheme) error {
return RegisterDefaults(scheme)
}

func SetDefaults_NebulaAutoscaler(obj *NebulaAutoscaler) {
if obj.Spec.GraphdPolicy.MinReplicas == nil {
obj.Spec.GraphdPolicy.MinReplicas = pointer.Int32(1)
}

if len(obj.Spec.GraphdPolicy.Metrics) == 0 {
utilizationDefaultVal := int32(DefaultCPUUtilization)
obj.Spec.GraphdPolicy.Metrics = []autoscalingv2.MetricSpec{
{
Type: autoscalingv2.ResourceMetricSourceType,
Resource: &autoscalingv2.ResourceMetricSource{
Name: corev1.ResourceCPU,
Target: autoscalingv2.MetricTarget{
Type: autoscalingv2.UtilizationMetricType,
AverageUtilization: &utilizationDefaultVal,
},
},
},
}
}
SetDefaults_NebulaAutoscalerBehavior(obj)
}

// SetDefaults_NebulaAutoscalerBehavior fills the behavior if it is not null
func SetDefaults_NebulaAutoscalerBehavior(obj *NebulaAutoscaler) {
// if behavior is specified, we should fill all the 'nil' values with the default ones
if obj.Spec.GraphdPolicy.Behavior != nil {
obj.Spec.GraphdPolicy.Behavior.ScaleUp = GenerateHPAScaleUpRules(obj.Spec.GraphdPolicy.Behavior.ScaleUp)
obj.Spec.GraphdPolicy.Behavior.ScaleDown = GenerateHPAScaleDownRules(obj.Spec.GraphdPolicy.Behavior.ScaleDown)
}
}

// GenerateHPAScaleUpRules returns a fully-initialized HPAScalingRules value
// We guarantee that no pointer in the structure will have the 'nil' value
func GenerateHPAScaleUpRules(scalingRules *autoscalingv2.HPAScalingRules) *autoscalingv2.HPAScalingRules {
defaultScalingRules := defaultHPAScaleUpRules.DeepCopy()
return copyHPAScalingRules(scalingRules, defaultScalingRules)
}

// GenerateHPAScaleDownRules returns a fully-initialized HPAScalingRules value
// We guarantee that no pointer in the structure will have the 'nil' value
// EXCEPT StabilizationWindowSeconds, for reasoning check the comment for defaultHPAScaleDownRules
func GenerateHPAScaleDownRules(scalingRules *autoscalingv2.HPAScalingRules) *autoscalingv2.HPAScalingRules {
defaultScalingRules := defaultHPAScaleDownRules.DeepCopy()
return copyHPAScalingRules(scalingRules, defaultScalingRules)
}

// copyHPAScalingRules copies all non-`nil` fields in HPA constraint structure
func copyHPAScalingRules(from, to *autoscalingv2.HPAScalingRules) *autoscalingv2.HPAScalingRules {
if from == nil {
return to
}
if from.SelectPolicy != nil {
to.SelectPolicy = from.SelectPolicy
}
if from.StabilizationWindowSeconds != nil {
to.StabilizationWindowSeconds = from.StabilizationWindowSeconds
}
if from.Policies != nil {
to.Policies = from.Policies
}
return to
}
2 changes: 2 additions & 0 deletions apis/autoscaling/v1alpha1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ limitations under the License.
// Package v1alpha1 contains v1alpha1 version's types in autoscaling.nebula-graph.io group.
//
// +groupName=autoscaling.nebula-graph.io
// +k8s:defaulter-gen=TypeMeta
// +k8s:defaulter-gen-input=github.com/vesoft-inc/nebula-operator/apis/autoscaling/v1alpha1
package v1alpha1
16 changes: 16 additions & 0 deletions apis/autoscaling/v1alpha1/nebulaautoscaler.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2023 Vesoft Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
16 changes: 16 additions & 0 deletions apis/autoscaling/v1alpha1/nebulaautoscaler_types.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2023 Vesoft Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
Expand Down
25 changes: 25 additions & 0 deletions apis/autoscaling/v1alpha1/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright 2023 Vesoft Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

func init() {
// We only register manually written functions here. The registration of the
// generated functions takes place in the generated files. The separation
// makes the code compile even when the generated files are missing.
localSchemeBuilder := SchemeBuilder.Register(&NebulaAutoscaler{}, &NebulaAutoscalerList{})
localSchemeBuilder.SchemeBuilder.Register(addDefaultingFuncs)
}
2 changes: 1 addition & 1 deletion apis/autoscaling/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions apis/autoscaling/v1alpha1/zz_generated.defaults.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 2 additions & 18 deletions cmd/autoscaler/app/autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import (
"flag"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
Expand All @@ -32,7 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/config"
"sigs.k8s.io/controller-runtime/pkg/healthz"

appsv1alpha1 "github.com/vesoft-inc/nebula-operator/apis/apps/v1alpha1"
"github.com/vesoft-inc/nebula-operator/apis/autoscaling/scheme"
"github.com/vesoft-inc/nebula-operator/apis/autoscaling/v1alpha1"
"github.com/vesoft-inc/nebula-operator/cmd/autoscaler/app/options"
"github.com/vesoft-inc/nebula-operator/pkg/controller/autoscaler"
Expand All @@ -41,19 +38,6 @@ import (
"github.com/vesoft-inc/nebula-operator/pkg/version"
)

var (
scheme = runtime.NewScheme()
)

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(v1alpha1.AddToScheme(clientgoscheme.Scheme))
utilruntime.Must(v1alpha1.AddToScheme(scheme))
utilruntime.Must(appsv1alpha1.AddToScheme(clientgoscheme.Scheme))
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}

// NewAutoscalerCommand creates a *cobra.Command object with default parameters
func NewAutoscalerCommand(ctx context.Context) *cobra.Command {
opts := options.NewOptions()
Expand Down Expand Up @@ -97,7 +81,7 @@ func Run(ctx context.Context, opts *options.Options) error {
}

ctrlOptions := ctrlruntime.Options{
Scheme: scheme,
Scheme: scheme.Scheme,
Logger: klog.Background(),
LeaderElection: opts.LeaderElection.LeaderElect,
LeaderElectionID: opts.LeaderElection.ResourceName,
Expand Down
Loading

0 comments on commit 2666240

Please sign in to comment.