|
| 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 convert provides a converter for CAPI core resources between API versions. |
| 18 | +package convert |
| 19 | + |
| 20 | +import ( |
| 21 | + "github.com/pkg/errors" |
| 22 | + "k8s.io/apimachinery/pkg/runtime" |
| 23 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 24 | + |
| 25 | + clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2" |
| 26 | + "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/scheme" |
| 27 | +) |
| 28 | + |
| 29 | +// SupportedTargetVersions defines all supported target API versions for conversion. |
| 30 | +var SupportedTargetVersions = []string{ |
| 31 | + clusterv1.GroupVersion.Version, |
| 32 | +} |
| 33 | + |
| 34 | +// Converter handles the conversion of CAPI core resources between API versions. |
| 35 | +type Converter struct { |
| 36 | + scheme *runtime.Scheme |
| 37 | + targetAPIGroup string |
| 38 | + targetGV schema.GroupVersion |
| 39 | + sourceGroupVersions []schema.GroupVersion |
| 40 | + knownAPIGroups []string |
| 41 | +} |
| 42 | + |
| 43 | +// NewConverter creates a new Converter instance. |
| 44 | +func NewConverter(targetAPIGroup string, targetGV schema.GroupVersion, sourceGroupVersions []schema.GroupVersion, knownAPIGroups []string) *Converter { |
| 45 | + return &Converter{ |
| 46 | + scheme: scheme.Scheme, |
| 47 | + targetAPIGroup: targetAPIGroup, |
| 48 | + targetGV: targetGV, |
| 49 | + sourceGroupVersions: sourceGroupVersions, |
| 50 | + knownAPIGroups: knownAPIGroups, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Convert processes multi-document YAML streams and converts resources to the target version. |
| 55 | +func (c *Converter) Convert(input []byte, toVersion string) (output []byte, messages []string, err error) { |
| 56 | + messages = make([]string, 0) |
| 57 | + |
| 58 | + targetGV := schema.GroupVersion{ |
| 59 | + Group: c.targetAPIGroup, |
| 60 | + Version: toVersion, |
| 61 | + } |
| 62 | + |
| 63 | + // Create GVK matcher for resource classification. |
| 64 | + matcher := newGVKMatcher(c.sourceGroupVersions, c.knownAPIGroups) |
| 65 | + |
| 66 | + // Parse input YAML stream. |
| 67 | + docs, err := parseYAMLStream(input, c.scheme, matcher) |
| 68 | + if err != nil { |
| 69 | + return nil, nil, errors.Wrap(err, "failed to parse YAML stream") |
| 70 | + } |
| 71 | + |
| 72 | + for i := range docs { |
| 73 | + doc := &docs[i] |
| 74 | + |
| 75 | + switch doc.typ { |
| 76 | + case resourceTypeConvertible: |
| 77 | + convertedObj, wasConverted, convErr := convertResource(doc.object, targetGV, c.scheme, c.targetAPIGroup) |
| 78 | + if convErr != nil { |
| 79 | + return nil, nil, errors.Wrapf(convErr, "failed to convert resource %s at index %d", doc.gvk.String(), doc.index) |
| 80 | + } |
| 81 | + |
| 82 | + if wasConverted { |
| 83 | + doc.object = convertedObj |
| 84 | + } else { |
| 85 | + // Resource that are already at target version. |
| 86 | + if msg := getInfoMessage(doc.gvk, toVersion, c.targetAPIGroup); msg != "" { |
| 87 | + messages = append(messages, msg) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + case resourceTypeKnown: |
| 92 | + // Pass through unchanged with info message. |
| 93 | + if msg := getInfoMessage(doc.gvk, toVersion, c.targetAPIGroup); msg != "" { |
| 94 | + messages = append(messages, msg) |
| 95 | + } |
| 96 | + |
| 97 | + case resourceTypePassThrough: |
| 98 | + // Non-target API group resource - pass through unchanged with info message. |
| 99 | + if msg := getInfoMessage(doc.gvk, toVersion, c.targetAPIGroup); msg != "" { |
| 100 | + messages = append(messages, msg) |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Serialize documents back to YAML. |
| 106 | + output, err = serializeYAMLStream(docs, c.scheme) |
| 107 | + if err != nil { |
| 108 | + return nil, nil, errors.Wrap(err, "failed to serialize output") |
| 109 | + } |
| 110 | + |
| 111 | + return output, messages, nil |
| 112 | +} |
0 commit comments