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

Viewed 26

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.

1 Answers

KubernetesHPAProducer.doDeleteHPA : To delete the HPA Autoscaler Pod follow this link

Below Might help you :

protected void doDeleteHPA(Exchange exchange, String operation) throws Exception {

   String hpaName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_HPA_NAME, String.class);

   String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);

   if (ObjectHelper.isEmpty(hpaName)) {

     LOG.error("Delete a specific hpa require specify a hpa name");

     throw new IllegalArgumentException("Delete a specific hpa require specify a hpa name");

   }

   if (ObjectHelper.isEmpty(namespaceName)) {

     LOG.error("Delete a specific hpa require specify a namespace name");

     throw new IllegalArgumentException("Delete a specific hpa require specify a namespace name");

   }

   boolean hpaDeleted = getEndpoint().getKubernetesClient().autoscaling().horizontalPodAutoscalers().inNamespace(namespaceName).withName(hpaName).delete();



   MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);

   exchange.getOut().setBody(hpaDeleted);

 }

}
Related