|
| 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 | + "strings" |
| 24 | + |
| 25 | + "github.com/pkg/errors" |
| 26 | + "github.com/spf13/cobra" |
| 27 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 28 | + |
| 29 | + clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" |
| 30 | + "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/migrate" |
| 31 | + "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/scheme" |
| 32 | +) |
| 33 | + |
| 34 | +type migrateOptions struct { |
| 35 | + output string |
| 36 | + toVersion string |
| 37 | +} |
| 38 | + |
| 39 | +var migrateOpts = &migrateOptions{} |
| 40 | + |
| 41 | +var supportedTargetVersions = []string{ |
| 42 | + clusterv1.GroupVersion.Version, |
| 43 | +} |
| 44 | + |
| 45 | +var migrateCmd = &cobra.Command{ |
| 46 | + Use: "migrate [SOURCE]", |
| 47 | + Short: "EXPERIMENTAL: Migrate cluster.x-k8s.io resources between API versions", |
| 48 | + Long: `EXPERIMENTAL: Migrate cluster.x-k8s.io resources between API versions. |
| 49 | +
|
| 50 | +This command is EXPERIMENTAL and may be removed in a future release! |
| 51 | +
|
| 52 | +Scope and limitations: |
| 53 | +- Only cluster.x-k8s.io resources are converted |
| 54 | +- Other CAPI API groups are passed through unchanged |
| 55 | +- ClusterClass patches are not migrated |
| 56 | +- Field order may change and comments will be removed in output |
| 57 | +- API version references are dropped during conversion (except ClusterClass and external |
| 58 | + remediation references) |
| 59 | +
|
| 60 | +Examples: |
| 61 | + # Migrate from file to stdout |
| 62 | + clusterctl migrate cluster.yaml |
| 63 | +
|
| 64 | + # Migrate from stdin to stdout |
| 65 | + cat cluster.yaml | clusterctl migrate |
| 66 | +
|
| 67 | + # Explicitly specify target <VERSION> |
| 68 | + clusterctl migrate cluster.yaml --to-version <VERSION> --output migrated-cluster.yaml`, |
| 69 | + |
| 70 | + Args: cobra.MaximumNArgs(1), |
| 71 | + RunE: func(_ *cobra.Command, args []string) error { |
| 72 | + return runMigrate(args) |
| 73 | + }, |
| 74 | +} |
| 75 | + |
| 76 | +func init() { |
| 77 | + migrateCmd.Flags().StringVarP(&migrateOpts.output, "output", "o", "", "Output file path (default: stdout)") |
| 78 | + migrateCmd.Flags().StringVar(&migrateOpts.toVersion, "to-version", clusterv1.GroupVersion.Version, fmt.Sprintf("Target API version for migration (supported: %s)", strings.Join(supportedTargetVersions, ", "))) |
| 79 | + |
| 80 | + RootCmd.AddCommand(migrateCmd) |
| 81 | +} |
| 82 | + |
| 83 | +func isSupportedTargetVersion(version string) bool { |
| 84 | + for _, v := range supportedTargetVersions { |
| 85 | + if v == version { |
| 86 | + return true |
| 87 | + } |
| 88 | + } |
| 89 | + return false |
| 90 | +} |
| 91 | + |
| 92 | +func runMigrate(args []string) error { |
| 93 | + if !isSupportedTargetVersion(migrateOpts.toVersion) { |
| 94 | + return errors.Errorf("invalid --to-version value %q: supported versions are %s", migrateOpts.toVersion, strings.Join(supportedTargetVersions, ", ")) |
| 95 | + } |
| 96 | + |
| 97 | + fmt.Fprint(os.Stderr, "WARNING: This command is EXPERIMENTAL and may be removed in a future release!") |
| 98 | + |
| 99 | + var input io.Reader |
| 100 | + var inputName string |
| 101 | + |
| 102 | + if len(args) == 0 { |
| 103 | + input = os.Stdin |
| 104 | + inputName = "stdin" |
| 105 | + } else { |
| 106 | + sourceFile := args[0] |
| 107 | + // #nosec G304 |
| 108 | + // command accepts user-provided file path by design |
| 109 | + file, err := os.Open(sourceFile) |
| 110 | + if err != nil { |
| 111 | + return errors.Wrapf(err, "failed to open input file %q", sourceFile) |
| 112 | + } |
| 113 | + defer file.Close() |
| 114 | + input = file |
| 115 | + inputName = sourceFile |
| 116 | + } |
| 117 | + |
| 118 | + // Determine output destination |
| 119 | + var output io.Writer |
| 120 | + var outputFile *os.File |
| 121 | + var err error |
| 122 | + |
| 123 | + if migrateOpts.output == "" { |
| 124 | + output = os.Stdout |
| 125 | + } else { |
| 126 | + outputFile, err = os.Create(migrateOpts.output) |
| 127 | + if err != nil { |
| 128 | + return errors.Wrapf(err, "failed to create output file %q", migrateOpts.output) |
| 129 | + } |
| 130 | + defer outputFile.Close() |
| 131 | + output = outputFile |
| 132 | + } |
| 133 | + |
| 134 | + // Create migration engine components |
| 135 | + parser := migrate.NewYAMLParser(scheme.Scheme) |
| 136 | + |
| 137 | + targetGV := schema.GroupVersion{ |
| 138 | + Group: clusterv1.GroupVersion.Group, |
| 139 | + Version: migrateOpts.toVersion, |
| 140 | + } |
| 141 | + |
| 142 | + converter, err := migrate.NewConverter(targetGV) |
| 143 | + if err != nil { |
| 144 | + return errors.Wrap(err, "failed to create converter") |
| 145 | + } |
| 146 | + |
| 147 | + engine, err := migrate.NewEngine(parser, converter) |
| 148 | + if err != nil { |
| 149 | + return errors.Wrap(err, "failed to create migration engine") |
| 150 | + } |
| 151 | + |
| 152 | + opts := migrate.MigrationOptions{ |
| 153 | + Input: input, |
| 154 | + Output: output, |
| 155 | + Errors: os.Stderr, |
| 156 | + ToVersion: migrateOpts.toVersion, |
| 157 | + } |
| 158 | + |
| 159 | + result, err := engine.Migrate(opts) |
| 160 | + if err != nil { |
| 161 | + return errors.Wrap(err, "migration failed") |
| 162 | + } |
| 163 | + |
| 164 | + if result.TotalResources > 0 { |
| 165 | + fmt.Fprintf(os.Stderr, "\nMigration completed:\n") |
| 166 | + fmt.Fprintf(os.Stderr, " Total resources processed: %d\n", result.TotalResources) |
| 167 | + fmt.Fprintf(os.Stderr, " Resources converted: %d\n", result.ConvertedCount) |
| 168 | + fmt.Fprintf(os.Stderr, " Resources skipped: %d\n", result.SkippedCount) |
| 169 | + |
| 170 | + if result.ErrorCount > 0 { |
| 171 | + fmt.Fprintf(os.Stderr, " Resources with errors: %d\n", result.ErrorCount) |
| 172 | + } |
| 173 | + |
| 174 | + if len(result.Warnings) > 0 { |
| 175 | + fmt.Fprintf(os.Stderr, " Warnings: %d\n", len(result.Warnings)) |
| 176 | + } |
| 177 | + |
| 178 | + fmt.Fprintf(os.Stderr, "\nSource: %s\n", inputName) |
| 179 | + if migrateOpts.output != "" { |
| 180 | + fmt.Fprintf(os.Stderr, "Output: %s\n", migrateOpts.output) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + if result.ErrorCount > 0 { |
| 185 | + return errors.Errorf("migration completed with %d errors", result.ErrorCount) |
| 186 | + } |
| 187 | + |
| 188 | + return nil |
| 189 | +} |
0 commit comments