I'm using k8s Go client and I want to apply HorizontalPodAutoscalers
on a Deployment
. I have tried multiple attempts but it always returns
{
"error": "the server could not find the requested resource"
}
This is my code:
func (k *KubeClient) createAutoScaler(deploymentName string) (*v22.HorizontalPodAutoscaler, error) {
autoscaler := &v22.HorizontalPodAutoscaler{
Spec: v22.HorizontalPodAutoscalerSpec{
ScaleTargetRef: v22.CrossVersionObjectReference{
Kind: "Deployment",
Name: deploymentName,
APIVersion: "apps/v1",
},
MinReplicas: pointer.Int32(2),
MaxReplicas: 5,
Metrics: []v22.MetricSpec{
{
Type: v22.ResourceMetricSourceType,
Resource: &v22.ResourceMetricSource{
Name: "cpu",
Target: v22.MetricTarget{
Type: "Utilization",
AverageUtilization: pointer.Int32(70),
},
},
},
//{
// Type: v22.ResourceMetricSourceType,
// Resource: &v22.ResourceMetricSource{
// Name: "memory",
// Target: v22.MetricTarget{
// Type: "Utilization",
// AverageUtilization: pointer.Int32(70),
// },
// },
//},
},
//Behavior: &v22.HorizontalPodAutoscalerBehavior{
// ScaleDown: &v22.HPAScalingRules{
// StabilizationWindowSeconds: pointer.Int32(120),
// Policies: []v22.HPAScalingPolicy{
// {
// Type: v22.PodsScalingPolicy,
// Value: 1,
// PeriodSeconds: 60,
// },
// },
// },
//},
},
}
apply, err := k.client.AutoscalingV2().HorizontalPodAutoscalers("mlu-showroom-test").
Create(context.Background(), autoscaler, apimetav1.CreateOptions{
TypeMeta: apimetav1.TypeMeta{
Kind: "Pod",
APIVersion: "apps/v1",
},
FieldValidation: "Ignore",
})
if err != nil {
return nil, err
}
return apply, nil
}
I have checked with kubectl get
, the deployment does exist.
I have tried creating the autoscaler using kubectl autoscale
and it works, I think the problem relies in the code but I'm not sure what goes wrong.
I cannot find any document on how to create a horizontal autoscaler using go-client, the code above is created by referencing the fields in yaml
format in this document: https://kube.netes.io/docs/tasks/run-application/horizontal-pod-autoscale/
If you have applied HorizontalAutoScaler with k8s go-client before, please share your knowledge.
After checking K8S version again, I understand there were two problems:
kubectl --kubeconfig=kubeconfig version
Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.7", GitCommit:"1dd5338295409edcfff11505e7bb246f0d325d15", GitTreeState:"clean", BuildDate:"2021-01-13T13:23:52Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.5", GitCommit:"5c99e2ac2ff9a3c549d9ca665e7bc05a3e18f07e", GitTreeState:"clean", BuildDate:"2021-12-16T08:32:32Z", GoVersion:"go1.16.12", Compiler:"gc", Platform:"linux/amd64"}
kube.netes v1.23
according to Kube.netes official document.After some modification, the working code looks like this
func (k *KubeClient) createAutoScaler(deploymentName string, namespace string) (*v12.HorizontalPodAutoscaler, error) {
autoscaler := &v12.HorizontalPodAutoscaler{
TypeMeta: apimetav1.TypeMeta{
Kind: "Pod",
APIVersion: "autoscaling/v1",
},
ObjectMeta: apimetav1.ObjectMeta{
Name: deploymentName,
Namespace: namespace,
},
Spec: v12.HorizontalPodAutoscalerSpec{
ScaleTargetRef: v12.CrossVersionObjectReference{
Kind: "Deployment",
Name: deploymentName,
APIVersion: "apps/v1",
},
MinReplicas: pointer.Int32(2),
MaxReplicas: 5,
TargetCPUUtilizationPercentage: pointer.Int32(70),
},
}
apply, err := k.client.AutoscalingV1().HorizontalPodAutoscalers(namespace).
Create(context.Background(), autoscaler, apimetav1.CreateOptions{})
if err != nil {
return nil, err
}
return apply, nil
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.