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

desired-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: false
  10. selector:
  11. matchLabels:
  12. app: nginx
  13. template:
  14. metadata:
  15. labels:
  16. app: nginx
  17. spec:
  18. containers:
  19. - image: nginx
  20. name: nginx
  21. serviceAccountName: test-sa

resourceinterpretercustomization.yaml

  1. apiVersion: config.karmada.io/v1alpha1
  2. kind: ResourceInterpreterCustomization
  3. metadata:
  4. name: declarative-configuration-example
  5. spec:
  6. target:
  7. apiVersion: apps/v1
  8. kind: Deployment
  9. customizations:
  10. replicaResource:
  11. luaScript: >
  12. local kube = require("kube")
  13. function GetReplicas(obj)
  14. replica = obj.spec.replicas
  15. requirement = kube.accuratePodRequirements(obj.spec.template)
  16. return replica, requirement
  17. end
  18. replicaRevision:
  19. luaScript: >
  20. function ReviseReplica(obj, desiredReplica)
  21. obj.spec.replicas = desiredReplica
  22. return obj
  23. end
  24. retention:
  25. luaScript: >
  26. function Retain(desiredObj, observedObj)
  27. desiredObj.spec.paused = observedObj.spec.paused
  28. return desiredObj
  29. end
  30. statusAggregation:
  31. luaScript: >
  32. function AggregateStatus(desiredObj, statusItems)
  33. if statusItems == nil then
  34. return desiredObj
  35. end
  36. if desiredObj.status == nil then
  37. desiredObj.status = {}
  38. end
  39. replicas = 0
  40. for i = 1, #statusItems do
  41. if statusItems[i].status ~= nil and statusItems[i].status.replicas ~= nil then
  42. replicas = replicas + statusItems[i].status.replicas
  43. end
  44. end
  45. desiredObj.status.replicas = replicas
  46. return desiredObj
  47. end
  48. statusReflection:
  49. luaScript: >
  50. function ReflectStatus (observedObj)
  51. return observedObj.status
  52. end
  53. healthInterpretation:
  54. luaScript: >
  55. function InterpretHealth(observedObj)
  56. return observedObj.status.readyReplicas == observedObj.spec.replicas
  57. end
  58. dependencyInterpretation:
  59. luaScript: >
  60. local kube = require("kube")
  61. function GetDependencies(desiredObj)
  62. refs = kube.getPodDependencies(desiredObj.spec.template, desiredObj.metadata.namespace)
  63. return refs
  64. end

status-file.yaml

  1. applied: true
  2. clusterName: member1
  3. health: Healthy
  4. status:
  5. availableReplicas: 1
  6. readyReplicas: 1
  7. replicas: 1
  8. updatedReplicas: 1
  9. ---
  10. applied: true
  11. clusterName: member2
  12. health: Healthy
  13. status:
  14. availableReplicas: 1
  15. readyReplicas: 1
  16. replicas: 1
  17. updatedReplicas: 1

验证 ResourceInterpreterCustomization 资源配置

  1. karmadactl interpret -f resourceinterpretercustomization.yaml --check

执行 ResourceInterpreterCustomization 特性操作

执行 InterpretReplica 规则

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

执行 Retain 规则

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

执行 ReviseReplica 规则

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

执行 InterpretStatus 规则

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

执行 InterpretHealth 规则

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

执行 InterpretDependency 规则

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

执行 AggregateStatus 规则

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