Skip to content

Commit afe130f

Browse files
authored
CLOUDP-350407: Minor translation fixes (#2823)
* CLOUDP-350407: Minor translation fixes * Improve comments and function names * fix typos
1 parent 7b27fbc commit afe130f

File tree

11 files changed

+149
-116
lines changed

11 files changed

+149
-116
lines changed

internal/autogen/translate/crds/crds.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import (
2323
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2424
)
2525

26-
// AssertMajorVersion checks the given majorVersion exists for the given kind and CRD version
26+
// AssertMajorVersion checks the given majorVersion exists for the given kind
27+
// and CRD version
2728
func AssertMajorVersion(specVersion *apiextensionsv1.CustomResourceDefinitionVersion, kind string, majorVersion string) error {
2829
props, err := GetOpenAPIProperties(kind, specVersion)
2930
if err != nil {
@@ -57,7 +58,8 @@ func CompileCRDSchema(openAPISchema *apiextensionsv1.JSONSchemaProps) (*jsonsche
5758
return schema, nil
5859
}
5960

60-
// SelectVersion returns the version from the CRD spec that matches the given version string
61+
// SelectVersion returns the version from the CRD spec that matches the given
62+
// version string
6163
func SelectVersion(spec *apiextensionsv1.CustomResourceDefinitionSpec, version string) *apiextensionsv1.CustomResourceDefinitionVersion {
6264
if len(spec.Versions) == 0 {
6365
return nil
@@ -73,7 +75,8 @@ func SelectVersion(spec *apiextensionsv1.CustomResourceDefinitionSpec, version s
7375
return nil
7476
}
7577

76-
// GetOpenAPIProperties digs up the schema properties of a given kind on a given CRD version
78+
// GetOpenAPIProperties digs up the schema properties of a given kind on a given
79+
// CRD version
7780
func GetOpenAPIProperties(kind string, version *apiextensionsv1.CustomResourceDefinitionVersion) (map[string]apiextensionsv1.JSONSchemaProps, error) {
7881
if version == nil {
7982
return nil, fmt.Errorf("missing version (nil) from %v spec", kind)
@@ -90,7 +93,8 @@ func GetOpenAPIProperties(kind string, version *apiextensionsv1.CustomResourceDe
9093
return version.Schema.OpenAPIV3Schema.Properties, nil
9194
}
9295

93-
// GetSpecPropertiesFor takes the properties value of a given field of a kind's properties set
96+
// GetSpecPropertiesFor takes the properties value of a given field of a kind's
97+
// properties set
9498
func GetSpecPropertiesFor(kind string, props map[string]apiextensionsv1.JSONSchemaProps, field string) (map[string]apiextensionsv1.JSONSchemaProps, error) {
9599
prop, ok := props[field]
96100
if !ok {

internal/autogen/translate/crds/parse.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var (
3838

3939
// Parse scans a YAML stream and returns the next CRD found.
4040
// If more than one CRD is present in the stream, calling again
41-
// on the same stream will return the next CRD found.
41+
// on the same stream returns the next CRD found.
4242
func Parse(scanner *bufio.Scanner) (*apiextensionsv1.CustomResourceDefinition, error) {
4343
var buffer bytes.Buffer
4444

@@ -80,6 +80,8 @@ func Parse(scanner *bufio.Scanner) (*apiextensionsv1.CustomResourceDefinition, e
8080
return nil, io.EOF
8181
}
8282

83+
// Decode reads raw content as bytes and tries to convertdecode as a CRD schema, or
84+
// will return an error if decoding fails
8385
func Decode(content []byte) (*apiextensionsv1.CustomResourceDefinition, error) {
8486
sch := runtime.NewScheme()
8587
_ = scheme.AddToScheme(sch)

internal/autogen/translate/refs/handler.go

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,67 +37,79 @@ type Handler struct {
3737
expand bool
3838
}
3939

40+
// NewHandler creates the basic context to expand or collapse references, such
41+
// context requires the Kubernetes object being translated and its dependencies
4042
func NewHandler(main client.Object, deps []client.Object) *Handler {
4143
return &Handler{context: newMapContext(main, deps)}
4244
}
4345

44-
func (h *Handler) ExpandReferences(obj, mappings map[string]any, fields ...string) error {
46+
// ExpandReferences uses the handler context to expand references on a given
47+
// unstructured value matching the main object being translated, using the
48+
// given reference mappings and acting on a particular path og the value object
49+
func (h *Handler) ExpandReferences(obj, mappings map[string]any, path ...string) error {
4550
h.expand = true
4651

47-
props, err := accessMappingPropsAt(mappings, fields)
52+
props, err := accessMappingPropsAt(mappings, path)
4853
if errors.Is(err, unstructured.ErrNotFound) {
4954
return nil
5055
} else if err != nil {
5156
return fmt.Errorf("failed to access mappings to expand references: %w", err)
5257
}
5358

54-
field, err := unstructured.AccessField[map[string]any](obj, fields...)
59+
field, err := unstructured.GetField[map[string]any](obj, path...)
5560
if err != nil {
56-
return fmt.Errorf("failed to access object's %v: %w", fields, err)
61+
return fmt.Errorf("failed to access object's %v: %w", path, err)
5762
}
5863
if err := h.scanProperties([]string{}, props, field); err != nil {
59-
return fmt.Errorf("failed to expand references at %v: %w", fields, err)
64+
return fmt.Errorf("failed to expand references at %v: %w", path, err)
6065
}
6166
return nil
6267
}
6368

64-
func (h *Handler) CollapseReferences(obj, mappings map[string]any, fields ...string) error {
69+
// CollapseReferences uses the handler context to collapse references on a given
70+
// unstructured value matching the main object being translated, using the
71+
// given reference mappings and acting on a particular path of the value object
72+
func (h *Handler) CollapseReferences(obj, mappings map[string]any, path ...string) error {
6573
h.expand = false
6674

67-
props, err := accessMappingPropsAt(mappings, fields)
75+
props, err := accessMappingPropsAt(mappings, path)
6876
if errors.Is(err, unstructured.ErrNotFound) {
6977
return nil
7078
} else if err != nil {
7179
return fmt.Errorf("failed to access mappings to collapse references: %w", err)
7280
}
7381

74-
field, err := unstructured.AccessField[map[string]any](obj, fields...)
82+
field, err := unstructured.GetField[map[string]any](obj, path...)
7583
if err != nil {
76-
return fmt.Errorf("failed to access object's %v: %w", fields, err)
84+
return fmt.Errorf("failed to access object's %v: %w", path, err)
7785
}
7886

7987
if err := h.scanProperties([]string{}, props, field); err != nil {
80-
return fmt.Errorf("failed to collapse references at %v: %w", fields, err)
88+
return fmt.Errorf("failed to collapse references at %v: %w", path, err)
8189
}
8290
return nil
8391
}
8492

93+
// Added returns any kubernetes objects created as references by ExpandReferences
8594
func (h *Handler) Added() []client.Object {
8695
return h.added
8796
}
8897

89-
func accessMappingPropsAt(mappings map[string]any, fields []string) (map[string]any, error) {
98+
// accessMappingPropsAt reads the mappings object at a given path
99+
func accessMappingPropsAt(mappings map[string]any, path []string) (map[string]any, error) {
90100
expandedPath := []string{"properties"}
91-
for _, field := range fields {
101+
for _, field := range path {
92102
expandedPath = append(expandedPath, field, "properties")
93103
}
94-
props, err := unstructured.AccessField[map[string]any](mappings, expandedPath...)
104+
props, err := unstructured.GetField[map[string]any](mappings, expandedPath...)
95105
if err != nil {
96106
return nil, fmt.Errorf("failed to access the API mapping properties for %v: %w", expandedPath, err)
97107
}
98108
return props, nil
99109
}
100110

111+
// scanProperties checks an object value path position holding field properties
112+
// against reference mappings that may apply at that path
101113
func (m *Handler) scanProperties(path []string, props, obj map[string]any) error {
102114
for key, prop := range props {
103115
mapping, ok := (prop).(map[string]any)
@@ -111,7 +123,7 @@ func (m *Handler) scanProperties(path []string, props, obj map[string]any) error
111123
}
112124
continue
113125
}
114-
rawField, err := unstructured.AccessField[any](obj, key)
126+
rawField, err := unstructured.GetField[any](obj, key)
115127
if errors.Is(err, unstructured.ErrNotFound) {
116128
continue
117129
}
@@ -135,8 +147,10 @@ func (m *Handler) scanProperties(path []string, props, obj map[string]any) error
135147
return nil
136148
}
137149

150+
// scanArray checks an object value path position holding an array against
151+
// reference mappings that may apply at that path
138152
func (m *Handler) scanArray(path []string, mapping map[string]any, list []any) error {
139-
mapItems, err := unstructured.AccessField[map[string]any](mapping, "items", "properties")
153+
mapItems, err := unstructured.GetField[map[string]any](mapping, "items", "properties")
140154
if err != nil {
141155
return fmt.Errorf("failed to access %q: %w", unstructured.Base(path), err)
142156
}
@@ -160,9 +174,11 @@ func (m *Handler) scanArray(path []string, mapping map[string]any, list []any) e
160174
return nil
161175
}
162176

177+
// scanObject checks an object value path position holding an object against
178+
// reference mappings that may apply at that path
163179
func (m *Handler) scanObject(path []string, mapName string, mapping, obj map[string]any) error {
164180
if mapping["properties"] != nil {
165-
props, err := unstructured.AccessField[map[string]any](mapping, "properties")
181+
props, err := unstructured.GetField[map[string]any](mapping, "properties")
166182
if err != nil {
167183
return fmt.Errorf("failed to access properties at %q: %w", path, err)
168184
}
@@ -174,6 +190,7 @@ func (m *Handler) scanObject(path []string, mapName string, mapping, obj map[str
174190
return fmt.Errorf("unsupported extension at %v with fields %v", path, unstructured.FieldsOf(mapping))
175191
}
176192

193+
// processReference kicks of a reference expansion or collapse
177194
func (m *Handler) processReference(path []string, mappingName string, mapping, obj map[string]any) error {
178195
rm := refMapping{}
179196
if err := unstructured.FromUnstructured(&rm, mapping); err != nil {
@@ -186,6 +203,8 @@ func (m *Handler) processReference(path []string, mappingName string, mapping, o
186203
return ref.Collapse(m.context, path, obj)
187204
}
188205

206+
// entryMatchingMapping returns the entry name key and value from an array that
207+
// matches the reference ebing collapsed or expanded
189208
func entryMatchingMapping(mapName string, mapping map[string]any, list []any, expand bool) (string, map[string]any, error) {
190209
key := mapName
191210
if expand {
@@ -200,6 +219,7 @@ func entryMatchingMapping(mapName string, mapping map[string]any, list []any, ex
200219
return key, m, err
201220
}
202221

222+
// findByExistingUniqueKey returns the value of an array holding a given key
203223
func findByExistingUniqueKey(list []any, key string) (map[string]any, error) {
204224
candidates := []map[string]any{}
205225
for _, item := range list {

internal/autogen/translate/refs/names.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"k8s.io/apimachinery/pkg/util/rand"
2222
)
2323

24+
// HashNames will return a hash corresponding to a name and optional arguments
2425
func HashNames(name string, args ...string) string {
2526
hasher := fnv.New64a()
2627
hasher.Write([]byte(name))
@@ -32,6 +33,7 @@ func HashNames(name string, args ...string) string {
3233
return rand.SafeEncodeString(fmt.Sprint(rawHash))
3334
}
3435

36+
// PrefixedName produces {prefix}-{hash} by using HashNames
3537
func PrefixedName(prefix string, name string, args ...string) string {
3638
return fmt.Sprintf("%s-%s", prefix, HashNames(name, args...))
3739
}

0 commit comments

Comments
 (0)