-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #437 from yikuaibro/vpa
feat: add vpa for AutoScaler
- Loading branch information
Showing
6 changed files
with
285 additions
and
0 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
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,76 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 vpa | ||
|
||
import ( | ||
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/metadata" | ||
v1 "k8s.io/api/autoscaling/v1" | ||
autoscalingv1 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" | ||
) | ||
|
||
// VerticalPodAutoscalerBuilder is a builder for VPA | ||
type VerticalPodAutoscalerBuilder interface { | ||
metadata.MetadataBuilder | ||
|
||
SetTargetRef(ref v1.CrossVersionObjectReference) VerticalPodAutoscalerBuilder | ||
SetUpdatePolicy(pu *autoscalingv1.PodUpdatePolicy) VerticalPodAutoscalerBuilder | ||
SetResourcePolicy(pr *autoscalingv1.PodResourcePolicy) VerticalPodAutoscalerBuilder | ||
SetRecommenders(rs []*autoscalingv1.VerticalPodAutoscalerRecommenderSelector) VerticalPodAutoscalerBuilder | ||
|
||
BuildVPA() *autoscalingv1.VerticalPodAutoscaler | ||
} | ||
|
||
// NewVerticalPodAutoscalerBuilder returns a VerticalPodAutoscalerBuilder for VPA | ||
func NewVerticalPodAutoscalerBuilder() VerticalPodAutoscalerBuilder { | ||
return &vpaBuilder{} | ||
} | ||
|
||
type vpaBuilder struct { | ||
vpa *autoscalingv1.VerticalPodAutoscaler | ||
metadata.MetadataBuilder | ||
} | ||
|
||
// SetTargetRef set the scale target | ||
func (v *vpaBuilder) SetTargetRef(ref v1.CrossVersionObjectReference) VerticalPodAutoscalerBuilder { | ||
v.vpa.Spec.TargetRef = &ref | ||
return v | ||
} | ||
|
||
// SetUpdatePolicy set the rules on how changes are applied to the pods | ||
func (v *vpaBuilder) SetUpdatePolicy(pu *autoscalingv1.PodUpdatePolicy) VerticalPodAutoscalerBuilder { | ||
v.vpa.Spec.UpdatePolicy = pu | ||
return v | ||
} | ||
|
||
// SetResourcePolicy set how the autoscaler computes recommended resources | ||
func (v *vpaBuilder) SetResourcePolicy(pr *autoscalingv1.PodResourcePolicy) VerticalPodAutoscalerBuilder { | ||
v.vpa.Spec.ResourcePolicy = pr | ||
return v | ||
} | ||
|
||
// SetRecommenders set the recommenders | ||
func (v *vpaBuilder) SetRecommenders(rs []*autoscalingv1.VerticalPodAutoscalerRecommenderSelector) VerticalPodAutoscalerBuilder { | ||
v.vpa.Spec.Recommenders = rs | ||
return v | ||
} | ||
|
||
// BuildVPA returns a VPA | ||
func (v *vpaBuilder) BuildVPA() *autoscalingv1.VerticalPodAutoscaler { | ||
v.vpa.ObjectMeta = *v.BuildMetadata() | ||
return v.vpa | ||
} |
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,92 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 vpa | ||
|
||
import ( | ||
"context" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
autoscalingv1 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// NewVerticalPodAutoscalerClient creates a new VerticalPodAutoscaler | ||
func NewVerticalPodAutoscalerClient(c client.Client) VerticalPodAutoscaler { | ||
return vpaClient{ | ||
getter: getter{ | ||
Client: c, | ||
}, | ||
setter: setter{ | ||
Client: c, | ||
}, | ||
} | ||
} | ||
|
||
// VerticalPodAutoscaler interface contains setter and getter | ||
type VerticalPodAutoscaler interface { | ||
Getter | ||
Setter | ||
} | ||
|
||
type vpaClient struct { | ||
getter | ||
setter | ||
} | ||
|
||
// Getter get VerticalPodAutoscaler from different parameters | ||
type Getter interface { | ||
GetByNamespacedName(context.Context, types.NamespacedName) (*autoscalingv1.VerticalPodAutoscaler, error) | ||
} | ||
|
||
type getter struct { | ||
client.Client | ||
} | ||
|
||
// GetByNamespacedName returns Deployment from given namespaced name | ||
func (dg getter) GetByNamespacedName(ctx context.Context, namespacedName types.NamespacedName) (*autoscalingv1.VerticalPodAutoscaler, error) { | ||
hpa := &autoscalingv1.VerticalPodAutoscaler{} | ||
if err := dg.Client.Get(ctx, namespacedName, hpa); err != nil { | ||
if apierrors.IsNotFound(err) { | ||
return nil, nil | ||
} | ||
return nil, err | ||
} else { | ||
return hpa, nil | ||
} | ||
} | ||
|
||
// Setter get VerticalPodAutoscaler from different parameters | ||
type Setter interface { | ||
Create(context.Context, *autoscalingv1.VerticalPodAutoscaler) error | ||
Update(context.Context, *autoscalingv1.VerticalPodAutoscaler) error | ||
} | ||
|
||
type setter struct { | ||
client.Client | ||
} | ||
|
||
// Create creates VerticalPodAutoscaler | ||
func (ds setter) Create(ctx context.Context, vp *autoscalingv1.VerticalPodAutoscaler) error { | ||
return ds.Client.Create(ctx, vp) | ||
} | ||
|
||
// Update updates VerticalPodAutoscaler | ||
func (ds setter) Update(ctx context.Context, vp *autoscalingv1.VerticalPodAutoscaler) error { | ||
return ds.Client.Update(ctx, vp) | ||
} |
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,60 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 autoscaler | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1" | ||
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/vpa" | ||
v1 "k8s.io/api/autoscaling/v1" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
autoscalingv1 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" | ||
) | ||
|
||
func (b builder) BuildVerticalPodAutoscaler(ctx context.Context, meta *metav1.ObjectMeta, gvk schema.GroupVersionKind, policy *v1alpha1.ScalingPolicy) *autoscalingv1.VerticalPodAutoscaler { | ||
blder := vpa.NewVerticalPodAutoscalerBuilder() | ||
blder.SetName(meta.Name).SetNamespace(meta.Namespace).SetLabels(meta.Labels).SetAnnotations(meta.Annotations).SetOwnerReferences([]metav1.OwnerReference{ | ||
*metav1.NewControllerRef(meta.GetObjectMeta(), gvk), | ||
}) | ||
|
||
blder.SetTargetRef(v1.CrossVersionObjectReference{ | ||
Kind: "ComputeNode", | ||
Name: policy.TargetSelector.ObjectRef.Name, | ||
APIVersion: "shardingsphere.apache.org/v1alpha1", | ||
}) | ||
|
||
if policy.Vertical.UpdatePolicy != nil { | ||
blder.SetUpdatePolicy(policy.Vertical.UpdatePolicy) | ||
} | ||
if policy.Vertical.ResourcePolicy != nil { | ||
blder.SetResourcePolicy(policy.Vertical.ResourcePolicy) | ||
} | ||
if policy.Vertical.Recommenders != nil { | ||
var r []*autoscalingv1.VerticalPodAutoscalerRecommenderSelector | ||
for _, recommender := range policy.Vertical.Recommenders { | ||
recommenderCopy := recommender | ||
r = append(r, &recommenderCopy) | ||
} | ||
blder.SetRecommenders(r) | ||
} | ||
|
||
return blder.BuildVPA() | ||
} |