-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add common base plugin to allow consumers use that
- Loading branch information
1 parent
d7bd9c7
commit 583baad
Showing
42 changed files
with
308 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}, | ||
) | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.