|
| 1 | +- name: Set up Kubernetes local cluster |
| 2 | + hosts: all |
| 3 | + roles: |
| 4 | + - install-k8s-jobs-dependences |
| 5 | + become: yes |
| 6 | + tasks: |
| 7 | + - name: Set up Kubernetes local cluster |
| 8 | + shell: |
| 9 | + cmd: | |
| 10 | + set -e |
| 11 | + apt-get install python-pip -y |
| 12 | + pip install -U python-openstackclient |
| 13 | +
|
| 14 | + export OS_DOMAIN_NAME=$(echo '{{ vexxhost_credentials.user_domain_name }}') |
| 15 | + export OS_AUTH_TYPE=$(echo '{{ vexxhost_credentials.auth_type }}') |
| 16 | + export OS_IDENTITY_API_VERSION=$(echo '{{ vexxhost_credentials.identity_api_version }}') |
| 17 | + export OS_VOLUME_API_VERSION=$(echo '{{ vexxhost_credentials.volume_api_version }}') |
| 18 | + export OS_INTERFACE=$(echo '{{ vexxhost_credentials.interface }}') |
| 19 | + export OS_AUTH_URL=$(echo '{{ vexxhost_credentials.auth_url }}') |
| 20 | + export OS_PROJECT_ID=$(echo '{{ vexxhost_credentials.project_id }}') |
| 21 | + export OS_PROJECT_NAME=$(echo '{{ vexxhost_credentials.project_name }}') |
| 22 | + export OS_USER_DOMAIN_NAME=$(echo '{{ vexxhost_credentials.user_domain_name }}') |
| 23 | + export OS_PROJECT_DOMAIN_ID=$(echo '{{ vexxhost_credentials.project_domain_id }}') |
| 24 | + export OS_USERNAME=$(echo '{{ vexxhost_credentials.username }}') |
| 25 | + export OS_PASSWORD=$(echo '{{ vexxhost_credentials.password }}') |
| 26 | + export OS_REGION_NAME=$(echo '{{ vexxhost_credentials.region_name }}') |
| 27 | +
|
| 28 | + if [[ ! -d "/etc/kubernetes/" ]]; then |
| 29 | + sudo mkdir -p /etc/kubernetes/ |
| 30 | + fi |
| 31 | + chown zuul /etc/kubernetes/ |
| 32 | + cat << EOF >> /etc/kubernetes/cloud-config |
| 33 | + [Global] |
| 34 | + domain-name = ${OS_PROJECT_DOMAIN_NAME-$OS_PROJECT_DOMAIN_ID} |
| 35 | + tenant-id = $OS_PROJECT_ID |
| 36 | + auth-url = $OS_AUTH_URL |
| 37 | + password = $OS_PASSWORD |
| 38 | + username = $OS_USERNAME |
| 39 | + region = $OS_REGION_NAME |
| 40 | + [BlockStorage] |
| 41 | + bs-version = v2 |
| 42 | + EOF |
| 43 | +
|
| 44 | + cat << EOF >> /etc/kubernetes/webhook.kubeconfig |
| 45 | + apiVersion: v1 |
| 46 | + clusters: |
| 47 | + - cluster: |
| 48 | + insecure-skip-tls-verify: true |
| 49 | + server: https://localhost:8443/webhook |
| 50 | + name: webhook |
| 51 | + contexts: |
| 52 | + - context: |
| 53 | + cluster: webhook |
| 54 | + user: webhook |
| 55 | + name: webhook |
| 56 | + current-context: webhook |
| 57 | + kind: Config |
| 58 | + preferences: {} |
| 59 | + users: |
| 60 | + - name: webhook |
| 61 | + EOF |
| 62 | +
|
| 63 | + set -x |
| 64 | + make depend |
| 65 | + make build |
| 66 | + mkdir -p "{{ ansible_user_dir }}/.kube" |
| 67 | + export API_HOST_IP="172.17.0.1" |
| 68 | + export KUBELET_HOST="0.0.0.0" |
| 69 | +
|
| 70 | + echo "Stopping firewall and allow all traffic..." |
| 71 | + iptables -F |
| 72 | + iptables -X |
| 73 | + iptables -t nat -F |
| 74 | + iptables -t nat -X |
| 75 | + iptables -t mangle -F |
| 76 | + iptables -t mangle -X |
| 77 | + iptables -P INPUT ACCEPT |
| 78 | + iptables -P FORWARD ACCEPT |
| 79 | + iptables -P OUTPUT ACCEPT |
| 80 | + export ALLOW_SECURITY_CONTEXT=true |
| 81 | + export ENABLE_CRI=false |
| 82 | + export ENABLE_HOSTPATH_PROVISIONER=true |
| 83 | + export ENABLE_SINGLE_CA_SIGNER=true |
| 84 | + # export KUBE_ENABLE_CLUSTER_DASHBOARD=true |
| 85 | + export KUBE_ENABLE_CLUSTER_DNS=false |
| 86 | + export LOG_LEVEL=10 |
| 87 | + # we want to use the openstack cloud provider |
| 88 | + export CLOUD_PROVIDER=openstack |
| 89 | + # we want to run a separate cloud-controller-manager for openstack |
| 90 | + export EXTERNAL_CLOUD_PROVIDER=true |
| 91 | + # DO NOT change the location of the cloud-config file. It is important for the old cinder provider to work |
| 92 | + export CLOUD_CONFIG=/etc/kubernetes/cloud-config |
| 93 | + # specify the OCCM binary |
| 94 | + export EXTERNAL_CLOUD_PROVIDER_BINARY="{{ ansible_user_dir }}/{{ zuul.project.src_dir }}/openstack-cloud-controller-manager" |
| 95 | + # Cleanup some directories just in case |
| 96 | + sudo rm -rf /var/lib/kubelet/* |
| 97 | +
|
| 98 | + # location of where the kubernetes processes log their output |
| 99 | + mkdir -p /opt/stack/logs/ |
| 100 | + export LOG_DIR=/opt/stack/logs |
| 101 | + # We need this for one of the conformance tests |
| 102 | + export ALLOW_PRIVILEGED=true |
| 103 | + # Just kick off all the processes and drop down to the command line |
| 104 | + export ENABLE_DAEMON=true |
| 105 | + # We need the hostname to match the name of the vm started by openstack |
| 106 | + export HOSTNAME_OVERRIDE=$(curl http://169.254.169.254/openstack/latest/meta_data.json | python -c "import sys, json; print json.load(sys.stdin)['name']") |
| 107 | + cp ./examples/webhook/policy.json /etc/kubernetes/ |
| 108 | +
|
| 109 | + pushd ${GOPATH}/src/k8s.io/kubernetes |
| 110 | + export AUTHORIZATION_MODE="Webhook,Node" |
| 111 | +
|
| 112 | + # TODO: Following is workaround for supporting keystone webhook in local-up-cluster.sh tool, it should be landed in the official kubernetes repo |
| 113 | + sed 's/curl --max-time 1/curl --max-time 5/g' -i ./hack/lib/util.sh |
| 114 | + sed '583,587 d' -i ./hack/local-up-cluster.sh |
| 115 | + sed '555 a \ --authentication-token-webhook-config-file=/etc/kubernetes/webhook.kubeconfig \\' -i ./hack/local-up-cluster.sh |
| 116 | + sed '555 a \ --authorization-webhook-config-file=/etc/kubernetes/webhook.kubeconfig \\' -i ./hack/local-up-cluster.sh |
| 117 | +
|
| 118 | + # -E preserves the current env vars, but we need to special case PATH |
| 119 | + sudo -E PATH=$PATH SHELLOPTS=$SHELLOPTS ./hack/local-up-cluster.sh -O |
| 120 | +
|
| 121 | + nohup "{{ ansible_user_dir }}/{{ zuul.project.src_dir }}/k8s-keystone-auth" \ |
| 122 | + --tls-cert-file /var/run/kubernetes/serving-kube-apiserver.crt \ |
| 123 | + --tls-private-key-file /var/run/kubernetes/serving-kube-apiserver.key \ |
| 124 | + --keystone-policy-file /etc/kubernetes/policy.json \ |
| 125 | + --log-dir=${LOG_DIR} \ |
| 126 | + --v=10 \ |
| 127 | + --keystone-url ${OS_AUTH_URL} >"${LOG_DIR}/keystone-auth.log" 2>&1 & |
| 128 | +
|
| 129 | + # sudo of local-up-cluster mucks with permissions |
| 130 | + sudo chmod -R 777 "{{ ansible_user_dir }}/.kube" |
| 131 | + sudo chmod 777 /var/run/kubernetes/client-admin.key |
| 132 | +
|
| 133 | + # set up the config we need for kubectl to work |
| 134 | + cluster/kubectl.sh config set-cluster local --server=https://localhost:6443 --certificate-authority=/var/run/kubernetes/server-ca.crt |
| 135 | + cluster/kubectl.sh config set-credentials myself --client-key=/var/run/kubernetes/client-admin.key --client-certificate=/var/run/kubernetes/client-admin.crt |
| 136 | + cluster/kubectl.sh config set-context local --cluster=local --user=myself |
| 137 | + cluster/kubectl.sh config use-context local |
| 138 | +
|
| 139 | + # Hack for RBAC for all for the new cloud-controller process, we need to do better than this |
| 140 | + cluster/kubectl.sh create clusterrolebinding --user system:serviceaccount:kube-system:default kube-system-cluster-admin-1 --clusterrole cluster-admin |
| 141 | + cluster/kubectl.sh create clusterrolebinding --user system:serviceaccount:kube-system:pvl-controller kube-system-cluster-admin-2 --clusterrole cluster-admin |
| 142 | + cluster/kubectl.sh create clusterrolebinding --user system:serviceaccount:kube-system:cloud-node-controller kube-system-cluster-admin-3 --clusterrole cluster-admin |
| 143 | + cluster/kubectl.sh create clusterrolebinding --user system:serviceaccount:kube-system:cloud-controller-manager kube-system-cluster-admin-4 --clusterrole cluster-admin |
| 144 | + cluster/kubectl.sh create clusterrolebinding --user system:serviceaccount:kube-system:shared-informers kube-system-cluster-admin-5 --clusterrole cluster-admin |
| 145 | + cluster/kubectl.sh create clusterrolebinding --user system:kube-controller-manager kube-system-cluster-admin-6 --clusterrole cluster-admin |
| 146 | +
|
| 147 | + { |
| 148 | + TOKEN=$(openstack token issue -f value -c id) |
| 149 | + authenticated_info=`cat << EOF | curl -kvs -XPOST -d @- https://localhost:8443/webhook | python -c "import sys, json; print json.load(sys.stdin)" |
| 150 | + { |
| 151 | + "apiVersion": "authentication.k8s.io/v1beta1", |
| 152 | + "kind": "TokenReview", |
| 153 | + "metadata": { |
| 154 | + "creationTimestamp": null |
| 155 | + }, |
| 156 | + "spec": { |
| 157 | + "token": "$TOKEN" |
| 158 | + } |
| 159 | + } |
| 160 | + EOF` |
| 161 | + base_body=`cat << EOF | python -c "import sys, json; print json.load(sys.stdin)" |
| 162 | + { |
| 163 | + "apiVersion": "authorization.k8s.io/v1beta1", |
| 164 | + "kind": "SubjectAccessReview", |
| 165 | + "spec": { |
| 166 | + "resourceAttributes": { |
| 167 | + "namespace": "default", |
| 168 | + "verb": "get", |
| 169 | + "group": "", |
| 170 | + "resource": "pods" |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + EOF` |
| 175 | + authorization_body=$(python -c "import json; s1=${authenticated_info}; s2=${base_body}; \ |
| 176 | + s2['spec']['user']=s1['status']['user']['username']; \ |
| 177 | + s2['spec']['group']=s1['status']['user']['groups']; \ |
| 178 | + s2['spec']['extra']=s1['status']['user']['extra'];print json.dumps(s2)") |
| 179 | + allowed=$(echo $authorization_body | curl -kvs -XPOST -d @- https://localhost:8443/webhook | python -mjson.tool) |
| 180 | + } 1> /dev/null 2>&1 |
| 181 | + echo ${allowed} |
| 182 | + [[ "${allowed}" =~ '"allowed": true' ]] && echo "Testing k8s-keystone-auth sucessfully!" |
| 183 | +
|
| 184 | + cluster/kubectl.sh config set-credentials openstackuser --auth-provider=openstack |
| 185 | + cluster/kubectl.sh config set-context --cluster=local --user=openstackuser openstackuser@local |
| 186 | + cluster/kubectl.sh config use-context openstackuser@local |
| 187 | + if ! cluster/kubectl.sh get pods; then |
| 188 | + echo "Testing kubernetes+keystone authentication and authorizatio failed!" |
| 189 | + exit 1 |
| 190 | + fi |
| 191 | + popd |
| 192 | + executable: /bin/bash |
| 193 | + chdir: '{{ zuul.project.src_dir }}' |
| 194 | + environment: '{{ golang_env }}' |
0 commit comments