javalinuxkubernetesfabric8fabric8-maven-plugin

How to delete HorizontalPodAutoscaler using Fabric8 k8s java client (version: 6.0.0)


Looks like there is no support to delete HorizontalPodAutoscaler using fabric8's K8S Java client ver:6.0.0.

Although It is straightforward to create HorizontalPodAutoscaler using fabric8's K8S Java client ver:6.0.0.

E.g.

 HorizontalPodAutoscalerStatus hpaStatus = k8sClient.resource(createHPA())
                .inNamespace(namespace)
                .createOrReplace().getStatus();
public HorizontalPodAutoscaler createHPA(){
return new HorizontalPodAutoscalerBuilder()
                .withNewMetadata()
                    .withName(applicationName)
                    .addToLabels("name", applicationName)
                .endMetadata()
                .withNewSpec()
                    .withNewScaleTargetRef()
                        .withApiVersion(hpaApiVersion)
                        .withKind("Deployment")
                        .withName(applicationName)
                    .endScaleTargetRef()
                    .withMinReplicas(minReplica)
                    .withMaxReplicas(maxReplica)
                    .addNewMetric()
                        .withType("Resource")
                        .withNewResource()
                            .withName("cpu")
                            .withNewTarget()
                                .withType("Utilization")
                                .withAverageUtilization(cpuAverageUtilization)
                            .endTarget()
                        .endResource()
                    .endMetric()
                    .addNewMetric()
                        .withType("Resource")
                        .withNewResource()
                            .withName("memory")
                            .withNewTarget() 
                                .withType("AverageValue")
                                .withAverageValue(new Quantity(memoryAverageValue))
                            .endTarget()
                        .endResource()
                    .endMetric()
                    .withNewBehavior()
                        .withNewScaleDown()
                            .addNewPolicy()
                                .withType("Pods")
                                .withValue(podScaleDownValue)
                                .withPeriodSeconds(podScaleDownPeriod)
                            .endPolicy()
                            .withStabilizationWindowSeconds(podScaledStabaliztionWindow)
                        .endScaleDown()
                    .endBehavior()
                .endSpec().build();
}

Any solution to delete HorizontalPodAutoscaler using fabric8's K8S Java client ver:6.0.0 will be appriciated.


Solution

  • First, Need to identify which API group (v1, v2beta1, v2beta2) was used during deployment creation based on the same API group the autoscaling function need to be call to get the HPA instance and then go ahead to perform any action on that HPA instance.

    In my case the deployment was created with v2beta2 API group, Below code snnipt helped me to deleted the HorizontalPodAutoscaler object from the provided name space.

    k8sClient.autoscaling().v2beta2().horizontalPodAutoscalers().inNamespace("test").withName("myhpa").delete()