|
| 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 | +} |
0 commit comments