k8s 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/bin/sh
  2. CA_NAME="ca"
  3. CA_NAMESPACE="step"
  4. DEMO_NAMESPACE="step-demo"
  5. DEMO_ENVIRONMENT="staging"
  6. # The name of an image pull secret (e.g., for private docker hub images)
  7. # Set to "none" for no pull secret.
  8. IMAGE_PULL_SECRET="none"
  9. while getopts "c:d:n:e:h:t:p:i:" opt; do
  10. case "$opt" in
  11. h)
  12. show_help
  13. exit 0
  14. ;;
  15. c)
  16. CA_NAMESPACE=$OPTARG
  17. ;;
  18. n)
  19. CA_NAME=$OPTARG
  20. ;;
  21. e)
  22. DEMO_ENVIRONMENT=$OPTARG
  23. ;;
  24. d)
  25. DEMO_NAMESPACE=$OPTARG
  26. ;;
  27. t)
  28. INSTALL_TYPE=$OPTARG
  29. ;;
  30. p)
  31. PROVISIONER_TYPE=$OPTARG
  32. ;;
  33. i)
  34. IMAGE_PULL_SECRET=$OPTARG
  35. ;;
  36. esac
  37. done
  38. shift $((OPTIND-1))
  39. # Various container images used throughout the script.
  40. STEP_CA_IMAGE="localhost:5000/smallstep/step-ca:latest"
  41. DIR=`pwd`
  42. PKI="$(dirname "$DIR")/pki"
  43. SECRETS="$PKI/secrets"
  44. ##
  45. # Certificate Authority installation (prints yaml to stdout).
  46. ##
  47. install_ca()
  48. {
  49. read -p "CA Private Key Password: " -s password
  50. (>&2 echo "")
  51. tmp=$(mktemp -d /tmp/step.XXXXXX)
  52. (
  53. cd "$tmp"
  54. # Bundle up certificates and private key into ConfigMap
  55. mkdir $tmp/certificates
  56. cp $SECRETS/root_ca.crt $tmp/certificates
  57. cp $SECRETS/intermediate_ca.crt $tmp/certificates/
  58. cp $SECRETS/intermediate_ca_key $tmp/certificates/
  59. # ConfigMap for CA configuration
  60. mkdir $tmp/config
  61. cp $DIR/ca.json $tmp/config/ca.json
  62. # Create the namespace
  63. echo "---" > $tmp/step-ca.yml
  64. echo "apiVersion: v1" >> $tmp/step-ca.yml
  65. echo "kind: Namespace" >> $tmp/step-ca.yml
  66. echo "metadata:" >> $tmp/step-ca.yml
  67. echo " name: $CA_NAMESPACE" >> $tmp/step-ca.yml
  68. echo "---" >> $tmp/step-ca.yml
  69. # Create certificates configmap
  70. echo "apiVersion: v1" >> $tmp/step-ca.yml
  71. echo "kind: ConfigMap" >> $tmp/step-ca.yml
  72. kubectl create configmap ca-certificates -n $CA_NAMESPACE --from-file `pwd`/certificates --dry-run -o yaml >> $tmp/step-ca.yml
  73. echo "" >> $tmp/step-ca.yml
  74. echo "---" >> $tmp/step-ca.yml
  75. # Create a CA configuration configmap
  76. echo "" >> $tmp/step-ca.yml
  77. echo "apiVersion: v1" >> $tmp/step-ca.yml
  78. echo "kind: ConfigMap" >> $tmp/step-ca.yml
  79. kubectl create configmap ca-config -n $CA_NAMESPACE --from-file `pwd`/config --dry-run -o yaml >> $tmp/step-ca.yml
  80. echo "" >> $tmp/step-ca.yml
  81. echo "---" >> $tmp/step-ca.yml
  82. # Create a secret with the CA password in it
  83. echo "" >> $tmp/step-ca.yml
  84. echo "apiVersion: v1" >> $tmp/step-ca.yml
  85. echo "kind: Secret" >> $tmp/step-ca.yml
  86. kubectl create secret generic ca-certificate-password -n $CA_NAMESPACE --from-literal=password="$password" --dry-run -o yaml >> $tmp/step-ca.yml
  87. )
  88. echo "" >> $tmp/step-ca.yml
  89. echo "---" >> $tmp/step-ca.yml
  90. echo "" >> $tmp/step-ca.yml
  91. cat <<EOF >> $tmp/step-ca.yml
  92. apiVersion: v1
  93. kind: Service
  94. metadata:
  95. labels:
  96. service: $CA_NAME
  97. name: $CA_NAME
  98. namespace: $CA_NAMESPACE
  99. spec:
  100. type: ClusterIP
  101. ports:
  102. - name: headless
  103. port: 443
  104. targetPort: 9000
  105. selector:
  106. service: $CA_NAME
  107. ---
  108. apiVersion: policy/v1beta1
  109. kind: PodDisruptionBudget
  110. metadata:
  111. name: $CA_NAME
  112. spec:
  113. minAvailable: 1
  114. selector:
  115. matchLabels:
  116. service: $CA_NAME
  117. ---
  118. apiVersion: extensions/v1beta1
  119. kind: Deployment
  120. metadata:
  121. name: $CA_NAME
  122. namespace: $CA_NAMESPACE
  123. spec:
  124. replicas: 1
  125. strategy:
  126. rollingUpdate:
  127. maxSurge: 25%
  128. maxUnavailable: 25%
  129. type: RollingUpdate
  130. template:
  131. metadata:
  132. creationTimestamp: null
  133. labels:
  134. service: $CA_NAME
  135. spec:
  136. containers:
  137. - name: $CA_NAME
  138. image: $STEP_CA_IMAGE
  139. resources:
  140. requests:
  141. cpu: 100m
  142. memory: 20Mi
  143. readinessProbe:
  144. httpGet:
  145. path: /health
  146. port: 443
  147. scheme: HTTPS
  148. initialDelaySeconds: 3
  149. periodSeconds: 3
  150. livenessProbe:
  151. httpGet:
  152. path: /health
  153. port: 443
  154. scheme: HTTPS
  155. initialDelaySeconds: 5
  156. periodSeconds: 3
  157. volumeMounts:
  158. - name: certificates
  159. mountPath: /home/step/.step/secrets
  160. readOnly: true
  161. - name: config
  162. mountPath: /home/step/.step/config
  163. readOnly: true
  164. - name: secrets
  165. mountPath: /home/step/secrets
  166. readOnly: true
  167. securityContext:
  168. runAsUser: 1000
  169. allowPrivilegeEscalation: false
  170. volumes:
  171. - name: certificates
  172. configMap:
  173. name: ca-certificates
  174. - name: config
  175. configMap:
  176. name: ca-config
  177. - name: secrets
  178. secret:
  179. secretName: ca-certificate-password
  180. EOF
  181. cat $tmp/step-ca.yml
  182. rm -rf "$tmp"
  183. }
  184. install_ca