-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(go/v4-alpha): add new golang plugin which uses the default golang ba…
…se v3 and the new alpha kustomze/v2-alpha
- Loading branch information
1 parent
a2b2be2
commit 17965d2
Showing
361 changed files
with
15,821 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Copyright 2020 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 v4 | ||
|
||
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,133 @@ | ||
/* | ||
Copyright 2020 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 v3 | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" | ||
|
||
//nolint:golint | ||
//nolint:revive | ||
. "github.com/onsi/ginkgo" | ||
|
||
//nolint:golint | ||
//nolint:revive | ||
. "github.com/onsi/gomega" | ||
|
||
"sigs.k8s.io/kubebuilder/v3/test/e2e/utils" | ||
) | ||
|
||
// GenerateV4 implements a go/v4(-alpha) plugin project defined by a TestContext. | ||
func GenerateV4(kbc *utils.TestContext, crdAndWebhookVersion string) { | ||
var err error | ||
|
||
By("initializing a project") | ||
err = kbc.Init( | ||
"--plugins", "go/v4-alpha", | ||
"--project-version", "3", | ||
"--domain", kbc.Domain, | ||
"--fetch-deps=false", | ||
) | ||
ExpectWithOffset(1, err).NotTo(HaveOccurred()) | ||
|
||
By("creating API definition") | ||
err = kbc.CreateAPI( | ||
"--group", kbc.Group, | ||
"--version", kbc.Version, | ||
"--kind", kbc.Kind, | ||
"--namespaced", | ||
"--resource", | ||
"--controller", | ||
"--make=false", | ||
"--crd-version", crdAndWebhookVersion, | ||
) | ||
ExpectWithOffset(1, err).NotTo(HaveOccurred()) | ||
|
||
By("implementing the API") | ||
ExpectWithOffset(1, pluginutil.InsertCode( | ||
filepath.Join(kbc.Dir, "api", kbc.Version, fmt.Sprintf("%s_types.go", strings.ToLower(kbc.Kind))), | ||
fmt.Sprintf(`type %sSpec struct { | ||
`, kbc.Kind), | ||
` // +optional | ||
Count int `+"`"+`json:"count,omitempty"`+"`"+` | ||
`)).Should(Succeed()) | ||
|
||
By("scaffolding mutating and validating webhooks") | ||
err = kbc.CreateWebhook( | ||
"--group", kbc.Group, | ||
"--version", kbc.Version, | ||
"--kind", kbc.Kind, | ||
"--defaulting", | ||
"--programmatic-validation", | ||
"--webhook-version", crdAndWebhookVersion, | ||
) | ||
ExpectWithOffset(1, err).NotTo(HaveOccurred()) | ||
|
||
By("implementing the mutating and validating webhooks") | ||
err = pluginutil.ImplementWebhooks(filepath.Join( | ||
kbc.Dir, "api", kbc.Version, | ||
fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind)))) | ||
ExpectWithOffset(1, err).NotTo(HaveOccurred()) | ||
|
||
By("uncomment kustomization.yaml to enable webhook and ca injection") | ||
ExpectWithOffset(1, pluginutil.UncommentCode( | ||
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"), | ||
"#- ../webhook", "#")).To(Succeed()) | ||
ExpectWithOffset(1, pluginutil.UncommentCode( | ||
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"), | ||
"#- ../certmanager", "#")).To(Succeed()) | ||
ExpectWithOffset(1, pluginutil.UncommentCode( | ||
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"), | ||
"#- ../prometheus", "#")).To(Succeed()) | ||
ExpectWithOffset(1, pluginutil.UncommentCode( | ||
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"), | ||
"#- manager_webhook_patch.yaml", "#")).To(Succeed()) | ||
ExpectWithOffset(1, pluginutil.UncommentCode( | ||
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"), | ||
"#- webhookcainjection_patch.yaml", "#")).To(Succeed()) | ||
ExpectWithOffset(1, pluginutil.UncommentCode(filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"), | ||
`#- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR | ||
# objref: | ||
# kind: Certificate | ||
# group: cert-manager.io | ||
# version: v1 | ||
# name: serving-cert # this name should match the one in certificate.yaml | ||
# fieldref: | ||
# fieldpath: metadata.namespace | ||
#- name: CERTIFICATE_NAME | ||
# objref: | ||
# kind: Certificate | ||
# group: cert-manager.io | ||
# version: v1 | ||
# name: serving-cert # this name should match the one in certificate.yaml | ||
#- name: SERVICE_NAMESPACE # namespace of the service | ||
# objref: | ||
# kind: Service | ||
# version: v1 | ||
# name: webhook-service | ||
# fieldref: | ||
# fieldpath: metadata.namespace | ||
#- name: SERVICE_NAME | ||
# objref: | ||
# kind: Service | ||
# version: v1 | ||
# name: webhook-service`, "#")).To(Succeed()) | ||
|
||
} |
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,91 @@ | ||
/* | ||
Copyright 2020 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 v4 | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" | ||
v3 "sigs.k8s.io/kubebuilder/v3/test/e2e/v3" | ||
|
||
//nolint:golint | ||
//nolint:revive | ||
. "github.com/onsi/ginkgo" | ||
|
||
//nolint:golint | ||
//nolint:revive | ||
. "github.com/onsi/gomega" | ||
|
||
"sigs.k8s.io/kubebuilder/v3/test/e2e/utils" | ||
) | ||
|
||
var _ = Describe("kubebuilder", func() { | ||
Context("project version 3", func() { | ||
var ( | ||
kbc *utils.TestContext | ||
) | ||
|
||
BeforeEach(func() { | ||
var err error | ||
kbc, err = utils.NewTestContext(util.KubebuilderBinName, "GO111MODULE=on") | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(kbc.Prepare()).To(Succeed()) | ||
|
||
By("installing the Prometheus operator") | ||
Expect(kbc.InstallPrometheusOperManager()).To(Succeed()) | ||
}) | ||
|
||
AfterEach(func() { | ||
By("clean up API objects created during the test") | ||
kbc.CleanupManifests(filepath.Join("config", "default")) | ||
|
||
By("uninstalling the Prometheus manager bundle") | ||
kbc.UninstallPrometheusOperManager() | ||
|
||
By("removing controller image and working dir") | ||
kbc.Destroy() | ||
}) | ||
|
||
Context("plugin base.go.kubebuilder.io/v4-alpha", func() { | ||
// Use cert-manager with v1 CRs. | ||
BeforeEach(func() { | ||
By("installing the cert-manager bundle") | ||
Expect(kbc.InstallCertManager(false)).To(Succeed()) | ||
}) | ||
AfterEach(func() { | ||
By("uninstalling the cert-manager bundle") | ||
kbc.UninstallCertManager(false) | ||
}) | ||
|
||
It("should generate a runnable project", func() { | ||
// Skip if cluster version < 1.16, when v1 CRDs and webhooks did not exist. | ||
if srvVer := kbc.K8sVersion.ServerVersion; srvVer.GetMajorInt() <= 1 && srvVer.GetMinorInt() < 17 { | ||
Skip(fmt.Sprintf("cluster version %s does not support v1 CRDs or webhooks", srvVer.GitVersion)) | ||
} | ||
|
||
GenerateV4(kbc, "v1") | ||
v3.Run(kbc) | ||
}) | ||
}) | ||
}) | ||
}) |
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,4 @@ | ||
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file | ||
# Ignore build and test binaries. | ||
bin/ | ||
testbin/ |
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,24 @@ | ||
|
||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
bin | ||
|
||
# Test binary, build with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Kubernetes Generated files - skip generated files, except for vendored files | ||
|
||
!vendor/**/zz_generated.* | ||
|
||
# editor and IDE paraphernalia | ||
.idea | ||
*.swp | ||
*.swo | ||
*~ |
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,34 @@ | ||
# Build the manager binary | ||
FROM golang:1.17 as builder | ||
|
||
WORKDIR /workspace | ||
# Copy the Go Modules manifests | ||
COPY go.mod go.mod | ||
COPY go.sum go.sum | ||
# cache deps before building and copying source so that we don't need to re-download as much | ||
# and so that source changes don't invalidate our downloaded layer | ||
RUN go mod download | ||
|
||
# Copy the go source | ||
COPY main.go main.go | ||
COPY api/ api/ | ||
COPY controllers/ controllers/ | ||
# https://github.com/kubernetes-sigs/kubebuilder-declarative-pattern/blob/master/docs/addon/walkthrough/README.md#adding-a-manifest | ||
# Stage channels and make readable | ||
COPY channels/ /channels/ | ||
RUN chmod -R a+rx /channels/ | ||
|
||
# Build | ||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go | ||
|
||
# Use distroless as minimal base image to package the manager binary | ||
# Refer to https://github.com/GoogleContainerTools/distroless for more details | ||
FROM gcr.io/distroless/static:nonroot | ||
WORKDIR / | ||
COPY --from=builder /workspace/manager . | ||
# copy channels | ||
COPY --from=builder /channels /channels | ||
|
||
USER 65532:65532 | ||
|
||
ENTRYPOINT ["/manager"] |
Oops, something went wrong.