Create Your First Custom Resource

📦 Create Your First Custom Resource Definition

1
kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/customresourcedefinition/shirt-resource-definition.yaml

By default, the Kubernetes Controller Manager caches every resource to support background operations such as garbage collection and internal indexing. To reduce memory usage and improve performance in high-volume environments, you can label specific resources with skip-controller-manager-metadata-caching=true to exclude them from being cached. That means Kubernetes can’t clean-up resources by ownerReference. Select a database which automatic GC is supported, or you have to do garbage collection on your own.

💡 Ensure Label with Mutation Admission Webhook

MutatingAdmissionPolicies allow you to modify (or “mutate”) incoming requests to the Kubernetes API.

However, if you only need a declarative policy to ensure a label on each resources, the MutatingAdmissionPolicy is a simpler and more effective choice. We’ve provided a simple example below, but for complete and detailed information, please refer to the following the link: Mutating Admission Policy.

⚠️ To use the feature, enable the MutatingAdmissionPolicy feature gate (which is off by default) and set --runtime-config=admissionregistration.k8s.io/v1beta1=true on the kube-apiserver.

👕 Create Your First Custom Resources

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cat | kubectl apply -f - <<EOF
---
apiVersion: stable.example.com/v1
kind: Shirt
metadata:
  name: example1
spec:
  color: blue
  size: S
---
apiVersion: stable.example.com/v1
kind: Shirt
metadata:
  name: example2
spec:
  color: blue
  size: M
---
apiVersion: stable.example.com/v1
kind: Shirt
metadata:
  name: example3
spec:
  color: green
  size: M
EOF

Verify the resources are exists.

1
kubectl get shirts
NAME       COLOR   SIZE
example1   blue    S
example2   blue    M
example3   green   M

<– Installation | Automation –>