|
| 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/exec" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/kubernetes-sigs/kubebuilder/test/e2e/framework" |
| 27 | + e2einternal "github.com/kubernetes-sigs/kubebuilder/test/internal/e2e" |
| 28 | + . "github.com/onsi/ginkgo" |
| 29 | + . "github.com/onsi/gomega" |
| 30 | +) |
| 31 | + |
| 32 | +var _ = Describe("v1 main workflow", func() { |
| 33 | + It("should perform main kubebuilder workflow successfully", func() { |
| 34 | + testSuffix := framework.RandomSuffix() |
| 35 | + c := initConfig(testSuffix) |
| 36 | + kubebuilderTest := e2einternal.NewKubebuilderTest(c.workDir, framework.TestContext.BinariesDir) |
| 37 | + |
| 38 | + prepare(c.workDir) |
| 39 | + defer cleanupv1(kubebuilderTest, c.workDir, c.controllerImageName) |
| 40 | + |
| 41 | + var controllerPodName string |
| 42 | + |
| 43 | + output, errrr := exec.Command("which", "kubebuilder").Output() |
| 44 | + fmt.Printf("output of which: %s", output) |
| 45 | + Expect(errrr).NotTo(HaveOccurred()) |
| 46 | + |
| 47 | + By("init v1 project") |
| 48 | + initOptions := []string{ |
| 49 | + "--project-version", "v1", |
| 50 | + "--domain", c.domain, |
| 51 | + "--dep", "true", |
| 52 | + } |
| 53 | + err := kubebuilderTest.Init(initOptions) |
| 54 | + Expect(err).NotTo(HaveOccurred()) |
| 55 | + |
| 56 | + By("creating api definition") |
| 57 | + crdOptions := []string{ |
| 58 | + "--group", c.group, |
| 59 | + "--version", c.version, |
| 60 | + "--kind", c.kind, |
| 61 | + "--namespaced", "false", |
| 62 | + "--resource", "true", |
| 63 | + "--controller", "true", |
| 64 | + } |
| 65 | + err = kubebuilderTest.CreateAPI(crdOptions) |
| 66 | + Expect(err).NotTo(HaveOccurred()) |
| 67 | + |
| 68 | + // TODO: enable this test after we support gen rbac for core type controller |
| 69 | + By("creating core-type resource controller") |
| 70 | + coreControllerOptions := []string{ |
| 71 | + "--group", "apps", |
| 72 | + "--version", "v1", |
| 73 | + "--kind", "Deployment", |
| 74 | + "--namespaced", "true", |
| 75 | + "--resource", "false", |
| 76 | + "--controller", "true", |
| 77 | + } |
| 78 | + err = kubebuilderTest.CreateAPI(coreControllerOptions) |
| 79 | + Expect(err).NotTo(HaveOccurred()) |
| 80 | + |
| 81 | + By("building image") |
| 82 | + makeDockerBuildOptions := []string{"docker-build"} |
| 83 | + err = kubebuilderTest.Make(makeDockerBuildOptions) |
| 84 | + Expect(err).NotTo(HaveOccurred()) |
| 85 | + |
| 86 | + By("pushing image") |
| 87 | + makeDockerPushOptions := []string{"docker-push"} |
| 88 | + err = kubebuilderTest.Make(makeDockerPushOptions) |
| 89 | + Expect(err).NotTo(HaveOccurred()) |
| 90 | + |
| 91 | + // NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission. |
| 92 | + // Otherwise, you may see "... is forbidden: attempt to grant extra privileges" |
| 93 | + // $ kubectl create clusterrolebinding myname-cluster-admin-binding --clusterrole=cluster-admin [email protected] |
| 94 | + // https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control |
| 95 | + By("deploying controller manager") |
| 96 | + makeDeployOptions := []string{"deploy"} |
| 97 | + err = kubebuilderTest.Make(makeDeployOptions) |
| 98 | + |
| 99 | + By("validate the controller-manager pod running as expected") |
| 100 | + verifyContollerUp := func() error { |
| 101 | + // Get pod name |
| 102 | + getOptions := []string{"get", "pods", "-l", "control-plane=controller-manager", "-n", fmt.Sprintf("e2e-%s-system", testSuffix), "-o", "go-template={{ range .items }}{{ if not .metadata.deletionTimestamp }}{{ .metadata.name }}{{ \"\\n\" }}{{ end }}{{ end }}"} |
| 103 | + podOutput, err := kubebuilderTest.RunKubectlCommand(framework.GetKubectlArgs(getOptions)) |
| 104 | + Expect(err).NotTo(HaveOccurred()) |
| 105 | + podNames := framework.ParseCmdOutput(podOutput) |
| 106 | + if len(podNames) != 1 { |
| 107 | + return fmt.Errorf("expect 1 controller pods running, but got %d", len(podNames)) |
| 108 | + } |
| 109 | + controllerPodName = podNames[0] |
| 110 | + Expect(controllerPodName).Should(ContainSubstring("controller-manager")) |
| 111 | + |
| 112 | + // Validate pod status |
| 113 | + getOptions = []string{"get", "pods", controllerPodName, "-o", "jsonpath={.status.phase}", "-n", fmt.Sprintf("e2e-%s-system", testSuffix)} |
| 114 | + status, err := kubebuilderTest.RunKubectlCommand(framework.GetKubectlArgs(getOptions)) |
| 115 | + Expect(err).NotTo(HaveOccurred()) |
| 116 | + if status != "Running" { |
| 117 | + return fmt.Errorf("controller pod in %s status", status) |
| 118 | + } |
| 119 | + |
| 120 | + return nil |
| 121 | + } |
| 122 | + Eventually(verifyContollerUp, 5*time.Minute, time.Second).Should(BeNil()) |
| 123 | + |
| 124 | + By("creating an instance of CR") |
| 125 | + inputFile := filepath.Join("config", "samples", fmt.Sprintf("%s_%s_%s.yaml", c.group, c.version, strings.ToLower(c.kind))) |
| 126 | + createOptions := []string{"apply", "-f", inputFile} |
| 127 | + _, err = kubebuilderTest.RunKubectlCommand(framework.GetKubectlArgs(createOptions)) |
| 128 | + Expect(err).NotTo(HaveOccurred()) |
| 129 | + |
| 130 | + By("validate the created resource object gets reconciled in controller") |
| 131 | + controllerContainerLogs := func() string { |
| 132 | + // Check container log to validate that the created resource object gets reconciled in controller |
| 133 | + logOptions := []string{"logs", controllerPodName, "-n", fmt.Sprintf("e2e-%s-system", testSuffix)} |
| 134 | + logOutput, err := kubebuilderTest.RunKubectlCommand(framework.GetKubectlArgs(logOptions)) |
| 135 | + Expect(err).NotTo(HaveOccurred()) |
| 136 | + |
| 137 | + return logOutput |
| 138 | + } |
| 139 | + Eventually(controllerContainerLogs, 3*time.Minute, time.Second).Should(ContainSubstring("Updating")) |
| 140 | + Eventually(controllerContainerLogs, 3*time.Minute, time.Second).Should(ContainSubstring("Updating")) |
| 141 | + }) |
| 142 | +}) |
0 commit comments