Skip to content

Commit

Permalink
create base plugin to allow which be used by consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Mar 25, 2021
1 parent d7bd9c7 commit 09a4b09
Show file tree
Hide file tree
Showing 43 changed files with 350 additions and 164 deletions.
22 changes: 16 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,32 @@ 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"
basev1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/base/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"
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(golangv3.Plugin{}.Name(), golangv3.Plugin{}.Version(),
basev1.Plugin{},
golangv3.Plugin{},
)

c, err := cli.New(
cli.WithCommandName("kubebuilder"),
cli.WithVersion(versionString()),
cli.WithPlugins(
&pluginv2.Plugin{},
&pluginv3.Plugin{},
&golangv2.Plugin{},
gov3Bundle,
&basev1.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
121 changes: 121 additions & 0 deletions pkg/plugins/base/v1/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
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/base/v1/scaffolds"
)

var _ plugin.InitSubcommand = &initSubcommand{}

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

// boilerplate options
license string
owner 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 base 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 new project with your domain and name in copyright
%[1]s init --plugins go/v3 --domain example.org --owner "Your name"
# Initialize a new 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'")

// boilerplate args
fs.StringVar(&p.license, "license", "apache2",
"license to use to boilerplate, may be one of 'apache2', 'none'")
fs.StringVar(&p.owner, "owner", "", "owner to add to the copyright")

}

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, p.license, p.owner)
scaffolder.InjectFS(fs)
err := scaffolder.Scaffold()
if err != nil {
return err
}

return nil
}
50 changes: 50 additions & 0 deletions pkg/plugins/base/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 = "base" + 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 }
115 changes: 115 additions & 0 deletions pkg/plugins/base/v1/scaffolds/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
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"

"github.com/spf13/afero"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/base/v1/scaffolds/internal/templates/hack"

"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/base/v1/scaffolds/internal/templates/config/kdefault"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/base/v1/scaffolds/internal/templates/config/manager"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/base/v1/scaffolds/internal/templates/config/prometheus"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/base/v1/scaffolds/internal/templates/config/rbac"
)

const (
imageName = "controller:latest"
)

var _ plugins.Scaffolder = &initScaffolder{}

type initScaffolder struct {
config config.Config
boilerplatePath string
license string
owner string

// 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, license, owner string) plugins.Scaffolder {
return &initScaffolder{
config: config,
boilerplatePath: hack.DefaultBoilerplatePath,
license: license,
owner: owner,
}
}

// 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 boilerplate file to disk
// The boilerplate file needs to be scaffolded as a separate step as it is going to
// be used by the rest of the files, even those scaffolded in this command call.
scaffold := machinery.NewScaffold(s.fs,
machinery.WithConfig(s.config),
)

bpFile := &hack.Boilerplate{
License: s.license,
Owner: s.owner,
}
bpFile.Path = s.boilerplatePath
if err := scaffold.Execute(bpFile); err != nil {
return err
}

boilerplate, err := afero.ReadFile(s.fs.FS, s.boilerplatePath)
if err != nil {
return err
}

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

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{},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
"path/filepath"
"time"

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

"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
)

// DefaultBoilerplatePath is the default path to the boilerplate file
var DefaultBoilerplatePath = filepath.Join("hack", "boilerplate.go.txt")
var DefaultBoilerplatePath = filepath.Join(plugins.DefaultBoilerplatePath)

var _ machinery.Template = &Boilerplate{}

Expand Down
9 changes: 7 additions & 2 deletions pkg/plugins/declarative/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"
"path/filepath"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins"

"github.com/spf13/afero"

Expand Down Expand Up @@ -95,9 +96,13 @@ func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
fmt.Println("updating scaffold with declarative pattern...")

// Load the boilerplate
bp, err := afero.ReadFile(fs.FS, filepath.Join("hack", "boilerplate.go.txt"))
bp, err := afero.ReadFile(fs.FS, plugins.DefaultBoilerplatePath)
if err != nil {
return fmt.Errorf("error updating scaffold: unable to load boilerplate: %w", err)
// To ensure the backwards compatibility with v2
bp, err = afero.ReadFile(fs.FS, filepath.Join("hack", "boilerplate.go.txt"))
if err != nil {
return fmt.Errorf("error updating scaffold: unable to load boilerplate: %w", err)
}
}
boilerplate := string(bp)

Expand Down
3 changes: 3 additions & 0 deletions pkg/plugins/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ package plugins

// DefaultNameQualifier is the suffix appended to all kubebuilder plugin names.
const DefaultNameQualifier = ".kubebuilder.io"

// DefaultBoilerplatePath is the default path to the boilerplate file for the plugins
const DefaultBoilerplatePath = "hack/boilerplate.txt"
Loading

0 comments on commit 09a4b09

Please sign in to comment.