Skip to content

Commit 54ec7ad

Browse files
authored
Feat/obj gen (#36)
* add obj gen * adding dependencies * parametrise upcloud CSI manifests
1 parent 6df46b2 commit 54ec7ad

File tree

591 files changed

+119456
-99
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

591 files changed

+119456
-99
lines changed

.DS_Store

8 KB
Binary file not shown.

.idea/.gitignore

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/upcloud-csi.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: debug
5+
namespace: kube-system
6+
spec:
7+
nodeName: cp-node-ffn6j
8+
containers:
9+
- image: quay.io/giantswarm/debug:latest
10+
command:
11+
- sh
12+
args:
13+
- "-c"
14+
- "sleep 100000000s"
15+
imagePullPolicy: IfNotPresent
16+
name: debug
17+
hostAliases:
18+
- hostnames:
19+
- kubernetes
20+
ip: 127.0.0.1
21+
hostNetwork: false
22+
tolerations:
23+
- effect: NoSchedule
24+
operator: Exists
25+
- effect: NoExecute
26+
operator: Exists
27+
- effect: PreferNoSchedule
28+
operator: Exists

deploy/kubernetes/alpha/tests/logs.txt

Lines changed: 182 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
apiVersion: v1
2+
kind: PersistentVolumeClaim
3+
metadata:
4+
name: csi-pvc
5+
spec:
6+
accessModes:
7+
- ReadWriteOnce
8+
resources:
9+
requests:
10+
storage: 10Gi
11+
storageClassName: upcloud-block-storage
12+
---
13+
kind: Pod
14+
apiVersion: v1
15+
metadata:
16+
name: csi-app
17+
labels:
18+
app: csi-app
19+
spec:
20+
securityContext:
21+
runAsUser: 1000
22+
runAsGroup: 2000
23+
fsGroup: 2000
24+
containers:
25+
- name: upcloud-test-pod
26+
image: busybox
27+
volumeMounts:
28+
- mountPath: "/data"
29+
name: upcloud-volume
30+
- name: sec-ctx-vol
31+
mountPath: "/empty-dir"
32+
command: [ "sleep", "100000000s" ]
33+
volumes:
34+
- name: upcloud-volume
35+
persistentVolumeClaim:
36+
claimName: csi-pvc
37+
- name: sec-ctx-vol
38+
emptyDir: {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#kubectl create secret docker-registry quay.io --docker-server=quay.io --docker-username=prudnikov --docker-password='*iXBaY3n6bXVKFr' -n kube-system
2+
kind: Secret
3+
apiVersion: v1
4+
type: Opaque
5+
metadata:
6+
name: upcloud
7+
namespace: kube-system
8+
data:
9+
username : aXBydWRuaWtvdg==
10+
password: I2k2UlRFaWczWkNOUFpS

deploy/kubernetes/embed.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package kubernetes
2+
3+
import _ "embed"
4+
5+
//go:embed crd-upcloud-csi.yaml
6+
var CRDTemplate string
7+
8+
//go:embed rbac-upcloud-csi.yaml
9+
var RbacTemplate string
10+
11+
//go:embed setup-upcloud-csi.yaml
12+
var CSITemplate string
13+
14+
//go:embed snapshot-webhook-upcloud-csi.yaml
15+
var SnapshotTemplate string

deploy/kubernetes/setup-upcloud-csi.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ reclaimPolicy: Retain
4545
#######################
4646
# Controller plugin
4747
#######################
48+
---
49+
apiVersion: v1
50+
kind: Secret
51+
metadata:
52+
name: upcloud
53+
namespace: kube-system
54+
data:
55+
username: ${UPCLOUD_CSI_USERNAME_B64}
56+
password: ${UPCLOUD_CSI_PASSWORD_B64}
57+
---
4858
kind: StatefulSet
4959
apiVersion: apps/v1
5060
metadata:
@@ -120,7 +130,7 @@ spec:
120130
- name: socket-dir
121131
mountPath: /var/lib/csi/sockets/pluginproxy/
122132
- name: csi-upcloud-plugin
123-
image: ghcr.io/upcloudltd/upcloud-csi:main
133+
image: ghcr.io/upcloudltd/upcloud-csi:${UPCLOUD_CSI_VERSION}
124134
args:
125135
- "--endpoint=$(CSI_ENDPOINT)"
126136
- "--nodehost=$(NODE_ID)"
@@ -242,7 +252,7 @@ spec:
242252
- name: registration-dir
243253
mountPath: /registration/
244254
- name: csi-upcloud-plugin
245-
image: ghcr.io/upcloudltd/upcloud-csi:main
255+
image: ghcr.io/upcloudltd/upcloud-csi:${UPCLOUD_CSI_VERSION}
246256
args:
247257
- "--endpoint=$(CSI_ENDPOINT)"
248258
- "--nodehost=$(NODE_ID)"

driver/objgen/objgen.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package objgen
2+
3+
import (
4+
"fmt"
5+
. "github.com/UpCloudLtd/upcloud-csi/deploy/kubernetes"
6+
7+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
8+
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/yamlprocessor"
9+
)
10+
11+
type Output struct {
12+
RawYaml []byte
13+
UnstructuredObjects []*unstructured.Unstructured
14+
//Not yet ready
15+
//DaemonSet []*appsv1.DaemonSet
16+
//ClusterRoleBinding []*rbacv1.ClusterRoleBinding
17+
//ClusterRole []*rbacv1.ClusterRole
18+
//ServiceAccount []*v1.ServiceAccount
19+
//ConfigMap []*v1.ConfigMap
20+
//Secret []*v1.Secret
21+
}
22+
23+
func Get(vars map[string]string) (*Output, error) {
24+
var (
25+
rawYaml []byte
26+
err error
27+
)
28+
29+
// First substitute variables to values
30+
processor := yamlprocessor.NewSimpleProcessor()
31+
32+
variableValueGetter := func(key string) (string, error) {
33+
val, exist := vars[key]
34+
35+
if !exist {
36+
return "", fmt.Errorf("variable %s is not defined in input variables", key)
37+
}
38+
return val, nil
39+
}
40+
41+
processTemplate := func(template string) ([]byte, error) {
42+
crdVars, err := processor.GetVariableMap([]byte(template))
43+
if err != nil {
44+
return nil, fmt.Errorf("can't parse variable list from template: %w", err)
45+
}
46+
47+
for key, _ := range crdVars {
48+
if _, exist := vars[key]; !exist {
49+
return nil, fmt.Errorf("variable %s is not defined in input variables", key)
50+
}
51+
}
52+
53+
processedTemplate, err := processor.Process([]byte(template), variableValueGetter)
54+
if err != nil {
55+
return nil, fmt.Errorf("can't substitute variables in cluster template: %w", err)
56+
}
57+
58+
processedTemplate = append(processedTemplate, []byte("---")...)
59+
60+
return processedTemplate, nil
61+
62+
}
63+
64+
output := &Output{}
65+
rawYaml, err = processTemplate(CRDTemplate)
66+
if err != nil {
67+
return nil, fmt.Errorf("can't parse CRDTemplate: %w", err)
68+
}
69+
70+
output.RawYaml = append(output.RawYaml, rawYaml...)
71+
72+
rawYaml, err = processTemplate(RbacTemplate)
73+
if err != nil {
74+
return nil, fmt.Errorf("can't parse RbacTemplate: %w", err)
75+
}
76+
77+
output.RawYaml = append(output.RawYaml, rawYaml...)
78+
79+
rawYaml, err = processTemplate(CSITemplate)
80+
if err != nil {
81+
return nil, fmt.Errorf("can't parse CSITemplate: %w", err)
82+
}
83+
84+
output.RawYaml = append(output.RawYaml, rawYaml...)
85+
86+
//for {
87+
// u := &unstructured.Unstructured{}
88+
// _, gvk, err := decoder.Decode(nil, u)
89+
// if err == io.EOF {
90+
// break
91+
// }
92+
// if runtime.IsNotRegisteredError(err) {
93+
// continue
94+
// }
95+
// if err != nil {
96+
// return nil, err
97+
// }
98+
//
99+
// switch gvk.Kind {
100+
//
101+
// case "DaemonSet":
102+
// obj := &appsv1.DaemonSet{}
103+
// if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, obj); err != nil {
104+
// return nil, fmt.Errorf("cannot convert object to %s: %w", gvk.Kind, err)
105+
// }
106+
// output.DaemonSet = append(output.DaemonSet, obj)
107+
//
108+
// case "ClusterRoleBinding":
109+
// obj := &rbacv1.ClusterRoleBinding{}
110+
// if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, obj); err != nil {
111+
// return nil, fmt.Errorf("cannot convert object to %s: %w", gvk.Kind, err)
112+
// }
113+
// output.ClusterRoleBinding = append(output.ClusterRoleBinding, obj)
114+
//
115+
// case "ClusterRole":
116+
// obj := &rbacv1.ClusterRole{}
117+
// if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, obj); err != nil {
118+
// return nil, fmt.Errorf("cannot convert object to %s: %w", gvk.Kind, err)
119+
// }
120+
// output.ClusterRole = append(output.ClusterRole, obj)
121+
//
122+
// case "ServiceAccount":
123+
// obj := &v1.ServiceAccount{}
124+
// if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, obj); err != nil {
125+
// return nil, fmt.Errorf("cannot convert object to %s: %w", gvk.Kind, err)
126+
// }
127+
// output.ServiceAccount = append(output.ServiceAccount, obj)
128+
//
129+
// case "ConfigMap":
130+
// obj := &v1.ConfigMap{}
131+
// if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, obj); err != nil {
132+
// return nil, fmt.Errorf("cannot convert object to %s: %w", gvk.Kind, err)
133+
// }
134+
// output.ConfigMap = append(output.ConfigMap, obj)
135+
//
136+
// case "Secret":
137+
// obj := &v1.Secret{}
138+
// if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, obj); err != nil {
139+
// return nil, fmt.Errorf("cannot convert object to %s: %w", gvk.Kind, err)
140+
// }
141+
// output.Secret = append(output.Secret, obj)
142+
//
143+
// default:
144+
// output.UnstructuredObjects = append(output.UnstructuredObjects, u)
145+
// }
146+
//}
147+
148+
return output, nil
149+
}

driver/objgen/objgen_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package objgen
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func Test_Instantination(t *testing.T) {
10+
11+
inputVars := map[string]string{
12+
13+
"UPCLOUD_CSI_USERNAME_B64": "dXNlcg==",
14+
"UPCLOUD_CSI_PASSWORD_B64": "cGFzcw==",
15+
16+
"UPCLOUD_CSI_VERSION": "v0.3.1",
17+
}
18+
19+
res, err := Get(inputVars)
20+
require.Nil(t, err)
21+
require.Zero(t, len(res.UnstructuredObjects))
22+
}

0 commit comments

Comments
 (0)