kubernetes Kustomize进行HPA版本升级

3yhwsihp  于 2023-08-03  发布在  Kubernetes
关注(0)|答案(1)|浏览(109)

我正在尝试为HPA版本从autoscaling/v2beta2autoscaling/v1编写一个kustomize补丁。我可以使用以下补丁更改版本

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

字符串
但我无法更改以下代码段

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


最新版本的语法。
我已经尝试过JsonPatch和其他各种方法来做到这一点,但我并不成功。
是否有任何方法可以编写补丁来实现此更改?
我的apiversion补丁的输出

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补丁

- 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


这个补丁不工作。
资源的期望输出

- type: Resource
   resource:
    name: memory
    targetAverageUtilization: 70

nx7onnlm

nx7onnlm1#

您不能使用replace操作来设置/spec/metrics/0/resource/targetAverageUtilization,因为原始资源中不存在该路径。您需要使用add操作来创建新密钥。下面的方法似乎有效:

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

字符串
使用原始清单作为输入,运行此kustomization会生成:

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

相关问题