Skip to content

Commit

Permalink
Use resource descriptor GVK for NewSession (#120)
Browse files Browse the repository at this point in the history
Description of changes:
Some version of K8s do not reliably return `TypeMeta` information when you call `apiReader.Get()` (see kubernetes/kubernetes#3030 and kubernetes/kubernetes#80609). This is a [known bug](kubernetes-sigs/controller-runtime#1517) in `controller-runtime` that they don't plan on fixing. 

Parts of the code, namely around setting up the user agent, currently rely on these fields - and they are currently being given an empty struct (with empty strings for all values). To work around this bug, this PR has introduced a new `GroupVersionKind()` getter in the `ResourceDescriptor` (replacing the existing `GroupKind`), which returns a static description of the GVK. Then, rather than referencing the `RuntimeObject` `TypeMeta` properties, we can reference this new GVK getter anywhere in the runtime. It also replaces any existing use of `GroupKind` to now use `GroupVersionKind`.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
RedbackThomson authored Mar 22, 2023
1 parent 02fe7bf commit e526d54
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 67 deletions.
16 changes: 7 additions & 9 deletions mocks/pkg/types/aws_resource_descriptor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions mocks/pkg/types/aws_resource_reconciler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions mocks/pkg/types/field_export_reconciler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions pkg/runtime/adoption_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (r *adoptionReconciler) reconcile(ctx context.Context, req ctrlrt.Request)
controllerRMF = v
break
}
if gk.Group != controllerRMF.ResourceDescriptor().GroupKind().Group {
if gk.Group != controllerRMF.ResourceDescriptor().GroupVersionKind().Group {
ackrtlog.DebugAdoptedResource(r.log, res, "target resource API group is not of this service. no-op")
return nil
}
Expand All @@ -112,11 +112,9 @@ func (r *adoptionReconciler) reconcile(ctx context.Context, req ctrlrt.Request)
region := r.getRegion(res)
roleARN := r.getRoleARN(acctID)
endpointURL := r.getEndpointURL(res)
gvk := targetDescriptor.GroupVersionKind()

sess, err := r.sc.NewSession(
region, &endpointURL, roleARN,
targetDescriptor.EmptyRuntimeObject().GetObjectKind().GroupVersionKind(),
)
sess, err := r.sc.NewSession(region, &endpointURL, roleARN, gvk)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/runtime/field_export_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
jq "github.com/itchyny/gojq"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
ctrlrt "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -106,7 +106,7 @@ func (r *fieldExportReconciler) reconcileFieldExport(ctx context.Context, req ct
controllerRMF = v
break
}
if sourceGK.Group != controllerRMF.ResourceDescriptor().GroupKind().Group {
if sourceGK.Group != controllerRMF.ResourceDescriptor().GroupVersionKind().Group {
ackrtlog.DebugFieldExport(r.log, feObject, "target resource API group is not of this service. no-op")
return nil
}
Expand Down Expand Up @@ -382,7 +382,7 @@ func (r *fieldExportReconciler) writeToSecret(

func (r *fieldExportReconciler) GetFieldExportsForResource(
ctx context.Context,
gk metav1.GroupKind,
gk schema.GroupKind,
nsn types.NamespacedName,
) ([]ackv1alpha1.FieldExport, error) {
listed := &ackv1alpha1.FieldExportList{}
Expand Down Expand Up @@ -682,7 +682,7 @@ func (r *fieldExportResourceReconciler) reconcileSourceResource(ctx context.Cont

// Get each of the exports referencing this AWS resource
exports, err := r.GetFieldExportsForResource(ctx,
*r.rd.GroupKind(),
r.rd.GroupVersionKind().GroupKind(),
types.NamespacedName{
Namespace: res.MetaObject().GetNamespace(),
Name: res.MetaObject().GetName(),
Expand Down
10 changes: 5 additions & 5 deletions pkg/runtime/field_export_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sobj "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
k8srtschema "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
ctrlrtzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
Expand Down Expand Up @@ -94,8 +94,8 @@ func mockFieldExportReconcilerWithResourceDescriptor(rd *mocks.AWSResourceDescri

func mockResourceDescriptor() *mocks.AWSResourceDescriptor {
rd := &mocks.AWSResourceDescriptor{}
rd.On("GroupKind").Return(
&metav1.GroupKind{
rd.On("GroupVersionKind").Return(
schema.GroupVersionKind{
Group: "bookstore.services.k8s.aws",
Kind: "fakeBook",
},
Expand Down Expand Up @@ -431,7 +431,7 @@ func TestFilterAllExports_HappyCase(t *testing.T) {
}
*list = mockList
})
gk := metav1.GroupKind{
gk := schema.GroupKind{
Group: BookGVK.Group,
Kind: BookGVK.Kind,
}
Expand Down Expand Up @@ -465,7 +465,7 @@ func TestSync_HappyCaseResourceNoExports(t *testing.T) {
}
*list = mockList
})
gk := metav1.GroupKind{
gk := schema.GroupKind{
Group: BookGVK.Group,
Kind: BookGVK.Kind,
}
Expand Down
19 changes: 10 additions & 9 deletions pkg/runtime/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
ctrlrt "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/predicate"
Expand Down Expand Up @@ -76,13 +76,14 @@ type resourceReconciler struct {
resyncPeriod time.Duration
}

// GroupKind returns the string containing the API group and kind reconciled by
// this reconciler
func (r *resourceReconciler) GroupKind() *metav1.GroupKind {
// GroupVersionKind returns the string containing the API group, version and
// kind reconciled by this reconciler
func (r *resourceReconciler) GroupVersionKind() *schema.GroupVersionKind {
if r.rd == nil {
return nil
}
return r.rd.GroupKind()
gvk := r.rd.GroupVersionKind()
return &gvk
}

// BindControllerManager sets up the AWSResourceReconciler with an instance
Expand Down Expand Up @@ -157,7 +158,7 @@ func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request)
region := r.getRegion(desired)
roleARN := r.getRoleARN(acctID)
endpointURL := r.getEndpointURL(desired)
gvk := desired.RuntimeObject().GetObjectKind().GroupVersionKind()
gvk := r.rd.GroupVersionKind()
sess, err := r.sc.NewSession(region, &endpointURL, roleARN, gvk)
if err != nil {
return ctrlrt.Result{}, err
Expand All @@ -170,7 +171,7 @@ func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request)
"region", region,
// All the fields for a resource that do not change during reconciliation
// can be initialized during resourceLogger creation
"kind", r.rd.GroupKind().Kind,
"kind", r.rd.GroupVersionKind().Kind,
"namespace", req.Namespace,
"name", req.Name,
)
Expand Down Expand Up @@ -1106,7 +1107,7 @@ func getResyncPeriod(rmf acktypes.AWSResourceManagerFactory, cfg ackcfg.Config)

// First, try to use a resource-specific resync period if provided in the resource
// resync period configuration.
resourceKind := rmf.ResourceDescriptor().GroupKind().Kind
resourceKind := rmf.ResourceDescriptor().GroupVersionKind().Kind
if duration, ok := drc[strings.ToLower(resourceKind)]; ok && duration > 0 {
return time.Duration(duration) * time.Second
}
Expand Down Expand Up @@ -1158,7 +1159,7 @@ func NewReconcilerWithClient(
rtLog := log.WithName("ackrt")
resyncPeriod := getResyncPeriod(rmf, cfg)
rtLog.V(1).Info("Initiating reconciler",
"reconciler kind", rmf.ResourceDescriptor().GroupKind().Kind,
"reconciler kind", rmf.ResourceDescriptor().GroupVersionKind().Kind,
"resync period seconds", resyncPeriod.Seconds(),
)
return &resourceReconciler{
Expand Down
6 changes: 3 additions & 3 deletions pkg/runtime/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sobj "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
k8srtschema "k8s.io/apimachinery/pkg/runtime/schema"
ctrlrtzap "sigs.k8s.io/controller-runtime/pkg/log/zap"

Expand Down Expand Up @@ -123,8 +123,8 @@ func managerFactoryMocks(
*ackmocks.AWSResourceDescriptor,
) {
rd := &ackmocks.AWSResourceDescriptor{}
rd.On("GroupKind").Return(
&metav1.GroupKind{
rd.On("GroupVersionKind").Return(
schema.GroupVersionKind{
Group: "bookstore.services.k8s.aws",
Kind: "fakeBook",
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (r *Registry) GetResourceManagerFactories() []types.AWSResourceManagerFacto
func (r *Registry) RegisterResourceManagerFactory(f types.AWSResourceManagerFactory) {
r.Lock()
defer r.Unlock()
r.resourceManagerFactories[f.ResourceDescriptor().GroupKind().String()] = f
r.resourceManagerFactories[f.ResourceDescriptor().GroupVersionKind().GroupKind().String()] = f
}

// NewRegistry returns a thread-safe Registry object
Expand Down
8 changes: 4 additions & 4 deletions pkg/runtime/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

ackrt "github.com/aws-controllers-k8s/runtime/pkg/runtime"

Expand All @@ -28,8 +28,8 @@ func TestRegistry(t *testing.T) {
require := require.New(t)

rd := &mocks.AWSResourceDescriptor{}
rd.On("GroupKind").Return(
&metav1.GroupKind{
rd.On("GroupVersionKind").Return(
schema.GroupVersionKind{
Group: "bookstore.services.k8s.aws",
Kind: "Book",
},
Expand All @@ -49,5 +49,5 @@ func TestRegistry(t *testing.T) {
require.Contains(rmfs, rmf)

rmf.AssertCalled(t, "ResourceDescriptor")
rd.AssertCalled(t, "GroupKind")
rd.AssertCalled(t, "GroupVersionKind")
}
2 changes: 1 addition & 1 deletion pkg/runtime/service_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (c *serviceController) WithResourceManagerFactories(
}

for _, rmf := range rmfs {
c.rmFactories[rmf.ResourceDescriptor().GroupKind().String()] = rmf
c.rmFactories[rmf.ResourceDescriptor().GroupVersionKind().GroupKind().String()] = rmf
}
return c
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/runtime/service_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func TestServiceController(t *testing.T) {
require := require.New(t)

rd := &mocks.AWSResourceDescriptor{}
rd.On("GroupKind").Return(
&metav1.GroupKind{
rd.On("GroupVersionKind").Return(
schema.GroupVersionKind{
Group: "bookstore.services.k8s.aws",
Kind: "fakeBook",
},
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestServiceController(t *testing.T) {

foundfakeBookRecon := false
for _, recon := range recons {
if recon.GroupKind().String() == "fakeBook.bookstore.services.k8s.aws" {
if recon.GroupVersionKind().GroupKind().String() == "fakeBook.bookstore.services.k8s.aws" {
foundfakeBookRecon = true
}
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/types/aws_resource_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package types

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
rtclient "sigs.k8s.io/controller-runtime/pkg/client"

ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
Expand All @@ -25,9 +25,10 @@ import (
// prototype for that AWSResource, and the relationships between the
// AWSResource and other AWSResources
type AWSResourceDescriptor interface {
// GroupKind returns a Kubernetes metav1.GroupKind struct that describes
// the API Group and Kind of CRs described by the descriptor
GroupKind() *metav1.GroupKind
// GroupVersionKind returns a Kubernetes schema.GroupVersionKind struct that
// describes the API Group, Version and Kind of CRs described by the
// descriptor
GroupVersionKind() schema.GroupVersionKind
// EmptyRuntimeObject returns an empty object prototype that may be used in
// apimachinery and k8s client operations
EmptyRuntimeObject() rtclient.Object
Expand Down
9 changes: 4 additions & 5 deletions pkg/types/aws_resource_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package types
import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
ctrlrt "sigs.k8s.io/controller-runtime"
)

Expand All @@ -34,10 +34,9 @@ type AWSResourceReconciler interface {
// BindControllerManager sets up the AWSResourceReconciler with an instance
// of an upstream controller-runtime.Manager
BindControllerManager(ctrlrt.Manager) error
// GroupKind returns the
// sigs.k8s.io/apimachinery/pkg/apis/meta/v1.GroupKind containing the API
// group and kind reconciled by this reconciler
GroupKind() *metav1.GroupKind
// GroupKind returns the schema.GroupVersionKind containing the API group,
// version and kind reconciled by this reconciler
GroupVersionKind() *schema.GroupVersionKind
// Sync ensures that the supplied AWSResource's backing API resource
// matches the supplied desired state.
//
Expand Down
Loading

0 comments on commit e526d54

Please sign in to comment.