-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mengqi Yu
committed
Jun 4, 2019
1 parent
e096922
commit 2413690
Showing
17 changed files
with
498 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,4 +58,4 @@ generate_vendor() { | |
} | ||
|
||
build_kb && \ | ||
generate_vendor $1 | ||
generate_vendor 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/sh | ||
|
||
# Copyright 2019 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
go get sigs.k8s.io/kind@v0.3.0 | ||
# TODO(mengqiy): use a released version after v2.1.0 is out. | ||
go get sigs.k8s.io/kustomize@f9c631e9eec7a2d6e46eb9e1bf5122f68b97d12d | ||
|
||
# You can use --image flag to specify the cluster version you want, e.g --image=kindest/node:v1.13.6, the supported version are listed at https://hub.docker.com/r/kindest/node/tags | ||
kind create cluster --config test/kind-config.yaml --image=kindest/node:v1.14.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package e2e | ||
|
||
import ( | ||
"path/filepath" | ||
) | ||
|
||
// runtime config specified to run e2e tests | ||
type config struct { | ||
domain string | ||
group string | ||
version string | ||
kind string | ||
controllerImageName string | ||
workDir string | ||
} | ||
|
||
// configWithSuffix init with a random suffix for test config stuff, | ||
// to avoid conflict when running tests synchronously. | ||
func configWithSuffix(testSuffix string) (*config, error) { | ||
testGroup := "bar" + testSuffix | ||
path, err := filepath.Abs("e2e-" + testSuffix) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &config{ | ||
domain: "example.com" + testSuffix, | ||
group: testGroup, | ||
version: "v1alpha1", | ||
kind: "Foo" + testSuffix, | ||
controllerImageName: "e2e-test/controller-manager:" + testSuffix, | ||
workDir: path, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package e2e | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
// Run e2e tests using the Ginkgo runner. | ||
func TestE2E(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
fmt.Fprintf(GinkgoWriter, "Starting kubebuilder suite\n") | ||
RunSpecs(t, "Kubebuilder e2e suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package e2e | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("kubebuilder", func() { | ||
Context("with v1 scaffolding", func() { | ||
imageName := "controller:v0.0.1" | ||
var testSuffix string | ||
var c *config | ||
var kbTest *kubebuilderTest | ||
|
||
BeforeEach(func() { | ||
var err error | ||
testSuffix, err = randomSuffix() | ||
Expect(err).NotTo(HaveOccurred()) | ||
c, err = configWithSuffix(testSuffix) | ||
Expect(err).NotTo(HaveOccurred()) | ||
kbTest = &kubebuilderTest{ | ||
Dir: c.workDir, | ||
Env: []string{"GO111MODULE=off"}, | ||
} | ||
prepare(c.workDir) | ||
}) | ||
|
||
AfterEach(func() { | ||
By("clean up created API objects during test process") | ||
resources, err := kbTest.RunKustomizeCommand("build", filepath.Join("config", "default")) | ||
if err != nil { | ||
fmt.Fprintf(GinkgoWriter, "error when running kustomize build during cleaning up: %v\n", err) | ||
} | ||
if _, err = kbTest.RunKubectlCommandWithInput(resources, "delete", "--recursive", "-f", "-"); err != nil { | ||
fmt.Fprintf(GinkgoWriter, "error when running kubectl delete during cleaning up: %v\n", err) | ||
} | ||
if _, err = kbTest.RunKubectlCommand( | ||
"delete", "--recursive", | ||
"-f", filepath.Join("config", "crds"), | ||
); err != nil { | ||
fmt.Fprintf(GinkgoWriter, "error when running kubectl delete during cleaning up crd: %v\n", err) | ||
} | ||
|
||
By("remove container image created during test") | ||
kbTest.CleanupImage(c.controllerImageName) | ||
|
||
By("remove test work dir") | ||
os.RemoveAll(c.workDir) | ||
}) | ||
|
||
It("should generate a runnable project", func() { | ||
// prepare v1 vendor | ||
By("untar the vendor tarball") | ||
cmd := exec.Command("tar", "-zxf", "../../../testdata/vendor.v1.tgz") | ||
cmd.Dir = c.workDir | ||
err := cmd.Run() | ||
Expect(err).Should(Succeed()) | ||
|
||
var controllerPodName string | ||
|
||
By("init v1 project") | ||
err = kbTest.Init( | ||
"--project-version", "1", | ||
"--domain", c.domain, | ||
"--dep=false") | ||
Expect(err).Should(Succeed()) | ||
|
||
By("creating api definition") | ||
err = kbTest.CreateAPI( | ||
"--group", c.group, | ||
"--version", c.version, | ||
"--kind", c.kind, | ||
"--namespaced", | ||
"--resource", | ||
"--controller", | ||
"--make=false") | ||
Expect(err).Should(Succeed()) | ||
|
||
By("creating core-type resource controller") | ||
err = kbTest.CreateAPI( | ||
"--group", "apps", | ||
"--version", "v1", | ||
"--kind", "Deployment", | ||
"--namespaced", | ||
"--resource=false", | ||
"--controller", | ||
"--make=false") | ||
Expect(err).Should(Succeed()) | ||
|
||
By("building image") | ||
err = kbTest.Make("docker-build", "IMG="+imageName) | ||
Expect(err).Should(Succeed()) | ||
|
||
By("loading docker image into kind cluster") | ||
err = kbTest.LoadImageToKindCluster(imageName) | ||
Expect(err).Should(Succeed()) | ||
|
||
// NOTE: If you want to run the test against a GKE cluster, you will need to grant yourself permission. | ||
// Otherwise, you may see "... is forbidden: attempt to grant extra privileges" | ||
// $ kubectl create clusterrolebinding myname-cluster-admin-binding --clusterrole=cluster-admin --user=myname@mycompany.com | ||
// https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control | ||
By("deploying controller manager") | ||
err = kbTest.Make("deploy") | ||
Expect(err).Should(Succeed()) | ||
|
||
By("validate the controller-manager pod running as expected") | ||
verifyControllerUp := func() error { | ||
// Get pod name | ||
podOutput, err := kbTest.RunKubectlGetPodsInNamespace( | ||
testSuffix, | ||
"-l", "control-plane=controller-manager", | ||
"-o", "go-template={{ range .items }}{{ if not .metadata.deletionTimestamp }}{{ .metadata.name }}{{ \"\\n\" }}{{ end }}{{ end }}", | ||
) | ||
Expect(err).NotTo(HaveOccurred()) | ||
podNames := getNonEmptyLines(podOutput) | ||
if len(podNames) != 1 { | ||
return fmt.Errorf("expect 1 controller pods running, but got %d", len(podNames)) | ||
} | ||
controllerPodName = podNames[0] | ||
Expect(controllerPodName).Should(ContainSubstring("controller-manager")) | ||
|
||
// Validate pod status | ||
status, err := kbTest.RunKubectlGetPodsInNamespace( | ||
testSuffix, | ||
controllerPodName, "-o", "jsonpath={.status.phase}", | ||
) | ||
Expect(err).NotTo(HaveOccurred()) | ||
if status != "Running" { | ||
return fmt.Errorf("controller pod in %s status", status) | ||
} | ||
|
||
return nil | ||
} | ||
Eventually(verifyControllerUp, 2*time.Minute, time.Second).Should(Succeed()) | ||
|
||
By("creating an instance of CR") | ||
inputFile := filepath.Join("config", "samples", fmt.Sprintf("%s_%s_%s.yaml", c.group, c.version, strings.ToLower(c.kind))) | ||
_, err = kbTest.RunKubectlCommand("apply", "-f", inputFile) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("validate the created resource object gets reconciled in controller") | ||
controllerContainerLogs := func() string { | ||
// Check container log to validate that the created resource object gets reconciled in controller | ||
logOutput, err := kbTest.RunKubectlCommand( | ||
"logs", controllerPodName, | ||
"-c", "manager", | ||
"-n", fmt.Sprintf("e2e-%s-system", testSuffix), | ||
) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
return logOutput | ||
} | ||
Eventually(controllerContainerLogs, 2*time.Minute, time.Second).Should(ContainSubstring("Updating")) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.