Skip to content

Commit

Permalink
add common base plugin to allow consumers use that
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Mar 25, 2021
1 parent d7bd9c7 commit 583baad
Show file tree
Hide file tree
Showing 42 changed files with 308 additions and 132 deletions.
24 changes: 17 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,36 @@ package main

import (
"log"

"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"
commonv1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/v1"
declarativev1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/declarative/v1"
pluginv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
pluginv3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang"
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
gov3Bundle, _ := plugin.NewBundle(golang.DefaultGoNameQualifier, plugin.Version{Number: 3},
commonv1.Plugin{},
golangv3.Plugin{},
)

c, err := cli.New(
cli.WithCommandName("kubebuilder"),
cli.WithVersion(versionString()),
cli.WithPlugins(
&pluginv2.Plugin{},
&pluginv3.Plugin{},
&golangv2.Plugin{},
gov3Bundle,
&commonv1.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
2 changes: 1 addition & 1 deletion pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (c *CLI) getInfoFromConfig(projectConfig config.Config) error {
// getInfoFromFlags obtains the project version and plugin keys from flags.
func (c *CLI) getInfoFromFlags(hasConfigFile bool) error {
// Partially parse the command line arguments
fs := pflag.NewFlagSet("base", pflag.ContinueOnError)
fs := pflag.NewFlagSet("common", pflag.ContinueOnError)

// Load the base command global flags
fs.AddFlagSet(c.cmd.PersistentFlags())
Expand Down
111 changes: 111 additions & 0 deletions pkg/plugins/common/v1/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
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 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/v1/scaffolds"
)

var _ plugin.InitSubcommand = &initSubcommand{}

type initSubcommand struct {
config config.Config
// For help text.
commandName string

// config options
domain string
name string
componentConfig bool
}

func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
p.commandName = cliMeta.CommandName

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 go/v3 --domain example.org
# Initialize a common project defining an specific project version
%[1]s init --plugins go/v3 --project-version 3
`, cliMeta.CommandName)
}

func (p *initSubcommand) BindFlags(fs *pflag.FlagSet) {
// project args
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)
err := scaffolder.Scaffold()
if err != nil {
return err
}

return nil
}
50 changes: 50 additions & 0 deletions pkg/plugins/common/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 = "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/v1/scaffolds/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
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 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/v1/scaffolds/internal/templates/config/kdefault"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/v1/scaffolds/internal/templates/config/manager"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/v1/scaffolds/internal/templates/config/prometheus"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/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{},
)
}
22 changes: 22 additions & 0 deletions pkg/plugins/golang/domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
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 golang

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

// DefaultGoNameQualifier is the suffix appended to all kubebuilder plugin names.
const DefaultGoNameQualifier = "go" + plugins.DefaultNameQualifier
4 changes: 2 additions & 2 deletions pkg/plugins/golang/v2/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
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"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang"
)

const pluginName = "go" + plugins.DefaultNameQualifier
const pluginName = golang.DefaultGoNameQualifier

var (
pluginVersion = plugin.Version{Number: 2}
Expand Down
Loading

0 comments on commit 583baad

Please sign in to comment.