Skip to content

Commit

Permalink
nebula autoscaler (#302)
Browse files Browse the repository at this point in the history
* nebula autoscaler

* error variable
  • Loading branch information
MegaByte875 authored Sep 28, 2023
1 parent 1a1d7fa commit 37218c3
Show file tree
Hide file tree
Showing 43 changed files with 3,895 additions and 135 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ RUN addgroup -S -g 65532 ng-user && \
-s /sbin/nologin -G ng-user -g ng-user ng-user

ADD bin/${TARGETDIR}/controller-manager /usr/local/bin/controller-manager
ADD bin/${BUILDPLATFORM}/autoscaler /usr/local/bin/autoscaler
ADD bin/${TARGETDIR}/scheduler /usr/local/bin/scheduler
USER 65532:65532
1 change: 1 addition & 0 deletions Dockerfile.multiarch
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ RUN addgroup -S -g 65532 ng-user && \
-s /sbin/nologin -G ng-user -g ng-user ng-user

ADD bin/${BUILDPLATFORM}/controller-manager /usr/local/bin/controller-manager
ADD bin/${BUILDPLATFORM}/autoscaler /usr/local/bin/autoscaler
ADD bin/${BUILDPLATFORM}/scheduler /usr/local/bin/scheduler
USER 65532:65532
9 changes: 1 addition & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ e2e: ginkgo kind ## Run e2e test.
##@ Build
build: ## Build binary.
$(GO_BUILD) -ldflags '$(LDFLAGS)' -o bin/$(TARGETDIR)/controller-manager cmd/controller-manager/main.go
$(GO_BUILD) -ldflags '$(LDFLAGS)' -o bin/$(TARGETDIR)/autoscaler cmd/autoscaler/main.go
$(GO_BUILD) -ldflags '$(LDFLAGS)' -o bin/$(TARGETDIR)/scheduler cmd/scheduler/main.go

helm-charts: ## Build helm charts.
Expand All @@ -81,14 +82,6 @@ helm-charts: ## Build helm charts.
mv nebula-operator-*.tgz nebula-cluster-*.tgz charts/
helm repo index charts/ --url https://github.com/vesoft-inc/nebula-operator/releases/download/v$(CHARTS_VERSION)

run: run-controller-manager

run-controller-manager: manifests generate check
go run -ldflags '$(LDFLAGS)' cmd/controller-manager/main.go

run-scheduler: manifests generate check
go run -ldflags '$(LDFLAGS)' cmd/scheduler/main.go

docker-build: ## Build docker images.
docker build --build-arg TARGETDIR=$(TARGETDIR) -t "${DOCKER_REPO}/nebula-operator:${IMAGE_TAG}" .

Expand Down
3 changes: 2 additions & 1 deletion apis/addtoscheme_apps_v1alpha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package apis

import (
"github.com/vesoft-inc/nebula-operator/apis/apps/v1alpha1"
asv1alpha1 "github.com/vesoft-inc/nebula-operator/apis/autoscaling/v1alpha1"
)

// nolint: gochecknoinits
func init() {
// Register the types with the Scheme so the components can map objects to GroupVersionKinds and back
AddToSchemes = append(AddToSchemes, v1alpha1.SchemeBuilder.AddToScheme)
AddToSchemes = append(AddToSchemes, v1alpha1.SchemeBuilder.AddToScheme, asv1alpha1.SchemeBuilder.AddToScheme)
}
6 changes: 3 additions & 3 deletions apis/apps/v1alpha1/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ const (
########## Black box ########
# Enable black box
--ng_black_box_switch=true
--ng_black_box_switch=false
# Black box log folder
--ng_black_box_home=black_box
# Black box dump metrics log period
Expand Down Expand Up @@ -328,7 +328,7 @@ const (
########## Black box ########
# Enable black box
--ng_black_box_switch=true
--ng_black_box_switch=false
# Black box log folder
--ng_black_box_home=black_box
# Black box dump metrics log period
Expand Down Expand Up @@ -526,7 +526,7 @@ const (
########## Black box ########
# Enable black box
--ng_black_box_switch=true
--ng_black_box_switch=false
# Black box log folder
--ng_black_box_home=black_box
# Black box dump metrics log period
Expand Down
20 changes: 20 additions & 0 deletions apis/autoscaling/v1alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
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 contains v1alpha1 version's types in autoscaling.nebula-graph.io group.
//
// +groupName=autoscaling.nebula-graph.io
package v1alpha1
43 changes: 43 additions & 0 deletions apis/autoscaling/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
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 contains API Schema definitions for the autoscaling v1alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=autoscaling.nebula-graph.io
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "autoscaling.nebula-graph.io", Version: "v1alpha1"}

SchemeGroupVersion = GroupVersion

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)

// Resource is required by pkg/client/listers/...
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}
10 changes: 10 additions & 0 deletions apis/autoscaling/v1alpha1/nebulaautoscaler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package v1alpha1

import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

func (na *NebulaAutoscaler) GetPollingPeriod() *metav1.Duration {
if na == nil {
return nil
}
return na.Spec.PollingPeriod
}
169 changes: 169 additions & 0 deletions apis/autoscaling/v1alpha1/nebulaautoscaler_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package v1alpha1

import (
"time"

autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
// DefaultPollingPeriod is the default period between 2 autoscaling API polls.
DefaultPollingPeriod = 15 * time.Second
)

type NebulaAutoscalerConditionType string

const (
// AutoscalerActive indicates that the HPA controller is able to scale if necessary:
// it's correctly configured, can fetch the desired metrics, and isn't disabled.
AutoscalerActive NebulaAutoscalerConditionType = "Active"

// AbleToScale indicates a lack of transient issues which prevent scaling from occurring,
// such as being in a backoff window, or being unable to access/update the target scale.
AbleToScale NebulaAutoscalerConditionType = "AbleToScale"

// AutoscalerLimited indicates that the calculated scale based on metrics would be above or
// below the range for the HPA, and has thus been capped.
AutoscalerLimited NebulaAutoscalerConditionType = "Limited"

// AutoscalerReady indicates that the nebula cluster is ready.
AutoscalerReady NebulaAutoscalerConditionType = "Ready"
)

// NebulaAutoscalerSpec defines the desired state of NebulaAutoscaler
type NebulaAutoscalerSpec struct {
// +kubebuilder:validation:Required
NebulaClusterRef NebulaClusterRef `json:"nebulaClusterRef"`

GraphdPolicy AutoscalingPolicySpec `json:"graphdPolicy"`

// +optional
// PollingPeriod is the period at which to synchronize with the autoscaling API.
PollingPeriod *metav1.Duration `json:"pollingPeriod,omitempty"`
}

type NebulaClusterRef struct {
// Name is the name of the NebulaCluster resource to scale automatically.
// +kubebuilder:validation:MinLength=1
Name string `json:"name"`
}

type AutoscalingPolicySpec struct {
// MinReplicas is the lower limit for the number of replicas to which the autoscaler
// can scale down.
// +optional
MinReplicas *int32 `json:"minReplicas,omitempty"`

// MaxReplicas is the upper limit for the number of replicas to which the autoscaler can scale up.
// It cannot be less that minReplicas.
MaxReplicas int32 `json:"maxReplicas"`

// Metrics contains the specifications for which to use to calculate the
// desired replica count (the maximum replica count across all metrics will
// be used).
// If not set, the default metric will be set to 80% average CPU utilization.
// +optional
Metrics []autoscalingv2.MetricSpec `json:"metrics,omitempty"`

// Behavior configures the scaling behavior of the target
// in both Up and Down directions (scaleUp and scaleDown fields respectively).
// If not set, the default HPAScalingRules for scale up and scale down are used.
// +optional
Behavior *autoscalingv2.HorizontalPodAutoscalerBehavior `json:"behavior,omitempty"`
}

type NebulaAutoscalerCondition struct {
// Type describes the current condition
Type NebulaAutoscalerConditionType `json:"type"`

// Status is the status of the condition (True, False, Unknown)
Status corev1.ConditionStatus `json:"status"`

// LastTransitionTime is the last time the condition transitioned from
// one status to another
// +optional
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`

// Reason is the reason for the condition's last transition.
// +optional
Reason string `json:"reason,omitempty"`

// Message is a human-readable explanation containing details about
// the transition
// +optional
Message string `json:"message,omitempty"`
}

// NebulaAutoscalerStatus defines the observed state of NebulaAutoscaler
type NebulaAutoscalerStatus struct {
// ObservedGeneration is the most recent generation observed by this autoscaler.
// +optional
ObservedGeneration *int64 `json:"observedGeneration,omitempty"`

// GraphdStatuses describes the status of Graphd autoscaling policies.
// +optional
GraphdStatus AutoscalingPolicyStatus `json:"graphdStatus,omitempty"`

// Conditions is the set of conditions required for this autoscaler to scale its target,
// and indicates whether those conditions are met.
// +optional
Conditions []NebulaAutoscalerCondition `json:"conditions,omitempty"`
}

type AutoscalingPolicyStatus struct {
// LastScaleTime is the last time the autoscaler scaled the number of pods,
// used by the autoscaler to control how often the number of pods is changed.
// +optional
LastScaleTime *metav1.Time `json:"lastScaleTime,omitempty"`

// CurrentReplicas is current number of replicas of pods managed by this autoscaler,
// as last seen by the autoscaler.
// +optional
CurrentReplicas int32 `json:"currentReplicas,omitempty"`

// DesiredReplicas is the desired number of replicas of pods managed by this autoscaler,
// as last calculated by the autoscaler.
DesiredReplicas int32 `json:"desiredReplicas"`

// CurrentMetrics is the last read state of the metrics used by this autoscaler.
// +optional
CurrentMetrics []autoscalingv2.MetricStatus `json:"currentMetrics"`
}

// +genclient
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName=na
// +kubebuilder:printcolumn:name="REFERENCE",type="string",JSONPath=".spec.nebulaClusterRef.name"
// +kubebuilder:printcolumn:name="MIN-REPLICAS",type="string",JSONPath=".spec.graphdPolicy.minReplicas"
// +kubebuilder:printcolumn:name="MAX-REPLICAS",type="string",JSONPath=".spec.graphdPolicy.maxReplicas"
// +kubebuilder:printcolumn:name="CURRENT-REPLICAS",type="string",JSONPath=".status.graphdStatus.currentReplicas"
// +kubebuilder:printcolumn:name="Active",type="string",JSONPath=".status.conditions[?(@.type=='Active')].status"
// +kubebuilder:printcolumn:name="AbleToScale",type="string",JSONPath=".status.conditions[?(@.type=='AbleToScale')].status"
// +kubebuilder:printcolumn:name="Limited",type="string",JSONPath=".status.conditions[?(@.type=='Limited')].status"
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].status"
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp",description="CreationTimestamp is a timestamp representing the server time when this object was created. It is represented in RFC3339 form and is in UTC."

// NebulaAutoscaler represents an NebulaAutoscaler resource in a Kubernetes cluster
type NebulaAutoscaler struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec NebulaAutoscalerSpec `json:"spec,omitempty"`
Status NebulaAutoscalerStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// NebulaAutoscalerList contains a list of NebulaAutoscaler
type NebulaAutoscalerList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []NebulaAutoscaler `json:"items"`
}

func init() {
SchemeBuilder.Register(&NebulaAutoscaler{}, &NebulaAutoscalerList{})
}
Loading

0 comments on commit 37218c3

Please sign in to comment.