Resource Definitions

Driver

Capability

Flavor

Resource Type

Horizontal Pod Autoscaler

This section contains a Resource Definition example for handling Kubernetes HorizontalPodAutoscaler by using the template Driver to configure your own HorizontalPodAutoscaler implementation. You can see this other example if you want to use the hpa Driver.

You can find a Score file example using the horizontal-pod-autoscaler resource type here .

Resource Definitions


hpa.yaml ( view on GitHub ) :

apiVersion: entity.humanitec.io/v1b1
kind: Definition
metadata:
  id: hpa
entity:
  driver_type: humanitec/template
  name: hpa
  type: horizontal-pod-autoscaler
  driver_inputs:
    values:
      templates:
        init: |
          {{ $defaultMaxReplicas := 3 }}
          {{ $defaultMinReplicas := 2 }}
          {{ $absoluteMaxReplicas := 10 }}
          {{ $defaultTargetUtilizationPercent := 80 }}
          workload: {{ index (splitList "." "${context.res.id}") 1 }}
          maxReplicas: {{ .resource.maxReplicas | default $defaultMaxReplicas | min $absoluteMaxReplicas }}
          minReplicas: {{ .resource.minReplicas | default $defaultMinReplicas }}
          targetCPUUtilizationPercentage: {{ .resource.targetCPUUtilizationPercentage | default $defaultTargetUtilizationPercent }}
        manifests: |
          hpa.yaml:
            location: namespace
            data:
              apiVersion: autoscaling/v2
              kind: HorizontalPodAutoscaler
              metadata:
                name: {{ .init.workload }}-hpa
              spec:
                maxReplicas: {{ .init.maxReplicas }}
                metrics:
                - resource:
                    name: cpu
                    target:
                      averageUtilization: {{ .init.targetCPUUtilizationPercentage }}
                      type: Utilization
                  type: Resource
                minReplicas: {{ .init.minReplicas }}
                scaleTargetRef:
                  apiVersion: apps/v1
                  kind: Deployment
                  name: {{ .init.workload }}
  criteria:
  - {}


hpa.tf ( view on GitHub ) :

resource "humanitec_resource_definition" "hpa" {
  driver_type = "humanitec/template"
  id          = "hpa"
  name        = "hpa"
  type        = "horizontal-pod-autoscaler"
  driver_inputs = {
    values_string = jsonencode({
      "templates" = {
        "init"      = <<END_OF_TEXT
{{ $defaultMaxReplicas := 3 }}
{{ $defaultMinReplicas := 2 }}
{{ $absoluteMaxReplicas := 10 }}
{{ $defaultTargetUtilizationPercent := 80 }}
workload: {{ index (splitList "." "$${context.res.id}") 1 }}
maxReplicas: {{ .resource.maxReplicas | default $defaultMaxReplicas | min $absoluteMaxReplicas }}
minReplicas: {{ .resource.minReplicas | default $defaultMinReplicas }}
targetCPUUtilizationPercentage: {{ .resource.targetCPUUtilizationPercentage | default $defaultTargetUtilizationPercent }}
END_OF_TEXT
        "manifests" = <<END_OF_TEXT
hpa.yaml:
  location: namespace
  data:
    apiVersion: autoscaling/v2
    kind: HorizontalPodAutoscaler
    metadata:
      name: {{ .init.workload }}-hpa
    spec:
      maxReplicas: {{ .init.maxReplicas }}
      metrics:
      - resource:
          name: cpu
          target:
            averageUtilization: {{ .init.targetCPUUtilizationPercentage }}
            type: Utilization
        type: Resource
      minReplicas: {{ .init.minReplicas }}
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: {{ .init.workload }}
END_OF_TEXT
      }
    })
  }
}

resource "humanitec_resource_definition_criteria" "hpa_criteria_0" {
  resource_definition_id = resource.humanitec_resource_definition.hpa.id

}

Top