1212import argparse
1313import logging
1414import sys
15- import os
16- import subprocess
1715from time import sleep
1816
1917import requests
2018
21- from deploy_stack import (
22- BootstrapManager ,
23- deploy_caas_stack ,
24- )
19+ from deploy_stack import BootstrapManager
2520from utility import (
2621 add_basic_testing_arguments ,
2722 configure_logging ,
2823 JujuAssertionError ,
2924)
3025
31- from jujucharm import (
32- local_charm_path
33- )
3426from jujupy .utility import until_timeout
27+ from jujupy .k8s_provider import (
28+ MicroK8s ,
29+ # KubernetesCore,
30+ )
3531
3632__metaclass__ = type
3733
3834
3935log = logging .getLogger ("assess_caas_charm_deployment" )
4036
41- JUJU_STORAGECLASS_NAME = "juju-storageclass"
42- HOST_PATH_PROVISIONER = """
43- apiVersion: v1
44- kind: ServiceAccount
45- metadata:
46- name: hostpath-provisioner
47- namespace: kube-system
48- ---
49-
50- apiVersion: rbac.authorization.k8s.io/v1beta1
51- kind: ClusterRole
52- metadata:
53- name: hostpath-provisioner
54- namespace: kube-system
55- rules:
56- - apiGroups: [""]
57- resources: ["persistentvolumes"]
58- verbs: ["get", "list", "watch", "create", "delete"]
59- - apiGroups: [""]
60- resources: ["persistentvolumeclaims"]
61- verbs: ["get", "list", "watch", "update"]
62- - apiGroups: ["storage.k8s.io"]
63- resources: ["storageclasses"]
64- verbs: ["get", "list", "watch"]
65- - apiGroups: [""]
66- resources: ["events"]
67- verbs: ["list", "watch", "create", "update", "patch"]
68- ---
69-
70- apiVersion: rbac.authorization.k8s.io/v1beta1
71- kind: ClusterRoleBinding
72- metadata:
73- name: hostpath-provisioner
74- namespace: kube-system
75- subjects:
76- - kind: ServiceAccount
77- name: hostpath-provisioner
78- namespace: kube-system
79- roleRef:
80- kind: ClusterRole
81- name: hostpath-provisioner
82- apiGroup: rbac.authorization.k8s.io
83- ---
84-
85- apiVersion: rbac.authorization.k8s.io/v1beta1
86- kind: Role
87- metadata:
88- name: hostpath-provisioner
89- namespace: kube-system
90- rules:
91- - apiGroups: [""]
92- resources: ["secrets"]
93- verbs: ["create", "get", "delete"]
94- ---
95-
96- apiVersion: rbac.authorization.k8s.io/v1beta1
97- kind: RoleBinding
98- metadata:
99- name: hostpath-provisioner
100- namespace: kube-system
101- roleRef:
102- apiGroup: rbac.authorization.k8s.io
103- kind: Role
104- name: hostpath-provisioner
105- subjects:
106- - kind: ServiceAccount
107- name: hostpath-provisioner
108- ---
109-
110- # -- Create a daemon set for web requests and send them to the nginx-ingress-controller
111- apiVersion: extensions/v1beta1
112- kind: DaemonSet
113- metadata:
114- name: hostpath-provisioner
115- namespace: kube-system
116- spec:
117- revisionHistoryLimit: 3
118- template:
119- metadata:
120- labels:
121- app: hostpath-provisioner
122- spec:
123- serviceAccountName: hostpath-provisioner
124- terminationGracePeriodSeconds: 0
125- containers:
126- - name: hostpath-provisioner
127- image: mazdermind/hostpath-provisioner:latest
128- imagePullPolicy: "IfNotPresent"
129- env:
130- - name: NODE_NAME
131- valueFrom:
132- fieldRef:
133- fieldPath: spec.nodeName
134- - name: PV_DIR
135- value: /mnt/kubernetes
136- volumeMounts:
137- - name: pv-volume
138- mountPath: /mnt/kubernetes
139- volumes:
140- - name: pv-volume
141- hostPath:
142- path: /mnt/kubernetes
143- ---
144-
145- # -- Create the standard storage class for running on-node hostpath storage
146- apiVersion: storage.k8s.io/v1
147- kind: StorageClass
148- metadata:
149- # namespace: kube-system
150- name: {class_name}
151- annotations:
152- storageclass.beta.kubernetes.io/is-default-class: "true"
153- labels:
154- kubernetes.io/cluster-service: "true"
155- addonmanager.kubernetes.io/mode: EnsureExists
156- provisioner: hostpath
157- ---
158- """
159-
16037
16138def check_app_healthy (url , timeout = 300 , success_hook = lambda : None , fail_hook = lambda : None ):
16239 if not callable (success_hook ) or not callable (fail_hook ):
@@ -180,57 +57,28 @@ def check_app_healthy(url, timeout=300, success_hook=lambda: None, fail_hook=lam
18057 raise JujuAssertionError ('gitlab is not healthy' )
18158
18259
183- def assess_caas_charm_deployment (client ):
184- # Deploy k8s bundle to spin up k8s cluster
185- bundle = local_charm_path (
186- charm = 'bundles-kubernetes-core-lxd.yaml' ,
187- repository = os .environ ['JUJU_REPOSITORY' ],
188- juju_ver = client .version
189- )
190-
191- caas_client = deploy_caas_stack (path = bundle , client = client , timeout = 4000 )
60+ def assess_caas_charm_deployment (caas_client ):
19261 external_hostname = caas_client .get_external_hostname ()
19362
194- if not caas_client .is_cluster_healthy :
63+ if not caas_client .check_cluster_healthy ( timeout = 60 ) :
19564 raise JujuAssertionError ('k8s cluster is not healthy because kubectl is not accessible' )
19665
197- # tmp fix kubernetes core ingress issue
198- ingress_controller_daemonset_name = 'daemonset.apps/nginx-ingress-kubernetes-worker-controller'
199- o = caas_client .kubectl (
200- 'patch' , ingress_controller_daemonset_name , '--patch' ,
201- '''
202- {"spec": {"template": {"spec": {"containers": [{"name": "nginx-ingress-kubernetes-worker","args": ["/nginx-ingress-controller", "--default-backend-service=$(POD_NAMESPACE)/default-http-backend", "--configmap=$(POD_NAMESPACE)/nginx-load-balancer-conf", "--enable-ssl-chain-completion=False", "--publish-status-address=%s"]}]}}}}
203- ''' % caas_client .get_first_worker_ip ()
204- )
205- log .info (o )
206-
207- o = caas_client .kubectl ('get' , ingress_controller_daemonset_name , '-o' , 'yaml' )
208- log .info (o )
209-
21066 # add caas model for deploying caas charms on top of it
21167 model_name = 'testcaas'
21268 k8s_model = caas_client .add_model (model_name )
21369
214- # ensure tmp dir for storage class.model_name
215- o = subprocess .check_output (
216- ('sudo' , 'mkdir' , '-p' , '/mnt/kubernetes/%s' % model_name ) # unfortunately, needs sudo
217- )
218- log .debug (o .decode ('UTF-8' ).strip ())
219-
220- # ensure storage class
221- caas_client .kubectl_apply (HOST_PATH_PROVISIONER .format (class_name = JUJU_STORAGECLASS_NAME ))
222-
223- # ensure storage pools for caas operator
70+ sc_name = caas_client .default_storage_class_name
71+ # ensure storage pools for caas operator using default sc.
22472 k8s_model .juju (
22573 'create-storage-pool' ,
226- ('operator-storage' , 'kubernetes' , 'storage-class=%s' % JUJU_STORAGECLASS_NAME )
74+ ('operator-storage' , 'kubernetes' , 'storage-class=%s' % sc_name )
22775 )
22876
22977 # ensure storage pools for mariadb
23078 mariadb_storage_pool_name = 'mariadb-pv'
23179 k8s_model .juju (
23280 'create-storage-pool' ,
233- (mariadb_storage_pool_name , 'kubernetes' , 'storage-class=%s' % JUJU_STORAGECLASS_NAME )
81+ (mariadb_storage_pool_name , 'kubernetes' , 'storage-class=%s' % sc_name )
23482 )
23583
23684 k8s_model .deploy (
@@ -240,7 +88,7 @@ def assess_caas_charm_deployment(client):
24088
24189 k8s_model .deploy (
24290 charm = "cs:~juju/mariadb-k8s-0" ,
243- storage = 'database=100M,{pool_name }' .format (pool_name = mariadb_storage_pool_name ),
91+ storage = 'database=100M,{}' .format (mariadb_storage_pool_name ),
24492 )
24593
24694 k8s_model .juju ('relate' , ('mariadb-k8s' , 'gitlab-k8s' ))
@@ -252,7 +100,6 @@ def success_hook():
252100
253101 def fail_hook ():
254102 success_hook ()
255- log .info (caas_client .kubectl ('get' , ingress_controller_daemonset_name , '-o' , 'yaml' ))
256103 log .info (caas_client .kubectl ('get' , 'pv,pvc' , '-n' , model_name ))
257104
258105 url = '{}://{}/{}' .format ('http' , external_hostname , 'gitlab-k8s' )
@@ -289,7 +136,13 @@ def main(argv=None):
289136 with bs_manager .booted_context (args .upload_tools ):
290137 client = bs_manager .client
291138 ensure_operator_image_path (client , image_path = args .caas_image )
292- assess_caas_charm_deployment (client )
139+ for k8s_provider in (
140+ # all k8s client we need support.
141+ MicroK8s ,
142+ # KubernetesCore # disable for now.
143+ ):
144+ caas_client = k8s_provider (client )
145+ assess_caas_charm_deployment (caas_client )
293146 return 0
294147
295148
0 commit comments