Skip to content

Commit

Permalink
Merge pull request #1551 from estroz/chore/add-plugin-v3
Browse files Browse the repository at this point in the history
pkg/plugin/v3: add plugin `v3-alpha`
  • Loading branch information
k8s-ci-robot committed Jun 16, 2020
2 parents 703e5f6 + 3eba593 commit 26e468a
Show file tree
Hide file tree
Showing 54 changed files with 4,147 additions and 9 deletions.
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"

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)")
}

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)
}
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

0 comments on commit 26e468a

Please sign in to comment.