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 authored and Camila Macedo committed Feb 15, 2020
1 parent 6987a7f commit 0eb3eb8
Show file tree
Hide file tree
Showing 152 changed files with 108 additions and 8,429 deletions.
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,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)

script: ./test_e2e.sh
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
49 changes: 27 additions & 22 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,18 @@ type apiOptions struct {
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
}

func (o *apiOptions) bindFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&o.runMake, "make", true, "if true, run make after generating files")

// todo: remove when V1 code be removed
cmd.Flags().BoolVar(&o.doResource, "resource", true,
"if set, generate the resource without prompting the user")
o.resourceFlag = cmd.Flag("resource")

cmd.Flags().BoolVar(&o.doController, "controller", true,
"if set, generate the controller without prompting the user")
o.controllerFlag = cmd.Flag("controller")
Expand All @@ -125,9 +124,6 @@ func (o *apiOptions) bindFlags(cmd *cobra.Command) {
"generates an API following an extension pattern (addon)")
}

cmd.Flags().BoolVar(&o.force, "force", false,
"attempt to create resource even if it already exists")

o.resource = &resource.Options{}
cmd.Flags().StringVar(&o.resource.Kind, "kind", "", "resource Kind")
cmd.Flags().StringVar(&o.resource.Group, "group", "", "resource Group")
Expand All @@ -152,10 +148,21 @@ func (o *apiOptions) validate(c *config.Config) error {
}

reader := bufio.NewReader(os.Stdin)
if !o.resourceFlag.Changed {
fmt.Println("Create Resource [y/n]")
o.doResource = internal.YesNo(reader)

// todo: remove when V1 be removed
if c.IsV1() {
if !o.resourceFlag.Changed {
fmt.Println("Create Resource [y/n]")
o.doResource = internal.YesNo(reader)
}
}

// todo: remove if when V1 be removed
// workaround because the V1 code still in the project
if c.IsV2() {
o.doResource = true
}

if !o.controllerFlag.Changed {
fmt.Println("Create Controller [y/n]")
o.doController = internal.YesNo(reader)
Expand All @@ -165,21 +172,19 @@ func (o *apiOptions) validate(c *config.Config) error {
if o.doResource {
// Skip the following check for v1 as resources aren't tracked
if !c.IsV1() {
// Check that resource doesn't exist or flag force was set
if !o.force {
resourceExists := false
for _, r := range c.Resources {
if r.Group == o.resource.Group &&
r.Version == o.resource.Version &&
r.Kind == o.resource.Kind {
resourceExists = true
break
}
}
if resourceExists {
return errors.New("API resource already exists")
resourceExists := false
for _, r := range c.Resources {
if r.Group == o.resource.Group &&
r.Version == o.resource.Version &&
r.Kind == o.resource.Kind {
resourceExists = true
break
}
}
if resourceExists {
fmt.Printf("API resource already exists. Creating just controller ...")
o.doResource = false
}
}

// The following check is v2 specific as multi-group isn't enabled by default
Expand Down
54 changes: 4 additions & 50 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ 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 +45,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.
Writes the following files:
- a boilerplate license file
Expand All @@ -59,8 +56,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 +81,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 +93,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 +142,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 +152,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
29 changes: 17 additions & 12 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")
// IsVersionSupported returns true if the project is already configured and it is v2
func IsVersionSupported() 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()
}
30 changes: 16 additions & 14 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.IsVersionSupported() {
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 alpha
alphaCmd := newAlphaCmd()
// kubebuilder alpha webhook (v1 only)
if internal.ConfiguredAndV1() {
alphaCmd.AddCommand(newWebhookCmd())

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

// 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
Loading

0 comments on commit 0eb3eb8

Please sign in to comment.