kubebuilder icon indicating copy to clipboard operation
kubebuilder copied to clipboard

:sparkles: add alpha update command

Open vitorfloriano opened this issue 5 months ago • 4 comments
trafficstars

🏗️ WIP: Add alpha update command for project version upgrades via synthetic merge

This PR introduces the alpha update command. It attempts to upgrade Kubebuilder project versions while preserving user modifications through a three-way merge with a synthetic ancestor.

🔧 This involves:

Adding the new alpha update command and the foundational update functionality.

🚀 New Command

  • Added update (NewUpdateCommand), which integrates logic to support project upgrades.

✨ Update Logic

  • Introduced a dedicated Update struct and supporting methods to manage project upgrades. This includes downloading Kubebuilder binaries, managing Git branches, and executing alpha generate in specific versions.

  • In this MVP, alpha update reads the current version from cliVersion in the PROJECT file, downloads the target binary, recreates a synthetic ancestry using Git branches, and runs a three-way merge. The user can override the cliVersion with --from-version.

🔨 Generate Command Enhancements

  • Renamed newAlphaCommand and alpha.NewScaffoldCommand to alpha.NewGenerateCommand and alpha.NewUpdateCommand to reflect clearer responsibilities.
vitorfloriano@pc:~/go/src/github.com/vitorfloriano/wordpress-operator-poc$ ../kubebuilder/bin/kubebuilder alpha -h
Alpha subcommands are for unstable features.

- Alpha subcommands are exploratory and may be removed without warning.
- No backwards compatibility is provided for any alpha subcommands.

Usage:
  kubebuilder alpha [command]

Available Commands:
  generate    Re-scaffold an existing Kuberbuilder project
  update      Update a Kubebuilder project to a newer version

Flags:
  -h, --help   help for alpha

Global Flags:
      --plugins strings   plugin keys to be used for this subcommand execution

Use "kubebuilder alpha [command] --help" for more information about a command.
vitorfloriano@pc:~/go/src/github.com/vitorfloriano/wordpress-operator-poc$ ../kubebuilder/bin/kubebuilder alpha update -h
Update a Kubebuilder project to a newer version using a three-way merge strategy.

This command helps you upgrade your Kubebuilder project by:
1. Creating a clean ancestor branch with the old version's scaffolding
2. Creating a current branch with your project's current state
3. Creating an upgrade branch with the new version's scaffolding
4. Attempting to merge the changes automatically

The process creates several Git branches to help you manage the upgrade:
- ancestor: Clean scaffolding from the original version
- current: Your project's current state
- upgrade: Clean scaffolding from the new version
- merge: Attempted automatic merge of upgrade into current

If conflicts occur during the merge, you'll need to resolve them manually.

Examples:
  # Update using the version specified in PROJECT file
  kubebuilder alpha update

  # Update from a specific version
  kubebuilder alpha update --from-version v3.0.0

Requirements:
- Must be run from the root of a Kubebuilder project
- Git repository must be clean (no uncommitted changes)
- PROJECT file must exist and contain a valid layout version

Usage:
  kubebuilder alpha update [flags]

Flags:
      --from-version string   Override the CLI version from PROJECT file. Specify the Kubebuilder version to upgrade from (e.g., 'v3.0.0' or '3.0.0')
  -h, --help                  help for update

Global Flags:
      --plugins strings   plugin keys to be used for this subcommand execution
vitorfloriano@pc:~/go/src/github.com/vitorfloriano/wordpress-operator-poc$ ../kubebuilder/bin/kubebuilder alpha update --from-version 4.5.0
INFO Overriding cliVersion field  from PROJECT file with --from-version v4.5.0 
INFO Downloading the Kubebuilder v4.5.0 binary from: https://github.com/kubernetes-sigs/kubebuilder/releases/download/v4.5.0/kubebuilder_linux_amd64 
INFO Kubebuilder version v4.5.0 succesfully downloaded to /tmp/kubebuilderv4.5.0-885552202/kubebuilder 
INFO Downloaded binary kept at /tmp/kubebuilderv4.5.0-885552202 for debugging purposes 
INFO Created and checked out ancestor branch      
INFO Successfully removed tracked files from ancestor branch 
INFO Successfully cleaned untracked files from ancestor branch 
INFO Successfully committed cleanup on ancestor   
INFO Succesfully checked out the PROJECT file from master branch 
WARN Using current working directory to re-scaffold the project 
WARN This directory will be cleaned up and all files removed before the re-generation 
INFO Cleaning directory:/home/vitorfloriano/go/src/github.com/vitorfloriano/wordpress-operator-poc 
INFO Running cleanup:
$ sh -c rm -rf /home/vitorfloriano/go/src/github.com/vitorfloriano/wordpress-operator-poc/* 
INFO kubebuilder init:
$ kubebuilder init --plugins go.kubebuilder.io/v4 --domain my.domain 
INFO Writing kustomize manifests for you to edit... 
INFO Writing scaffold for you to edit...          
INFO Get controller runtime:
$ go get sigs.k8s.io/[email protected] 
INFO Update dependencies:
$ go mod tidy           
Next: define a resource with:
$ kubebuilder create api
INFO kubebuilder create api:
$ kubebuilder create api --plural wordpresses --group example.com --version v1 --kind Wordpress --resource --namespaced --controller 
INFO Writing kustomize manifests for you to edit... 
INFO Writing scaffold for you to edit...          
INFO api/v1/wordpress_types.go                    
INFO api/v1/groupversion_info.go                  
INFO internal/controller/suite_test.go            
INFO internal/controller/wordpress_controller.go  
INFO internal/controller/wordpress_controller_test.go 
INFO Update dependencies:
$ go mod tidy           
INFO Running make:
$ make generate                
mkdir -p /home/vitorfloriano/go/src/github.com/vitorfloriano/wordpress-operator-poc/bin
Downloading sigs.k8s.io/controller-tools/cmd/[email protected]
/home/vitorfloriano/go/src/github.com/vitorfloriano/wordpress-operator-poc/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
Next: implement your new API and generate the manifests (e.g. CRDs,CRs) with:
$ make manifests
INFO Grafana plugin not found, skipping migration 
INFO Deploy-image plugin not found, skipping migration 
INFO Successfully ran alpha generate using Kubebuilder v4.5.0 
INFO Successfully staged all changes in ancestor  
INFO Successfully commited changes in ancestor    
INFO Successfully checked out current branch off ancestor 
INFO Successfully checked out content from main onto current branch 
INFO Successfully staged all changes in current   
INFO Successfully commited changes in current     
INFO Successfully checked out upgrade branch off ancestor 
WARN Using current working directory to re-scaffold the project 
WARN This directory will be cleaned up and all files removed before the re-generation 
INFO Cleaning directory:/home/vitorfloriano/go/src/github.com/vitorfloriano/wordpress-operator-poc 
INFO Running cleanup:
$ sh -c rm -rf /home/vitorfloriano/go/src/github.com/vitorfloriano/wordpress-operator-poc/* 
INFO Running cleanup:
$ sh -c find "/home/vitorfloriano/go/src/github.com/vitorfloriano/wordpress-operator-poc" -mindepth 1 -maxdepth 1 ! -name '.git' ! -name 'PROJECT' -exec rm -rf {} + 
INFO kubebuilder init:
$ kubebuilder init --plugins go.kubebuilder.io/v4 --domain my.domain --repo github.com/vitorfloriano/wordpress-operator-poc 
INFO Writing kustomize manifests for you to edit... 
INFO Writing scaffold for you to edit...          
INFO Get controller runtime:
$ go get sigs.k8s.io/[email protected] 
INFO Update dependencies:
$ go mod tidy           
Next: define a resource with:
$ kubebuilder create api
INFO kubebuilder create api:
$ kubebuilder create api --plural wordpresses --group example.com --version v1 --kind Wordpress --resource --namespaced --controller 
INFO Writing kustomize manifests for you to edit... 
INFO Writing scaffold for you to edit...          
INFO api/v1/wordpress_types.go                    
INFO api/v1/groupversion_info.go                  
INFO internal/controller/suite_test.go            
INFO internal/controller/wordpress_controller.go  
INFO internal/controller/wordpress_controller_test.go 
INFO Update dependencies:
$ go mod tidy           
INFO Running make:
$ make generate                
mkdir -p /home/vitorfloriano/go/src/github.com/vitorfloriano/wordpress-operator-poc/bin
Downloading sigs.k8s.io/controller-tools/cmd/[email protected]
/home/vitorfloriano/go/src/github.com/vitorfloriano/wordpress-operator-poc/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
Next: implement your new API and generate the manifests (e.g. CRDs,CRs) with:
$ make manifests
INFO Grafana plugin not found, skipping migration 
INFO Deploy-image plugin not found, skipping migration 
INFO Running: make manifests                      
INFO Running make manifests:
$ make manifests     
/home/vitorfloriano/go/src/github.com/vitorfloriano/wordpress-operator-poc/bin/controller-gen rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
INFO Running: make generate                       
INFO Running make generate:
$ make generate       
/home/vitorfloriano/go/src/github.com/vitorfloriano/wordpress-operator-poc/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
INFO Running: make fmt                            
INFO Running make fmt:
$ make fmt                 
go fmt ./...
INFO Running: make vet                            
INFO Running make vet:
$ make vet                 
go vet ./...
INFO Running: make lint-fix                       
INFO Running make lint-fix:
$ make lint-fix       
Downloading github.com/golangci/golangci-lint/v2/cmd/[email protected]
/home/vitorfloriano/go/src/github.com/vitorfloriano/wordpress-operator-poc/bin/golangci-lint run --fix
0 issues.
INFO Successfully ran alpha generate on upgrade branch 
INFO Successfully staged all changes in upgrade branch 
INFO Successfully commited changes in upgrade branch 
WARN Merge with conflicts. Please resolve them manually 

vitorfloriano avatar Jun 13 '25 04:06 vitorfloriano