Skip to content

Commit

Permalink
Merge pull request #117 from tnthornton/this-is-fine
Browse files Browse the repository at this point in the history
Utilize unstructured informer cache
  • Loading branch information
tnthornton authored May 1, 2023
2 parents 22a8306 + ca22d14 commit 2ff8300
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 43 deletions.
6 changes: 2 additions & 4 deletions internal/graph/resolvers/apiextensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ func (r *xrd) getCrd(ctx context.Context, group string, names *model.CompositeRe
}

nn := types.NamespacedName{Name: fmt.Sprintf("%s.%s", names.Plural, group)}
in := &unstructured.CustomResourceDefinition{}
in.SetAPIVersion("apiextensions.k8s.io/v1")
in.SetKind("CustomResourceDefinition")
if err := c.Get(ctx, nn, in); err != nil {
in := unstructured.NewCRD()
if err := c.Get(ctx, nn, in.GetUnstructured()); err != nil {
graphql.AddError(ctx, errors.Wrap(err, errGetCRD))
return nil, nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/graph/resolvers/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (r *managedResource) Definition(ctx context.Context, obj *model.ManagedReso

nn := types.NamespacedName{Name: fmt.Sprintf("%s.%s", name, gv.Group)}
in := unstructured.NewCRD()
err = c.Get(ctx, nn, in)
err = c.Get(ctx, nn, in.GetUnstructured())

if err != nil && !kerrors.IsNotFound(err) {
graphql.AddError(ctx, errors.Wrap(err, errGetCRD))
Expand All @@ -86,7 +86,7 @@ func (r *managedResource) Definition(ctx context.Context, obj *model.ManagedReso
// can find the matching one.
if kerrors.IsNotFound(err) {
lin := unstructured.NewCRDList()
if err := c.List(ctx, lin); err != nil {
if err := c.List(ctx, lin.GetUnstructuredList()); err != nil {
graphql.AddError(ctx, errors.Wrap(err, errListCRDs))
return nil, nil
}
Expand Down
14 changes: 5 additions & 9 deletions internal/graph/resolvers/managed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestManagedResourceDefinition(t *testing.T) {
clients: ClientCacheFn(func(_ auth.Credentials, _ ...clients.GetOption) (client.Client, error) {
return &test.MockClient{
MockGet: test.NewMockGetFn(nil, func(obj client.Object) error {
*obj.(*unstructured.CustomResourceDefinition) = *crd
*obj.(*kunstructured.Unstructured) = *crd.GetUnstructured()
return nil
}),
}, nil
Expand All @@ -168,10 +168,8 @@ func TestManagedResourceDefinition(t *testing.T) {
return errNotFound
}),
MockList: test.NewMockListFn(nil, func(obj client.ObjectList) error {
*obj.(*unstructured.CustomResourceDefinitionList) = unstructured.CustomResourceDefinitionList{
UnstructuredList: kunstructured.UnstructuredList{
Items: []kunstructured.Unstructured{otherGroup.Unstructured, otherKind.Unstructured, crdDifferingPlural.Unstructured},
},
*obj.(*kunstructured.UnstructuredList) = kunstructured.UnstructuredList{
Items: []kunstructured.Unstructured{otherGroup.Unstructured, otherKind.Unstructured, crdDifferingPlural.Unstructured},
}
return nil
}),
Expand All @@ -196,10 +194,8 @@ func TestManagedResourceDefinition(t *testing.T) {
return errNotFound
}),
MockList: test.NewMockListFn(nil, func(obj client.ObjectList) error {
*obj.(*unstructured.CustomResourceDefinitionList) = unstructured.CustomResourceDefinitionList{
UnstructuredList: kunstructured.UnstructuredList{
Items: []kunstructured.Unstructured{otherGroup.Unstructured, otherKind.Unstructured},
},
*obj.(*kunstructured.UnstructuredList) = kunstructured.UnstructuredList{
Items: []kunstructured.Unstructured{otherGroup.Unstructured, otherKind.Unstructured},
}
return nil
}),
Expand Down
7 changes: 4 additions & 3 deletions internal/graph/resolvers/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

"github.com/upbound/xgql/internal/auth"
"github.com/upbound/xgql/internal/graph/model"
xunstructured "github.com/upbound/xgql/internal/unstructured"
)

const (
Expand Down Expand Up @@ -173,13 +174,13 @@ func (r *providerRevisionStatus) Objects(ctx context.Context, obj *model.Provide
continue
}

crd := &kextv1.CustomResourceDefinition{}
if err := c.Get(ctx, types.NamespacedName{Name: ref.Name}, crd); err != nil {
crd := xunstructured.NewCRD()
if err := c.Get(ctx, types.NamespacedName{Name: ref.Name}, crd.GetUnstructured()); err != nil {
graphql.AddError(ctx, errors.Wrap(err, errGetCRD))
continue
}

out.Nodes = append(out.Nodes, model.GetCustomResourceDefinitionFromCRD(crd))
out.Nodes = append(out.Nodes, model.GetCustomResourceDefinition(crd))
out.TotalCount++
}

Expand Down
3 changes: 2 additions & 1 deletion internal/graph/resolvers/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/upbound/xgql/internal/clients"
"github.com/upbound/xgql/internal/graph/generated"
"github.com/upbound/xgql/internal/graph/model"
xunstructured "github.com/upbound/xgql/internal/unstructured"
)

var (
Expand Down Expand Up @@ -316,7 +317,7 @@ func TestProviderActiveRevision(t *testing.T) {
func TestProviderRevisionStatusObjects(t *testing.T) {
errBoom := errors.New("boom")

gcrd := model.GetCustomResourceDefinitionFromCRD(&kextv1.CustomResourceDefinition{})
gcrd := model.GetCustomResourceDefinition(&xunstructured.CustomResourceDefinition{})

type args struct {
ctx context.Context
Expand Down
4 changes: 2 additions & 2 deletions internal/graph/resolvers/providerconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (r *providerConfig) Definition(ctx context.Context, obj *model.ProviderConf
nn := types.NamespacedName{Name: fmt.Sprintf("%s.%s", name, gv.Group)}

in := unstructured.NewCRD()
err = c.Get(ctx, nn, in)
err = c.Get(ctx, nn, in.GetUnstructured())

if err != nil && !kerrors.IsNotFound(err) {
graphql.AddError(ctx, errors.Wrap(err, errGetCRD))
Expand All @@ -85,7 +85,7 @@ func (r *providerConfig) Definition(ctx context.Context, obj *model.ProviderConf
// can find the matching one.
if kerrors.IsNotFound(err) {
lin := unstructured.NewCRDList()
if err := c.List(ctx, lin); err != nil {
if err := c.List(ctx, lin.GetUnstructuredList()); err != nil {
graphql.AddError(ctx, errors.Wrap(err, errListCRDs))
return nil, nil
}
Expand Down
14 changes: 5 additions & 9 deletions internal/graph/resolvers/providerconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestProviderConfigDefinition(t *testing.T) {
clients: ClientCacheFn(func(_ auth.Credentials, _ ...clients.GetOption) (client.Client, error) {
return &test.MockClient{
MockGet: test.NewMockGetFn(nil, func(obj client.Object) error {
*obj.(*unstructured.CustomResourceDefinition) = *crd
*obj.(*kunstructured.Unstructured) = *crd.GetUnstructured()
return nil
}),
}, nil
Expand All @@ -166,10 +166,8 @@ func TestProviderConfigDefinition(t *testing.T) {
return errNotFound
}),
MockList: test.NewMockListFn(nil, func(obj client.ObjectList) error {
*obj.(*unstructured.CustomResourceDefinitionList) = unstructured.CustomResourceDefinitionList{
UnstructuredList: kunstructured.UnstructuredList{
Items: []kunstructured.Unstructured{otherGroup.Unstructured, otherKind.Unstructured, crdDifferingPlural.Unstructured},
},
*obj.(*kunstructured.UnstructuredList) = kunstructured.UnstructuredList{
Items: []kunstructured.Unstructured{otherGroup.Unstructured, otherKind.Unstructured, crdDifferingPlural.Unstructured},
}
return nil
}),
Expand All @@ -194,10 +192,8 @@ func TestProviderConfigDefinition(t *testing.T) {
return errNotFound
}),
MockList: test.NewMockListFn(nil, func(obj client.ObjectList) error {
*obj.(*unstructured.CustomResourceDefinitionList) = unstructured.CustomResourceDefinitionList{
UnstructuredList: kunstructured.UnstructuredList{
Items: []kunstructured.Unstructured{otherGroup.Unstructured, otherKind.Unstructured},
},
*obj.(*kunstructured.UnstructuredList) = kunstructured.UnstructuredList{
Items: []kunstructured.Unstructured{otherGroup.Unstructured, otherKind.Unstructured},
}
return nil
}),
Expand Down
2 changes: 1 addition & 1 deletion internal/graph/resolvers/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func (r *query) CustomResourceDefinitions(ctx context.Context, revision *model.R
}

in := xunstructured.NewCRDList()
if err := c.List(ctx, in); err != nil {
if err := c.List(ctx, in.GetUnstructuredList()); err != nil {
graphql.AddError(ctx, errors.Wrap(err, errListConfigs))
return nil, nil
}
Expand Down
20 changes: 8 additions & 12 deletions internal/graph/resolvers/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,12 +1067,10 @@ func TestQueryCustomResourceDefinitions(t *testing.T) {
clients: ClientCacheFn(func(_ auth.Credentials, _ ...clients.GetOption) (client.Client, error) {
return &test.MockClient{
MockList: test.NewMockListFn(nil, func(obj client.ObjectList) error {
*obj.(*xunstructured.CustomResourceDefinitionList) = xunstructured.CustomResourceDefinitionList{
UnstructuredList: unstructured.UnstructuredList{
Items: []unstructured.Unstructured{
dangler.Unstructured,
owned.Unstructured,
},
*obj.(*unstructured.UnstructuredList) = unstructured.UnstructuredList{
Items: []unstructured.Unstructured{
dangler.Unstructured,
owned.Unstructured,
},
}
return nil
Expand All @@ -1097,12 +1095,10 @@ func TestQueryCustomResourceDefinitions(t *testing.T) {
clients: ClientCacheFn(func(_ auth.Credentials, _ ...clients.GetOption) (client.Client, error) {
return &test.MockClient{
MockList: test.NewMockListFn(nil, func(obj client.ObjectList) error {
*obj.(*xunstructured.CustomResourceDefinitionList) = xunstructured.CustomResourceDefinitionList{
UnstructuredList: unstructured.UnstructuredList{
Items: []unstructured.Unstructured{
dangler.Unstructured,
owned.Unstructured,
},
*obj.(*unstructured.UnstructuredList) = unstructured.UnstructuredList{
Items: []unstructured.Unstructured{
dangler.Unstructured,
owned.Unstructured,
},
}
return nil
Expand Down
5 changes: 5 additions & 0 deletions internal/unstructured/customresourcedefinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ func (c *CustomResourceDefinition) GetUnstructured() *unstructured.Unstructured
return &c.Unstructured
}

// GetUnstructuredList of this CustomResourceDefinitionList.
func (c *CustomResourceDefinitionList) GetUnstructuredList() *unstructured.UnstructuredList {
return &c.UnstructuredList
}

// GetSpecGroup of this CustomResourceDefinition.
func (c *CustomResourceDefinition) GetSpecGroup() string {
p, err := fieldpath.Pave(c.Object).GetString("spec.group")
Expand Down

0 comments on commit 2ff8300

Please sign in to comment.