Skip to content

Commit

Permalink
*: upgrade kubebuilder and add go/v3-alpha to the list of available
Browse files Browse the repository at this point in the history
plugins

testdata: regenerate samples with go/v3-alpha

test: test go/v3-alpha
  • Loading branch information
estroz committed Dec 4, 2020
1 parent a9c8051 commit f8e26b5
Show file tree
Hide file tree
Showing 62 changed files with 1,220 additions and 529 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ require (
rsc.io/letsencrypt v0.0.3 // indirect
sigs.k8s.io/controller-runtime v0.6.3
sigs.k8s.io/controller-tools v0.3.0
sigs.k8s.io/kubebuilder/v2 v2.3.2-0.20201111001842-c158f4fa4207
sigs.k8s.io/kubebuilder/v2 v2.3.2-0.20201204182959-6b775b860148
sigs.k8s.io/yaml v1.2.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1229,8 +1229,8 @@ sigs.k8s.io/controller-runtime v0.6.3 h1:SBbr+inLPEKhvlJtrvDcwIpm+uhDvp63Bl72xYJ
sigs.k8s.io/controller-runtime v0.6.3/go.mod h1:WlZNXcM0++oyaQt4B7C2lEE5JYRs8vJUzRP4N4JpdAY=
sigs.k8s.io/controller-tools v0.3.0 h1:y3YD99XOyWaXkiF1kd41uRvfp/64teWcrEZFuHxPhJ4=
sigs.k8s.io/controller-tools v0.3.0/go.mod h1:enhtKGfxZD1GFEoMgP8Fdbu+uKQ/cq1/WGJhdVChfvI=
sigs.k8s.io/kubebuilder/v2 v2.3.2-0.20201111001842-c158f4fa4207 h1:JtlaBrtWymJbh6ea+TJUZkzBb0x0dMKV+P0+ZPaldxk=
sigs.k8s.io/kubebuilder/v2 v2.3.2-0.20201111001842-c158f4fa4207/go.mod h1:J/D/179LBZhQOhRvmMRNbje/Bk+PjbN0/fzUupmO7+U=
sigs.k8s.io/kubebuilder/v2 v2.3.2-0.20201204182959-6b775b860148 h1:Wcp7eE1mnaXcmI2ltDoAaCKmX1cE4sGkd70JNQ49gBY=
sigs.k8s.io/kubebuilder/v2 v2.3.2-0.20201204182959-6b775b860148/go.mod h1:J/D/179LBZhQOhRvmMRNbje/Bk+PjbN0/fzUupmO7+U=
sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0=
sigs.k8s.io/kustomize v2.0.3+incompatible/go.mod h1:MkjgH3RdOWrievjo6c9T245dYlB5QeXV4WCbnt/PEpU=
sigs.k8s.io/structured-merge-diff/v3 v3.0.0-20200116222232-67a7b8c61874/go.mod h1:PlARxl6Hbt/+BC80dRLi1qAmnMqwqDg62YvvVkZjemw=
Expand Down
21 changes: 13 additions & 8 deletions hack/generate/samples/internal/go/memcached_with_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package gosamples
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

Expand Down Expand Up @@ -59,6 +60,8 @@ func (mh *MemcachedGoWithWebhooks) Prepare() {
func (mh *MemcachedGoWithWebhooks) Run() {
log.Infof("creating the project")
err := mh.ctx.Init(
// TODO(estroz): remove this once go/v3-alpha is stabilized and the default plugin.
"--plugins", "go/v3-alpha",
"--repo", "github.com/example/memcached-operator",
"--domain",
mh.ctx.Domain)
Expand Down Expand Up @@ -92,6 +95,9 @@ func (mh *MemcachedGoWithWebhooks) Run() {

mh.ctx.CreateBundle()

_, err = mh.ctx.Run(exec.Command("go", "fmt", "./..."))
pkg.CheckError("formatting project", err)

// Clean up built binaries, if any.
pkg.CheckError("cleaning up", os.RemoveAll(filepath.Join(mh.ctx.Dir, "bin")))
}
Expand Down Expand Up @@ -129,15 +135,15 @@ func (mh *MemcachedGoWithWebhooks) uncommentKustomizationFile() {
# objref:
# kind: Certificate
# group: cert-manager.io
# version: v1alpha2
# 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: v1alpha2
# version: v1
# name: serving-cert # this name should match the one in certificate.yaml
#- name: SERVICE_NAMESPACE # namespace of the service
# objref:
Expand Down Expand Up @@ -173,7 +179,8 @@ func (mh *MemcachedGoWithWebhooks) implementingWebhooks() {
// Add imports
err = kbtestutils.InsertCode(webhookPath,
"import (",
"\"errors\"")
// TODO(estroz): remove runtime dep when --programmatic-validation is added to `ccreate webhook` above.
"\"errors\"\n\n\"k8s.io/apimachinery/pkg/runtime\"")
pkg.CheckError("adding imports", err)
}

Expand All @@ -195,9 +202,6 @@ func (mh *MemcachedGoWithWebhooks) implementingController() {
pkg.CheckError("adding rbac", err)

// Replace 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)))
Expand Down Expand Up @@ -410,8 +414,9 @@ const watchCustomizedFragment = `return ctrl.NewControllerManagedBy(mgr).
Complete(r)
`

const webhooksFragment = `// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// +kubebuilder:webhook:verbs=create;update,path=/validate-cache-example-com-v1alpha1-memcached,mutating=false,failurePolicy=fail,groups=cache.example.com,resources=memcacheds,versions=v1alpha1,name=vmemcached.kb.io
const webhooksFragment = `
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// +kubebuilder:webhook:path=/validate-cache-example-com-v1alpha1-memcached,mutating=false,failurePolicy=fail,sideEffects=None,groups=cache.example.com,resources=memcacheds,verbs=create;update,versions=v1alpha1,name=vmemcached.kb.io,admissionReviewVersions={v1beta1}
var _ webhook.Validator = &Memcached{}
Expand Down
18 changes: 1 addition & 17 deletions hack/generate/samples/internal/pkg/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
package pkg

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

"github.com/operator-framework/operator-sdk/internal/testutils"
)

Expand All @@ -29,18 +25,6 @@ type SampleContext struct {

// NewSampleContext returns a SampleContext containing a new kubebuilder TestContext.
func NewSampleContext(binary string, path string, env ...string) (s SampleContext, err error) {
s.TestContext, err = testutils.NewTestContext(binary, env...)
// If the path was informed then this should be the dir used
if strings.TrimSpace(path) != "" {
path, err = filepath.Abs(path)
if err != nil {
return s, err
}
s.CmdContext.Dir = path
s.ProjectName = strings.ToLower(filepath.Base(s.Dir))
s.ImageName = fmt.Sprintf("quay.io/example/%s:v0.0.1", s.ProjectName)
s.BundleImageName = fmt.Sprintf("quay.io/example/%s-bundle:v0.0.1", s.ProjectName)
}

s.TestContext, err = testutils.NewPartialTestContext(binary, path, env...)
return s, err
}
6 changes: 4 additions & 2 deletions internal/cmd/operator-sdk/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"github.com/operator-framework/operator-sdk/internal/cmd/operator-sdk/olm"
"github.com/operator-framework/operator-sdk/internal/cmd/operator-sdk/run"
"github.com/operator-framework/operator-sdk/internal/cmd/operator-sdk/scorecard"
"github.com/operator-framework/operator-sdk/internal/cmd/operator-sdk/version"
"github.com/operator-framework/operator-sdk/internal/flags"
ansiblev1 "github.com/operator-framework/operator-sdk/internal/plugins/ansible/v1"
golangv2 "github.com/operator-framework/operator-sdk/internal/plugins/golang/v2"
golangv3 "github.com/operator-framework/operator-sdk/internal/plugins/golang/v3"
helmv1 "github.com/operator-framework/operator-sdk/internal/plugins/helm/v1"
"github.com/operator-framework/operator-sdk/internal/util/projutil"

Expand All @@ -43,7 +43,6 @@ var commands = []*cobra.Command{
olm.NewCmd(),
run.NewCmd(),
scorecard.NewCmd(),
version.NewCmd(),
}

func Run() error {
Expand All @@ -57,12 +56,15 @@ func Run() error {
func GetPluginsCLIAndRoot() (cli.CLI, *cobra.Command) {
c, err := cli.New(
cli.WithCommandName("operator-sdk"),
cli.WithVersion(makeVersionString()),
cli.WithPlugins(
&golangv2.Plugin{},
&golangv3.Plugin{},
&helmv1.Plugin{},
&ansiblev1.Plugin{},
),
cli.WithDefaultPlugins(
// TODO(estroz): make go/v3-alpha plugin the default once stabilized.
&golangv2.Plugin{},
),
cli.WithExtraCommands(commands...),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package version_test
package cli

import (
"testing"
Expand All @@ -23,5 +23,5 @@ import (

func TestVersion(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Version Cmd Suite")
RunSpecs(t, "CLI Suite")
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package version
package cli

import (
"fmt"
"runtime"

"github.com/spf13/cobra"

ver "github.com/operator-framework/operator-sdk/internal/version"
)

func NewCmd() *cobra.Command {
versionCmd := &cobra.Command{
Use: "version",
Short: "Prints the version of operator-sdk",
Run: func(cmd *cobra.Command, args []string) {
run()
},
}
return versionCmd
}

func run() {
fmt.Printf("operator-sdk version: %q, commit: %q, kubernetes version: %q, go version: %q, GOOS: %q, GOARCH: %q\n",
func makeVersionString() string {
return fmt.Sprintf("operator-sdk version: %q, commit: %q, kubernetes version: %q, go version: %q, GOOS: %q, GOARCH: %q",
ver.GitVersion, ver.GitCommit, ver.KubernetesVersion, runtime.Version(), runtime.GOOS, runtime.GOARCH)
}
41 changes: 41 additions & 0 deletions internal/cmd/operator-sdk/cli/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 cli

import (
"fmt"
"runtime"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

ver "github.com/operator-framework/operator-sdk/internal/version"
)

var _ = Describe("printVersion", func() {
It("prints the correct version info", func() {
expVersion := makeVersionString()
version := ver.GitVersion
if version == "unknown" {
version = ver.Version
}
Expect(expVersion).To(ContainSubstring(fmt.Sprintf("version: %q", version)))
Expect(expVersion).To(ContainSubstring(fmt.Sprintf("commit: %q", ver.GitCommit)))
Expect(expVersion).To(ContainSubstring(fmt.Sprintf("kubernetes version: %q", ver.KubernetesVersion)))
Expect(expVersion).To(ContainSubstring(fmt.Sprintf("go version: %q", runtime.Version())))
Expect(expVersion).To(ContainSubstring(fmt.Sprintf("GOOS: %q", runtime.GOOS)))
Expect(expVersion).To(ContainSubstring(fmt.Sprintf("GOARCH: %q", runtime.GOARCH)))
})
})
65 changes: 0 additions & 65 deletions internal/cmd/operator-sdk/version/cmd_test.go

This file was deleted.

8 changes: 6 additions & 2 deletions internal/kubebuilder/cmdutil/cmdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ limitations under the License.

package cmdutil

import "sigs.k8s.io/kubebuilder/v2/pkg/plugin/scaffold"
// Scaffolder interface creates files to set up a controller manager
type Scaffolder interface {
// Scaffold performs the scaffolding
Scaffold() error
}

// RunOptions represent the types used to implement the different commands
type RunOptions interface {
// - Step 1: verify that the command can be run (e.g., go version, project version, arguments, ...)
Validate() error
// - Step 2: create the Scaffolder instance
GetScaffolder() (scaffold.Scaffolder, error)
GetScaffolder() (Scaffolder, error)
// - Step 3: call the Scaffold method of the Scaffolder instance. Doesn't need any method
// - Step 4: finish the command execution
PostScaffold() error
Expand Down
3 changes: 1 addition & 2 deletions internal/plugins/ansible/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"sigs.k8s.io/kubebuilder/v2/pkg/model/config"
"sigs.k8s.io/kubebuilder/v2/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v2/pkg/plugin"
"sigs.k8s.io/kubebuilder/v2/pkg/plugin/scaffold"

"github.com/operator-framework/operator-sdk/internal/kubebuilder/cmdutil"
"github.com/operator-framework/operator-sdk/internal/plugins/ansible/v1/scaffolds"
Expand Down Expand Up @@ -152,7 +151,7 @@ func (p *createAPIPSubcommand) Validate() error {
return nil
}

func (p *createAPIPSubcommand) GetScaffolder() (scaffold.Scaffolder, error) {
func (p *createAPIPSubcommand) GetScaffolder() (cmdutil.Scaffolder, error) {
return scaffolds.NewCreateAPIScaffolder(p.config, p.createOptions), nil
}

Expand Down
Loading

0 comments on commit f8e26b5

Please sign in to comment.