|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package e2e |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "os/exec" |
| 23 | + "path/filepath" |
| 24 | + "strings" |
| 25 | + "time" |
| 26 | + |
| 27 | + . "github.com/onsi/ginkgo" |
| 28 | + . "github.com/onsi/gomega" |
| 29 | +) |
| 30 | + |
| 31 | +var _ = Describe("kubebuilder", func() { |
| 32 | + Context("with v1 scaffolding", func() { |
| 33 | + imageName := "controller:v0.0.1" |
| 34 | + var testSuffix string |
| 35 | + var c *config |
| 36 | + var kbTest *kubebuilderTest |
| 37 | + |
| 38 | + BeforeEach(func() { |
| 39 | + var err error |
| 40 | + testSuffix, err = randomSuffix() |
| 41 | + Expect(err).NotTo(HaveOccurred()) |
| 42 | + c, err = configWithSuffix(testSuffix) |
| 43 | + Expect(err).NotTo(HaveOccurred()) |
| 44 | + kbTest = &kubebuilderTest{ |
| 45 | + Dir: c.workDir, |
| 46 | + Env: []string{"GO111MODULE=off"}, |
| 47 | + } |
| 48 | + prepare(c.workDir) |
| 49 | + }) |
| 50 | + |
| 51 | + AfterEach(func() { |
| 52 | + By("clean up created API objects during test process") |
| 53 | + resources, err := kbTest.RunKustomizeCommand("build", filepath.Join("config", "default")) |
| 54 | + if err != nil { |
| 55 | + fmt.Fprintf(GinkgoWriter, "error when running kustomize build during cleaning up: %v\n", err) |
| 56 | + } |
| 57 | + if _, err = kbTest.RunKubectlCommandWithInput(resources, "delete", "--recursive", "-f", "-"); err != nil { |
| 58 | + fmt.Fprintf(GinkgoWriter, "error when running kubectl delete during cleaning up: %v\n", err) |
| 59 | + } |
| 60 | + if _, err = kbTest.RunKubectlCommand( |
| 61 | + "delete", "--recursive", |
| 62 | + "-f", filepath.Join("config", "crds"), |
| 63 | + ); err != nil { |
| 64 | + fmt.Fprintf(GinkgoWriter, "error when running kubectl delete during cleaning up crd: %v\n", err) |
| 65 | + } |
| 66 | + |
| 67 | + By("remove container image created during test") |
| 68 | + kbTest.CleanupImage(c.controllerImageName) |
| 69 | + |
| 70 | + By("remove test work dir") |
| 71 | + os.RemoveAll(c.workDir) |
| 72 | + }) |
| 73 | + |
| 74 | + It("should generate a runnable project", func() { |
| 75 | + // prepare v1 vendor |
| 76 | + By("untar the vendor tarball") |
| 77 | + cmd := exec.Command("tar", "-zxf", "../../../testdata/vendor.v1.tgz") |
| 78 | + cmd.Dir = c.workDir |
| 79 | + err := cmd.Run() |
| 80 | + Expect(err).Should(Succeed()) |
| 81 | + |
| 82 | + var controllerPodName string |
| 83 | + |
| 84 | + By("init v1 project") |
| 85 | + err = kbTest.Init( |
| 86 | + "--project-version", "1", |
| 87 | + "--domain", c.domain, |
| 88 | + "--dep=false") |
| 89 | + Expect(err).Should(Succeed()) |
| 90 | + |
| 91 | + By("creating api definition") |
| 92 | + err = kbTest.CreateAPI( |
| 93 | + "--group", c.group, |
| 94 | + "--version", c.version, |
| 95 | + "--kind", c.kind, |
| 96 | + "--namespaced", |
| 97 | + "--resource", |
| 98 | + "--controller", |
| 99 | + "--make=false") |
| 100 | + Expect(err).Should(Succeed()) |
| 101 | + |
| 102 | + By("creating core-type resource controller") |
| 103 | + err = kbTest.CreateAPI( |
| 104 | + "--group", "apps", |
| 105 | + "--version", "v1", |
| 106 | + "--kind", "Deployment", |
| 107 | + "--namespaced", |
| 108 | + "--resource=false", |
| 109 | + "--controller", |
| 110 | + "--make=false") |
| 111 | + Expect(err).Should(Succeed()) |
| 112 | + |
| 113 | + By("building image") |
| 114 | + err = kbTest.Make("docker-build", "IMG="+imageName) |
| 115 | + Expect(err).Should(Succeed()) |
| 116 | + |
| 117 | + By("loading docker image into kind cluster") |
| 118 | + err = kbTest.LoadImageToKindCluster(imageName) |
| 119 | + Expect(err).Should(Succeed()) |
| 120 | + |
| 121 | + // NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission. |
| 122 | + // Otherwise, you may see "... is forbidden: attempt to grant extra privileges" |
| 123 | + // $ kubectl create clusterrolebinding myname-cluster-admin-binding --clusterrole=cluster-admin --user=myname@mycompany.com |
| 124 | + // https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control |
| 125 | + By("deploying controller manager") |
| 126 | + err = kbTest.Make("deploy") |
| 127 | + Expect(err).Should(Succeed()) |
| 128 | + |
| 129 | + By("validate the controller-manager pod running as expected") |
| 130 | + verifyControllerUp := func() error { |
| 131 | + // Get pod name |
| 132 | + podOutput, err := kbTest.RunKubectlGetPodsInNamespace( |
| 133 | + testSuffix, |
| 134 | + "-l", "control-plane=controller-manager", |
| 135 | + "-o", "go-template={{ range .items }}{{ if not .metadata.deletionTimestamp }}{{ .metadata.name }}{{ \"\\n\" }}{{ end }}{{ end }}", |
| 136 | + ) |
| 137 | + Expect(err).NotTo(HaveOccurred()) |
| 138 | + podNames := getNonEmptyLines(podOutput) |
| 139 | + if len(podNames) != 1 { |
| 140 | + return fmt.Errorf("expect 1 controller pods running, but got %d", len(podNames)) |
| 141 | + } |
| 142 | + controllerPodName = podNames[0] |
| 143 | + Expect(controllerPodName).Should(ContainSubstring("controller-manager")) |
| 144 | + |
| 145 | + // Validate pod status |
| 146 | + status, err := kbTest.RunKubectlGetPodsInNamespace( |
| 147 | + testSuffix, |
| 148 | + controllerPodName, "-o", "jsonpath={.status.phase}", |
| 149 | + ) |
| 150 | + Expect(err).NotTo(HaveOccurred()) |
| 151 | + if status != "Running" { |
| 152 | + return fmt.Errorf("controller pod in %s status", status) |
| 153 | + } |
| 154 | + |
| 155 | + return nil |
| 156 | + } |
| 157 | + Eventually(verifyControllerUp, 2*time.Minute, time.Second).Should(Succeed()) |
| 158 | + |
| 159 | + By("creating an instance of CR") |
| 160 | + inputFile := filepath.Join("config", "samples", fmt.Sprintf("%s_%s_%s.yaml", c.group, c.version, strings.ToLower(c.kind))) |
| 161 | + _, err = kbTest.RunKubectlCommand("apply", "-f", inputFile) |
| 162 | + Expect(err).NotTo(HaveOccurred()) |
| 163 | + |
| 164 | + By("validate the created resource object gets reconciled in controller") |
| 165 | + controllerContainerLogs := func() string { |
| 166 | + // Check container log to validate that the created resource object gets reconciled in controller |
| 167 | + logOutput, err := kbTest.RunKubectlCommand( |
| 168 | + "logs", controllerPodName, |
| 169 | + "-c", "manager", |
| 170 | + "-n", fmt.Sprintf("e2e-%s-system", testSuffix), |
| 171 | + ) |
| 172 | + Expect(err).NotTo(HaveOccurred()) |
| 173 | + |
| 174 | + return logOutput |
| 175 | + } |
| 176 | + Eventually(controllerContainerLogs, 2*time.Minute, time.Second).Should(ContainSubstring("Updating")) |
| 177 | + }) |
| 178 | + }) |
| 179 | +}) |
0 commit comments