Skip to content

Commit a275433

Browse files
authored
Add bootctl move command (#166)
* Add bootctl move command * Update copyrights * Add comments to bootctl move command
1 parent c280645 commit a275433

File tree

16 files changed

+681
-5
lines changed

16 files changed

+681
-5
lines changed

REUSE.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ SPDX-PackageDownloadLocation = "https://github.com/ironcore-dev/boot-operator"
66
[[annotations]]
77
path = [".github/**", ".gitignore", "CODEOWNERS", "Dockerfile", "Makefile", "PROJECT", "config/**", "gen/**", "go.mod", "go.sum", "hack/**", "server/**", "templates/**", "internal/**", "cmd/**", "api/**", "config/**", "test/**", "CONTRIBUTING.md", "PROJECT", "mkdocs.yml", ".dockerignore", ".golangci.yml", "REUSE.toml"]
88
precedence = "aggregate"
9-
SPDX-FileCopyrightText = "2024 SAP SE or an SAP affiliate company and IronCore contributors"
9+
SPDX-FileCopyrightText = "2025 SAP SE or an SAP affiliate company and IronCore contributors"
1010
SPDX-License-Identifier = "Apache-2.0"
1111

1212
[[annotations]]
1313
path = ["docs/**", "README.md"]
1414
precedence = "aggregate"
15-
SPDX-FileCopyrightText = "2024 SAP SE or an SAP affiliate company and IronCore contributors"
15+
SPDX-FileCopyrightText = "2025 SAP SE or an SAP affiliate company and IronCore contributors"
1616
SPDX-License-Identifier = "Apache-2.0"

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/bootctl/app/app.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package app
5+
6+
import (
7+
"github.com/spf13/cobra"
8+
"k8s.io/apimachinery/pkg/runtime"
9+
10+
bootv1alphav1 "github.com/ironcore-dev/boot-operator/api/v1alpha1"
11+
metalv1alpha1 "github.com/ironcore-dev/metal-operator/api/v1alpha1"
12+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
13+
)
14+
15+
const Name string = "bootctl"
16+
17+
var scheme = runtime.NewScheme()
18+
19+
func init() {
20+
utilruntime.Must(bootv1alphav1.AddToScheme(scheme))
21+
utilruntime.Must(metalv1alpha1.AddToScheme(scheme))
22+
}
23+
24+
func NewCommand() *cobra.Command {
25+
root := &cobra.Command{
26+
Use: Name,
27+
Short: "CLI client for boot-operator",
28+
Args: cobra.NoArgs,
29+
}
30+
root.AddCommand(NewMoveCommand())
31+
return root
32+
}

cmd/bootctl/app/move.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package app
5+
6+
import (
7+
"fmt"
8+
"log/slog"
9+
10+
"github.com/spf13/cobra"
11+
"k8s.io/client-go/tools/clientcmd"
12+
"sigs.k8s.io/controller-runtime/pkg/client"
13+
14+
utils "github.com/ironcore-dev/boot-operator/cmdutils"
15+
)
16+
17+
var (
18+
sourceKubeconfig string
19+
targetKubeconfig string
20+
namespace string
21+
requireOwners bool
22+
dryRun bool
23+
verbose bool
24+
)
25+
26+
func NewMoveCommand() *cobra.Command {
27+
move := &cobra.Command{
28+
Use: "move",
29+
Short: "Move boot-operator CRs from one cluster to another",
30+
RunE: runMove,
31+
}
32+
move.Flags().StringVar(&sourceKubeconfig, "source-kubeconfig", "", "Kubeconfig pointing to the source cluster")
33+
move.Flags().StringVar(&targetKubeconfig, "target-kubeconfig", "", "Kubeconfig pointing to the target cluster")
34+
move.Flags().StringVar(&namespace, "namespace", "",
35+
"namespace to filter CRs to migrate. Defaults to all namespaces if not specified")
36+
move.Flags().BoolVar(&requireOwners, "require-owners", false, "if set to true, an error will be returned if for any custom resource an owner ServerBootConfiguration is not present in the target cluster")
37+
move.Flags().BoolVar(&dryRun, "dry-run", false, "show what would be moved without executing the migration")
38+
move.Flags().BoolVar(&verbose, "verbose", false, "enable verbose logging for detailed output during migration")
39+
_ = move.MarkFlagRequired("source-kubeconfig")
40+
_ = move.MarkFlagRequired("target-kubeconfig")
41+
42+
if verbose {
43+
slog.SetLogLoggerLevel(slog.LevelDebug)
44+
}
45+
return move
46+
}
47+
48+
func makeClient(kubeconfig string) (client.Client, error) {
49+
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
50+
if err != nil {
51+
return nil, fmt.Errorf("failed to load cluster kubeconfig: %w", err)
52+
}
53+
return client.New(cfg, client.Options{Scheme: scheme})
54+
}
55+
56+
func makeClients() (utils.Clients, error) {
57+
var clients utils.Clients
58+
var err error
59+
60+
clients.Source, err = makeClient(sourceKubeconfig)
61+
if err != nil {
62+
return clients, fmt.Errorf("failed to construct a source cluster client: %w", err)
63+
}
64+
clients.Target, err = makeClient(targetKubeconfig)
65+
if err != nil {
66+
return clients, fmt.Errorf("failed to construct a target cluster client: %w", err)
67+
}
68+
return clients, nil
69+
}
70+
71+
func runMove(cmd *cobra.Command, args []string) error {
72+
clients, err := makeClients()
73+
if err != nil {
74+
return err
75+
}
76+
ctx := cmd.Context()
77+
78+
return utils.Move(ctx, clients, scheme, namespace, requireOwners, dryRun)
79+
}

cmd/bootctl/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"os"
9+
10+
"github.com/ironcore-dev/boot-operator/cmd/bootctl/app"
11+
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
12+
)
13+
14+
func main() {
15+
if err := app.NewCommand().ExecuteContext(signals.SetupSignalHandler()); err != nil {
16+
fmt.Fprintln(os.Stderr, err)
17+
os.Exit(1)
18+
}
19+
}

cmdutils/clients.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package cmdutils
5+
6+
import "sigs.k8s.io/controller-runtime/pkg/client"
7+
8+
// Clients structure stores information about source and destination cluster clients.
9+
type Clients struct {
10+
Source client.Client
11+
Target client.Client
12+
}

0 commit comments

Comments
 (0)