Skip to content
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
merged 1 commit into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import (
"sigs.k8s.io/kubebuilder/cmd/version"
"sigs.k8s.io/kubebuilder/pkg/cli"
pluginv2 "sigs.k8s.io/kubebuilder/pkg/plugin/v2"
pluginv3 "sigs.k8s.io/kubebuilder/pkg/plugin/v3"
)

func main() {
c, err := cli.New(
cli.WithPlugins(
&pluginv2.Plugin{},
&pluginv3.Plugin{},
),
cli.WithDefaultPlugins(
&pluginv2.Plugin{},
Expand Down
22 changes: 16 additions & 6 deletions generate_testdata.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,26 @@ build_kb() {
# project-version.
#
scaffold_test_project() {
project=$1
version=$2
local project=$1
local version=$2
local plugin=${3:-""}

testdata_dir=$(pwd)/testdata
mkdir -p ./testdata/$project
rm -rf ./testdata/$project/*
pushd .
cd testdata/$project
kb=$testdata_dir/../bin/kubebuilder
oldgopath=$GOPATH
# Set "--plugins $plugin" if $plugin is not null.
local plugin_flag="${plugin:+--plugins $plugin}"

if [ $version == "2" ] || [ $version == "3-alpha" ]; then
if [ $version == "2" ] && [ -n "$plugin_flag" ]; then
echo "--plugins flag may not be set for project versions less than 3"
exit 1
fi

header_text "Starting to generate projects with version $version"
header_text "Generating $project"

Expand All @@ -46,7 +56,7 @@ scaffold_test_project() {
go mod init sigs.k8s.io/kubebuilder/testdata/$project # our repo autodetection will traverse up to the kb module if we don't do this

header_text "initializing $project ..."
$kb init --project-version $version --domain testproject.org --license apache2 --owner "The Kubernetes authors"
$kb init $plugin_flag --project-version $version --domain testproject.org --license apache2 --owner "The Kubernetes authors"

if [ $project == "project-v2" ] || [ $project == "project-v3" ]; then
header_text 'Creating APIs ...'
Expand Down Expand Up @@ -91,6 +101,6 @@ build_kb
scaffold_test_project project-v2 2
scaffold_test_project project-v2-multigroup 2
scaffold_test_project project-v2-addon 2
scaffold_test_project project-v3 3-alpha
scaffold_test_project project-v3-multigroup 3-alpha
scaffold_test_project project-v3-addon 3-alpha
scaffold_test_project project-v3 3-alpha go/v3-alpha
scaffold_test_project project-v3-multigroup 3-alpha go/v3-alpha
scaffold_test_project project-v3-addon 3-alpha go/v3-alpha
213 changes: 213 additions & 0 deletions pkg/plugin/v3/api.go
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"
Copy link
Contributor

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 something

Copy link
Contributor Author

@estroz estroz Jun 11, 2020

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.

Copy link
Member

@camilamacedo86 camilamacedo86 Jun 12, 2020

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?

Copy link
Contributor Author

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.

Copy link
Member

@camilamacedo86 camilamacedo86 Jun 12, 2020

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


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
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
Loading