Skip to content

Commit 35e239d

Browse files
committed
adopt only if APIGroup match
1 parent 10d1b53 commit 35e239d

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

pkg/runtime/adoption_reconciler.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
ackmetrics "github.com/aws-controllers-k8s/runtime/pkg/metrics"
3131
"github.com/aws-controllers-k8s/runtime/pkg/requeue"
3232
ackrtcache "github.com/aws-controllers-k8s/runtime/pkg/runtime/cache"
33+
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
3334
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
3435
"k8s.io/apimachinery/pkg/runtime/schema"
3536
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -85,6 +86,18 @@ func (r *adoptionReconciler) reconcile(req ctrlrt.Request) error {
8586
}
8687

8788
gk := r.getTargetResourceGroupKind(res)
89+
90+
// Check if the target API group matches with the controller
91+
var controllerRMF acktypes.AWSResourceManagerFactory
92+
for _, v := range r.sc.GetResourceManagerFactories() {
93+
controllerRMF = v
94+
break
95+
}
96+
if gk.Group != controllerRMF.ResourceDescriptor().GroupKind().Group {
97+
ackrtlog.DebugAdoptedResource(r.log, res, "target resource API group is not of this service. no-op")
98+
return nil
99+
}
100+
88101
// Look up the rmf for the given target resource GVK
89102
rmf, ok := (r.sc.GetResourceManagerFactories())[gk.String()]
90103
if !ok {
@@ -111,8 +124,7 @@ func (r *adoptionReconciler) reconcile(req ctrlrt.Request) error {
111124
return err
112125
}
113126

114-
// TODO(RedbackThomson): Better logging for adopted resources
115-
r.log.Info("starting adoption reconciliation")
127+
ackrtlog.InfoAdoptedResource(r.log, res, "starting adoption reconciliation")
116128

117129
rm, err := rmf.ManagerFor(
118130
r.cfg, r.log, r.metrics, r, sess, acctID, region,
@@ -460,7 +472,7 @@ func NewAdoptionReconciler(
460472
return &adoptionReconciler{
461473
reconciler: reconciler{
462474
sc: sc,
463-
log: log.WithName("ackrt"),
475+
log: log.WithName("adopted-reconciler"),
464476
cfg: cfg,
465477
metrics: metrics,
466478
},

pkg/runtime/log/resource.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package log
1616
import (
1717
"github.com/go-logr/logr"
1818

19+
"github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
1920
acktypes "github.com/aws-controllers-k8s/runtime/pkg/types"
2021
)
2122

@@ -67,3 +68,50 @@ func InfoResource(
6768
) {
6869
AdaptResource(log, res, additionalValues...).V(0).Info(msg)
6970
}
71+
72+
// AdaptAdoptedResource returns a logger with log values set for the adopted
73+
// resource's kind, namespace, name, etc
74+
func AdaptAdoptedResource(
75+
log logr.Logger,
76+
res *v1alpha1.AdoptedResource,
77+
additionalValues ...interface{},
78+
) logr.Logger {
79+
ns := res.Namespace
80+
resName := res.Name
81+
generation := res.Generation
82+
group := res.Spec.Kubernetes.Group
83+
kind := res.Spec.Kubernetes.Kind
84+
vals := []interface{}{
85+
"target_group", group,
86+
"target_kind", kind,
87+
"namespace", ns,
88+
"name", resName,
89+
"generation", generation,
90+
}
91+
if len(additionalValues) > 0 {
92+
vals = append(vals, additionalValues...)
93+
}
94+
return log.WithValues(vals...)
95+
}
96+
97+
// DebugAdoptedResource writes a supplied log message about a adopted resource that
98+
// includes a set of standard log values for the resource's kind, namespace, name, etc
99+
func DebugAdoptedResource(
100+
log logr.Logger,
101+
res *v1alpha1.AdoptedResource,
102+
msg string,
103+
additionalValues ...interface{},
104+
) {
105+
AdaptAdoptedResource(log, res, additionalValues...).V(1).Info(msg)
106+
}
107+
108+
// InfoAdoptedResource writes a supplied log message about a adopted resource that
109+
// includes a set of standard log values for the resource's kind, namespace, name, etc
110+
func InfoAdoptedResource(
111+
log logr.Logger,
112+
res *v1alpha1.AdoptedResource,
113+
msg string,
114+
additionalValues ...interface{},
115+
) {
116+
AdaptAdoptedResource(log, res, additionalValues...).V(0).Info(msg)
117+
}

0 commit comments

Comments
 (0)