Skip to content

Commit

Permalink
Merge pull request #2106 from camilamacedo86/config-base
Browse files Browse the repository at this point in the history
✨ add the common plugin(s) to allow it to be used by consumers
  • Loading branch information
k8s-ci-robot authored Apr 7, 2021
2 parents 3eb373c + 9e816f5 commit d5e70a5
Show file tree
Hide file tree
Showing 27 changed files with 273 additions and 73 deletions.
23 changes: 17 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,33 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/cli"
cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2"
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
kustomizecommonv1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang"
declarativev1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/declarative/v1"
pluginv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
pluginv3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3"
golangv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
golangv3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3"
)

func main() {

// Bundle plugin which built the golang projects scaffold by Kubebuilder go/v3
gov3Bundle, _ := plugin.NewBundle(golang.DefaultNameQualifier, plugin.Version{Number: 3},
kustomizecommonv1.Plugin{},
golangv3.Plugin{},
)

c, err := cli.New(
cli.WithCommandName("kubebuilder"),
cli.WithVersion(versionString()),
cli.WithPlugins(
&pluginv2.Plugin{},
&pluginv3.Plugin{},
golangv2.Plugin{},
gov3Bundle,
&kustomizecommonv1.Plugin{},
&declarativev1.Plugin{},
),
cli.WithDefaultPlugins(cfgv2.Version, &pluginv2.Plugin{}),
cli.WithDefaultPlugins(cfgv3.Version, &pluginv3.Plugin{}),
cli.WithDefaultPlugins(cfgv2.Version, golangv2.Plugin{}),
cli.WithDefaultPlugins(cfgv3.Version, gov3Bundle),
cli.WithDefaultProjectVersion(cfgv3.Version),
cli.WithCompletion(),
)
Expand Down
105 changes: 105 additions & 0 deletions pkg/plugins/common/kustomize/v1/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Copyright 2021 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 v1

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

"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/internal/validation"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds"
)

var _ plugin.InitSubcommand = &initSubcommand{}

type initSubcommand struct {
config config.Config

// config options
domain string
name string
componentConfig bool
}

func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
subcmdMeta.Description = `Initialize a common project including the following files:
- a "PROJECT" file that stores project configuration
- several YAML files for project deployment under the "config" directory
`
subcmdMeta.Examples = fmt.Sprintf(` # Initialize a common project with your domain and name in copyright
%[1]s init --plugins common/v3 --domain example.org
# Initialize a common project defining an specific project version
%[1]s init --plugins common/v3 --project-version 3
`, cliMeta.CommandName)
}

func (p *initSubcommand) BindFlags(fs *pflag.FlagSet) {
fs.StringVar(&p.domain, "domain", "my.domain", "domain for groups")
fs.StringVar(&p.name, "project-name", "", "name of this project")
fs.BoolVar(&p.componentConfig, "component-config", false,
"create a versioned ComponentConfig file, may be 'true' or 'false'")
}

func (p *initSubcommand) InjectConfig(c config.Config) error {
p.config = c

if err := p.config.SetDomain(p.domain); err != nil {
return err
}

// Assign a default project name
if p.name == "" {
dir, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting current directory: %v", err)
}
p.name = strings.ToLower(filepath.Base(dir))
}
// Check if the project name is a valid k8s namespace (DNS 1123 label).
if err := validation.IsDNS1123Label(p.name); err != nil {
return fmt.Errorf("project name (%s) is invalid: %v", p.name, err)
}
if err := p.config.SetProjectName(p.name); err != nil {
return err
}

if p.componentConfig {
if err := p.config.SetComponentConfig(); err != nil {
return err
}
}

return nil
}

func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {
scaffolder := scaffolds.NewInitScaffolder(p.config)
scaffolder.InjectFS(fs)
if err := scaffolder.Scaffold(); err != nil {
return err
}

return nil
}
50 changes: 50 additions & 0 deletions pkg/plugins/common/kustomize/v1/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2021 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 v1

import (
"sigs.k8s.io/kubebuilder/v3/pkg/config"
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
)

const pluginName = "kustomize.common." + plugins.DefaultNameQualifier

var (
pluginVersion = plugin.Version{Number: 1}
supportedProjectVersions = []config.Version{cfgv3.Version}
)

var _ plugin.Init = Plugin{}

// Plugin implements the plugin.Full interface
type Plugin struct {
initSubcommand
}

// Name returns the name of the plugin
func (Plugin) Name() string { return pluginName }

// Version returns the version of the plugin
func (Plugin) Version() plugin.Version { return pluginVersion }

// SupportedProjectVersions returns an array with all project versions supported by the plugin
func (Plugin) SupportedProjectVersions() []config.Version { return supportedProjectVersions }

// GetInitSubcommand will return the subcommand which is responsible for scaffolding init project
func (p Plugin) GetInitSubcommand() plugin.InitSubcommand { return &p.initSubcommand }
84 changes: 84 additions & 0 deletions pkg/plugins/common/kustomize/v1/scaffolds/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2021 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 scaffolds

import (
"fmt"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/kdefault"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/manager"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/prometheus"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/rbac"
)

const (
imageName = "controller:latest"
)

var _ plugins.Scaffolder = &initScaffolder{}

type initScaffolder struct {
config config.Config

// fs is the filesystem that will be used by the scaffolder
fs machinery.Filesystem
}

// NewInitScaffolder returns a new Scaffolder for project initialization operations
func NewInitScaffolder(config config.Config) plugins.Scaffolder {
return &initScaffolder{
config: config,
}
}

// InjectFS implements cmdutil.Scaffolder
func (s *initScaffolder) InjectFS(fs machinery.Filesystem) {
s.fs = fs
}

// Scaffold implements cmdutil.Scaffolder
func (s *initScaffolder) Scaffold() error {
fmt.Println("Writing scaffold for you to edit...")

// Initialize the machinery.Scaffold that will write the files to disk
scaffold := machinery.NewScaffold(s.fs,
machinery.WithConfig(s.config),
)

return scaffold.Execute(
&rbac.Kustomization{},
&rbac.AuthProxyRole{},
&rbac.AuthProxyRoleBinding{},
&rbac.AuthProxyService{},
&rbac.AuthProxyClientRole{},
&rbac.RoleBinding{},
&rbac.LeaderElectionRole{},
&rbac.LeaderElectionRoleBinding{},
&rbac.ServiceAccount{},
&manager.Kustomization{},
&manager.Config{Image: imageName},
&manager.ControllerManagerConfig{},
&kdefault.Kustomization{},
&kdefault.ManagerAuthProxyPatch{},
&kdefault.ManagerConfigPatch{},
&prometheus.Kustomization{},
&prometheus.Monitor{},
)
}
11 changes: 7 additions & 4 deletions pkg/plugins/golang/declarative/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/declarative/v1/internal/templates"
goPluginV3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3"
goPluginV2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
)

const (
Expand Down Expand Up @@ -134,10 +134,13 @@ func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
}

// Ensure that we are pinning sigs.k8s.io/kubebuilder-declarative-pattern version
kbDeclarativePattern := kbDeclarativePatternForV2
// Just pin an old value for go/v2. It shows fine for now. However, we should improve/change it
// if we see that more rules based on the plugins version are required.
kbDeclarativePattern := kbDeclarativePatternForV3
for _, pluginKey := range p.config.GetPluginChain() {
if pluginKey == plugin.KeyFor(goPluginV3.Plugin{}) {
kbDeclarativePattern = kbDeclarativePatternForV3
if pluginKey == plugin.KeyFor(goPluginV2.Plugin{}) {
kbDeclarativePattern = kbDeclarativePatternForV2
break
}
}
err = util.RunCmd("Get declarative pattern", "go", "get",
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugins/golang/declarative/v1/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang"
)

const pluginName = "declarative." + golang.DefaultGoNameQualifier
const pluginName = "declarative." + golang.DefaultNameQualifier

var (
pluginVersion = plugin.Version{Number: 1}
Expand Down
7 changes: 5 additions & 2 deletions pkg/plugins/golang/domain.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/*
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.
Expand All @@ -15,5 +18,5 @@ package golang

import "sigs.k8s.io/kubebuilder/v3/pkg/plugins"

// DefaultGoNameQualifier is the suffix appended to all kubebuilder plugin names for Golang operators.
const DefaultGoNameQualifier = "go." + plugins.DefaultNameQualifier
// DefaultNameQualifier is the suffix appended to all kubebuilder plugin names for Golang operators.
const DefaultNameQualifier = "go." + plugins.DefaultNameQualifier
Loading

0 comments on commit d5e70a5

Please sign in to comment.