-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pkg/plugin/v3: add plugin v3-alpha
#1551
Merged
k8s-ci-robot
merged 1 commit into
kubernetes-sigs:master
from
estroz:chore/add-plugin-v3
Jun 16, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,213 @@ | ||
/* | ||
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 v3 | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/spf13/pflag" | ||
|
||
"sigs.k8s.io/kubebuilder/internal/cmdutil" | ||
"sigs.k8s.io/kubebuilder/pkg/model" | ||
"sigs.k8s.io/kubebuilder/pkg/model/config" | ||
"sigs.k8s.io/kubebuilder/pkg/model/resource" | ||
"sigs.k8s.io/kubebuilder/pkg/plugin" | ||
"sigs.k8s.io/kubebuilder/pkg/plugin/internal/util" | ||
"sigs.k8s.io/kubebuilder/pkg/plugin/scaffold" | ||
"sigs.k8s.io/kubebuilder/pkg/plugin/v3/scaffolds" | ||
"sigs.k8s.io/kubebuilder/plugins/addon" | ||
) | ||
|
||
// (used only to gen api with --pattern=addon) | ||
// KbDeclarativePatternVersion is the sigs.k8s.io/kubebuilder-declarative-pattern version | ||
// TODO: remove this when a better solution for using addons is implemented. | ||
const KbDeclarativePatternVersion = "v0.0.0-20200522144838-848d48e5b073" | ||
|
||
type createAPIPlugin struct { | ||
config *config.Config | ||
|
||
// pattern indicates that we should use a plugin to build according to a pattern | ||
pattern string | ||
|
||
resource *resource.Options | ||
|
||
// Check if we have to scaffold resource and/or controller | ||
resourceFlag *pflag.Flag | ||
controllerFlag *pflag.Flag | ||
doResource bool | ||
doController bool | ||
|
||
// force indicates that the resource should be created even if it already exists | ||
force bool | ||
|
||
// runMake indicates whether to run make or not after scaffolding APIs | ||
runMake bool | ||
} | ||
|
||
var ( | ||
_ plugin.CreateAPI = &createAPIPlugin{} | ||
_ cmdutil.RunOptions = &createAPIPlugin{} | ||
) | ||
|
||
func (p createAPIPlugin) UpdateContext(ctx *plugin.Context) { | ||
ctx.Description = `Scaffold a Kubernetes API by creating a Resource definition and / or a Controller. | ||
|
||
create resource will prompt the user for if it should scaffold the Resource and / or Controller. To only | ||
scaffold a Controller for an existing Resource, select "n" for Resource. To only define | ||
the schema for a Resource without writing a Controller, select "n" for Controller. | ||
|
||
After the scaffold is written, api will run make on the project. | ||
` | ||
ctx.Examples = fmt.Sprintf(` # Create a frigates API with Group: ship, Version: v1beta1 and Kind: Frigate | ||
%s create api --group ship --version v1beta1 --kind Frigate | ||
|
||
# Edit the API Scheme | ||
nano api/v1beta1/frigate_types.go | ||
|
||
# Edit the Controller | ||
nano controllers/frigate/frigate_controller.go | ||
|
||
# Edit the Controller Test | ||
nano controllers/frigate/frigate_controller_test.go | ||
|
||
# Install CRDs into the Kubernetes cluster using kubectl apply | ||
make install | ||
|
||
# Regenerate code and run against the Kubernetes cluster configured by ~/.kube/config | ||
make run | ||
`, | ||
ctx.CommandName) | ||
} | ||
|
||
func (p *createAPIPlugin) BindFlags(fs *pflag.FlagSet) { | ||
fs.BoolVar(&p.runMake, "make", true, "if true, run make after generating files") | ||
|
||
fs.BoolVar(&p.doResource, "resource", true, | ||
"if set, generate the resource without prompting the user") | ||
p.resourceFlag = fs.Lookup("resource") | ||
fs.BoolVar(&p.doController, "controller", true, | ||
"if set, generate the controller without prompting the user") | ||
p.controllerFlag = fs.Lookup("controller") | ||
|
||
// TODO: remove this when a better solution for using addons is implemented. | ||
if os.Getenv("KUBEBUILDER_ENABLE_PLUGINS") != "" { | ||
fs.StringVar(&p.pattern, "pattern", "", | ||
"generates an API following an extension pattern (addon)") | ||
} | ||
Comment on lines
+113
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto here |
||
|
||
fs.BoolVar(&p.force, "force", false, | ||
"attempt to create resource even if it already exists") | ||
p.resource = &resource.Options{} | ||
fs.StringVar(&p.resource.Kind, "kind", "", "resource Kind") | ||
fs.StringVar(&p.resource.Group, "group", "", "resource Group") | ||
fs.StringVar(&p.resource.Version, "version", "", "resource Version") | ||
fs.BoolVar(&p.resource.Namespaced, "namespaced", true, "resource is namespaced") | ||
} | ||
|
||
func (p *createAPIPlugin) InjectConfig(c *config.Config) { | ||
p.config = c | ||
} | ||
|
||
func (p *createAPIPlugin) Run() error { | ||
return cmdutil.Run(p) | ||
} | ||
|
||
func (p *createAPIPlugin) Validate() error { | ||
if err := p.resource.Validate(); err != nil { | ||
return err | ||
} | ||
|
||
// TODO: re-evaluate whether y/n input still makes sense. We should probably always | ||
// scaffold the resource and controller. | ||
reader := bufio.NewReader(os.Stdin) | ||
if !p.resourceFlag.Changed { | ||
fmt.Println("Create Resource [y/n]") | ||
p.doResource = util.YesNo(reader) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. at some point we should re-evaluate the yes/no stuff. IIRC we had wanted to do it for v2, but didn't want to break compat |
||
} | ||
if !p.controllerFlag.Changed { | ||
fmt.Println("Create Controller [y/n]") | ||
p.doController = util.YesNo(reader) | ||
} | ||
|
||
// In case we want to scaffold a resource API we need to do some checks | ||
if p.doResource { | ||
// Check that resource doesn't exist or flag force was set | ||
if !p.force && p.config.HasResource(p.resource.GVK()) { | ||
return errors.New("API resource already exists") | ||
} | ||
|
||
// Check that the provided group can be added to the project | ||
if !p.config.MultiGroup && len(p.config.Resources) != 0 && !p.config.HasGroup(p.resource.Group) { | ||
return fmt.Errorf("multiple groups are not allowed by default, " + | ||
"to enable multi-group visit kubebuilder.io/migration/multi-group.html") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (p *createAPIPlugin) GetScaffolder() (scaffold.Scaffolder, error) { | ||
// Load the boilerplate | ||
bp, err := ioutil.ReadFile(filepath.Join("hack", "boilerplate.go.txt")) // nolint:gosec | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to load boilerplate: %v", err) | ||
} | ||
|
||
// Load the requested plugins | ||
plugins := make([]model.Plugin, 0) | ||
switch strings.ToLower(p.pattern) { | ||
case "": | ||
// Default pattern | ||
case "addon": | ||
plugins = append(plugins, &addon.Plugin{}) | ||
default: | ||
return nil, fmt.Errorf("unknown pattern %q", p.pattern) | ||
} | ||
|
||
// Create the actual resource from the resource options | ||
res := p.resource.NewResource(p.config, p.doResource) | ||
return scaffolds.NewAPIScaffolder(p.config, string(bp), res, p.doResource, p.doController, plugins), nil | ||
} | ||
|
||
func (p *createAPIPlugin) PostScaffold() error { | ||
// Load the requested plugins | ||
switch strings.ToLower(p.pattern) { | ||
case "": | ||
// Default pattern | ||
case "addon": | ||
// Ensure that we are pinning sigs.k8s.io/kubebuilder-declarative-pattern version | ||
// TODO: either find a better way to inject this version (ex. tools.go). | ||
err := util.RunCmd("Get kubebuilder-declarative-pattern dependency", "go", "get", | ||
"sigs.k8s.io/kubebuilder-declarative-pattern@"+KbDeclarativePatternVersion) | ||
if err != nil { | ||
return err | ||
} | ||
default: | ||
return fmt.Errorf("unknown pattern %q", p.pattern) | ||
} | ||
|
||
if p.runMake { | ||
return util.RunCmd("Running make", "make") | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need this? I'm assuming going forward we just want
--plugin declarative
or somethingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this attempts to fix a CI issue where the testdata scaffolded with addons enabled will pull the latest kubebuilder-declarative-pattern commit, which messes up
git diff
.We should probably scaffold a
tools.go
containing a pinned version of this.I could also see kustomize and controller-tools versions being pinned this way, which are currently dealt with in the Makefile.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix was done in https://github.com/kubernetes-sigs/kubebuilder/pull/1527/files?
I just n understand why we are changing its implementation here. Should not we just keep the scope to add new plugin version in this PR? I mean, why change it here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't be changed here, just added a TODO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Yep, we have an issue tracked for we create a plugin for that. See: #1543. The solution made was just to fix it until we are able to move forward and provide a plugin for that.
c/c @DirectXMan12