<p>I want to create HPA using the Fabric8 API. I want to dynamically add metrics to the MetricSpecBuider.
Something like the following,</p>
<pre><code>        HorizontalPodAutoscaler horizontalPodAutoscaler = new HorizontalPodAutoscalerBuilder()
            .withNewMetadata().withName(&quot;${config.metadata_name}-hpa&quot;).endMetadata()
            .withNewSpec()
            .withNewScaleTargetRef()
            .withApiVersion(&quot;apps/v1&quot;)
            .withKind(&quot;Deployment&quot;)
            .withName(&quot;${config.metadata_name}&quot;)
            .endScaleTargetRef()
            .withMinReplicas(1)
            .withMaxReplicas(10)
            .addToMetrics(new io.fabric8.kubernetes.api.model.MetricSpecBuilder()
            for (metric in config.deploy_env.metrics) {
                .withType(metric.type)
                .withNewResource()
                .withName(metric.resource.name)
                .withNewTarget()
                .withType(metric.resource.target.type)
                .withAverageUtilization(metric.resource.target.averageUtilization)
                .endTarget()
                .endResource()
            }
            .build())
            .endSpec()
            .build()
        return horizontalPodAutoscaler
</code></pre>
<p>However, this results in an error.</p>
<pre><code>81: unexpected token: for @ line 81, column 4.
11:19:14                for (metric in config.deploy_env.metrics) {
11:19:14        ^
11:19:14  
11:19:14  1 error
11:19:14  
</code></pre>
<p>Can someone please let me know how to achieve it ?</p>
<p>Thanks !</p>

Taseer Ahmed
<p>I'm from Fabric8 team. I think you should be able to create a new metric dynamically using <code>addNewMetric()</code> method in the builder. I think you need to something like this in order to add new metrics dynamically. You can also create a <code>List&lt;MetricSpec&gt;</code> dynamically and assign it to builder with <code>withMetrics(metricsList)</code>.</p>
<p>This code is based of Fabric8 Kubernetes Client v4.10.3:</p>
<pre><code>HorizontalPodAutoscalerBuilder horizontalPodAutoscalerBuilder = new HorizontalPodAutoscalerBuilder()
        .withNewMetadata().withName(&quot;the-hpa&quot;).withNamespace(&quot;default&quot;).endMetadata()
        .withNewSpec()
        .withMetrics()
        .withNewScaleTargetRef()
        .withApiVersion(&quot;apps/v1&quot;)
        .withKind(&quot;Deployment&quot;)
        .withName(&quot;the-deployment&quot;)
        .endScaleTargetRef()
        .withMinReplicas(1)
        .withMaxReplicas(10)
        .withNewBehavior()
        .withNewScaleDown()
        .addNewPolicy()
        .withType(&quot;Pods&quot;)
        .withValue(4)
        .withPeriodSeconds(60)
        .endPolicy()
        .addNewPolicy()
        .withType(&quot;Percent&quot;)
        .withValue(10)
        .withPeriodSeconds(60)
        .endPolicy()
        .endScaleDown()
        .endBehavior()
        .endSpec();

for (int i = 0; i &lt; metricInput.length; i++) {
    horizontalPodAutoscalerBuilder
            .editOrNewSpec()
            .addNewMetric()
            .withType(metricInput[i])
            // ...
            .endMetric()
            .endSpec();
}

return horizontalPodAutoscalerBuilder.build()
</code></pre>
