From bd0fb4fa5fdd9f0a2db282efb2e49744bac7236e Mon Sep 17 00:00:00 2001 From: chaunceyjiang Date: Mon, 6 Jun 2022 14:28:03 +0800 Subject: [PATCH] Add a CRD sample Signed-off-by: chaunceyjiang --- samples/guestbook/README.md | 46 ++++++++++ samples/guestbook/guestbook.yaml | 8 ++ .../guestbooks-clusterpropagationolicy.yaml | 14 ++++ samples/guestbook/guestbooks-crd.yaml | 83 +++++++++++++++++++ .../guestbook/guestbooks-overridepolicy.yaml | 20 +++++ .../guestbooks-propagationpolicy.yaml | 12 +++ 6 files changed, 183 insertions(+) create mode 100644 samples/guestbook/README.md create mode 100644 samples/guestbook/guestbook.yaml create mode 100644 samples/guestbook/guestbooks-clusterpropagationolicy.yaml create mode 100644 samples/guestbook/guestbooks-crd.yaml create mode 100644 samples/guestbook/guestbooks-overridepolicy.yaml create mode 100644 samples/guestbook/guestbooks-propagationpolicy.yaml diff --git a/samples/guestbook/README.md b/samples/guestbook/README.md new file mode 100644 index 000000000..c89b12366 --- /dev/null +++ b/samples/guestbook/README.md @@ -0,0 +1,46 @@ +# Propagate CRD application +The following steps demonstrating how to propagate a [Guestbook](https://book.kubebuilder.io/quick-start.html#create-a-project) which is defined by CRD. + +Assume you are under the guestbook directory. +```bash +cd samples/guestbook +``` +and set the KUBECONFIG environment with Karmada configuration. +```bash +export KUBECONFIG=${HOME}/.kube/karmada.config +``` +1. Create Guestbook CRD in Karmada +```bash +kubectl apply -f guestbooks-crd.yaml +``` +The CRD should be applied to `karmada-apiserver`. +2. Create ClusterPropagationPolicy that will propagate Guestbook CRD to member cluster +```bash +kubectl apply -f guestbooks-clusterpropagationolicy.yaml +``` +The CRD will be propagated to member clusters according to the rules defined in ClusterPropagationPolicy +>Note: We can only use ClusterPropagationPolicy not PropagationPolicy here. +> Please refer to FAQ Difference between [PropagationPolicy and ClusterPropagationPolicy](https://github.com/karmada-io/karmada/blob/master/docs/frequently-asked-questions.md#what-is-the-difference-between-propagationpolicy-and-clusterpropagationpolicy) +> for more details. +3. Create a Guestbook named `guestbook-sample` in Karmada +```bash +kubectl apply -f guestbook.yaml +``` +4. Create PropagationPolicy that will propagate `guestbook-sample` to member cluster +```bash +kubectl apply -f guestbooks-propagationpolicy.yaml +``` +5. Check the `guestbook-sample` status from Karmada +```bash +kubectl get guestbook -oyaml +``` +6. Create OverridePolicy that will override the size field of guestbook-sample in member cluster +```bash +kubectl apply -f guestbooks-overridepolicy.yaml +``` +7. Check the size field of `guestbook-sample` from member cluster +```bash +kubectl --kubeconfig=${HOME}/.kube/members.config config use-context member1 +kubectl --kubeconfig=${HOME}/.kube/members.config get guestbooks -o yaml +``` +If it works as expected, the `.spec.size` will be overwritten to `4`. \ No newline at end of file diff --git a/samples/guestbook/guestbook.yaml b/samples/guestbook/guestbook.yaml new file mode 100644 index 000000000..b5d398ec0 --- /dev/null +++ b/samples/guestbook/guestbook.yaml @@ -0,0 +1,8 @@ +apiVersion: webapp.my.domain/v1 +kind: Guestbook +metadata: + name: guestbook-sample +spec: + size: 2 + configMapName: test + alias: Name diff --git a/samples/guestbook/guestbooks-clusterpropagationolicy.yaml b/samples/guestbook/guestbooks-clusterpropagationolicy.yaml new file mode 100644 index 000000000..c70fd674b --- /dev/null +++ b/samples/guestbook/guestbooks-clusterpropagationolicy.yaml @@ -0,0 +1,14 @@ +# propagationpolicy.yaml +apiVersion: policy.karmada.io/v1alpha1 +kind: ClusterPropagationPolicy +metadata: + name: example-policy # The default namespace is `default`. +spec: + resourceSelectors: + - apiVersion: apiextensions.k8s.io/v1 + kind: CustomResourceDefinition + name: guestbooks.webapp.my.domain + placement: + clusterAffinity: + clusterNames: + - member1 diff --git a/samples/guestbook/guestbooks-crd.yaml b/samples/guestbook/guestbooks-crd.yaml new file mode 100644 index 000000000..bc60462be --- /dev/null +++ b/samples/guestbook/guestbooks-crd.yaml @@ -0,0 +1,83 @@ +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.8.0 + creationTimestamp: null + name: guestbooks.webapp.my.domain +spec: + group: webapp.my.domain + names: + kind: Guestbook + listKind: GuestbookList + plural: guestbooks + singular: guestbook + scope: Namespaced + versions: + - name: v1 + schema: + openAPIV3Schema: + description: Guestbook is the Schema for the guestbooks API + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: GuestbookSpec defines the desired state of Guestbook + properties: + alias: + enum: + - Phone + - Address + - Name + type: string + configMapName: + description: Name of the ConfigMap for GuestbookSpec's configuration + maxLength: 15 + minLength: 1 + type: string + size: + description: Quantity of instances + format: int32 + maximum: 10 + minimum: 1 + type: integer + required: + - configMapName + - size + type: object + status: + description: GuestbookStatus defines the observed state of Guestbook + properties: + active: + description: PodName of the active Guestbook node. + type: string + standby: + description: PodNames of the standby Guestbook nodes. + items: + type: string + type: array + required: + - active + - standby + type: object + type: object + served: true + storage: true + subresources: + status: {} +status: + acceptedNames: + kind: "" + plural: "" + conditions: [] + storedVersions: [] diff --git a/samples/guestbook/guestbooks-overridepolicy.yaml b/samples/guestbook/guestbooks-overridepolicy.yaml new file mode 100644 index 000000000..961c5a113 --- /dev/null +++ b/samples/guestbook/guestbooks-overridepolicy.yaml @@ -0,0 +1,20 @@ +apiVersion: policy.karmada.io/v1alpha1 +kind: OverridePolicy +metadata: + name: guestbook-sample +spec: + resourceSelectors: + - apiVersion: webapp.my.domain/v1 + kind: Guestbook + overrideRules: + - targetCluster: + clusterNames: + - member1 + overriders: + plaintext: + - path: /spec/size + operator: replace + value: 4 + - path: /metadata/annotations + operator: add + value: {"OverridePolicy":"test"} diff --git a/samples/guestbook/guestbooks-propagationpolicy.yaml b/samples/guestbook/guestbooks-propagationpolicy.yaml new file mode 100644 index 000000000..800b55fdf --- /dev/null +++ b/samples/guestbook/guestbooks-propagationpolicy.yaml @@ -0,0 +1,12 @@ +apiVersion: policy.karmada.io/v1alpha1 +kind: PropagationPolicy +metadata: + name: example-policy +spec: + resourceSelectors: + - apiVersion: webapp.my.domain/v1 + kind: Guestbook + placement: + clusterAffinity: + clusterNames: + - member1