-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Camila Macedo
committed
Mar 31, 2022
1 parent
657e296
commit 51bdad6
Showing
40 changed files
with
2,730 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
Copyright 2022 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 v2 | ||
|
||
import ( | ||
"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.CreateAPISubcommand = &createAPISubcommand{} | ||
|
||
type createAPISubcommand struct { | ||
createSubcommand | ||
} | ||
|
||
func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error { | ||
if err := p.configure(); err != nil { | ||
return err | ||
} | ||
scaffolder := scaffolds.NewAPIScaffolder(p.config, *p.resource, p.force) | ||
scaffolder.InjectFS(fs) | ||
return scaffolder.Scaffold() | ||
} |
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,58 @@ | ||
/* | ||
Copyright 2022 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 v2 | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/spf13/pflag" | ||
|
||
"sigs.k8s.io/kubebuilder/v3/pkg/config" | ||
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource" | ||
) | ||
|
||
type createSubcommand struct { | ||
config config.Config | ||
resource *resource.Resource | ||
|
||
flagSet *pflag.FlagSet | ||
|
||
// force indicates whether to scaffold files even if they exist. | ||
force bool | ||
} | ||
|
||
func (p *createSubcommand) BindFlags(fs *pflag.FlagSet) { p.flagSet = fs } | ||
|
||
func (p *createSubcommand) InjectConfig(c config.Config) error { | ||
p.config = c | ||
return nil | ||
} | ||
|
||
func (p *createSubcommand) InjectResource(res *resource.Resource) error { | ||
p.resource = res | ||
return nil | ||
} | ||
|
||
func (p *createSubcommand) configure() (err error) { | ||
if forceFlag := p.flagSet.Lookup("force"); forceFlag != nil { | ||
if p.force, err = strconv.ParseBool(forceFlag.Value.String()); 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,101 @@ | ||
/* | ||
Copyright 2022 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 v2 | ||
|
||
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 a 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) | ||
return scaffolder.Scaffold() | ||
} |
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,65 @@ | ||
/* | ||
Copyright 2022 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 v2 | ||
|
||
import ( | ||
"sigs.k8s.io/kubebuilder/v3/pkg/config" | ||
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3" | ||
"sigs.k8s.io/kubebuilder/v3/pkg/model/stage" | ||
"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: 2, Stage: stage.Alpha} | ||
supportedProjectVersions = []config.Version{cfgv3.Version} | ||
) | ||
|
||
var ( | ||
_ plugin.Init = Plugin{} | ||
_ plugin.CreateAPI = Plugin{} | ||
_ plugin.CreateWebhook = Plugin{} | ||
) | ||
|
||
// Plugin implements the plugin.Full interface | ||
type Plugin struct { | ||
initSubcommand | ||
createAPISubcommand | ||
createWebhookSubcommand | ||
} | ||
|
||
// 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 } | ||
|
||
// GetCreateAPISubcommand will return the subcommand which is responsible for scaffolding apis | ||
func (p Plugin) GetCreateAPISubcommand() plugin.CreateAPISubcommand { return &p.createAPISubcommand } | ||
|
||
// GetCreateWebhookSubcommand will return the subcommand which is responsible for scaffolding webhooks | ||
func (p Plugin) GetCreateWebhookSubcommand() plugin.CreateWebhookSubcommand { | ||
return &p.createWebhookSubcommand | ||
} |
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,87 @@ | ||
/* | ||
Copyright 2022 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/model/resource" | ||
"sigs.k8s.io/kubebuilder/v3/pkg/plugins" | ||
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/crd" | ||
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/crd/patches" | ||
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/rbac" | ||
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/kustomize/v1/scaffolds/internal/templates/config/samples" | ||
) | ||
|
||
var _ plugins.Scaffolder = &apiScaffolder{} | ||
|
||
// apiScaffolder contains configuration for generating scaffolding for Go type | ||
// representing the API and controller that implements the behavior for the API. | ||
type apiScaffolder struct { | ||
config config.Config | ||
resource resource.Resource | ||
|
||
// fs is the filesystem that will be used by the scaffolder | ||
fs machinery.Filesystem | ||
|
||
// force indicates whether to scaffold files even if they exist. | ||
force bool | ||
} | ||
|
||
// NewAPIScaffolder returns a new Scaffolder for API/controller creation operations | ||
func NewAPIScaffolder(config config.Config, res resource.Resource, force bool) plugins.Scaffolder { | ||
return &apiScaffolder{ | ||
config: config, | ||
resource: res, | ||
force: force, | ||
} | ||
} | ||
|
||
// InjectFS implements cmdutil.Scaffolder | ||
func (s *apiScaffolder) InjectFS(fs machinery.Filesystem) { | ||
s.fs = fs | ||
} | ||
|
||
// Scaffold implements cmdutil.Scaffolder | ||
func (s *apiScaffolder) Scaffold() error { | ||
fmt.Println("Writing kustomize manifests for you to edit...") | ||
|
||
// Initialize the machinery.Scaffold that will write the files to disk | ||
scaffold := machinery.NewScaffold(s.fs, | ||
machinery.WithConfig(s.config), | ||
machinery.WithResource(&s.resource), | ||
) | ||
|
||
// Keep track of these values before the update | ||
if s.resource.HasAPI() { | ||
if err := scaffold.Execute( | ||
&samples.CRDSample{Force: s.force}, | ||
&rbac.CRDEditorRole{}, | ||
&rbac.CRDViewerRole{}, | ||
&patches.EnableWebhookPatch{}, | ||
&patches.EnableCAInjectionPatch{}, | ||
&crd.Kustomization{}, | ||
&crd.KustomizeConfig{}, | ||
); err != nil { | ||
return fmt.Errorf("error scaffolding kustomize API manifests: %v", err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.