|
| 1 | +// Module included in the following assemblies: |
| 2 | +// |
| 3 | +// * storage/container_storage_interface/persistent-storage-csi-vol-populators.adoc |
| 4 | + |
| 5 | +:_mod-docs-content-type: PROCEDURE |
| 6 | +[id="persistent-storage-csi-vol-populator-procedure-admin_{context}"] |
| 7 | += Creating CRDs for volume populators |
| 8 | + |
| 9 | +The following procedure explains how to create an example "hello, world" custom resource definition (CRD) for a volume populator. |
| 10 | + |
| 11 | +Users can then create instances of this CRD to populate persistent volume claims (PVCs). |
| 12 | + |
| 13 | +.Prerequisites |
| 14 | + |
| 15 | +* Access to the {product-title} web console. |
| 16 | +
|
| 17 | +* Access to the cluster with cluster-admin privileges. |
| 18 | +
|
| 19 | +.Procedure |
| 20 | + |
| 21 | +. Create a namespace for the logical grouping and operation of the populator, and related resources, using the following example YAML file: |
| 22 | ++ |
| 23 | +.Example namespace YAML file |
| 24 | +[source,yaml] |
| 25 | +---- |
| 26 | +apiVersion: v1 |
| 27 | +kind: Namespace |
| 28 | +metadata: |
| 29 | + name: hello |
| 30 | +---- |
| 31 | + |
| 32 | +. Create a CRD for your data source using the following example YAML file: |
| 33 | ++ |
| 34 | +.Example CRD YAML file |
| 35 | +[source,yaml] |
| 36 | +---- |
| 37 | +apiVersion: apiextensions.k8s.io/v1 |
| 38 | +kind: CustomResourceDefinition |
| 39 | +metadata: |
| 40 | + name: hellos.hello.example.com |
| 41 | +spec: |
| 42 | + group: hello.example.com |
| 43 | + names: |
| 44 | + kind: Hello |
| 45 | + listKind: HelloList |
| 46 | + plural: hellos |
| 47 | + singular: hello |
| 48 | + scope: Namespaced |
| 49 | + versions: |
| 50 | + - name: v1alpha1 |
| 51 | + schema: |
| 52 | + openAPIV3Schema: |
| 53 | + description: Hello is a specification for a Hello resource |
| 54 | + properties: |
| 55 | + apiVersion: |
| 56 | + description: 'APIVersion defines the versioned schema of this representation |
| 57 | + of an object. Servers should convert recognized schemas to the latest |
| 58 | + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' |
| 59 | + type: string |
| 60 | + kind: |
| 61 | + description: 'Kind is a string value representing the REST resource this |
| 62 | + object represents. Servers may infer this from the endpoint the client |
| 63 | + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' |
| 64 | + type: string |
| 65 | + spec: |
| 66 | + description: HelloSpec is the spec for a Hello resource |
| 67 | + properties: |
| 68 | + fileContents: |
| 69 | + type: string |
| 70 | + fileName: |
| 71 | + type: string |
| 72 | + required: |
| 73 | + - fileContents |
| 74 | + - fileName |
| 75 | + type: object |
| 76 | + required: |
| 77 | + - spec |
| 78 | + type: object |
| 79 | + served: true |
| 80 | + storage: true |
| 81 | +---- |
| 82 | + |
| 83 | +. Deploy the controller by creating a `ServiceAccount`, `ClusterRole`, `ClusterRoleBindering`, and `Deployment` to run the logic that implements the population: |
| 84 | + |
| 85 | +.. Create a service account for the populator using the following example YAML file: |
| 86 | ++ |
| 87 | +.Example service account YAML file |
| 88 | +[source,yaml] |
| 89 | +---- |
| 90 | +apiVersion: v1 |
| 91 | +kind: ServiceAccount |
| 92 | +metadata: |
| 93 | + name: hello-account |
| 94 | + namespace: hello <1> |
| 95 | +---- |
| 96 | +<1> Reference the namespace that you created earlier. |
| 97 | + |
| 98 | +.. Create a cluster role for the populator using the following example YAML file: |
| 99 | ++ |
| 100 | +.Example cluster role YAML file |
| 101 | +[source,yaml] |
| 102 | +---- |
| 103 | +kind: ClusterRole |
| 104 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 105 | +metadata: |
| 106 | + name: hello-role |
| 107 | +rules: |
| 108 | + - apiGroups: [hello.example.com] |
| 109 | + resources: [hellos] |
| 110 | + verbs: [get, list, watch] |
| 111 | +---- |
| 112 | + |
| 113 | +.. Create a cluster role binding using the following example YAML file: |
| 114 | ++ |
| 115 | +.Example cluster role binding YAML file |
| 116 | +[source,yaml] |
| 117 | +---- |
| 118 | +kind: ClusterRoleBinding |
| 119 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 120 | +metadata: |
| 121 | + name: hello-binding <1> |
| 122 | +subjects: |
| 123 | + - kind: ServiceAccount |
| 124 | + name: hello-account <2> |
| 125 | + namespace: hello <3> |
| 126 | +roleRef: |
| 127 | + kind: ClusterRole |
| 128 | + name: hello-role <4> |
| 129 | + apiGroup: rbac.authorization.k8s.io |
| 130 | +---- |
| 131 | +<1> Role binding name. |
| 132 | +<2> Reference the name of the service account that you created earlier. |
| 133 | +<3> Reference the name of the namespace for the service account that you created earlier. |
| 134 | +<4> Reference the cluster role you created earlier. |
| 135 | + |
| 136 | +.. Create a Deployment for the populator using the following example YAML file: |
| 137 | ++ |
| 138 | +.Example deployment YAML file |
| 139 | +[source,yaml] |
| 140 | +---- |
| 141 | +kind: Deployment |
| 142 | +apiVersion: apps/v1 |
| 143 | +metadata: |
| 144 | + name: hello-populator |
| 145 | + namespace: hello <1> |
| 146 | +spec: |
| 147 | + selector: |
| 148 | + matchLabels: |
| 149 | + app: hello |
| 150 | + template: |
| 151 | + metadata: |
| 152 | + labels: |
| 153 | + app: hello |
| 154 | + spec: |
| 155 | + serviceAccount: hello-account <2> |
| 156 | + containers: |
| 157 | + - name: hello |
| 158 | + image: registry.k8s.io/sig-storage/hello-populator:v1.0.1 |
| 159 | + imagePullPolicy: IfNotPresent |
| 160 | + args: |
| 161 | + - --mode=controller |
| 162 | + - --image-name=registry.k8s.io/sig-storage/hello-populator:v1.0.1 |
| 163 | + - --http-endpoint=:8080 |
| 164 | + ports: |
| 165 | + - containerPort: 8080 |
| 166 | + name: http-endpoint |
| 167 | + protocol: TCP |
| 168 | +---- |
| 169 | +<1> Reference the namespace that you created earlier. |
| 170 | +<2> Reference the service account that you created earlier. |
| 171 | + |
| 172 | +. Create a volume populator to register the `kind:Hello` resource as a valid data source for the volume using the following example YAML file: |
| 173 | ++ |
| 174 | +.Example volume populator YAML file |
| 175 | +[source,yaml] |
| 176 | +---- |
| 177 | +kind: VolumePopulator |
| 178 | +apiVersion: populator.storage.k8s.io/v1beta1 |
| 179 | +metadata: |
| 180 | + name: hello-populator <1> |
| 181 | +sourceKind: |
| 182 | + group: hello.example.com |
| 183 | + kind: Hello |
| 184 | +---- |
| 185 | +<1> Volume populator name. |
| 186 | ++ |
| 187 | +PVCs that use an unregistered populator generate an event: "The datasource for this PVC does not match any registered VolumePopulator", indicating that the PVC might not be provisioned because you are using an unknown (unregistered) populator. |
| 188 | + |
| 189 | +.Next steps |
| 190 | +* You can now create CR instances of this CRD to populate PVCs. |
0 commit comments