make hpa for sts work

This commit is contained in:
GLYASAI 2020-05-27 13:23:51 +08:00
parent fbb93ee562
commit 0bff2247ce
10 changed files with 47 additions and 47 deletions

View File

@ -59,7 +59,7 @@ func (a *refreshXPAController) applyOne(clientset kubernetes.Interface, app *v1.
for _, hpa := range app.GetDelHPAs() {
logrus.Debugf("hpa name: %s; start deleting hpa.", hpa.GetName())
err := clientset.AutoscalingV2beta1().HorizontalPodAutoscalers(hpa.GetNamespace()).Delete(hpa.GetName(), &metav1.DeleteOptions{})
err := clientset.AutoscalingV2beta2().HorizontalPodAutoscalers(hpa.GetNamespace()).Delete(hpa.GetName(), &metav1.DeleteOptions{})
if err != nil {
// don't return error, hope it is ok next time
logrus.Warningf("error deleting secret(%#v): %v", hpa, err)

View File

@ -160,7 +160,7 @@ func (s *startController) startOne(app v1.AppService) error {
if hpas := app.GetHPAs(); len(hpas) != 0 {
for _, hpa := range hpas {
if len(hpa.ResourceVersion) == 0 {
_, err := s.manager.client.AutoscalingV2beta1().HorizontalPodAutoscalers(hpa.GetNamespace()).Create(hpa)
_, err := s.manager.client.AutoscalingV2beta2().HorizontalPodAutoscalers(hpa.GetNamespace()).Create(hpa)
if err != nil && !errors.IsAlreadyExists(err) {
logrus.Debugf("hpa: %#v", hpa)
return fmt.Errorf("create hpa: %v", err)

View File

@ -147,7 +147,7 @@ func (s *stopController) stopOne(app v1.AppService) error {
if hpas := app.GetHPAs(); len(hpas) != 0 {
for _, hpa := range hpas {
err := s.manager.client.AutoscalingV2beta1().HorizontalPodAutoscalers(hpa.GetNamespace()).Delete(hpa.GetName(), &metav1.DeleteOptions{})
err := s.manager.client.AutoscalingV2beta2().HorizontalPodAutoscalers(hpa.GetNamespace()).Delete(hpa.GetName(), &metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
return fmt.Errorf("delete hpa: %v", err)
}

View File

@ -23,7 +23,7 @@ import (
"github.com/Sirupsen/logrus"
"k8s.io/api/autoscaling/v2beta1"
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -52,13 +52,13 @@ func TenantServiceAutoscaler(as *v1.AppService, dbmanager db.Manager) error {
return nil
}
func newHPAs(as *v1.AppService, dbmanager db.Manager) ([]*v2beta1.HorizontalPodAutoscaler, error) {
func newHPAs(as *v1.AppService, dbmanager db.Manager) ([]*autoscalingv2.HorizontalPodAutoscaler, error) {
xpaRules, err := dbmanager.TenantServceAutoscalerRulesDao().ListEnableOnesByServiceID(as.ServiceID)
if err != nil {
return nil, err
}
var hpas []*v2beta1.HorizontalPodAutoscaler
var hpas []*autoscalingv2.HorizontalPodAutoscaler
for _, rule := range xpaRules {
metrics, err := dbmanager.TenantServceAutoscalerRuleMetricsDao().ListByRuleID(rule.RuleID)
if err != nil {
@ -67,7 +67,7 @@ func newHPAs(as *v1.AppService, dbmanager db.Manager) ([]*v2beta1.HorizontalPodA
var kind, name string
if as.GetStatefulSet() != nil {
kind, name = "Statefulset", as.GetStatefulSet().GetName()
kind, name = "StatefulSet", as.GetStatefulSet().GetName()
} else {
kind, name = "Deployment", as.GetDeployment().GetName()
}
@ -85,32 +85,36 @@ func newHPAs(as *v1.AppService, dbmanager db.Manager) ([]*v2beta1.HorizontalPodA
return hpas, nil
}
func createResourceMetrics(metric *model.TenantServiceAutoscalerRuleMetrics) v2beta1.MetricSpec {
ms := v2beta1.MetricSpec{
Type: v2beta1.ResourceMetricSourceType,
Resource: &v2beta1.ResourceMetricSource{
func createResourceMetrics(metric *model.TenantServiceAutoscalerRuleMetrics) autoscalingv2.MetricSpec {
ms := autoscalingv2.MetricSpec{
Type: autoscalingv2.ResourceMetricSourceType,
Resource: &autoscalingv2.ResourceMetricSource{
Name: str2ResourceName[metric.MetricsName],
},
}
if metric.MetricTargetType == "utilization" {
value := int32(metric.MetricTargetValue)
ms.Resource.TargetAverageUtilization = &value
ms.Resource.Target = autoscalingv2.MetricTarget{
Type: autoscalingv2.UtilizationMetricType,
AverageUtilization: &value,
}
}
if metric.MetricTargetType == "average_value" {
ms.Resource.Target.Type = autoscalingv2.AverageValueMetricType
if metric.MetricsName == "cpu" {
ms.Resource.TargetAverageValue = resource.NewMilliQuantity(int64(metric.MetricTargetValue), resource.DecimalSI)
ms.Resource.Target.AverageValue = resource.NewMilliQuantity(int64(metric.MetricTargetValue), resource.DecimalSI)
}
if metric.MetricsName == "memory" {
ms.Resource.TargetAverageValue = resource.NewQuantity(int64(metric.MetricTargetValue*1024*1024), resource.BinarySI)
ms.Resource.Target.AverageValue = resource.NewQuantity(int64(metric.MetricTargetValue*1024*1024), resource.BinarySI)
}
}
return ms
}
func newHPA(namespace, kind, name string, labels map[string]string, rule *model.TenantServiceAutoscalerRules, metrics []*model.TenantServiceAutoscalerRuleMetrics) *v2beta1.HorizontalPodAutoscaler {
hpa := &v2beta1.HorizontalPodAutoscaler{
func newHPA(namespace, kind, name string, labels map[string]string, rule *model.TenantServiceAutoscalerRules, metrics []*model.TenantServiceAutoscalerRuleMetrics) *autoscalingv2.HorizontalPodAutoscaler {
hpa := &autoscalingv2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: rule.RuleID,
Namespace: namespace,
@ -118,10 +122,10 @@ func newHPA(namespace, kind, name string, labels map[string]string, rule *model.
},
}
spec := v2beta1.HorizontalPodAutoscalerSpec{
spec := autoscalingv2.HorizontalPodAutoscalerSpec{
MinReplicas: util.Int32(int32(rule.MinReplicas)),
MaxReplicas: int32(rule.MaxReplicas),
ScaleTargetRef: v2beta1.CrossVersionObjectReference{
ScaleTargetRef: autoscalingv2.CrossVersionObjectReference{
Kind: kind,
Name: name,
APIVersion: "apps/v1",

View File

@ -17,10 +17,6 @@ func TestCreateMetricSpec(t *testing.T) {
metricSpec := createResourceMetrics(metric)
t.Logf("%#v", metricSpec)
if metricSpec.Resource.TargetAverageValue == nil {
t.Errorf("empty target average value.")
}
}
func TestNewHPA(t *testing.T) {
@ -55,7 +51,7 @@ func TestNewHPA(t *testing.T) {
t.Fatalf("error creating k8s clientset: %s", err.Error())
}
_, err = clientset.AutoscalingV2beta1().HorizontalPodAutoscalers(hpa.GetNamespace()).Create(hpa)
_, err = clientset.AutoscalingV2beta2().HorizontalPodAutoscalers(hpa.GetNamespace()).Create(hpa)
if err != nil {
t.Fatalf("create hpa: %v", err)
}

View File

@ -23,7 +23,7 @@ import (
"time"
"github.com/Sirupsen/logrus"
v2beta1 "k8s.io/api/autoscaling/v2beta1"
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
corev1 "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
@ -266,11 +266,11 @@ func EnsureService(new *corev1.Service, clientSet kubernetes.Interface) error {
}
// EnsureHPA -
func EnsureHPA(new *v2beta1.HorizontalPodAutoscaler, clientSet kubernetes.Interface) {
_, err := clientSet.AutoscalingV2beta1().HorizontalPodAutoscalers(new.Namespace).Get(new.Name, metav1.GetOptions{})
func EnsureHPA(new *autoscalingv2.HorizontalPodAutoscaler, clientSet kubernetes.Interface) {
_, err := clientSet.AutoscalingV2beta2().HorizontalPodAutoscalers(new.Namespace).Get(new.Name, metav1.GetOptions{})
if err != nil {
if k8sErrors.IsNotFound(err) {
_, err = clientSet.AutoscalingV2beta1().HorizontalPodAutoscalers(new.Namespace).Create(new)
_, err = clientSet.AutoscalingV2beta2().HorizontalPodAutoscalers(new.Namespace).Create(new)
if err != nil {
logrus.Warningf("error creating hpa %+v: %v", new, err)
}
@ -279,7 +279,7 @@ func EnsureHPA(new *v2beta1.HorizontalPodAutoscaler, clientSet kubernetes.Interf
logrus.Errorf("error getting hpa(%s): %v", fmt.Sprintf("%s/%s", new.Namespace, new.Name), err)
return
}
_, err = clientSet.AutoscalingV2beta1().HorizontalPodAutoscalers(new.Namespace).Update(new)
_, err = clientSet.AutoscalingV2beta2().HorizontalPodAutoscalers(new.Namespace).Update(new)
if err != nil {
logrus.Warningf("error updating hpa %+v: %v", new, err)
return
@ -430,7 +430,7 @@ func UpgradeClaims(clientset *kubernetes.Clientset, as *v1.AppService, old, new
}
}
if claim != nil {
logrus.Infof("claim is exists, do not create again, and can't update it", claim.Name)
logrus.Infof("claim is exists, do not create again, and can't update it: %s", claim.Name)
} else {
claim, err = clientset.CoreV1().PersistentVolumeClaims(n.Namespace).Update(n)
if err != nil {

View File

@ -20,7 +20,7 @@ package store
import (
appsv1 "k8s.io/client-go/listers/apps/v1"
"k8s.io/client-go/listers/autoscaling/v2beta1"
autoscalingv2 "k8s.io/client-go/listers/autoscaling/v2beta2"
corev1 "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/listers/extensions/v1beta1"
storagev1 "k8s.io/client-go/listers/storage/v1"
@ -39,5 +39,5 @@ type Lister struct {
Nodes corev1.NodeLister
StorageClass storagev1.StorageClassLister
Claims corev1.PersistentVolumeClaimLister
HorizontalPodAutoscaler v2beta1.HorizontalPodAutoscalerLister
HorizontalPodAutoscaler autoscalingv2.HorizontalPodAutoscalerLister
}

View File

@ -39,7 +39,7 @@ import (
workerutil "github.com/goodrain/rainbond/worker/util"
"github.com/jinzhu/gorm"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/autoscaling/v2beta1"
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
corev1 "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
storagev1 "k8s.io/api/storage/v1"
@ -190,8 +190,8 @@ func NewStore(clientset kubernetes.Interface,
store.informers.Events = infFactory.Core().V1().Events().Informer()
store.informers.HorizontalPodAutoscaler = infFactory.Autoscaling().V2beta1().HorizontalPodAutoscalers().Informer()
store.listers.HorizontalPodAutoscaler = infFactory.Autoscaling().V2beta1().HorizontalPodAutoscalers().Lister()
store.informers.HorizontalPodAutoscaler = infFactory.Autoscaling().V2beta2().HorizontalPodAutoscalers().Informer()
store.listers.HorizontalPodAutoscaler = infFactory.Autoscaling().V2beta2().HorizontalPodAutoscalers().Lister()
isThirdParty := func(ep *corev1.Endpoints) bool {
return ep.Labels["service-kind"] == model.ServiceKindThirdParty.String()
@ -578,14 +578,14 @@ func (a *appRuntimeStore) OnAdd(obj interface{}) {
}
}
}
if hpa, ok := obj.(*v2beta1.HorizontalPodAutoscaler); ok {
if hpa, ok := obj.(*autoscalingv2.HorizontalPodAutoscaler); ok {
serviceID := hpa.Labels["service_id"]
version := hpa.Labels["version"]
createrID := hpa.Labels["creater_id"]
if serviceID != "" && version != "" && createrID != "" {
appservice, err := a.getAppService(serviceID, version, createrID, true)
if err == conversion.ErrServiceNotFound {
a.conf.KubeClient.AutoscalingV2beta1().HorizontalPodAutoscalers(hpa.GetNamespace()).Delete(hpa.GetName(), &metav1.DeleteOptions{})
a.conf.KubeClient.AutoscalingV2beta2().HorizontalPodAutoscalers(hpa.GetNamespace()).Delete(hpa.GetName(), &metav1.DeleteOptions{})
}
if appservice != nil {
appservice.SetHPA(hpa)
@ -620,7 +620,7 @@ func (a *appRuntimeStore) OnAdd(obj interface{}) {
}
}
func (a *appRuntimeStore) listHPAEvents(hpa *v2beta1.HorizontalPodAutoscaler) error {
func (a *appRuntimeStore) listHPAEvents(hpa *autoscalingv2.HorizontalPodAutoscaler) error {
namespace, name := hpa.GetNamespace(), hpa.GetName()
eventsInterface := a.clientset.CoreV1().Events(hpa.GetNamespace())
selector := eventsInterface.GetFieldSelector(&name, &namespace, nil, nil)
@ -764,7 +764,7 @@ func (a *appRuntimeStore) OnDeletes(objs ...interface{}) {
}
}
}
if hpa, ok := obj.(*v2beta1.HorizontalPodAutoscaler); ok {
if hpa, ok := obj.(*autoscalingv2.HorizontalPodAutoscaler); ok {
serviceID := hpa.Labels["service_id"]
version := hpa.Labels["version"]
createrID := hpa.Labels["creater_id"]

View File

@ -25,7 +25,7 @@ import (
"time"
"github.com/eapache/channels"
v2beta1 "k8s.io/api/autoscaling/v2beta1"
autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"

View File

@ -25,7 +25,7 @@ import (
"github.com/Sirupsen/logrus"
v1 "k8s.io/api/apps/v1"
v2beta1 "k8s.io/api/autoscaling/v2beta1"
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
corev1 "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
storagev1 "k8s.io/api/storage/v1"
@ -104,8 +104,8 @@ type AppService struct {
tenant *corev1.Namespace
statefulset *v1.StatefulSet
deployment *v1.Deployment
hpas []*v2beta1.HorizontalPodAutoscaler
delHPAs []*v2beta1.HorizontalPodAutoscaler
hpas []*autoscalingv2.HorizontalPodAutoscaler
delHPAs []*autoscalingv2.HorizontalPodAutoscaler
replicasets []*v1.ReplicaSet
services []*corev1.Service
delServices []*corev1.Service
@ -642,12 +642,12 @@ func (a *AppService) DeleteClaim(claim *corev1.PersistentVolumeClaim) {
}
// SetHPAs -
func (a *AppService) SetHPAs(hpas []*v2beta1.HorizontalPodAutoscaler) {
func (a *AppService) SetHPAs(hpas []*autoscalingv2.HorizontalPodAutoscaler) {
a.hpas = hpas
}
// SetHPA -
func (a *AppService) SetHPA(hpa *v2beta1.HorizontalPodAutoscaler) {
func (a *AppService) SetHPA(hpa *autoscalingv2.HorizontalPodAutoscaler) {
if len(a.hpas) > 0 {
for i, old := range a.hpas {
if old.GetName() == hpa.GetName() {
@ -660,17 +660,17 @@ func (a *AppService) SetHPA(hpa *v2beta1.HorizontalPodAutoscaler) {
}
// GetHPAs -
func (a *AppService) GetHPAs() []*v2beta1.HorizontalPodAutoscaler {
func (a *AppService) GetHPAs() []*autoscalingv2.HorizontalPodAutoscaler {
return a.hpas
}
// GetDelHPAs -
func (a *AppService) GetDelHPAs() []*v2beta1.HorizontalPodAutoscaler {
func (a *AppService) GetDelHPAs() []*autoscalingv2.HorizontalPodAutoscaler {
return a.delHPAs
}
// DelHPA -
func (a *AppService) DelHPA(hpa *v2beta1.HorizontalPodAutoscaler) {
func (a *AppService) DelHPA(hpa *autoscalingv2.HorizontalPodAutoscaler) {
if len(a.hpas) == 0 {
return
}