-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: properly determine list objects in schemagen #3205
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
provider/pkg/gen/testdata/identify-list-kinds/list-kind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
-- definitions -- | ||
{ | ||
"com.pulumi.k8sversion.test.TestResource": { | ||
"properties": { | ||
"apiVersion": { | ||
"description": "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources", | ||
"type": "string" | ||
}, | ||
"kind": { | ||
"description": "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds", | ||
"type": "string" | ||
}, | ||
"metadata": { | ||
"description": "Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata", | ||
"type": "object", | ||
"properties": { | ||
"fakeField": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"spec": { | ||
"type": "string" | ||
} | ||
}, | ||
"x-kubernetes-group-version-kind": [ | ||
{ | ||
"group": "k8sversion.pulumi.com", | ||
"kind": "TestResource", | ||
"version": "test" | ||
} | ||
], | ||
"x-kubernetes-selectable-fields": [] | ||
}, | ||
"com.pulumi.k8sversion.test.TestResourceList": { | ||
"description": "TestResourceList is a list of TestResource", | ||
"type": "object", | ||
"properties": { | ||
"apiVersion": { | ||
"description": "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources", | ||
"type": "string" | ||
}, | ||
"items": { | ||
"description": "List of testresources. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md", | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/com.pulumi.k8sversion.test.TestResource" | ||
} | ||
}, | ||
"kind": { | ||
"description": "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds", | ||
"type": "string" | ||
}, | ||
"metadata": { | ||
"description": "Standard list metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds", | ||
"type": "object", | ||
"properties": { | ||
"fakeField": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
}, | ||
"x-kubernetes-group-version-kind": [ | ||
{ | ||
"group": "k8sversion.pulumi.com", | ||
"kind": "TestResourceList", | ||
"version": "test" | ||
} | ||
], | ||
"x-kubernetes-selectable-fields": [] | ||
}, | ||
"com.pulumi.k8sversion.test.TestResourceNotRealList": { | ||
"description": "TestResourceNotRealList is not a real list object.", | ||
"type": "object", | ||
"properties": { | ||
"apiVersion": { | ||
"description": "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources", | ||
"type": "string" | ||
}, | ||
"kind": { | ||
"description": "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds", | ||
"type": "string" | ||
}, | ||
"metadata": { | ||
"description": "Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata", | ||
"type": "object", | ||
"properties": { | ||
"fakeField": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"spec": { | ||
"type": "string" | ||
} | ||
}, | ||
"x-kubernetes-group-version-kind": [ | ||
{ | ||
"group": "k8sversion.pulumi.com", | ||
"kind": "TestResourceNotRealList", | ||
"version": "test" | ||
} | ||
], | ||
"x-kubernetes-selectable-fields": [] | ||
}, | ||
"com.pulumi.k8sversion.test.TestResourceNotRealListList": { | ||
"description": "TestResourceNotRealListList is a list of TestResourceNotRealList", | ||
"type": "object", | ||
"properties": { | ||
"apiVersion": { | ||
"description": "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources", | ||
"type": "string" | ||
}, | ||
"items": { | ||
"description": "List of testresourcelists. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md", | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/com.pulumi.k8sversion.test.TestResourceNotRealList" | ||
} | ||
}, | ||
"kind": { | ||
"description": "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds", | ||
"type": "string" | ||
}, | ||
"metadata": { | ||
"description": "Standard list metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds", | ||
"type": "object", | ||
"properties": { | ||
"fakeField": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
}, | ||
"x-kubernetes-group-version-kind": [ | ||
{ | ||
"group": "k8sversion.pulumi.com", | ||
"kind": "TestResourceNotRealListList", | ||
"version": "test" | ||
} | ||
], | ||
"x-kubernetes-selectable-fields": [] | ||
} | ||
} | ||
|
||
-- kinds -- | ||
- k8sversion.test.TestResource | ||
- k8sversion.test.TestResourceNotRealList | ||
|
||
-- listKinds -- | ||
- k8sversion.test.TestResourceList | ||
- k8sversion.test.TestResourceNotRealListList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,7 @@ type KindConfig struct { | |
defaultAPIVersion string | ||
|
||
isNested bool | ||
isList bool // Indicates if this kind is a list. | ||
|
||
schemaPkgName string | ||
} | ||
|
@@ -486,6 +487,7 @@ func createGroups(definitionsJSON map[string]any, allowHyphens bool) []GroupConf | |
} | ||
def.canonicalGroup = group | ||
} | ||
|
||
return def | ||
}). | ||
WhereT(func(d definition) bool { return d.canonicalGroup != "" }). | ||
|
@@ -593,13 +595,23 @@ func createGroups(definitionsJSON map[string]any, allowHyphens bool) []GroupConf | |
|
||
defaultAPIVersion := d.defaultAPIVersion() | ||
isTopLevel := d.isTopLevel() | ||
isList := false | ||
|
||
ps := linq.From(d.data["properties"]). | ||
OrderByT(func(kv linq.KeyValue) string { return kv.Key.(string) }). | ||
SelectT(func(kv linq.KeyValue) Property { | ||
propName := kv.Key.(string) | ||
prop := d.data["properties"].(map[string]any)[propName].(map[string]any) | ||
|
||
// Determine if kind is a list resource if it contains an `items` property that is an array and Kind name ends in `List`. | ||
// Ref: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#types-kinds | ||
propType, ok := prop["type"].(string) | ||
if ok { | ||
if propName == "items" && propType == "array" && strings.HasSuffix(d.gvk.Kind, "List") { | ||
isList = true | ||
} | ||
} | ||
|
||
schemaType := makeSchemaType(prop, canonicalGroups) | ||
|
||
// `-` is invalid in variable names, so replace with `_` | ||
|
@@ -693,6 +705,14 @@ func createGroups(definitionsJSON map[string]any, allowHyphens bool) []GroupConf | |
|
||
return fmt.Sprintf("%s/%s", gStripped, v) | ||
} | ||
|
||
// These resources are hard-coded as lists as they do not adhere to the normal conventions. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The list is nested under the |
||
if d.gvk.Group == "meta" && | ||
d.gvk.Version == "v1" && | ||
(d.gvk.Kind == "APIResourceList" || d.gvk.Kind == "APIGroupList") { | ||
isList = true | ||
} | ||
|
||
return linq.From([]KindConfig{ | ||
{ | ||
kind: d.gvk.Kind, | ||
|
@@ -708,6 +728,7 @@ func createGroups(definitionsJSON map[string]any, allowHyphens bool) []GroupConf | |
defaultAPIVersion: defaultAPIVersion, | ||
isNested: !isTopLevel, | ||
schemaPkgName: schemaPkgName(apiVersion), | ||
isList: isList, | ||
}, | ||
}) | ||
}). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright 2016-2024, Pulumi Corporation. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// nolint: lll | ||
package gen | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/tools/txtar" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// TestCreateGroups_IdentifyListKinds loads txtar files under testdata/identify-list-kinds and uses them to | ||
// craft an unstructured map[string]any definitions file for createGroups. | ||
// The goal of this test is to ensure we can accurately distinuguish between singletons and lists of kinds. | ||
// | ||
// The test files should contain the following files: | ||
// - definitions: a JSON file containing the definitions | ||
// - kinds: a YAML file containing a list of kinds that are singletons | ||
// - listKinds: a YAML file containing a list of kinds that are lists/collections of singleton kinds | ||
func TestCreateGroups_IdentifyListKinds(t *testing.T) { | ||
dir := filepath.Join("testdata/identify-list-kinds") | ||
tests, err := os.ReadDir(dir) | ||
require.NoError(t, err) | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.Name(), func(t *testing.T) { | ||
archive, err := txtar.ParseFile(filepath.Join(dir, tt.Name())) | ||
require.NoError(t, err) | ||
|
||
var definitions map[string]any | ||
var kinds, listKinds map[string]struct{} | ||
|
||
for _, f := range archive.Files { | ||
var parsed []string | ||
switch f.Name { | ||
case "definitions": | ||
err := json.Unmarshal(f.Data, &definitions) | ||
require.NoError(t, err, f.Name) | ||
case "kinds": | ||
err = yaml.Unmarshal(f.Data, &parsed) | ||
require.NoError(t, err, f.Name) | ||
kinds = sliceToSet(parsed) | ||
case "listKinds": | ||
err = yaml.Unmarshal(f.Data, &parsed) | ||
require.NoError(t, err, f.Name) | ||
listKinds = sliceToSet(parsed) | ||
default: | ||
t.Fatal("unrecognized filename", f.Name) | ||
} | ||
} | ||
|
||
configGroups := createGroups(definitions, true) | ||
|
||
// Loop through all parsed kinds and ensure they are accounted for. | ||
for _, g := range configGroups { | ||
for _, v := range g.versions { | ||
for _, kind := range v.kinds { | ||
gvk := gvkToString(kind.gvk.Group, kind.gvk.Version, kind.gvk.Kind) | ||
if kind.isList { | ||
delete(listKinds, gvk) | ||
} else { | ||
delete(kinds, gvk) | ||
} | ||
} | ||
} | ||
} | ||
|
||
assert.Equal(t, 0, len(kinds), "kinds not found while parsing: %v", kinds) | ||
assert.Equal(t, 0, len(listKinds), "listKinds not found while parsing: %v", listKinds) | ||
}) | ||
} | ||
} | ||
|
||
func gvkToString(group, version, kind string) string { | ||
return group + "." + version + "." + kind | ||
} | ||
|
||
func sliceToSet(slice []string) map[string]struct{} { | ||
set := make(map[string]struct{}) | ||
for _, item := range slice { | ||
set[item] = struct{}{} | ||
} | ||
return set | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A custom resource that ends in
List
, but is not a List kind.