-1

I m trying to write a kustomize patch for HPA version from autoscaling/v2beta2 to autoscaling/v1 . I am able to change the version with the following patch

patches:
- patch: |-
    - op: replace
      path: "/apiVersion"
      value: autoscaling/v1
  target:
    group: autoscaling
    version: v2beta2

But I m unable to change the following snippet

- type: Resource
  resource:
    name: memory
    target:
      type: Utilization
      averageUtilization: 70

to the latest version syntax.

I have tried JsonPatch and various other ways to do this, but I m not succssful in doing that.

Is there anyway to write a patch to achieve this change?

Output of my apiversion patch

  apiVersion: autoscaling/v1
  kind: HorizontalPodAutoscaler
  metadata:
    labels:
      environment: production
    name: piebot
    namespace: piebot
  spec:
    maxReplicas: 3
    metrics:
    - resource:
        name: cpu
        target:
          averageUtilization: 70
          type: Utilization
      type: Resource
    - resource:
        name: memory
        target:
          averageUtilization: 70
          type: Utilization
      type: Resource
    minReplicas: 1
    scaleTargetRef:
      apiVersion: apps/v1
      kind: Deployment
      name: piebot

JSON patch I tried

- op: replace
  path: /apiVersion
  value: autoscaling/v2beta2
- op: replace
  path: /spec/metrics/0/resource/targetAverageUtilization
  value: 70
- op: replace
  path: /spec/metrics/1/resource/targetAverageUtilization
  value: 70

Thsi patch doesnt work.

Desired output for resource

- type: Resource
   resource:
    name: memory
    targetAverageUtilization: 70
Vini
  • 1,978
  • 8
  • 40
  • 82
  • How should the second snippet look after modification? What does your patch attempt look like? – larsks Jul 21 '23 at 12:16
  • I have updated the question with the desired output and the output i get wth my current patch – Vini Jul 21 '23 at 12:53

1 Answers1

1

You can't use a replace operation to set /spec/metrics/0/resource/targetAverageUtilization because that path doesn't exist in your original resource. You need to use an add operation to create a new key. The following seems to work:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- hpa.yaml

patches:
- patch: |-
    - op: replace
      path: "/apiVersion"
      value: autoscaling/v1
    - op: add
      path: /spec/metrics/0/resource/targetAverageUtilization
      value: 70
    - op: add
      path: /spec/metrics/1/resource/targetAverageUtilization
      value: 70
    - op: remove
      path: /spec/metrics/0/resource/target
    - op: remove
      path: /spec/metrics/1/resource/target
  target:
    group: autoscaling
    version: v2beta2

With your original manifest as input, running this kustomization produces:

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  labels:
    environment: production
  name: piebot
  namespace: piebot
spec:
  maxReplicas: 3
  metrics:
  - resource:
      name: cpu
      targetAverageUtilization: 70
    type: Resource
  - resource:
      name: memory
      targetAverageUtilization: 70
    type: Resource
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: piebot
larsks
  • 277,717
  • 41
  • 399
  • 399
  • This works, but it would fail if the resource I apply the patch doesnt have the path. For instance if my HPA doesnt have resource for cpu, this would fail. – Vini Jul 24 '23 at 10:39
  • I am confident that this answer resolves the situation you presented in your question. If you'd like to ask a new question I would be happy to take a look. – larsks Jul 24 '23 at 11:37