Karmadactl用法约定

karmadactl推荐用法约定。

karmadactl interpret

YAML文件准备

observed-deploy-nginx.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx
  5. labels:
  6. app: nginx
  7. spec:
  8. replicas: 3
  9. paused: true
  10. selector:
  11. matchLabels:
  12. app: nginx
  13. template:
  14. metadata:
  15. labels:
  16. app: nginx
  17. spec:
  18. nodeSelector:
  19. foo: bar
  20. containers:
  21. - image: nginx
  22. name: nginx
  23. resources:
  24. limits:
  25. cpu: 100m
  26. status:
  27. availableReplicas: 2
  28. observedGeneration: 1
  29. readyReplicas: 2
  30. replicas: 2
  31. updatedReplicas: 2
  32. ``` desired-deploy-nginx.yaml

apiVersion: apps/v1 kind: Deployment metadata: name: nginx labels: app: nginx spec: replicas: 3 paused: false selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers:

  1. - image: nginx
  2. name: nginx
  3. serviceAccountName: test-sa

apiVersion: config.karmada.io/v1alpha1 kind: ResourceInterpreterCustomization metadata: name: declarative-configuration-example spec: target: apiVersion: apps/v1 kind: Deployment customizations: replicaResource: luaScript: > local kube = require(“kube”) function GetReplicas(obj) replica = obj.spec.replicas requirement = kube.accuratePodRequirements(obj.spec.template) return replica, requirement end replicaRevision: luaScript: > function ReviseReplica(obj, desiredReplica) obj.spec.replicas = desiredReplica return obj end retention: luaScript: > function Retain(desiredObj, observedObj) desiredObj.spec.paused = observedObj.spec.paused return desiredObj end statusAggregation: luaScript: > function AggregateStatus(desiredObj, statusItems) if statusItems == nil then return desiredObj end if desiredObj.status == nil then desiredObj.status = {} end replicas = 0 for i = 1, #statusItems do if statusItems[i].status ~= nil and statusItems[i].status.replicas ~= nil then replicas = replicas + statusItems[i].status.replicas end end desiredObj.status.replicas = replicas return desiredObj end statusReflection: luaScript: > function ReflectStatus (observedObj) return observedObj.status end healthInterpretation: luaScript: > function InterpretHealth(observedObj) return observedObj.status.readyReplicas == observedObj.spec.replicas end dependencyInterpretation: luaScript: > function GetDependencies(desiredObj) dependentSas = {} refs = {} if desiredObj.spec.template.spec.serviceAccountName ~= nil and desiredObj.spec.template.spec.serviceAccountName ~= ‘default’ then dependentSas[desiredObj.spec.template.spec.serviceAccountName] = true end local idx = 1 for key, value in pairs(dependentSas) do dependObj = {} dependObj.apiVersion = ‘v1’ dependObj.kind = ‘ServiceAccount’ dependObj.name = key dependObj.namespace = desiredObj.metadata.namespace refs[idx] = dependObj idx = idx + 1 end return refs end

applied: true clusterName: member1 health: Healthy status: availableReplicas: 1 readyReplicas: 1 replicas: 1

updatedReplicas: 1

applied: true clusterName: member2 health: Healthy status: availableReplicas: 1 readyReplicas: 1 replicas: 1 updatedReplicas: 1

  1. ### 验证 ResourceInterpreterCustomization 资源配置

karmadactl interpret -f resourceinterpretercustomization.yaml —check

  1. ### 执行 ResourceInterpreterCustomization 特性操作
  2. #### 执行 InterpretReplica 规则

karmadactl interpret -f resourceinterpretercustomization.yaml —observed-file observed-deploy-nginx.yaml —operation=InterpretReplica

  1. #### 执行 Retain 规则

karmadactl interpret -f resourceinterpretercustomization.yaml —desired-file desired-deploy-nginx.yaml —observed-file observed-deploy-nginx.yaml —operation Retain

  1. #### 执行 ReviseReplica 规则

karmadactl interpret -f resourceinterpretercustomization.yaml —desired-replica 3 —observed-file observed-deploy-nginx.yaml —operation ReviseReplica

  1. #### 执行 InterpretStatus 规则

karmadactl interpret -f resourceinterpretercustomization.yaml —observed-file observed-deploy-nginx.yaml —operation InterpretStatus

  1. #### 执行 InterpretHealth 规则

karmadactl interpret -f resourceinterpretercustomization.yaml —observed-file observed-deploy-nginx.yaml —operation InterpretHealth

  1. #### 执行 InterpretDependency 规则

karmadactl interpret -f resourceinterpretercustomization.yaml —desired-file desired-deploy-nginx.yaml —operation InterpretDependency

  1. #### 执行 AggregateStatus 规则

karmadactl interpret -f resourceinterpretercustomization.yaml —desired-file desired-deploy-nginx.yaml —operation AggregateStatus —status-file status-file.yaml ```