private static Optional buildHorizontalPodAutoscaler()

in spark-submission-worker/src/main/java/org/apache/spark/k8s/operator/SparkClusterResourceSpec.java [296:355]


  private static Optional<HorizontalPodAutoscaler> buildHorizontalPodAutoscaler(
      String clusterName, String namespace, ClusterSpec spec) {
    var instanceConfig = spec.getClusterTolerations().getInstanceConfig();
    if (instanceConfig.getMinWorkers() >= instanceConfig.getMaxWorkers()) {
      return Optional.empty();
    }
    HorizontalPodAutoscalerSpec horizontalPodAutoscalerSpec;
    if (spec.getWorkerSpec().getHorizontalPodAutoscalerSpec() != null) {
      horizontalPodAutoscalerSpec = spec.getWorkerSpec().getHorizontalPodAutoscalerSpec();
    } else {
      horizontalPodAutoscalerSpec =
          new HorizontalPodAutoscalerSpecBuilder()
              .addToMetrics(
                  new MetricSpecBuilder()
                      .withType("ContainerResource")
                      .withNewContainerResource()
                      .withName("cpu")
                      .withContainer("worker")
                      .withNewTarget()
                      .withType("Utilization")
                      .withAverageUtilization(30)
                      .endTarget()
                      .endContainerResource()
                      .build())
              .withNewBehavior()
              .withNewScaleUp()
              .addNewPolicy()
              .withType("Pods")
              .withValue(1)
              .withPeriodSeconds(60)
              .endPolicy()
              .endScaleUp()
              .withNewScaleDown()
              .addNewPolicy()
              .withType("Pods")
              .withValue(1)
              .withPeriodSeconds(600)
              .endPolicy()
              .endScaleDown()
              .endBehavior()
              .build();
    }
    return Optional.of(
        new HorizontalPodAutoscalerBuilder()
            .withNewMetadata()
            .withNamespace(namespace)
            .withName(clusterName + "-worker-hpa")
            .addToLabels(LABEL_SPARK_VERSION_NAME, spec.getRuntimeVersions().getSparkVersion())
            .endMetadata()
            .withNewSpecLike(horizontalPodAutoscalerSpec)
            .withNewScaleTargetRef()
            .withApiVersion("apps/v1")
            .withKind("StatefulSet")
            .withName(clusterName + "-worker")
            .endScaleTargetRef()
            .withMinReplicas(instanceConfig.getMinWorkers())
            .withMaxReplicas(instanceConfig.getMaxWorkers())
            .endSpec()
            .build());
  }