feat: Add kubernetes lib for HPA
Signed-off-by: mlycore <maxwell92@126.com>
diff --git a/shardingsphere-operator/pkg/kubernetes/hpa/builder.go b/shardingsphere-operator/pkg/kubernetes/hpa/builder.go
index a12416f..7f56e1d 100644
--- a/shardingsphere-operator/pkg/kubernetes/hpa/builder.go
+++ b/shardingsphere-operator/pkg/kubernetes/hpa/builder.go
@@ -19,58 +19,64 @@
import (
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/metadata"
- autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
+ autoscalingv2 "k8s.io/api/autoscaling/v2"
)
+// HorizontalPodAutoscalerBuilder is a builder for HPA
type HorizontalPodAutoscalerBuilder interface {
metadata.MetadataBuilder
- SetScaleTargetRef(ref autoscalingv2beta2.CrossVersionObjectReference) HorizontalPodAutoscalerBuilder
+ SetScaleTargetRef(ref autoscalingv2.CrossVersionObjectReference) HorizontalPodAutoscalerBuilder
SetMinReplicas(n int32) HorizontalPodAutoscalerBuilder
SetMaxReplicas(n int32) HorizontalPodAutoscalerBuilder
- SetMetrics(specs []autoscalingv2beta2.MetricSpec) HorizontalPodAutoscalerBuilder
- SetBehavior(bh *autoscalingv2beta2.HorizontalPodAutoscalerBehavior) HorizontalPodAutoscalerBuilder
+ SetMetrics(specs []autoscalingv2.MetricSpec) HorizontalPodAutoscalerBuilder
+ SetBehavior(bh *autoscalingv2.HorizontalPodAutoscalerBehavior) HorizontalPodAutoscalerBuilder
- BuildHPA() *autoscalingv2beta2.HorizontalPodAutoscaler
+ BuildHPA() *autoscalingv2.HorizontalPodAutoscaler
}
+// NewHorizontalPodAutoScalerBuilder returns a HorizontalPodAutoScalerBuilder for HPA
func NewHorizontalPodAutoScalerBuilder() HorizontalPodAutoscalerBuilder {
- return &hpaBuilder{
- hpa: &autoscalingv2beta2.HorizontalPodAutoscaler{},
- MetadataBuilder: metadata.NewMetadataBuilder(),
- }
+ return &hpaBuilder{}
}
type hpaBuilder struct {
- hpa *autoscalingv2beta2.HorizontalPodAutoscaler
+ hpa *autoscalingv2.HorizontalPodAutoscaler
metadata.MetadataBuilder
}
-func (b *hpaBuilder) SetScaleTargetRef(ref autoscalingv2beta2.CrossVersionObjectReference) HorizontalPodAutoscalerBuilder {
+// SetScaleTargetRef set the scale target
+func (b *hpaBuilder) SetScaleTargetRef(ref autoscalingv2.CrossVersionObjectReference) HorizontalPodAutoscalerBuilder {
b.hpa.Spec.ScaleTargetRef = ref
return b
}
+// SetMinReplicas set the min replicas
func (b *hpaBuilder) SetMinReplicas(n int32) HorizontalPodAutoscalerBuilder {
b.hpa.Spec.MinReplicas = &n
return b
}
+// SetMaxReplicas set the max replicas
func (b *hpaBuilder) SetMaxReplicas(n int32) HorizontalPodAutoscalerBuilder {
b.hpa.Spec.MaxReplicas = n
return b
}
-func (b *hpaBuilder) SetMetrics(specs []autoscalingv2beta2.MetricSpec) HorizontalPodAutoscalerBuilder {
+// SetMetrics set the metrics
+func (b *hpaBuilder) SetMetrics(specs []autoscalingv2.MetricSpec) HorizontalPodAutoscalerBuilder {
b.hpa.Spec.Metrics = specs
return b
}
-func (b *hpaBuilder) SetBehavior(bh *autoscalingv2beta2.HorizontalPodAutoscalerBehavior) HorizontalPodAutoscalerBuilder {
+// SetBehavior set the behavior
+func (b *hpaBuilder) SetBehavior(bh *autoscalingv2.HorizontalPodAutoscalerBehavior) HorizontalPodAutoscalerBuilder {
b.hpa.Spec.Behavior = bh
return b
}
-func (b *hpaBuilder) BuildHPA() *autoscalingv2beta2.HorizontalPodAutoscaler {
+// BuildHPA returns a HPA
+func (b *hpaBuilder) BuildHPA() *autoscalingv2.HorizontalPodAutoscaler {
+ b.hpa.ObjectMeta = *b.BuildMetadata()
return b.hpa
}
diff --git a/shardingsphere-operator/pkg/kubernetes/hpa/hpa.go b/shardingsphere-operator/pkg/kubernetes/hpa/hpa.go
index 8e83d63..b367a5c 100644
--- a/shardingsphere-operator/pkg/kubernetes/hpa/hpa.go
+++ b/shardingsphere-operator/pkg/kubernetes/hpa/hpa.go
@@ -20,7 +20,7 @@
import (
"context"
- autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
+ autoscalingv2 "k8s.io/api/autoscaling/v2"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -51,7 +51,7 @@
// Getter get HorizontalPodAutoscaler from different parameters
type Getter interface {
- GetByNamespacedName(context.Context, types.NamespacedName) (*autoscalingv2beta2.HorizontalPodAutoscaler, error)
+ GetByNamespacedName(context.Context, types.NamespacedName) (*autoscalingv2.HorizontalPodAutoscaler, error)
}
type getter struct {
@@ -59,8 +59,8 @@
}
// GetByNamespacedName returns Deployment from given namespaced name
-func (dg getter) GetByNamespacedName(ctx context.Context, namespacedName types.NamespacedName) (*autoscalingv2beta2.HorizontalPodAutoscaler, error) {
- hpa := &autoscalingv2beta2.HorizontalPodAutoscaler{}
+func (dg getter) GetByNamespacedName(ctx context.Context, namespacedName types.NamespacedName) (*autoscalingv2.HorizontalPodAutoscaler, error) {
+ hpa := &autoscalingv2.HorizontalPodAutoscaler{}
if err := dg.Client.Get(ctx, namespacedName, hpa); err != nil {
if apierrors.IsNotFound(err) {
return nil, nil
@@ -73,8 +73,8 @@
// Setter get HorizontalPodAutoscaler from different parameters
type Setter interface {
- Create(context.Context, *autoscalingv2beta2.HorizontalPodAutoscaler) error
- Update(context.Context, *autoscalingv2beta2.HorizontalPodAutoscaler) error
+ Create(context.Context, *autoscalingv2.HorizontalPodAutoscaler) error
+ Update(context.Context, *autoscalingv2.HorizontalPodAutoscaler) error
}
type setter struct {
@@ -82,11 +82,11 @@
}
// Create creates HorizontalPodAutoscaler
-func (ds setter) Create(ctx context.Context, dp *autoscalingv2beta2.HorizontalPodAutoscaler) error {
+func (ds setter) Create(ctx context.Context, dp *autoscalingv2.HorizontalPodAutoscaler) error {
return ds.Client.Create(ctx, dp)
}
// Update updates HorizontalPodAutoscaler
-func (ds setter) Update(ctx context.Context, dp *autoscalingv2beta2.HorizontalPodAutoscaler) error {
+func (ds setter) Update(ctx context.Context, dp *autoscalingv2.HorizontalPodAutoscaler) error {
return ds.Client.Update(ctx, dp)
}