Skip to content

Commit b067880

Browse files
authored
CLOUDP-350407: Public interface tweaks for translate (#2827)
1 parent afe130f commit b067880

36 files changed

+557
-500
lines changed

internal/autogen/translate/crds/crds.go

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

26-
// AssertMajorVersion checks the given majorVersion exists for the given kind
27-
// and CRD version
26+
// AssertMajorVersion checks the given SDK majorVersion exists for the given
27+
// Kubernetes kind and CRD version
2828
func AssertMajorVersion(specVersion *apiextensionsv1.CustomResourceDefinitionVersion, kind string, majorVersion string) error {
29-
props, err := GetOpenAPIProperties(kind, specVersion)
29+
props, err := getOpenAPIProperties(kind, specVersion)
3030
if err != nil {
3131
return fmt.Errorf("failed to enumerate CRD schema properties: %w", err)
3232
}
33-
specProps, err := GetSpecPropertiesFor(kind, props, "spec")
33+
specProps, err := getSpecPropertiesFor(kind, props, "spec")
3434
if err != nil {
3535
return fmt.Errorf("failed to enumerate CRD spec properties: %w", err)
3636
}
@@ -41,7 +41,9 @@ func AssertMajorVersion(specVersion *apiextensionsv1.CustomResourceDefinitionVer
4141
return nil
4242
}
4343

44-
// CompileCRDSchema compiles the given JSON schema properties
44+
// CompileCRDSchema compiles the given JSON properties as a type schema.
45+
// Enables validating whether or not some JSON or unstructured data comforms
46+
// with such type schema.
4547
func CompileCRDSchema(openAPISchema *apiextensionsv1.JSONSchemaProps) (*jsonschema.Schema, error) {
4648
schemaBytes, err := json.Marshal(openAPISchema)
4749
if err != nil {
@@ -58,8 +60,7 @@ func CompileCRDSchema(openAPISchema *apiextensionsv1.JSONSchemaProps) (*jsonsche
5860
return schema, nil
5961
}
6062

61-
// SelectVersion returns the version from the CRD spec that matches the given
62-
// version string
63+
// SelectVersion extracts the CRD version definition matching the given version.
6364
func SelectVersion(spec *apiextensionsv1.CustomResourceDefinitionSpec, version string) *apiextensionsv1.CustomResourceDefinitionVersion {
6465
if len(spec.Versions) == 0 {
6566
return nil
@@ -75,9 +76,9 @@ func SelectVersion(spec *apiextensionsv1.CustomResourceDefinitionSpec, version s
7576
return nil
7677
}
7778

78-
// GetOpenAPIProperties digs up the schema properties of a given kind on a given
79-
// CRD version
80-
func GetOpenAPIProperties(kind string, version *apiextensionsv1.CustomResourceDefinitionVersion) (map[string]apiextensionsv1.JSONSchemaProps, error) {
79+
// getOpenAPIProperties extracts the schema properties of a given CRD version.
80+
// The kind is passed as a reference of the object this version is from.
81+
func getOpenAPIProperties(kind string, version *apiextensionsv1.CustomResourceDefinitionVersion) (map[string]apiextensionsv1.JSONSchemaProps, error) {
8182
if version == nil {
8283
return nil, fmt.Errorf("missing version (nil) from %v spec", kind)
8384
}
@@ -93,9 +94,9 @@ func GetOpenAPIProperties(kind string, version *apiextensionsv1.CustomResourceDe
9394
return version.Schema.OpenAPIV3Schema.Properties, nil
9495
}
9596

96-
// GetSpecPropertiesFor takes the properties value of a given field of a kind's
97+
// getSpecPropertiesFor takes the properties value of a given field of a kind's
9798
// properties set
98-
func GetSpecPropertiesFor(kind string, props map[string]apiextensionsv1.JSONSchemaProps, field string) (map[string]apiextensionsv1.JSONSchemaProps, error) {
99+
func getSpecPropertiesFor(kind string, props map[string]apiextensionsv1.JSONSchemaProps, field string) (map[string]apiextensionsv1.JSONSchemaProps, error) {
99100
prop, ok := props[field]
100101
if !ok {
101102
return nil, fmt.Errorf("kind %q spec is missing field %q on", kind, field)

internal/autogen/translate/crds/crds_test.go

Lines changed: 0 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package crds_test
1717
import (
1818
"bufio"
1919
"bytes"
20-
"fmt"
2120
"testing"
2221

2322
"github.com/stretchr/testify/assert"
@@ -108,173 +107,3 @@ func TestSelectVersion(t *testing.T) {
108107
})
109108
}
110109
}
111-
112-
func TestGetOpenAPIProperties(t *testing.T) {
113-
const kind = "MyTestCRD"
114-
115-
happyPathVersion := &apiextensionsv1.CustomResourceDefinitionVersion{
116-
Name: "v1",
117-
Schema: &apiextensionsv1.CustomResourceValidation{
118-
OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{
119-
Properties: map[string]apiextensionsv1.JSONSchemaProps{
120-
"spec": {
121-
Type: "object",
122-
Properties: map[string]apiextensionsv1.JSONSchemaProps{
123-
"fieldA": {Type: "string"},
124-
},
125-
},
126-
},
127-
},
128-
},
129-
}
130-
131-
testCases := []struct {
132-
name string
133-
version *apiextensionsv1.CustomResourceDefinitionVersion
134-
wantProperties map[string]apiextensionsv1.JSONSchemaProps
135-
wantErrMsg string
136-
}{
137-
{
138-
name: "should return properties",
139-
version: happyPathVersion,
140-
wantProperties: happyPathVersion.Schema.OpenAPIV3Schema.Properties,
141-
wantErrMsg: "",
142-
},
143-
{
144-
name: "should return error if version is nil",
145-
version: nil,
146-
wantProperties: nil,
147-
wantErrMsg: fmt.Sprintf("missing version (nil) from %v spec", kind),
148-
},
149-
{
150-
name: "should return error if schema is nil",
151-
version: &apiextensionsv1.CustomResourceDefinitionVersion{
152-
Name: "v1",
153-
Schema: nil, // The point of failure
154-
},
155-
wantProperties: nil,
156-
wantErrMsg: fmt.Sprintf("missing version schema from %v spec", kind),
157-
},
158-
{
159-
name: "error - should return error if OpenAPIV3Schema is nil",
160-
version: &apiextensionsv1.CustomResourceDefinitionVersion{
161-
Name: "v1",
162-
Schema: &apiextensionsv1.CustomResourceValidation{
163-
OpenAPIV3Schema: nil, // The point of failure
164-
},
165-
},
166-
wantProperties: nil,
167-
wantErrMsg: fmt.Sprintf("missing version OpenAPI Schema from %v spec", kind),
168-
},
169-
{
170-
name: "should return error if Properties map is nil",
171-
version: &apiextensionsv1.CustomResourceDefinitionVersion{
172-
Name: "v1",
173-
Schema: &apiextensionsv1.CustomResourceValidation{
174-
OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{
175-
Properties: nil, // The point of failure
176-
},
177-
},
178-
},
179-
wantProperties: nil,
180-
wantErrMsg: fmt.Sprintf("missing version OpenAPI Properties from %v spec", kind),
181-
},
182-
}
183-
184-
for _, tc := range testCases {
185-
t.Run(tc.name, func(t *testing.T) {
186-
gotProperties, err := crds.GetOpenAPIProperties(kind, tc.version)
187-
if tc.wantErrMsg != "" {
188-
require.Error(t, err)
189-
require.EqualError(t, err, tc.wantErrMsg)
190-
require.Nil(t, gotProperties)
191-
} else {
192-
require.NoError(t, err)
193-
require.Equal(t, tc.wantProperties, gotProperties)
194-
}
195-
})
196-
}
197-
}
198-
199-
func TestGetSpecPropertiesFor(t *testing.T) {
200-
const kind = "MyTestCRD"
201-
202-
// Reusable properties for the test cases
203-
nestedProperties := map[string]apiextensionsv1.JSONSchemaProps{
204-
"replicas": {Type: "integer"},
205-
"image": {Type: "string"},
206-
}
207-
208-
testCases := []struct {
209-
name string
210-
props map[string]apiextensionsv1.JSONSchemaProps
211-
field string
212-
wantProperties map[string]apiextensionsv1.JSONSchemaProps
213-
wantErrMsg string
214-
}{
215-
{
216-
name: "should return nested properties",
217-
props: map[string]apiextensionsv1.JSONSchemaProps{
218-
"spec": {
219-
Type: "object",
220-
Properties: nestedProperties,
221-
},
222-
"status": {
223-
Type: "object",
224-
},
225-
},
226-
field: "spec",
227-
wantProperties: nestedProperties,
228-
wantErrMsg: "", // Expect no error
229-
},
230-
{
231-
name: "field is missing from properties map",
232-
props: map[string]apiextensionsv1.JSONSchemaProps{
233-
"status": {Type: "object"},
234-
},
235-
field: "spec", // This field does not exist
236-
wantProperties: nil,
237-
wantErrMsg: fmt.Sprintf("kind %q spec is missing field %q on", kind, "spec"),
238-
},
239-
{
240-
name: "field is not of type object",
241-
props: map[string]apiextensionsv1.JSONSchemaProps{
242-
"spec": {Type: "string"}, // The point of failure
243-
},
244-
field: "spec",
245-
wantProperties: nil,
246-
wantErrMsg: fmt.Sprintf("kind %q field %q expected to be object but is %v", kind, "spec", "string"),
247-
},
248-
{
249-
name: "field is an object but has no nested properties",
250-
props: map[string]apiextensionsv1.JSONSchemaProps{
251-
"spec": {
252-
Type: "object",
253-
Properties: nil, // The nested map is nil
254-
},
255-
},
256-
field: "spec",
257-
wantProperties: nil, // Expecting a nil map is correct here
258-
wantErrMsg: "",
259-
},
260-
}
261-
262-
for _, tc := range testCases {
263-
t.Run(tc.name, func(t *testing.T) {
264-
// Act
265-
gotProperties, err := crds.GetSpecPropertiesFor(kind, tc.props, tc.field)
266-
267-
// Assert
268-
if tc.wantErrMsg != "" {
269-
// We expect an error
270-
require.Error(t, err)
271-
require.EqualError(t, err, tc.wantErrMsg)
272-
require.Nil(t, gotProperties)
273-
} else {
274-
// We expect success
275-
require.NoError(t, err)
276-
require.Equal(t, tc.wantProperties, gotProperties)
277-
}
278-
})
279-
}
280-
}

0 commit comments

Comments
 (0)