Skip to content

Commit 05271e4

Browse files
Add conditional for starting adoption reconciler (#59)
Issue #, if available: aws-controllers-k8s/community#1006 Description of changes: Use cluster introspection to determine whether the `AdoptedResource` CRD has been installed, starting the adoption reconciler only if they have been. This will allow cluster operators to intentionally leave the `AdoptedResource` uninstalled, if they wish to handle this separately. I have tested these changes by uninstalling the `AdoptedResource` CRD and then running the controller. I see the following line in the logs to prove it is working: ``` 2021-10-19T14:52:42.975-0700 INFO adoption AdoptedResource CRD not installed. The adoption reconciler will not be started ``` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 7abfd4e commit 05271e4

File tree

1 file changed

+59
-5
lines changed

1 file changed

+59
-5
lines changed

pkg/runtime/service_controller.go

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ import (
1818

1919
"github.com/go-logr/logr"
2020
"github.com/prometheus/client_golang/prometheus"
21+
"k8s.io/apimachinery/pkg/api/meta"
22+
"k8s.io/apimachinery/pkg/runtime/schema"
23+
"k8s.io/client-go/discovery"
2124
kubernetes "k8s.io/client-go/kubernetes"
2225
ctrlrt "sigs.k8s.io/controller-runtime"
26+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
2327

28+
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
2429
ackcfg "github.com/aws-controllers-k8s/runtime/pkg/config"
2530
ackmetrics "github.com/aws-controllers-k8s/runtime/pkg/metrics"
2631
ackrtcache "github.com/aws-controllers-k8s/runtime/pkg/runtime/cache"
@@ -87,6 +92,44 @@ func (c *serviceController) GetResourceManagerFactories() map[string]acktypes.AW
8792
return c.rmFactories
8893
}
8994

95+
// GetAdoptedResourceInstalled returns whether the AdoptedResource CRD has been
96+
// installed into the cluster, and is accessible by the service controller.
97+
func (c *serviceController) GetAdoptedResourceInstalled(mgr ctrlrt.Manager) (bool, error) {
98+
clusterConfig := mgr.GetConfig()
99+
clientSet, err := kubernetes.NewForConfig(clusterConfig)
100+
if err != nil {
101+
return false, err
102+
}
103+
104+
gv := schema.GroupVersion{
105+
Group: ackv1alpha1.GroupVersion.Group,
106+
Version: ackv1alpha1.GroupVersion.Version,
107+
}
108+
109+
// Ensure GV is supported
110+
if err = discovery.ServerSupportsVersion(clientSet, gv); err != nil {
111+
return false, nil
112+
}
113+
114+
restMapperClient, err := apiutil.NewDiscoveryRESTMapper(clusterConfig)
115+
if err != nil {
116+
return false, err
117+
}
118+
119+
adoptionResourceGVR := schema.GroupVersionResource{
120+
Group: ackv1alpha1.GroupVersion.Group,
121+
Version: ackv1alpha1.GroupVersion.Version,
122+
Resource: "adoptedresources",
123+
}
124+
125+
// Ensure individual kind is supported
126+
if _, err := restMapperClient.KindFor(adoptionResourceGVR); meta.IsNoMatchError(err) {
127+
return false, nil
128+
}
129+
130+
return true, nil
131+
}
132+
90133
// WithLogger sets up the service controller with the supplied logger
91134
func (c *serviceController) WithLogger(log logr.Logger) acktypes.ServiceController {
92135
c.log = log
@@ -130,7 +173,9 @@ func (c *serviceController) WithResourceManagerFactories(
130173

131174
// BindControllerManager takes a `controller-runtime.Manager`, creates all the
132175
// AWSResourceReconcilers needed for the service and binds all of the
133-
// reconcilers within the service controller with that manager
176+
// reconcilers within the service controller with that manager. The adoption
177+
// reconciler will only be started if the types have been registered in the
178+
// cluster.
134179
func (c *serviceController) BindControllerManager(mgr ctrlrt.Manager, cfg ackcfg.Config) error {
135180
c.metaLock.Lock()
136181
defer c.metaLock.Unlock()
@@ -153,11 +198,20 @@ func (c *serviceController) BindControllerManager(mgr ctrlrt.Manager, cfg ackcfg
153198
c.reconcilers = append(c.reconcilers, rec)
154199
}
155200

156-
rec := NewAdoptionReconciler(c, c.log, cfg, c.metrics, cache)
157-
if err := rec.BindControllerManager(mgr); err != nil {
158-
return err
201+
adoptionInstalled, err := c.GetAdoptedResourceInstalled(mgr)
202+
adoptionLogger := c.log.WithName("adoption")
203+
if err != nil {
204+
adoptionLogger.Error(err, "unable to determine if the AdoptedResource CRD is installed in the cluster")
205+
} else if !adoptionInstalled {
206+
adoptionLogger.Info("AdoptedResource CRD not installed. The adoption reconciler will not be started")
207+
} else {
208+
rec := NewAdoptionReconciler(c, adoptionLogger, cfg, c.metrics, cache)
209+
if err := rec.BindControllerManager(mgr); err != nil {
210+
return err
211+
}
212+
c.adoptionReconciler = rec
159213
}
160-
c.adoptionReconciler = rec
214+
161215
return nil
162216
}
163217

0 commit comments

Comments
 (0)