apiserver_manifest_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package gci
  14. import (
  15. "bytes"
  16. "encoding/base64"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "reflect"
  22. "strings"
  23. "testing"
  24. "k8s.io/api/core/v1"
  25. )
  26. const (
  27. /*
  28. Template for defining the environment state of configure-helper.sh
  29. The environment of configure-helper.sh is initially configured via kube-env file. However, as deploy-helper
  30. executes new variables are created. ManifestTestCase does not care where a variable came from. However, future
  31. test scenarios, may require such a distinction.
  32. The list of variables is, by no means, complete - this is what is required to run currently defined tests.
  33. */
  34. deployHelperEnv = `
  35. readonly KUBE_HOME={{.KubeHome}}
  36. readonly KUBE_API_SERVER_LOG_PATH=${KUBE_HOME}/kube-apiserver.log
  37. readonly KUBE_API_SERVER_AUDIT_LOG_PATH=${KUBE_HOME}/kube-apiserver-audit.log
  38. readonly CLOUD_CONFIG_OPT=--cloud-config=/etc/gce.conf
  39. readonly CA_CERT_BUNDLE_PATH=/foo/bar
  40. readonly APISERVER_SERVER_CERT_PATH=/foo/bar
  41. readonly APISERVER_SERVER_KEY_PATH=/foo/bar
  42. readonly APISERVER_CLIENT_CERT_PATH=/foo/bar
  43. readonly CLOUD_CONFIG_MOUNT="{\"name\": \"cloudconfigmount\",\"mountPath\": \"/etc/gce.conf\", \"readOnly\": true},"
  44. readonly CLOUD_CONFIG_VOLUME="{\"name\": \"cloudconfigmount\",\"hostPath\": {\"path\": \"/etc/gce.conf\", \"type\": \"FileOrCreate\"}},"
  45. readonly INSECURE_PORT_MAPPING="{ \"name\": \"local\", \"containerPort\": 8080, \"hostPort\": 8080},"
  46. readonly DOCKER_REGISTRY="k8s.gcr.io"
  47. readonly ENABLE_LEGACY_ABAC=false
  48. readonly ETC_MANIFESTS=${KUBE_HOME}/etc/kubernetes/manifests
  49. readonly KUBE_API_SERVER_DOCKER_TAG=v1.11.0-alpha.0.1808_3c7452dc11645d-dirty
  50. readonly LOG_OWNER_USER=$(id -un)
  51. readonly LOG_OWNER_GROUP=$(id -gn)
  52. readonly SERVICEACCOUNT_ISSUER=https://foo.bar.baz
  53. readonly SERVICEACCOUNT_KEY_PATH=/foo/bar/baz.key
  54. {{if .EncryptionProviderConfig}}
  55. ENCRYPTION_PROVIDER_CONFIG={{.EncryptionProviderConfig}}
  56. {{end}}
  57. ENCRYPTION_PROVIDER_CONFIG_PATH={{.EncryptionProviderConfigPath}}
  58. {{if .CloudKMSIntegration}}
  59. readonly CLOUD_KMS_INTEGRATION=true
  60. {{end}}
  61. `
  62. kubeAPIServerManifestFileName = "kube-apiserver.manifest"
  63. kubeAPIServerStartFuncName = "start-kube-apiserver"
  64. )
  65. type kubeAPIServerEnv struct {
  66. KubeHome string
  67. EncryptionProviderConfigPath string
  68. EncryptionProviderConfig string
  69. CloudKMSIntegration bool
  70. }
  71. type kubeAPIServerManifestTestCase struct {
  72. *ManifestTestCase
  73. }
  74. func newKubeAPIServerManifestTestCase(t *testing.T) *kubeAPIServerManifestTestCase {
  75. return &kubeAPIServerManifestTestCase{
  76. ManifestTestCase: newManifestTestCase(t, kubeAPIServerManifestFileName, kubeAPIServerStartFuncName, nil),
  77. }
  78. }
  79. func (c *kubeAPIServerManifestTestCase) invokeTest(e kubeAPIServerEnv, kubeEnv string) {
  80. c.mustInvokeFunc(kubeEnv, e)
  81. c.mustLoadPodFromManifest()
  82. }
  83. func TestEncryptionProviderFlag(t *testing.T) {
  84. var (
  85. // command": [
  86. // "/bin/sh", - Index 0
  87. // "-c", - Index 1
  88. // "exec /usr/local/bin/kube-apiserver " - Index 2
  89. execArgsIndex = 2
  90. encryptionConfigFlag = "--encryption-provider-config"
  91. )
  92. testCases := []struct {
  93. desc string
  94. encryptionProviderConfig string
  95. wantFlag bool
  96. }{
  97. {
  98. desc: "ENCRYPTION_PROVIDER_CONFIG is set",
  99. encryptionProviderConfig: base64.StdEncoding.EncodeToString([]byte("foo")),
  100. wantFlag: true,
  101. },
  102. {
  103. desc: "ENCRYPTION_PROVIDER_CONFIG is not set",
  104. encryptionProviderConfig: "",
  105. wantFlag: false,
  106. },
  107. }
  108. for _, tc := range testCases {
  109. t.Run(tc.desc, func(t *testing.T) {
  110. c := newKubeAPIServerManifestTestCase(t)
  111. defer c.tearDown()
  112. e := kubeAPIServerEnv{
  113. KubeHome: c.kubeHome,
  114. EncryptionProviderConfigPath: filepath.Join(c.kubeHome, "encryption-provider-config.yaml"),
  115. EncryptionProviderConfig: tc.encryptionProviderConfig,
  116. }
  117. c.invokeTest(e, deployHelperEnv)
  118. execArgs := c.pod.Spec.Containers[0].Command[execArgsIndex]
  119. flagIsInArg := strings.Contains(execArgs, encryptionConfigFlag)
  120. flag := fmt.Sprintf("%s=%s", encryptionConfigFlag, e.EncryptionProviderConfigPath)
  121. switch {
  122. case tc.wantFlag && !flagIsInArg:
  123. t.Fatalf("Got %q,\n want flags to contain %q", execArgs, flag)
  124. case !tc.wantFlag && flagIsInArg:
  125. t.Fatalf("Got %q,\n do not want flags to contain %q", execArgs, encryptionConfigFlag)
  126. case tc.wantFlag && flagIsInArg && !strings.Contains(execArgs, flag):
  127. t.Fatalf("Got flags: %q, want it to contain %q", execArgs, flag)
  128. }
  129. })
  130. }
  131. }
  132. func TestEncryptionProviderConfig(t *testing.T) {
  133. c := newKubeAPIServerManifestTestCase(t)
  134. defer c.tearDown()
  135. p := filepath.Join(c.kubeHome, "encryption-provider-config.yaml")
  136. e := kubeAPIServerEnv{
  137. KubeHome: c.kubeHome,
  138. EncryptionProviderConfigPath: p,
  139. EncryptionProviderConfig: base64.StdEncoding.EncodeToString([]byte("foo")),
  140. }
  141. c.mustInvokeFunc(deployHelperEnv, e)
  142. if _, err := os.Stat(p); err != nil {
  143. c.t.Fatalf("Expected encryption provider config to be written to %s, but stat failed with error: %v", p, err)
  144. }
  145. got, err := ioutil.ReadFile(p)
  146. if err != nil {
  147. c.t.Fatalf("Failed to read encryption provider config %s", p)
  148. }
  149. want := []byte("foo")
  150. if !bytes.Equal(got, want) {
  151. c.t.Fatalf("got encryptionConfig:\n%q\n, want encryptionConfig:\n%q", got, want)
  152. }
  153. }
  154. func TestKMSIntegration(t *testing.T) {
  155. var (
  156. socketPath = "/var/run/kmsplugin"
  157. dirOrCreate = v1.HostPathType(v1.HostPathDirectoryOrCreate)
  158. socketName = "kmssocket"
  159. )
  160. testCases := []struct {
  161. desc string
  162. cloudKMSIntegration bool
  163. wantVolume v1.Volume
  164. wantVolMount v1.VolumeMount
  165. }{
  166. {
  167. desc: "CLOUD_KMS_INTEGRATION is set",
  168. cloudKMSIntegration: true,
  169. wantVolume: v1.Volume{
  170. Name: socketName,
  171. VolumeSource: v1.VolumeSource{
  172. HostPath: &v1.HostPathVolumeSource{
  173. Path: socketPath,
  174. Type: &dirOrCreate,
  175. },
  176. },
  177. },
  178. wantVolMount: v1.VolumeMount{
  179. Name: socketName,
  180. MountPath: socketPath,
  181. },
  182. },
  183. {
  184. desc: "CLOUD_KMS_INTEGRATION is not set",
  185. cloudKMSIntegration: false,
  186. },
  187. }
  188. for _, tc := range testCases {
  189. t.Run(tc.desc, func(t *testing.T) {
  190. c := newKubeAPIServerManifestTestCase(t)
  191. defer c.tearDown()
  192. var e = kubeAPIServerEnv{
  193. KubeHome: c.kubeHome,
  194. EncryptionProviderConfigPath: filepath.Join(c.kubeHome, "encryption-provider-config.yaml"),
  195. EncryptionProviderConfig: base64.StdEncoding.EncodeToString([]byte("foo")),
  196. CloudKMSIntegration: tc.cloudKMSIntegration,
  197. }
  198. c.invokeTest(e, deployHelperEnv)
  199. // By this point, we can be sure that kube-apiserver manifest is a valid POD.
  200. var gotVolume v1.Volume
  201. for _, v := range c.pod.Spec.Volumes {
  202. if v.Name == socketName {
  203. gotVolume = v
  204. break
  205. }
  206. }
  207. if !reflect.DeepEqual(gotVolume, tc.wantVolume) {
  208. t.Errorf("got volume %v, want %v", gotVolume, tc.wantVolume)
  209. }
  210. var gotVolumeMount v1.VolumeMount
  211. for _, v := range c.pod.Spec.Containers[0].VolumeMounts {
  212. if v.Name == socketName {
  213. gotVolumeMount = v
  214. break
  215. }
  216. }
  217. if !reflect.DeepEqual(gotVolumeMount, tc.wantVolMount) {
  218. t.Errorf("got volumeMount %v, want %v", gotVolumeMount, tc.wantVolMount)
  219. }
  220. })
  221. }
  222. }