Skip to content

Commit abf10b7

Browse files
committed
Add populator library and "hello" example
1 parent da63433 commit abf10b7

File tree

13 files changed

+1856
-0
lines changed

13 files changed

+1856
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

example/hello-populator/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello-populator

example/hello-populator/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM gcr.io/distroless/static:latest
2+
ADD hello-populator /
3+
ENTRYPOINT ["/hello-populator"]

example/hello-populator/Makefile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2021 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
export CGO_ENABLED=0
16+
IMAGE_NAME?=hello-populator
17+
18+
REV=$(shell git describe --long --tags --match='v*' --dirty)
19+
20+
all: build
21+
22+
compile:
23+
go build -ldflags '-s -w -X main.version=$(REV)' -o hello-populator main.go
24+
25+
build: compile
26+
docker build -f Dockerfile -t $(IMAGE_NAME) .
27+
28+
clean:
29+
go clean -i -x ./...
30+
31+
.PHONY: all compile build clean

example/hello-populator/README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
2+
# Hello World Populator Example
3+
4+
This example demonstrates an extremely simple populator implementation.
5+
6+
## How To
7+
8+
Install kubernetes 1.17 or later, and enable the AnyVolumeDataSource
9+
feature gate.
10+
11+
Build the image from code:
12+
13+
`make all`
14+
15+
Make sure you have a repo you can push to, and set the variable
16+
17+
`YOUR_REPO=...`
18+
19+
Push the image to your repo:
20+
21+
```
22+
docker tag hello-populator:latest ${YOUR_REPO}/hello-populator:latest
23+
docker push ${YOUR_REPO}/hello-populator:latest
24+
```
25+
26+
Install the CRD:
27+
28+
`kubectl apply -f crd.yaml`
29+
30+
Install the controller:
31+
32+
`kubectl apply -f deploy.yaml`
33+
34+
Create a CR:
35+
36+
```
37+
kubectl create -f - << EOF
38+
apiVersion: hello.k8s.io/v1alpha1
39+
kind: Hello
40+
metadata:
41+
name: hello1
42+
spec:
43+
fileName: test.txt
44+
fileContents: Hello, world!
45+
EOF
46+
```
47+
48+
Create a PVC:
49+
50+
```
51+
kubectl create -f - << EOF
52+
apiVersion: v1
53+
kind: PersistentVolumeClaim
54+
metadata:
55+
name: pvc1
56+
spec:
57+
accessModes:
58+
- ReadWriteOnce
59+
resources:
60+
requests:
61+
storage: 10Mi
62+
dataSource:
63+
apiGroup: hello.k8s.io
64+
kind: Hello
65+
name: hello1
66+
EOF
67+
```
68+
69+
Create a job to print the contents of the pre-populated file inside the PVC:
70+
71+
```
72+
kubectl create -f - << EOF
73+
apiVersion: batch/v1
74+
kind: Job
75+
metadata:
76+
name: job1
77+
spec:
78+
template:
79+
spec:
80+
containers:
81+
- name: test
82+
image: busybox:1.31.1
83+
command:
84+
- cat
85+
- /mnt/test.txt
86+
volumeMounts:
87+
- mountPath: /mnt
88+
name: vol
89+
restartPolicy: Never
90+
volumes:
91+
- name: vol
92+
persistentVolumeClaim:
93+
claimName: pvc1
94+
EOF
95+
```
96+
97+
Wait for the job to complete:
98+
99+
`kubectl wait --for=condition=Complete --timeout=120s job/job1`
100+
101+
Get the logs from the job to verify that it worked:
102+
103+
`kubectl logs job/job1`

example/hello-populator/crd.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
apiVersion: apiextensions.k8s.io/v1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: hellos.hello.k8s.io
5+
annotations:
6+
api-approved.kubernetes.io: unapproved
7+
spec:
8+
group: hello.k8s.io
9+
names:
10+
kind: Hello
11+
listKind: HelloList
12+
plural: hellos
13+
singular: hello
14+
scope: Namespaced
15+
versions:
16+
- name: v1alpha1
17+
schema:
18+
openAPIV3Schema:
19+
description: Hello is a specification for a Hello resource
20+
properties:
21+
apiVersion:
22+
description: 'APIVersion defines the versioned schema of this representation
23+
of an object. Servers should convert recognized schemas to the latest
24+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
25+
type: string
26+
kind:
27+
description: 'Kind is a string value representing the REST resource this
28+
object represents. Servers may infer this from the endpoint the client
29+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
30+
type: string
31+
spec:
32+
description: HelloSpec is the spec for a Hello resource
33+
properties:
34+
fileContents:
35+
type: string
36+
fileName:
37+
type: string
38+
required:
39+
- fileContents
40+
- fileName
41+
type: object
42+
required:
43+
- spec
44+
type: object
45+
served: true
46+
storage: true
47+
status:
48+
acceptedNames:
49+
kind: ""
50+
plural: ""
51+
conditions: []
52+
storedVersions: []
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: hello
5+
---
6+
apiVersion: v1
7+
kind: ServiceAccount
8+
metadata:
9+
name: hello-account
10+
namespace: hello
11+
---
12+
kind: ClusterRole
13+
apiVersion: rbac.authorization.k8s.io/v1
14+
metadata:
15+
name: hello-role
16+
rules:
17+
- apiGroups: [""]
18+
resources: [persistentvolumes]
19+
verbs: [get, list, watch, patch]
20+
- apiGroups: [""]
21+
resources: [persistentvolumeclaims]
22+
verbs: [get, list, watch, patch, create, delete]
23+
- apiGroups: [""]
24+
resources: [pods]
25+
verbs: [get, list, watch, create, delete]
26+
- apiGroups: [storage.k8s.io]
27+
resources: [storageclasses]
28+
verbs: [get, list, watch]
29+
30+
- apiGroups: [hello.k8s.io]
31+
resources: [hellos]
32+
verbs: [get, list, watch]
33+
---
34+
kind: ClusterRoleBinding
35+
apiVersion: rbac.authorization.k8s.io/v1
36+
metadata:
37+
name: hello-binding
38+
subjects:
39+
- kind: ServiceAccount
40+
name: hello-account
41+
namespace: hello
42+
roleRef:
43+
kind: ClusterRole
44+
name: hello-role
45+
apiGroup: rbac.authorization.k8s.io
46+
---
47+
kind: Deployment
48+
apiVersion: apps/v1
49+
metadata:
50+
name: hello-populator
51+
namespace: hello
52+
spec:
53+
selector:
54+
matchLabels:
55+
app: hello
56+
template:
57+
metadata:
58+
labels:
59+
app: hello
60+
spec:
61+
serviceAccount: hello-account
62+
containers:
63+
- name: hello
64+
image: hello-populator:latest
65+
imagePullPolicy: IfNotPresent
66+
args:
67+
- --mode=controller
68+
- --image-name=hello-populator:latest
69+
---
70+
kind: VolumePopulator
71+
apiVersion: populator.storage.k8s.io/v1alpha1
72+
metadata:
73+
name: hello-populator
74+
sourceKind:
75+
group: hello.k8s.io
76+
kind: Hello

example/hello-populator/go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/kubernetes-csi/lib-volume-populator/example/hello-populator
2+
3+
go 1.15
4+
5+
require (
6+
github.com/kubernetes-csi/lib-volume-populator v0.0.0
7+
k8s.io/apimachinery v0.19.9
8+
k8s.io/klog/v2 v2.8.0
9+
)
10+
11+
replace github.com/kubernetes-csi/lib-volume-populator => ../..

0 commit comments

Comments
 (0)