Skip to content
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

Adding APIGroups back to the schema #230

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/resources/apigroups/apigroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ import (

"github.com/rancher/apiserver/pkg/store/empty"
"github.com/rancher/apiserver/pkg/types"
wschemas "github.com/rancher/wrangler/v3/pkg/schemas"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
)

var BaseSchema = types.APISchema{
Schema: &wschemas.Schema{
ID: "apigroup",
Attributes: map[string]interface{}{
"group": "",
"kind": "APIGroup",
"version": "v1",
},
},
}

func Template(discovery discovery.DiscoveryInterface) schema.Template {
return schema.Template{
ID: "apigroup",
Expand Down
11 changes: 11 additions & 0 deletions pkg/schema/converter/k8stonorman.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"github.com/rancher/apiserver/pkg/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/steve/pkg/attributes"
"github.com/rancher/steve/pkg/resources/apigroups"
v1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/apiextensions.k8s.io/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
Expand Down Expand Up @@ -82,6 +84,8 @@ func GetGVKForKind(kind *proto.Kind) *schema.GroupVersionKind {
func ToSchemas(crd v1.CustomResourceDefinitionClient, client discovery.DiscoveryInterface) (map[string]*types.APISchema, error) {
result := map[string]*types.APISchema{}

addTemplateBased(result)

if err := addDiscovery(client, result); err != nil {
return nil, err
}
Expand All @@ -96,3 +100,10 @@ func ToSchemas(crd v1.CustomResourceDefinitionClient, client discovery.Discovery

return result, nil
}

// some schemas are not based on real resources but are filled-in by a template later on. This function adds the base
// schema so that these endpoints are still recognizable in the api
func addTemplateBased(schemas map[string]*types.APISchema) {
apiGroupGVK := attributes.GVK(&apigroups.BaseSchema)
schemas[GVKToVersionedSchemaID(apiGroupGVK)] = &apigroups.BaseSchema
}
88 changes: 61 additions & 27 deletions pkg/schema/converter/k8stonorman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/golang/mock/gomock"
openapiv2 "github.com/google/gnostic-models/openapiv2"
"github.com/rancher/apiserver/pkg/types"
"github.com/rancher/steve/pkg/resources/apigroups"
"github.com/rancher/steve/pkg/schema/table"
"github.com/rancher/wrangler/v3/pkg/generic/fake"
wranglerSchema "github.com/rancher/wrangler/v3/pkg/schemas"
Expand All @@ -20,34 +21,51 @@ import (
)

func TestToSchemas(t *testing.T) {
gvkExtensionMap := map[any]any{
gvkExtensionGroup: "TestGroup",
gvkExtensionVersion: "v1",
gvkExtensionKind: "TestResource",
}
gvkExtensionSlice := []any{gvkExtensionMap}
extensionSliceYaml, err := yaml.Marshal(gvkExtensionSlice)
require.NoError(t, err)
gvkSchema := openapiv2.NamedSchema{
Name: "TestResources",
Value: &openapiv2.Schema{
Description: "TestResources are test resource created for unit tests",
Type: &openapiv2.TypeItem{
Value: []string{"object"},
},
Properties: &openapiv2.Properties{
AdditionalProperties: []*openapiv2.NamedSchema{},
},
VendorExtension: []*openapiv2.NamedAny{
{
Name: gvkExtensionName,
Value: &openapiv2.Any{
Yaml: string(extensionSliceYaml),
createNamedSchema := func(name string, description string, gvk schema.GroupVersionKind) (*openapiv2.NamedSchema, error) {
gvkExtensionMap := map[any]any{
gvkExtensionGroup: gvk.Group,
gvkExtensionVersion: gvk.Version,
gvkExtensionKind: gvk.Kind,
}
gvkExtensionSlice := []any{gvkExtensionMap}
extensionSliceYaml, err := yaml.Marshal(gvkExtensionSlice)
if err != nil {
return nil, fmt.Errorf("unable to create named schema for %s: %w", name, err)
}
return &openapiv2.NamedSchema{
Name: name,
Value: &openapiv2.Schema{
Description: description,
Type: &openapiv2.TypeItem{
Value: []string{"object"},
},
Properties: &openapiv2.Properties{
AdditionalProperties: []*openapiv2.NamedSchema{},
},
VendorExtension: []*openapiv2.NamedAny{
{
Name: gvkExtensionName,
Value: &openapiv2.Any{
Yaml: string(extensionSliceYaml),
},
},
},
},
},
}, nil
}
apiGroupDescription := "APIGroup contains the name, the supported versions, and the preferred version of a group"
gvkSchema, err := createNamedSchema("TestResources", "TestResources are test resource created for unit tests", schema.GroupVersionKind{
Group: "TestGroup",
Version: "v1",
Kind: "TestResource",
})
require.NoError(t, err)
apiGroupSchema, err := createNamedSchema("ApiGroups", apiGroupDescription, schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "APIGroup",
})
require.NoError(t, err)
tests := []struct {
name string
groups []schema.GroupVersion
Expand Down Expand Up @@ -160,6 +178,7 @@ func TestToSchemas(t *testing.T) {
Description: "Test Resource for unit tests",
},
},
"core.v1.apigroup": &apigroups.BaseSchema,
},
},
{
Expand Down Expand Up @@ -194,6 +213,7 @@ func TestToSchemas(t *testing.T) {
},
},
},
"core.v1.apigroup": &apigroups.BaseSchema,
},
},
{
Expand Down Expand Up @@ -260,8 +280,10 @@ func TestToSchemas(t *testing.T) {
},
},
},
wantError: false,
desiredSchema: map[string]*types.APISchema{},
wantError: false,
desiredSchema: map[string]*types.APISchema{
"core.v1.apigroup": &apigroups.BaseSchema,
},
},
{
name: "discovery error",
Expand Down Expand Up @@ -305,6 +327,7 @@ func TestToSchemas(t *testing.T) {
},
},
},
"core.v1.apigroup": &apigroups.BaseSchema,
},
},
{
Expand All @@ -326,7 +349,7 @@ func TestToSchemas(t *testing.T) {
crds: []v1.CustomResourceDefinition{},
document: &openapiv2.Document{
Definitions: &openapiv2.Definitions{
AdditionalProperties: []*openapiv2.NamedSchema{&gvkSchema},
AdditionalProperties: []*openapiv2.NamedSchema{gvkSchema, apiGroupSchema},
},
},
wantError: false,
Expand All @@ -346,6 +369,17 @@ func TestToSchemas(t *testing.T) {
},
},
},
"core.v1.apigroup": {
Schema: &wranglerSchema.Schema{
ID: "apigroup",
Attributes: map[string]interface{}{
"group": "",
"kind": "APIGroup",
"version": "v1",
},
Description: apiGroupDescription,
},
},
},
},
{
Expand Down
Loading