Skip to content

Commit e526d54

Browse files
Use resource descriptor GVK for NewSession (#120)
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.
1 parent 02fe7bf commit e526d54

15 files changed

+64
-67
lines changed

mocks/pkg/types/aws_resource_descriptor.go

Lines changed: 7 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mocks/pkg/types/aws_resource_reconciler.go

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mocks/pkg/types/field_export_reconciler.go

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/runtime/adoption_reconciler.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (r *adoptionReconciler) reconcile(ctx context.Context, req ctrlrt.Request)
8989
controllerRMF = v
9090
break
9191
}
92-
if gk.Group != controllerRMF.ResourceDescriptor().GroupKind().Group {
92+
if gk.Group != controllerRMF.ResourceDescriptor().GroupVersionKind().Group {
9393
ackrtlog.DebugAdoptedResource(r.log, res, "target resource API group is not of this service. no-op")
9494
return nil
9595
}
@@ -112,11 +112,9 @@ func (r *adoptionReconciler) reconcile(ctx context.Context, req ctrlrt.Request)
112112
region := r.getRegion(res)
113113
roleARN := r.getRoleARN(acctID)
114114
endpointURL := r.getEndpointURL(res)
115+
gvk := targetDescriptor.GroupVersionKind()
115116

116-
sess, err := r.sc.NewSession(
117-
region, &endpointURL, roleARN,
118-
targetDescriptor.EmptyRuntimeObject().GetObjectKind().GroupVersionKind(),
119-
)
117+
sess, err := r.sc.NewSession(region, &endpointURL, roleARN, gvk)
120118
if err != nil {
121119
return err
122120
}

pkg/runtime/field_export_reconciler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
jq "github.com/itchyny/gojq"
2323
"github.com/pkg/errors"
2424
corev1 "k8s.io/api/core/v1"
25-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2625
runtime "k8s.io/apimachinery/pkg/runtime"
26+
"k8s.io/apimachinery/pkg/runtime/schema"
2727
"k8s.io/apimachinery/pkg/types"
2828
ctrlrt "sigs.k8s.io/controller-runtime"
2929
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -106,7 +106,7 @@ func (r *fieldExportReconciler) reconcileFieldExport(ctx context.Context, req ct
106106
controllerRMF = v
107107
break
108108
}
109-
if sourceGK.Group != controllerRMF.ResourceDescriptor().GroupKind().Group {
109+
if sourceGK.Group != controllerRMF.ResourceDescriptor().GroupVersionKind().Group {
110110
ackrtlog.DebugFieldExport(r.log, feObject, "target resource API group is not of this service. no-op")
111111
return nil
112112
}
@@ -382,7 +382,7 @@ func (r *fieldExportReconciler) writeToSecret(
382382

383383
func (r *fieldExportReconciler) GetFieldExportsForResource(
384384
ctx context.Context,
385-
gk metav1.GroupKind,
385+
gk schema.GroupKind,
386386
nsn types.NamespacedName,
387387
) ([]ackv1alpha1.FieldExport, error) {
388388
listed := &ackv1alpha1.FieldExportList{}
@@ -682,7 +682,7 @@ func (r *fieldExportResourceReconciler) reconcileSourceResource(ctx context.Cont
682682

683683
// Get each of the exports referencing this AWS resource
684684
exports, err := r.GetFieldExportsForResource(ctx,
685-
*r.rd.GroupKind(),
685+
r.rd.GroupVersionKind().GroupKind(),
686686
types.NamespacedName{
687687
Namespace: res.MetaObject().GetNamespace(),
688688
Name: res.MetaObject().GetName(),

pkg/runtime/field_export_reconciler_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import (
2424
"github.com/stretchr/testify/require"
2525
"go.uber.org/zap/zapcore"
2626
corev1 "k8s.io/api/core/v1"
27-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2827
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2928
k8sobj "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
29+
"k8s.io/apimachinery/pkg/runtime/schema"
3030
k8srtschema "k8s.io/apimachinery/pkg/runtime/schema"
3131
"k8s.io/apimachinery/pkg/types"
3232
ctrlrtzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
@@ -94,8 +94,8 @@ func mockFieldExportReconcilerWithResourceDescriptor(rd *mocks.AWSResourceDescri
9494

9595
func mockResourceDescriptor() *mocks.AWSResourceDescriptor {
9696
rd := &mocks.AWSResourceDescriptor{}
97-
rd.On("GroupKind").Return(
98-
&metav1.GroupKind{
97+
rd.On("GroupVersionKind").Return(
98+
schema.GroupVersionKind{
9999
Group: "bookstore.services.k8s.aws",
100100
Kind: "fakeBook",
101101
},
@@ -431,7 +431,7 @@ func TestFilterAllExports_HappyCase(t *testing.T) {
431431
}
432432
*list = mockList
433433
})
434-
gk := metav1.GroupKind{
434+
gk := schema.GroupKind{
435435
Group: BookGVK.Group,
436436
Kind: BookGVK.Kind,
437437
}
@@ -465,7 +465,7 @@ func TestSync_HappyCaseResourceNoExports(t *testing.T) {
465465
}
466466
*list = mockList
467467
})
468-
gk := metav1.GroupKind{
468+
gk := schema.GroupKind{
469469
Group: BookGVK.Group,
470470
Kind: BookGVK.Kind,
471471
}

pkg/runtime/reconciler.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/pkg/errors"
2626
corev1 "k8s.io/api/core/v1"
2727
apierrors "k8s.io/apimachinery/pkg/api/errors"
28-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/apimachinery/pkg/runtime/schema"
2929
ctrlrt "sigs.k8s.io/controller-runtime"
3030
"sigs.k8s.io/controller-runtime/pkg/client"
3131
"sigs.k8s.io/controller-runtime/pkg/predicate"
@@ -76,13 +76,14 @@ type resourceReconciler struct {
7676
resyncPeriod time.Duration
7777
}
7878

79-
// GroupKind returns the string containing the API group and kind reconciled by
80-
// this reconciler
81-
func (r *resourceReconciler) GroupKind() *metav1.GroupKind {
79+
// GroupVersionKind returns the string containing the API group, version and
80+
// kind reconciled by this reconciler
81+
func (r *resourceReconciler) GroupVersionKind() *schema.GroupVersionKind {
8282
if r.rd == nil {
8383
return nil
8484
}
85-
return r.rd.GroupKind()
85+
gvk := r.rd.GroupVersionKind()
86+
return &gvk
8687
}
8788

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

11071108
// First, try to use a resource-specific resync period if provided in the resource
11081109
// resync period configuration.
1109-
resourceKind := rmf.ResourceDescriptor().GroupKind().Kind
1110+
resourceKind := rmf.ResourceDescriptor().GroupVersionKind().Kind
11101111
if duration, ok := drc[strings.ToLower(resourceKind)]; ok && duration > 0 {
11111112
return time.Duration(duration) * time.Second
11121113
}
@@ -1158,7 +1159,7 @@ func NewReconcilerWithClient(
11581159
rtLog := log.WithName("ackrt")
11591160
resyncPeriod := getResyncPeriod(rmf, cfg)
11601161
rtLog.V(1).Info("Initiating reconciler",
1161-
"reconciler kind", rmf.ResourceDescriptor().GroupKind().Kind,
1162+
"reconciler kind", rmf.ResourceDescriptor().GroupVersionKind().Kind,
11621163
"resync period seconds", resyncPeriod.Seconds(),
11631164
)
11641165
return &resourceReconciler{

pkg/runtime/reconciler_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
"github.com/stretchr/testify/require"
2525
"go.uber.org/zap/zapcore"
2626
corev1 "k8s.io/api/core/v1"
27-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2827
k8sobj "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28+
"k8s.io/apimachinery/pkg/runtime/schema"
2929
k8srtschema "k8s.io/apimachinery/pkg/runtime/schema"
3030
ctrlrtzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
3131

@@ -123,8 +123,8 @@ func managerFactoryMocks(
123123
*ackmocks.AWSResourceDescriptor,
124124
) {
125125
rd := &ackmocks.AWSResourceDescriptor{}
126-
rd.On("GroupKind").Return(
127-
&metav1.GroupKind{
126+
rd.On("GroupVersionKind").Return(
127+
schema.GroupVersionKind{
128128
Group: "bookstore.services.k8s.aws",
129129
Kind: "fakeBook",
130130
},

pkg/runtime/registry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (r *Registry) GetResourceManagerFactories() []types.AWSResourceManagerFacto
4444
func (r *Registry) RegisterResourceManagerFactory(f types.AWSResourceManagerFactory) {
4545
r.Lock()
4646
defer r.Unlock()
47-
r.resourceManagerFactories[f.ResourceDescriptor().GroupKind().String()] = f
47+
r.resourceManagerFactories[f.ResourceDescriptor().GroupVersionKind().GroupKind().String()] = f
4848
}
4949

5050
// NewRegistry returns a thread-safe Registry object

pkg/runtime/registry_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"testing"
1818

1919
"github.com/stretchr/testify/require"
20-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
"k8s.io/apimachinery/pkg/runtime/schema"
2121

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

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

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

5151
rmf.AssertCalled(t, "ResourceDescriptor")
52-
rd.AssertCalled(t, "GroupKind")
52+
rd.AssertCalled(t, "GroupVersionKind")
5353
}

0 commit comments

Comments
 (0)