Skip to content

Commit

Permalink
clean code - remove V1 implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Feb 5, 2020
1 parent 00875c7 commit 6fafcb9
Show file tree
Hide file tree
Showing 175 changed files with 79 additions and 10,337 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ go_import_path: sigs.k8s.io/kubebuilder
services: docker

# Install must be set to prevent default `go get` to run.
# The dependencies have already been vendored by `dep` so
# we don't need to fetch them.
install: skip

before_script: PATH=$PATH:$(pwd)
Expand Down
5 changes: 2 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ Please see https://git.k8s.io/community/CLA.md for more info.

## Prerequisites
- [go](https://golang.org/dl/) version v1.13+.
- [dep](https://github.com/golang/dep) dep v0.5+
- [docker](https://docs.docker.com/install/) version 17.03+.
- [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) version v1.11.3+.
- [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) version v1.14.1+.
- [kustomize](https://sigs.k8s.io/kustomize/docs/INSTALL.md) v3.1.0+
- Access to a Kubernetes v1.11.3+ cluster.
- Access to a Kubernetes v1.14.1+ cluster.

## Contributing steps
1. Submit an issue describing your proposed change to the repo in question.
Expand Down
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ generate: ## Update/generate all mock data. You should run this commands to upda
generate-testdata: ## Update/generate the testdata in $GOPATH/src/sigs.k8s.io/kubebuilder
GO111MODULE=on ./generate_golden.sh

.PHONY: generate-vendor
generate-vendor: ## (Deprecated) Update/generate the vendor by using the path $GOPATH/src/sigs.k8s.io/kubebuilder-test
GO111MODULE=off ./generate_vendor.sh

.PHONY: generate-setup
generate-setup: ## Current workarround to generate the testdata with the correct controller-gen version
- rm -rf $(CONTROLLER_GEN_BIN_PATH)
Expand Down
53 changes: 3 additions & 50 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@ limitations under the License.
package main

import (
"bufio"
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/cmd/internal"
"sigs.k8s.io/kubebuilder/internal/config"
"sigs.k8s.io/kubebuilder/pkg/scaffold"
Expand All @@ -48,7 +44,7 @@ func newInitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initialize a new project",
Long: `Initialize a new project including vendor/ directory and Go package directories.
Long: `Initialize a new project including Go package directories.
Writes the following files:
- a boilerplate license file
Expand All @@ -59,8 +55,6 @@ Writes the following files:
- a Patch file for customizing image for manager manifests
- a Patch file for enabling prometheus metrics
- a cmd/manager/main.go to run
project will prompt the user to run 'dep ensure' after writing the project files.
`,
Example: `# Scaffold a project using the apache2 license with "The Kubernetes authors" as owners
kubebuilder init --domain example.org --license apache2 --owner "The Kubernetes authors"
Expand All @@ -86,11 +80,6 @@ type initOptions struct {
license string
owner string

// deprecated flags
depFlag *flag.Flag
depArgs []string
dep bool

// flags
fetchDeps bool
skipGoVersionCheck bool
Expand All @@ -103,18 +92,6 @@ func (o *initOptions) bindFlags(cmd *cobra.Command) {
// dependency args
cmd.Flags().BoolVar(&o.fetchDeps, "fetch-deps", true, "ensure dependencies are downloaded")

// deprecated dependency args
cmd.Flags().BoolVar(&o.dep, "dep", true, "if specified, determines whether dep will be used.")
o.depFlag = cmd.Flag("dep")
cmd.Flags().StringArrayVar(&o.depArgs, "depArgs", nil, "additional arguments for dep")

if err := cmd.Flags().MarkDeprecated("dep", "use the fetch-deps flag instead"); err != nil {
log.Printf("error to mark dep flag as deprecated: %v", err)
}
if err := cmd.Flags().MarkDeprecated("depArgs", "will be removed with version 1 scaffolding"); err != nil {
log.Printf("error to mark dep flag as deprecated: %v", err)
}

// boilerplate args
cmd.Flags().StringVar(&o.license, "license", "apache2",
"license to use to boilerplate, may be one of 'apache2', 'none'")
Expand Down Expand Up @@ -164,18 +141,6 @@ func (o *initOptions) validate(c *config.Config) error {
c.Repo = repoPath
}

// v1 only checks
if c.IsV1() {
// v1 is deprecated
internal.PrintV1DeprecationWarning()

// Verify dep is installed
if _, err := exec.LookPath("dep"); err != nil {
return fmt.Errorf("dep is not installed: %v\n"+
"Follow steps at: https://golang.github.io/dep/docs/installation.html", err)
}
}

return nil
}

Expand All @@ -186,20 +151,8 @@ func (o *initOptions) scaffolder(c *config.Config) (scaffold.Scaffolder, error)
func (o *initOptions) postScaffold(c *config.Config) error {
switch {
case c.IsV1():
if !o.depFlag.Changed {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Run `dep ensure` to fetch dependencies (Recommended) [y/n]?")
o.dep = internal.YesNo(reader)
}
if !o.dep {
fmt.Println("Skipping fetching dependencies.")
return nil
}

err := internal.RunCmd("Fetching dependencies", "dep", append([]string{"ensure"}, o.depArgs...)...)
if err != nil {
return err
}
return fmt.Errorf("The v1 projects are deprecated since Feb 1, 2020.\n" +
"See how to upgrade your project to v2: https://book.kubebuilder.io/migration/guide.html\n")

case c.IsV2():
// Ensure that we are pinning controller-runtime version
Expand Down
31 changes: 18 additions & 13 deletions cmd/internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@ limitations under the License.
package internal

import (
"fmt"
"log"
"os"

"sigs.k8s.io/kubebuilder/internal/config"
)

const (
noticeColor = "\033[1;36m%s\033[0m"
)

// ConfiguredAndV1 returns true if the project is already configured and it is v1
func ConfiguredAndV1() bool {
projectConfig, err := config.Read()
// IsConfigured returns true if the project is already configured
func IsConfigured() bool {
_, err := config.Read()

if os.IsNotExist(err) {
return false
Expand All @@ -40,10 +35,20 @@ func ConfiguredAndV1() bool {
log.Fatalf("failed to read the configuration file: %v", err)
}

return projectConfig.IsV1()
return true
}

func PrintV1DeprecationWarning() {
fmt.Printf(noticeColor, "[Deprecation Notice] The v1 projects are deprecated and will not be supported beyond "+
"Feb 1, 2020.\nSee how to upgrade your project to v2: https://book.kubebuilder.io/migration/guide.html\n")
}
// IsSupportable returns true if the project is already configured and it is v2
func IsSupportable() bool {
projectConfig, err := config.Read()

if os.IsNotExist(err) {
return false
}

if err != nil {
log.Fatalf("failed to read the configuration file: %v", err)
}

return projectConfig.IsV2()
}
32 changes: 17 additions & 15 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"fmt"
"log"

"github.com/spf13/cobra"
Expand All @@ -27,6 +28,11 @@ import (
"sigs.k8s.io/kubebuilder/pkg/scaffold"
)

// Just to show with colours that the V1 is no longer supported
const (
noticeColor = "\033[1;36m%s\033[0m"
)

// commandOptions represent the types used to implement the different commands
type commandOptions interface {
// bindFlags binds the command flags to the fields in the options struct
Expand Down Expand Up @@ -78,19 +84,22 @@ func run(options commandOptions) error {
}

func buildCmdTree() *cobra.Command {
if internal.ConfiguredAndV1() {
internal.PrintV1DeprecationWarning()
if internal.IsConfigured() && !internal.IsSupportable() {
fmt.Printf(noticeColor, "The v1 projects are deprecated since Feb 1, 2020.\n"+
"See how to upgrade your project to v2: https://book.kubebuilder.io/migration/guide.html\n")
}

// kubebuilder
rootCmd := newRootCmd()

// kubebuilder update (v1 only)
if !internal.IsSupportable() {
rootCmd.AddCommand(newUpdateCmd())
}

// kubebuilder alpha
alphaCmd := newAlphaCmd()
// kubebuilder alpha webhook (v1 only)
if internal.ConfiguredAndV1() {
alphaCmd.AddCommand(newWebhookCmd())
}

// Only add alpha group if it has subcommands
if alphaCmd.HasSubCommands() {
rootCmd.AddCommand(alphaCmd)
Expand All @@ -100,10 +109,8 @@ func buildCmdTree() *cobra.Command {
createCmd := newCreateCmd()
// kubebuilder create api
createCmd.AddCommand(newAPICmd())
// kubebuilder create webhook (v2 only)
if !internal.ConfiguredAndV1() {
createCmd.AddCommand(newWebhookV2Cmd())
}
createCmd.AddCommand(newWebhookV2Cmd())

// Only add create group if it has subcommands
if createCmd.HasSubCommands() {
rootCmd.AddCommand(createCmd)
Expand All @@ -115,11 +122,6 @@ func buildCmdTree() *cobra.Command {
// kubebuilder init
rootCmd.AddCommand(newInitCmd())

// kubebuilder update (v1 only)
if internal.ConfiguredAndV1() {
rootCmd.AddCommand(newUpdateCmd())
}

// kubebuilder version
rootCmd.AddCommand(version.NewVersionCmd())

Expand Down
105 changes: 0 additions & 105 deletions cmd/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/spf13/cobra"

"sigs.k8s.io/kubebuilder/cmd/internal"
"sigs.k8s.io/kubebuilder/internal/config"
"sigs.k8s.io/kubebuilder/pkg/model/resource"
"sigs.k8s.io/kubebuilder/pkg/scaffold"
Expand All @@ -40,110 +39,6 @@ func (e webhookError) Error() string {
return fmt.Sprintf("failed to create webhook: %v", e.err)
}

func newWebhookCmd() *cobra.Command {
options := &webhookV1Options{}

cmd := &cobra.Command{
Use: "webhook",
Short: "Scaffold a webhook server",
Long: `Scaffold a webhook server if there is no existing server.
Scaffolds webhook handlers based on group, version, kind and other user inputs.
This command is only available for v1 scaffolding project.
`,
Example: ` # Create webhook for CRD of group crew, version v1 and kind FirstMate.
# Set type to be mutating and operations to be create and update.
kubebuilder alpha webhook --group crew --version v1 --kind FirstMate --type=mutating --operations=create,update
`,
Run: func(_ *cobra.Command, _ []string) {
if err := run(options); err != nil {
log.Fatal(webhookError{err})
}
},
}

options.bindFlags(cmd)

return cmd
}

var _ commandOptions = &webhookV1Options{}

type webhookV1Options struct {
resource *resource.Options
server string
webhookType string
operations []string
doMake bool
}

func (o *webhookV1Options) bindFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.server, "server", "default", "name of the server")
cmd.Flags().StringVar(&o.webhookType, "type", "", "webhook type, e.g. mutating or validating")
cmd.Flags().StringSliceVar(&o.operations, "operations", []string{"create"},
"the operations that the webhook will intercept, e.g. create, update, delete and connect")

cmd.Flags().BoolVar(&o.doMake, "make", true, "if true, run make after generating files")

o.resource = &resource.Options{}
cmd.Flags().StringVar(&o.resource.Group, "group", "", "resource Group")
cmd.Flags().StringVar(&o.resource.Version, "version", "", "resource Version")
cmd.Flags().StringVar(&o.resource.Kind, "kind", "", "resource Kind")
cmd.Flags().StringVar(&o.resource.Plural, "resource", "", "resource Resource")
}

func (o *webhookV1Options) loadConfig() (*config.Config, error) {
projectConfig, err := config.Load()
if os.IsNotExist(err) {
return nil, errors.New("unable to find configuration file, project must be initialized")
}

return projectConfig, err
}

func (o *webhookV1Options) validate(c *config.Config) error {
if !c.IsV1() {
return fmt.Errorf("webhook scaffolding is no longer alpha for version %s", c.Version)
}

if err := o.resource.Validate(); err != nil {
return err
}

return nil
}

func (o *webhookV1Options) scaffolder(c *config.Config) (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)
}

// Create the actual resource from the resource options
var res *resource.Resource
switch {
case c.IsV1():
res = o.resource.NewV1Resource(&c.Config, false)
case c.IsV2():
res = o.resource.NewResource(&c.Config, false)
default:
return nil, fmt.Errorf("unknown project version %v", c.Version)
}

return scaffold.NewV1WebhookScaffolder(&c.Config, string(bp), res, o.server, o.webhookType, o.operations), nil
}

func (o *webhookV1Options) postScaffold(_ *config.Config) error {
if o.doMake {
err := internal.RunCmd("Running make", "make")
if err != nil {
return err
}
}

return nil
}

func newWebhookV2Cmd() *cobra.Command {
options := &webhookV2Options{}

Expand Down
Loading

0 comments on commit 6fafcb9

Please sign in to comment.