-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0d3147
commit 2666240
Showing
11 changed files
with
297 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ else | |
GOBIN=$(shell go env GOBIN) | ||
endif | ||
|
||
all: build | ||
all: generate manifests build-operator | ||
|
||
##@ General | ||
|
||
|
@@ -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. | ||
|
||
|
@@ -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 ] || { \ | ||
|
@@ -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]) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.