Skip to content

Commit dce13d3

Browse files
committed
Uses RestMapping and chnages annotation
1 parent 9baf863 commit dce13d3

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

pkg/patterns/declarative/reconciler.go

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ type Reconciler struct {
5656
mgr manager.Manager
5757

5858
// recorder is the EventRecorder for creating k8s events
59-
recorder recorder.EventRecorder
59+
recorder recorder.EventRecorder
60+
dynamicClient dynamic.Interface
6061

62+
restMapper meta.RESTMapper
6163
options reconcilerParams
6264
}
6365

@@ -86,7 +88,19 @@ func (r *Reconciler) Init(mgr manager.Manager, prototype DeclarativeObject, opts
8688
r.mgr = mgr
8789
globalObjectTracker.mgr = mgr
8890

89-
if err := r.applyOptions(opts...); err != nil {
91+
d, err := dynamic.NewForConfig(r.config)
92+
if err != nil {
93+
return err
94+
}
95+
r.dynamicClient = d
96+
97+
restMapper, err := apiutil.NewDiscoveryRESTMapper(r.config)
98+
if err != nil {
99+
return err
100+
}
101+
r.restMapper = restMapper
102+
103+
if err = r.applyOptions(opts...); err != nil {
90104
return err
91105
}
92106

@@ -187,33 +201,19 @@ func (r *Reconciler) reconcileExists(ctx context.Context, name types.NamespacedN
187201
return reconcile.Result{}, err
188202
}
189203

190-
// dynamic config
191-
dynamicClientset, err := dynamic.NewForConfig(r.config)
192-
if err != nil {
193-
log.Error(err,"Unable to create dynamic client")
194-
return reconcile.Result{}, err
195-
}
196-
197-
newItems := []*manifest.Object{}
204+
var newItems []*manifest.Object
198205
for _, obj := range objects.Items {
199206

200-
// Uses unsafe method?? Is it safe?
201-
getOptions := metav1.GetOptions{}
202-
gvk, _ := meta.UnsafeGuessKindToResource(obj.GroupVersionKind())
203-
ns := obj.UnstructuredObject().GetNamespace()
204-
unstruct, err := dynamicClientset.Resource(gvk).Namespace(ns).Get(context.Background(),
205-
obj.Name, getOptions)
206-
if err != nil{
207+
unstruct, err := getObjectFromCluster(obj, r)
208+
if err != nil && !apierrors.IsNotFound(err) {
207209
log.WithValues("name", obj.Name).Error(err, "Unable to get resource")
208210
}
209211
if unstruct != nil {
210212
annotations := unstruct.GetAnnotations()
211-
if ignoreAnnotation, ok := annotations["addons.operators.ignore"]; ok {
212-
if ignoreAnnotation == "true" {
213-
log.WithValues("kind", obj.Kind).WithValues("name", obj.Name).Info("Found ignore annotation on object, " +
214-
"skipping object")
215-
continue
216-
}
213+
if _, ok := annotations["addons.k8s.io/ignore"]; ok {
214+
log.WithValues("kind", obj.Kind).WithValues("name", obj.Name).Info("Found ignore annotation on object, " +
215+
"skipping object")
216+
continue
217217
}
218218
}
219219
newItems = append(newItems, obj)
@@ -550,3 +550,21 @@ func parseListKind(infos *manifest.Objects) (*manifest.Objects, error) {
550550
func (r *Reconciler) CollectMetrics() bool {
551551
return r.options.metrics
552552
}
553+
554+
func getObjectFromCluster(obj *manifest.Object, r *Reconciler) (*unstructured.
555+
Unstructured, error) {
556+
getOptions := metav1.GetOptions{}
557+
gvk := obj.GroupVersionKind()
558+
559+
mapping, err := r.restMapper.RESTMapping(obj.GroupKind(), gvk.Version)
560+
if err != nil {
561+
return nil, fmt.Errorf("unable to get resource: %v", err)
562+
}
563+
ns := obj.UnstructuredObject().GetNamespace()
564+
unstruct, err := r.dynamicClient.Resource(mapping.Resource).Namespace(ns).Get(context.Background(),
565+
obj.Name, getOptions)
566+
if err != nil {
567+
return nil, fmt.Errorf("unable to get mapping for resource: %v", err)
568+
}
569+
return unstruct, nil
570+
}

0 commit comments

Comments
 (0)