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 f6ecbe2
Show file tree
Hide file tree
Showing 152 changed files with 94 additions and 8,403 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
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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+.
- [kustomize](https://sigs.k8s.io/kustomize/docs/INSTALL.md) v3.1.0+
Expand Down
8 changes: 2 additions & 6 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 All @@ -74,11 +70,11 @@ test: ## Run the go tests ($ go test -v ./cmd/... ./pkg/...)

.PHONY: test-ci
test-ci: ## Run the unit tests (used in the CI)
./setup.sh
./test.sh

.PHONY: test-e2e
.PHONY: test-ci-e2e
test-e2e: ## Run the integration tests (used in the CI)
# todo: it need to be fixed to allow contributtors run it locally
./test_e2e.sh

.PHONY: test-coverage
Expand Down
29 changes: 14 additions & 15 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (o *apiOptions) bindFlags(cmd *cobra.Command) {
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 Down Expand Up @@ -163,23 +164,21 @@ func (o *apiOptions) validate(c *config.Config) error {

// In case we want to scaffold a resource API we need to do some checks
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")

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

// The following check is v2 specific as multi-group isn't enabled by default
Expand Down
34 changes: 3 additions & 31 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ limitations under the License.
package main

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

Expand All @@ -48,7 +46,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 +57,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 Down Expand Up @@ -164,18 +160,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 +170,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.IsV1()
}
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 f6ecbe2

Please sign in to comment.