diff --git a/examples/README.md b/examples/customresourceinterpreter/README.md similarity index 100% rename from examples/README.md rename to examples/customresourceinterpreter/README.md diff --git a/examples/karmadactlinterpret/README.md b/examples/karmadactlinterpret/README.md new file mode 100644 index 000000000..1e6276472 --- /dev/null +++ b/examples/karmadactlinterpret/README.md @@ -0,0 +1,45 @@ +# Examples for Karmadactl interpret command + +This example shows how to use the `karmadactl interpret` command easily. + +*Validate the ResourceInterpreterCustomization configuration* + +```shell +karmadactl interpret -f resourceinterpretercustomization.yaml --check +``` + +*Execute the InterpretReplica rule* + +```shell +karmadactl interpret -f resourceinterpretercustomization.yaml --observed-file observed-deploy-nginx.yaml --operation=InterpretReplica +``` + +*Execute the Retain rule* + +```shell +karmadactl interpret -f resourceinterpretercustomization.yaml --desired-file desired-deploy-nginx.yaml --observed-file observed-deploy-nginx.yaml --operation Retain +``` + +*Execute the InterpretStatus rule* + +```shell +karmadactl interpret -f resourceinterpretercustomization.yaml --observed-file observed-deploy-nginx.yaml --operation InterpretStatus +``` + +*Execute the InterpretHealth rule* + +```shell +karmadactl interpret -f resourceinterpretercustomization.yaml --observed-file observed-deploy-nginx.yaml --operation InterpretHealth +``` + +*Execute the InterpretDependency rule* + +```shell +karmadactl interpret -f resourceinterpretercustomization.yaml --desired-file desired-deploy-nginx.yaml --operation InterpretDependency +``` + +*Execute the AggregateStatus rule* + +```shell +karmadactl interpret -f resourceinterpretercustomization.yaml --desired-file desired-deploy-nginx.yaml --operation AggregateStatus --status-file status-file.yaml +``` diff --git a/examples/karmadactlinterpret/desired-deploy-nginx.yaml b/examples/karmadactlinterpret/desired-deploy-nginx.yaml new file mode 100644 index 000000000..3f2ef11ce --- /dev/null +++ b/examples/karmadactlinterpret/desired-deploy-nginx.yaml @@ -0,0 +1,21 @@ +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: + - image: nginx + name: nginx + serviceAccountName: test-sa diff --git a/examples/karmadactlinterpret/observed-deploy-nginx.yaml b/examples/karmadactlinterpret/observed-deploy-nginx.yaml new file mode 100644 index 000000000..c78927646 --- /dev/null +++ b/examples/karmadactlinterpret/observed-deploy-nginx.yaml @@ -0,0 +1,26 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx + labels: + app: nginx +spec: + replicas: 3 + paused: true + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - image: nginx + name: nginx +status: + availableReplicas: 2 + observedGeneration: 1 + readyReplicas: 2 + replicas: 2 + updatedReplicas: 2 diff --git a/examples/karmadactlinterpret/resourceinterpretercustomization.yaml b/examples/karmadactlinterpret/resourceinterpretercustomization.yaml new file mode 100644 index 000000000..c2141ef95 --- /dev/null +++ b/examples/karmadactlinterpret/resourceinterpretercustomization.yaml @@ -0,0 +1,76 @@ +apiVersion: config.karmada.io/v1alpha1 +kind: ResourceInterpreterCustomization +metadata: + name: declarative-configuration-example +spec: + target: + apiVersion: apps/v1 + kind: Deployment + customizations: + replicaResource: + luaScript: > + function GetReplicas(obj) + replica = obj.spec.replicas + requirement = {} + 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 ~= '' 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 diff --git a/examples/karmadactlinterpret/status-file.yaml b/examples/karmadactlinterpret/status-file.yaml new file mode 100644 index 000000000..4d03d8ee4 --- /dev/null +++ b/examples/karmadactlinterpret/status-file.yaml @@ -0,0 +1,17 @@ +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