Skip to content

Commit

Permalink
feat: add v3-alpha plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Nov 14, 2020
1 parent b35e0c2 commit 91055c1
Show file tree
Hide file tree
Showing 114 changed files with 2,258 additions and 316 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ website/node_modules/
website/tech-doc-hugo

# Ensure that will not commit the bin gen in the go sample
testdata/go/memcached-operator/bin/*
*/bin/*

# Ignore molecule samples testdata if it be generated in the testdata/ diretory
testdata/ansible/memcached-molecule-operator
Expand Down
43 changes: 43 additions & 0 deletions changelog/fragments/add-go-v3-alpha.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,46 @@ entries:
#
# What is the pull request number (without the "#")?
# pull_request_override: 0
# Migration can be defined to automatically add a section to
# the migration guide. This is required for breaking changes.
migration:
header: Migrate your project to use the plugin v3/go-alpha
body: >
`v3+` plugins are still in alpha phase and are not scaffolded by default.
You are able to use them, try it out and get all improvements made so far by initializing projects with
the arg `--plugins=go/v3-alpha`:
```sh
operator-sdk init --domain my.domain --plugins=go.kubebuilder.io/v3-alpha
```
Currently, the plugin `v3-alpha` has NO breaking changes. However, until it
is declared stable breaking changes can be made related to K8s API deprecations
of `v1beta1` versions of `CustomResourceDefinition` and `ValidatingWebhookConfiguration`
and to upgrade the `cert-manager` for example:
- [cert-manager related configuration should be migrated to cert-manager.io/v1 #1666](https://github.com/kubernetes-sigs/kubebuilder/issues/1666)
- [Set preserveUnknownFields to false in the CRD conversion webhook patch #933](https://github.com/kubernetes-sigs/kubebuilder/issues/933)
- [Migrate existing KB project to v1 CRDs and Webhooks with minimal user effort #1065](https://github.com/kubernetes-sigs/kubebuilder/issues/1065)
Following the steps
1. Update your PROJECT file
Update the `layout` setting to the new plugin version `go.sdk.operatorframework.io/v3-alpha` as follows:
```sh
$ cat PROJECT
domain: example.com
layout: go.kubebuilder.io/v3-alpha
projectName: memcached-operator
repo: github.com/example/memcached-operator
resources:
- group: cache
kind: Memcached
version: v1alpha1
version: 3-alpha
plugins:
go.sdk.operatorframework.io/v3-alpha: {}
```
4 changes: 2 additions & 2 deletions hack/generate/samples/internal/ansible/memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (ma *MemcachedAnsible) Prepare() {
ma.ctx.Kind = "Memcached"
}

// Run the steps to create the Memcached Ansible Sample
// RunV2 the steps to create the Memcached Ansible Sample
func (ma *MemcachedAnsible) Run() {
log.Infof("creating the project")
err := ma.ctx.Init(
Expand Down Expand Up @@ -114,7 +114,7 @@ func (ma *MemcachedAnsible) addingAnsibleTask() {
// GenerateMemcachedAnsibleSample will call all actions to create the directory and generate the sample
// The Context to run the samples are not the same in the e2e test. In this way, note that it should NOT
// be called in the e2e tests since it will call the Prepare() to set the sample context and generate the files
// in the testdata directory. The e2e tests only ought to use the Run() method with the TestContext.
// in the testdata directory. The e2e tests only ought to use the RunV2() method with the TestContext.
func GenerateMemcachedAnsibleSample(samplesPath string) {
ctx, err := pkg.NewSampleContext(testutils.BinaryName, filepath.Join(samplesPath, "ansible", "memcached-operator"),
"GO111MODULE=on")
Expand Down
4 changes: 2 additions & 2 deletions hack/generate/samples/internal/ansible/molecule.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (ma *MoleculeAnsible) Prepare() {
ma.ctx.Kind = "Memcached"
}

// Run the steps to create the Memcached Ansible Sample
// RunV2 the steps to create the Memcached Ansible Sample
func (ma *MoleculeAnsible) Run() {
memcached := NewMemcachedAnsible(ma.ctx)
memcached.Run()
Expand Down Expand Up @@ -166,7 +166,7 @@ func (ma *MoleculeAnsible) Run() {
// GenerateMoleculeAnsibleSample will call all actions to create the directory and generate the sample
// The Context to run the samples are not the same in the e2e test. In this way, note that it should NOT
// be called in the e2e tests since it will call the Prepare() to set the sample context and generate the files
// in the testdata directory. The e2e tests only ought to use the Run() method with the TestContext.
// in the testdata directory. The e2e tests only ought to use the RunV2() method with the TestContext.
func GenerateMoleculeAnsibleSample(path string) {
ctx, err := pkg.NewSampleContext(testutils.BinaryName, filepath.Join(path, "memcached-molecule-operator"),
"GO111MODULE=on")
Expand Down
131 changes: 131 additions & 0 deletions hack/generate/samples/internal/go/generate_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2020 The Operator-SDK 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 gosamples

import (
"fmt"
"os"
"path/filepath"
"strings"

log "github.com/sirupsen/logrus"
kbtestutils "sigs.k8s.io/kubebuilder/v2/test/e2e/utils"

"github.com/operator-framework/operator-sdk/hack/generate/samples/internal/pkg"
"github.com/operator-framework/operator-sdk/internal/testutils"
)

// RunV2 the steps to create the Memcached with Webhooks Go Sample
func (mh *MemcachedGoWithWebhooks) RunV2() {
log.Infof("creating the project")
err := mh.ctx.Init(
"--repo", "github.com/example/memcached-operator",
"--domain",
mh.ctx.Domain)
pkg.CheckError("creating the project", err)

err = mh.ctx.CreateAPI(
"--group", mh.ctx.Group,
"--version", mh.ctx.Version,
"--kind", mh.ctx.Kind,
"--controller", "true",
"--resource", "true")
pkg.CheckError("scaffolding apis", err)

log.Infof("implementing the API")
mh.implementingAPI()

log.Infof("implementing the Controller")
mh.implementingControllerV2()

log.Infof("scaffolding webhook")
err = mh.ctx.CreateWebhook(
"--group", mh.ctx.Group,
"--version", mh.ctx.Version,
"--kind", mh.ctx.Kind,
"--defaulting",
"--defaulting")
pkg.CheckError("scaffolding webhook", err)

mh.implementingWebhooks()
mh.uncommentKustomizationFile()

mh.ctx.CreateBundle()

// Clean up built binaries, if any.
pkg.CheckError("cleaning up", os.RemoveAll(filepath.Join(mh.ctx.Dir, "bin")))
}

// GenerateMemcachedGoWithWebhooksSampleV2 will call all actions to create the directory and generate the sample
// Note that it should NOT be called in the e2e tests.
func GenerateMemcachedGoWithWebhooksSampleV2(samplesPath string) {
log.Infof("starting to generate Go memcached sample with webhooks")
ctx, err := pkg.NewSampleContext(testutils.BinaryName, filepath.Join(samplesPath, "go", "v2", "memcached-operator"), "GO111MODULE=on")
pkg.CheckError("generating Go memcached with webhooks context", err)

memcached := NewMemcachedGoWithWebhooks(&ctx)
memcached.Prepare()
memcached.RunV2()
}

// implementingController will customize the Controller
func (mh *MemcachedGoWithWebhooks) implementingControllerV2() {
controllerPath := filepath.Join(mh.ctx.Dir, "controllers", fmt.Sprintf("%s_controller.go",
strings.ToLower(mh.ctx.Kind)))

// Add imports
log.Infof("adding imports")
err := kbtestutils.InsertCode(controllerPath,
"import (",
importsFragment)
pkg.CheckError("adding imports", err)

// Add RBAC permissions on top of reconcile
log.Infof("adding RBAC permissions on top of reconcile")
err = kbtestutils.InsertCode(controllerPath,
"verbs=get;update;patch",
rbacFragmentV2)
pkg.CheckError("adding rbac", err)

log.Infof("replacing reconcile content")
err = testutils.ReplaceInFile(controllerPath, "_ = context.Background()", "ctx := context.Background()")
pkg.CheckError("replacing reconcile content", err)

err = testutils.ReplaceInFile(controllerPath,
fmt.Sprintf("_ = r.Log.WithValues(\"%s\", req.NamespacedName)", strings.ToLower(mh.ctx.Kind)),
fmt.Sprintf("log := r.Log.WithValues(\"%s\", req.NamespacedName)", strings.ToLower(mh.ctx.Kind)))
pkg.CheckError("replacing reconcile content", err)

// Add reconcile implementation
err = testutils.ReplaceInFile(controllerPath,
"// your logic here", reconcileFragment)
pkg.CheckError("replacing reconcile", err)

// Add helpers funcs to the controller
err = kbtestutils.InsertCode(controllerPath,
"return ctrl.Result{}, nil\n}", controllerFuncsFragment)
pkg.CheckError("adding helpers methods in the controller", err)

// Add watch for the Kind
err = testutils.ReplaceInFile(controllerPath,
fmt.Sprintf(watchOriginalFragment, mh.ctx.Group, mh.ctx.Version, mh.ctx.Kind),
fmt.Sprintf(watchCustomizedFragment, mh.ctx.Group, mh.ctx.Version, mh.ctx.Kind))
pkg.CheckError("replacing reconcile", err)
}

const rbacFragmentV2 = `
// +kubebuilder:rbac:groups=cache.example.com,resources=memcacheds/finalizers,verbs=update
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;`
127 changes: 127 additions & 0 deletions hack/generate/samples/internal/go/generate_v3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright 2020 The Operator-SDK 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 gosamples

import (
"fmt"
"os"
"path/filepath"
"strings"

log "github.com/sirupsen/logrus"
kbtestutils "sigs.k8s.io/kubebuilder/v2/test/e2e/utils"

"github.com/operator-framework/operator-sdk/hack/generate/samples/internal/pkg"
"github.com/operator-framework/operator-sdk/internal/testutils"
)

// RunV2 the steps to create the Memcached with Webhooks Go Sample
func (mh *MemcachedGoWithWebhooks) RunV3() {
log.Infof("creating the project")
err := mh.ctx.Init(
"--plugins", "go/v3-alpha",
"--repo", "github.com/example/memcached-operator",
"--domain",
mh.ctx.Domain)
pkg.CheckError("creating the project", err)

err = mh.ctx.CreateAPI(
"--group", mh.ctx.Group,
"--version", mh.ctx.Version,
"--kind", mh.ctx.Kind,
"--controller", "true",
"--resource", "true")
pkg.CheckError("scaffolding apis", err)

log.Infof("implementing the API")
mh.implementingAPI()

log.Infof("implementing the Controller")
mh.implementingControllerV3()

log.Infof("scaffolding webhook")
err = mh.ctx.CreateWebhook(
"--group", mh.ctx.Group,
"--version", mh.ctx.Version,
"--kind", mh.ctx.Kind,
"--defaulting",
"--defaulting")
pkg.CheckError("scaffolding webhook", err)

mh.implementingWebhooks()
mh.uncommentKustomizationFile()

mh.ctx.CreateBundle()

// Clean up built binaries, if any.
pkg.CheckError("cleaning up", os.RemoveAll(filepath.Join(mh.ctx.Dir, "bin")))
}

// GenerateMemcachedGoWithWebhooksSampleV2 will call all actions to create the directory and generate the sample
// Note that it should NOT be called in the e2e tests.
func GenerateMemcachedGoWithWebhooksSampleV3(samplesPath string) {
log.Infof("starting to generate Go memcached sample with webhooks")
ctx, err := pkg.NewSampleContext(testutils.BinaryName, filepath.Join(samplesPath, "go", "v3", "memcached-operator"), "GO111MODULE=on")
pkg.CheckError("generating Go memcached with webhooks context", err)

memcached := NewMemcachedGoWithWebhooks(&ctx)
memcached.Prepare()
memcached.RunV3()
}

// implementingController will customize the Controller
func (mh *MemcachedGoWithWebhooks) implementingControllerV3() {
controllerPath := filepath.Join(mh.ctx.Dir, "controllers", fmt.Sprintf("%s_controller.go",
strings.ToLower(mh.ctx.Kind)))

// Add imports
log.Infof("adding imports")
err := kbtestutils.InsertCode(controllerPath,
"import (",
importsFragment)
pkg.CheckError("adding imports", err)

// Add RBAC permissions on top of reconcile
log.Infof("adding RBAC permissions on top of reconcile")
err = kbtestutils.InsertCode(controllerPath,
"verbs=get;update;patch",
rbacFragmentV3)
pkg.CheckError("adding rbac", err)

err = testutils.ReplaceInFile(controllerPath,
fmt.Sprintf("_ = r.Log.WithValues(\"%s\", req.NamespacedName)", strings.ToLower(mh.ctx.Kind)),
fmt.Sprintf("log := r.Log.WithValues(\"%s\", req.NamespacedName)", strings.ToLower(mh.ctx.Kind)))
pkg.CheckError("replacing reconcile content", err)

// Add reconcile implementation
err = testutils.ReplaceInFile(controllerPath,
"// your logic here", reconcileFragment)
pkg.CheckError("replacing reconcile", err)

// Add helpers funcs to the controller
err = kbtestutils.InsertCode(controllerPath,
"return ctrl.Result{}, nil\n}", controllerFuncsFragment)
pkg.CheckError("adding helpers methods in the controller", err)

// Add watch for the Kind
err = testutils.ReplaceInFile(controllerPath,
fmt.Sprintf(watchOriginalFragment, mh.ctx.Group, mh.ctx.Version, mh.ctx.Kind),
fmt.Sprintf(watchCustomizedFragment, mh.ctx.Group, mh.ctx.Version, mh.ctx.Kind))
pkg.CheckError("replacing reconcile", err)
}

const rbacFragmentV3 = `
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;`
Loading

0 comments on commit 91055c1

Please sign in to comment.