Add a CRD sample

Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
This commit is contained in:
chaunceyjiang 2022-06-06 14:28:03 +08:00
parent 14e673be3d
commit bd0fb4fa5f
6 changed files with 183 additions and 0 deletions

View File

@ -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`.

View File

@ -0,0 +1,8 @@
apiVersion: webapp.my.domain/v1
kind: Guestbook
metadata:
name: guestbook-sample
spec:
size: 2
configMapName: test
alias: Name

View File

@ -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

View File

@ -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: []

View File

@ -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"}

View File

@ -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