@@ -26,6 +26,7 @@ import (
26
26
27
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
28
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
29
+ "k8s.io/apimachinery/pkg/runtime"
29
30
"k8s.io/apimachinery/pkg/runtime/schema"
30
31
"sigs.k8s.io/controller-runtime/pkg/controller"
31
32
crthandler "sigs.k8s.io/controller-runtime/pkg/handler"
@@ -64,12 +65,19 @@ func Add(mgr manager.Manager, options Options) *controller.Controller {
64
65
ManageStatus : options .ManageStatus ,
65
66
}
66
67
67
- // Register the GVK with the schema
68
- mgr .GetScheme ().AddKnownTypeWithName (options .GVK , & unstructured.Unstructured {})
69
- metav1 .AddToGroupVersion (mgr .GetScheme (), schema.GroupVersion {
70
- Group : options .GVK .Group ,
71
- Version : options .GVK .Version ,
72
- })
68
+ scheme := mgr .GetScheme ()
69
+ _ , err := scheme .New (options .GVK )
70
+ if runtime .IsNotRegisteredError (err ) {
71
+ // Register the GVK with the schema
72
+ scheme .AddKnownTypeWithName (options .GVK , & unstructured.Unstructured {})
73
+ metav1 .AddToGroupVersion (mgr .GetScheme (), schema.GroupVersion {
74
+ Group : options .GVK .Group ,
75
+ Version : options .GVK .Version ,
76
+ })
77
+ } else if err != nil {
78
+ log .Error (err , "" )
79
+ os .Exit (1 )
80
+ }
73
81
74
82
//Create new controller runtime controller and set the controller to watch GVK.
75
83
c , err := controller .New (fmt .Sprintf ("%v-controller" , strings .ToLower (options .GVK .Kind )), mgr , controller.Options {
0 commit comments