diff --git a/README.md b/README.md index 60847a3..c96052c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # ansible-role-k8s-storage +Deploy storage class to kubernetes cluster diff --git a/bin/gen_list.sh b/bin/gen_list.sh new file mode 100755 index 0000000..07e0584 --- /dev/null +++ b/bin/gen_list.sh @@ -0,0 +1,6 @@ +#!/bin/bash +wget https://raw.githubusercontent.com/rancher/local-path-provisioner/master/deploy/local-path-storage.yaml +kubernetes-split-yaml local-path-storage.yaml > generated.log +mv generated local +cat generated.log | while read LIGNE; do if [ $(echo "${LIGNE}" | grep -c ^File) -eq 1 ]; then echo -n "${LIGNE} "; else echo "${LIGNE}"; fi; done | grep ^File | sort -V | sed 's|.*\(generated/\)\(.*\.yaml\)| - "local/\2"|' +sed 's|/opt/local-path-provisioner|/mnt/shared|' -i local/local-path-config-ConfigMap.yaml \ No newline at end of file diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..24eaaa4 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,3 @@ +my_context: flamykube +storage_localpath: true +storage_default_storageclass: local-path \ No newline at end of file diff --git a/files/local-path-storage_20200316.yaml b/files/local-path-storage_20200316.yaml new file mode 100644 index 0000000..d83c368 --- /dev/null +++ b/files/local-path-storage_20200316.yaml @@ -0,0 +1,105 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: local-path-storage +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: local-path-provisioner-service-account + namespace: local-path-storage +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: local-path-provisioner-role +rules: +- apiGroups: [""] + resources: ["nodes", "persistentvolumeclaims"] + verbs: ["get", "list", "watch"] +- apiGroups: [""] + resources: ["endpoints", "persistentvolumes", "pods"] + verbs: ["*"] +- apiGroups: [""] + resources: ["events"] + verbs: ["create", "patch"] +- apiGroups: ["storage.k8s.io"] + resources: ["storageclasses"] + verbs: ["get", "list", "watch"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: local-path-provisioner-bind +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: local-path-provisioner-role +subjects: +- kind: ServiceAccount + name: local-path-provisioner-service-account + namespace: local-path-storage +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: local-path-provisioner + namespace: local-path-storage +spec: + replicas: 1 + selector: + matchLabels: + app: local-path-provisioner + template: + metadata: + labels: + app: local-path-provisioner + spec: + serviceAccountName: local-path-provisioner-service-account + containers: + - name: local-path-provisioner + image: rancher/local-path-provisioner:v0.0.12 + imagePullPolicy: IfNotPresent + command: + - local-path-provisioner + - --debug + - start + - --config + - /etc/config/config.json + volumeMounts: + - name: config-volume + mountPath: /etc/config/ + env: + - name: POD_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + volumes: + - name: config-volume + configMap: + name: local-path-config +--- +apiVersion: storage.k8s.io/v1 +kind: StorageClass +metadata: + name: local-path +provisioner: rancher.io/local-path +volumeBindingMode: WaitForFirstConsumer +reclaimPolicy: Delete +--- +kind: ConfigMap +apiVersion: v1 +metadata: + name: local-path-config + namespace: local-path-storage +data: + config.json: |- + { + "nodePathMap":[ + { + "node":"DEFAULT_PATH_FOR_NON_LISTED_NODES", + "paths":["/opt/local-path-provisioner"] + } + ] + } + diff --git a/files/local/local-path-StorageClass.yaml b/files/local/local-path-StorageClass.yaml new file mode 100644 index 0000000..be90ae1 --- /dev/null +++ b/files/local/local-path-StorageClass.yaml @@ -0,0 +1,7 @@ +apiVersion: storage.k8s.io/v1 +kind: StorageClass +metadata: + name: local-path +provisioner: rancher.io/local-path +volumeBindingMode: WaitForFirstConsumer +reclaimPolicy: Delete diff --git a/files/local/local-path-config-ConfigMap.yaml b/files/local/local-path-config-ConfigMap.yaml new file mode 100644 index 0000000..318bade --- /dev/null +++ b/files/local/local-path-config-ConfigMap.yaml @@ -0,0 +1,16 @@ +kind: ConfigMap +apiVersion: v1 +metadata: + name: local-path-config + namespace: local-path-storage +data: + config.json: |- + { + "nodePathMap":[ + { + "node":"DEFAULT_PATH_FOR_NON_LISTED_NODES", + "paths":["/mnt/shared"] + } + ] + } + diff --git a/files/local/local-path-provisioner-Deployment.yaml b/files/local/local-path-provisioner-Deployment.yaml new file mode 100644 index 0000000..8bf9abd --- /dev/null +++ b/files/local/local-path-provisioner-Deployment.yaml @@ -0,0 +1,38 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: local-path-provisioner + namespace: local-path-storage +spec: + replicas: 1 + selector: + matchLabels: + app: local-path-provisioner + template: + metadata: + labels: + app: local-path-provisioner + spec: + serviceAccountName: local-path-provisioner-service-account + containers: + - name: local-path-provisioner + image: rancher/local-path-provisioner:v0.0.12 + imagePullPolicy: IfNotPresent + command: + - local-path-provisioner + - --debug + - start + - --config + - /etc/config/config.json + volumeMounts: + - name: config-volume + mountPath: /etc/config/ + env: + - name: POD_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + volumes: + - name: config-volume + configMap: + name: local-path-config diff --git a/files/local/local-path-provisioner-bind-ClusterRoleBinding.yaml b/files/local/local-path-provisioner-bind-ClusterRoleBinding.yaml new file mode 100644 index 0000000..cd52d87 --- /dev/null +++ b/files/local/local-path-provisioner-bind-ClusterRoleBinding.yaml @@ -0,0 +1,12 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: local-path-provisioner-bind +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: local-path-provisioner-role +subjects: +- kind: ServiceAccount + name: local-path-provisioner-service-account + namespace: local-path-storage diff --git a/files/local/local-path-provisioner-role-ClusterRole.yaml b/files/local/local-path-provisioner-role-ClusterRole.yaml new file mode 100644 index 0000000..e0d39d1 --- /dev/null +++ b/files/local/local-path-provisioner-role-ClusterRole.yaml @@ -0,0 +1,17 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: local-path-provisioner-role +rules: +- apiGroups: [""] + resources: ["nodes", "persistentvolumeclaims"] + verbs: ["get", "list", "watch"] +- apiGroups: [""] + resources: ["endpoints", "persistentvolumes", "pods"] + verbs: ["*"] +- apiGroups: [""] + resources: ["events"] + verbs: ["create", "patch"] +- apiGroups: ["storage.k8s.io"] + resources: ["storageclasses"] + verbs: ["get", "list", "watch"] diff --git a/files/local/local-path-provisioner-service-account-ServiceAccount.yaml b/files/local/local-path-provisioner-service-account-ServiceAccount.yaml new file mode 100644 index 0000000..61efd57 --- /dev/null +++ b/files/local/local-path-provisioner-service-account-ServiceAccount.yaml @@ -0,0 +1,5 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: local-path-provisioner-service-account + namespace: local-path-storage diff --git a/files/local/local-path-storage-Namespace.yaml b/files/local/local-path-storage-Namespace.yaml new file mode 100644 index 0000000..b1222ff --- /dev/null +++ b/files/local/local-path-storage-Namespace.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: local-path-storage diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..d715382 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,7 @@ +galaxy_info: + author: Adrien Reslinger + description: Install storage class to a cluster + company: Flaminem + min_ansible_version: 2.9 + galaxy_tags: [] + diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..dab772e --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,39 @@ +- name: Local Path setup + block: + - name: namespace + k8s: + context: "{{ my_context }}" + state: present + name: kubernetes-dashboard + api_version: v1 + kind: Namespace + + - name: dashboard install + k8s: + state: present + context: "{{ my_context }}" + resource_definition: "{{ lookup('file', item) | from_yaml }}" + with_items: + - "local/local-path-storage-Namespace.yaml" + - "local/local-path-provisioner-service-account-ServiceAccount.yaml" + - "local/local-path-provisioner-role-ClusterRole.yaml" + - "local/local-path-provisioner-bind-ClusterRoleBinding.yaml" + - "local/local-path-provisioner-Deployment.yaml" + - "local/local-path-StorageClass.yaml" + - "local/local-path-config-ConfigMap.yaml" + when: + - storage_localpath == true + + - name: Select the default StorageClass + k8s: + state: present + context: "{{ my_context }}" + definition: + apiVersion: v1 + kind: StorageClass + metadata: + name: "{{ storage_default_storageclass }}" + annotations: + storageclass.kubernetes.io/is-default-class: "true" + when: + - storage_default_storageclass is defined