|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/spf13/cobra" |
| 25 | + |
| 26 | + "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/migrate" |
| 27 | + "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/scheme" |
| 28 | +) |
| 29 | + |
| 30 | +type migrateOptions struct { |
| 31 | + output string |
| 32 | + toVersion string |
| 33 | +} |
| 34 | + |
| 35 | +var migrateOpts = &migrateOptions{} |
| 36 | + |
| 37 | +var migrateCmd = &cobra.Command{ |
| 38 | + Use: "migrate [SOURCE]", |
| 39 | + Short: "EXPERIMENTAL: Migrate cluster.x-k8s.io resources between API versions", |
| 40 | + Long: `EXPERIMENTAL: Migrate cluster.x-k8s.io resources between API versions. |
| 41 | +
|
| 42 | +This command is EXPERIMENTAL and currently supports only cluster.x-k8s.io core resources |
| 43 | +(Cluster, MachineDeployment, etc.) with v1beta2 as the only supported target version. |
| 44 | +
|
| 45 | +SCOPE AND LIMITATIONS (Experimental Status): |
| 46 | +- Only cluster.x-k8s.io resources are converted |
| 47 | +- Other CAPI API groups are passed through unchanged |
| 48 | +- Currently only supports v1beta2 as target version (--to-version flag must be "v1beta2") |
| 49 | +- ClusterClass patches are not migrated |
| 50 | +- Field order may change and comments will be removed in output |
| 51 | +- API version references are dropped during conversion (except cluster class and external |
| 52 | + remediation references) |
| 53 | +
|
| 54 | +Examples: |
| 55 | + # Migrate from file to stdout |
| 56 | + clusterctl migrate cluster.yaml |
| 57 | +
|
| 58 | + # Migrate from stdin to stdout |
| 59 | + cat cluster.yaml | clusterctl migrate |
| 60 | +
|
| 61 | + # Migrate to output file |
| 62 | + clusterctl migrate cluster.yaml --output migrated-cluster.yaml |
| 63 | +
|
| 64 | + # Explicitly specify target version (currently only v1beta2 is supported) |
| 65 | + clusterctl migrate cluster.yaml --to-version v1beta2`, |
| 66 | + |
| 67 | + Args: cobra.MaximumNArgs(1), |
| 68 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 69 | + return runMigrate(args) |
| 70 | + }, |
| 71 | +} |
| 72 | + |
| 73 | +func init() { |
| 74 | + migrateCmd.Flags().StringVarP(&migrateOpts.output, "output", "o", "", "Output file path (default: stdout)") |
| 75 | + migrateCmd.Flags().StringVar(&migrateOpts.toVersion, "to-version", "v1beta2", "Target API version for migration (currently only v1beta2 is supported)") |
| 76 | + |
| 77 | + RootCmd.AddCommand(migrateCmd) |
| 78 | +} |
| 79 | + |
| 80 | +func runMigrate(args []string) error { |
| 81 | + if migrateOpts.toVersion != "v1beta2" { |
| 82 | + return fmt.Errorf("invalid --to-version value %q: currently only v1beta2 is supported", migrateOpts.toVersion) |
| 83 | + } |
| 84 | + |
| 85 | + fmt.Fprintf(os.Stderr, "WARNING: This command is EXPERIMENTAL and currently supports only cluster.x-k8s.io resources.\n") |
| 86 | + fmt.Fprintf(os.Stderr, "Only v1beta2 is supported as target version. Other CAPI API groups are passed through unchanged.\n") |
| 87 | + fmt.Fprintf(os.Stderr, "See 'clusterctl migrate --help' for scope, limitations, and usage details.\n\n") |
| 88 | + |
| 89 | + var input io.Reader |
| 90 | + var inputName string |
| 91 | + |
| 92 | + if len(args) == 0 { |
| 93 | + input = os.Stdin |
| 94 | + inputName = "stdin" |
| 95 | + } else { |
| 96 | + sourceFile := args[0] |
| 97 | + file, err := os.Open(sourceFile) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("failed to open input file %q: %w", sourceFile, err) |
| 100 | + } |
| 101 | + defer file.Close() |
| 102 | + input = file |
| 103 | + inputName = sourceFile |
| 104 | + } |
| 105 | + |
| 106 | + // Determine output destination |
| 107 | + var output io.Writer |
| 108 | + var outputFile *os.File |
| 109 | + var err error |
| 110 | + |
| 111 | + if migrateOpts.output == "" { |
| 112 | + output = os.Stdout |
| 113 | + } else { |
| 114 | + outputFile, err = os.Create(migrateOpts.output) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("failed to create output file %q: %w", migrateOpts.output, err) |
| 117 | + } |
| 118 | + defer outputFile.Close() |
| 119 | + output = outputFile |
| 120 | + } |
| 121 | + |
| 122 | + // Create migration engine components |
| 123 | + parser := migrate.NewYAMLParser(scheme.Scheme) |
| 124 | + converter, err := migrate.NewConverter() |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("failed to create converter: %w", err) |
| 127 | + } |
| 128 | + |
| 129 | + engine, err := migrate.NewEngine(parser, converter) |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("failed to create migration engine: %w", err) |
| 132 | + } |
| 133 | + |
| 134 | + opts := migrate.MigrationOptions{ |
| 135 | + Input: input, |
| 136 | + Output: output, |
| 137 | + Errors: os.Stderr, |
| 138 | + ToVersion: migrateOpts.toVersion, |
| 139 | + } |
| 140 | + |
| 141 | + result, err := engine.Migrate(opts) |
| 142 | + if err != nil { |
| 143 | + return fmt.Errorf("migration failed: %w", err) |
| 144 | + } |
| 145 | + |
| 146 | + if result.TotalResources > 0 { |
| 147 | + fmt.Fprintf(os.Stderr, "\nMigration completed:\n") |
| 148 | + fmt.Fprintf(os.Stderr, " Total resources processed: %d\n", result.TotalResources) |
| 149 | + fmt.Fprintf(os.Stderr, " Resources converted: %d\n", result.ConvertedCount) |
| 150 | + fmt.Fprintf(os.Stderr, " Resources skipped: %d\n", result.SkippedCount) |
| 151 | + |
| 152 | + if result.ErrorCount > 0 { |
| 153 | + fmt.Fprintf(os.Stderr, " Resources with errors: %d\n", result.ErrorCount) |
| 154 | + } |
| 155 | + |
| 156 | + if len(result.Warnings) > 0 { |
| 157 | + fmt.Fprintf(os.Stderr, " Warnings: %d\n", len(result.Warnings)) |
| 158 | + } |
| 159 | + |
| 160 | + fmt.Fprintf(os.Stderr, "\nSource: %s\n", inputName) |
| 161 | + if migrateOpts.output != "" { |
| 162 | + fmt.Fprintf(os.Stderr, "Output: %s\n", migrateOpts.output) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if result.ErrorCount > 0 { |
| 167 | + return fmt.Errorf("migration completed with %d errors", result.ErrorCount) |
| 168 | + } |
| 169 | + |
| 170 | + return nil |
| 171 | +} |
0 commit comments