Skip to content

Commit

Permalink
Uses RestMapping and chnages annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
somtochiama committed Jul 22, 2020
1 parent 26d545f commit 0105582
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions pkg/patterns/declarative/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package declarative
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/client-go/dynamic"
"path/filepath"
"strings"
Expand Down Expand Up @@ -51,7 +50,7 @@ type Reconciler struct {
kubectl kubectlClient

mgr manager.Manager

dynamicClient dynamic.Interface
options reconcilerParams
}

Expand All @@ -75,7 +74,13 @@ func (r *Reconciler) Init(mgr manager.Manager, prototype DeclarativeObject, opts
r.config = mgr.GetConfig()
r.mgr = mgr

if err := r.applyOptions(opts...); err != nil {
d, err := dynamic.NewForConfig(r.config)
if err != nil {
return err
}
r.dynamicClient = d

if err = r.applyOptions(opts...); err != nil {
return err
}

Expand Down Expand Up @@ -146,33 +151,32 @@ func (r *Reconciler) reconcileExists(ctx context.Context, name types.NamespacedN
return reconcile.Result{}, err
}

// dynamic config
dynamicClientset, err := dynamic.NewForConfig(r.config)
if err != nil {
log.Error(err,"Unable to create dynamic client")
return reconcile.Result{}, err
}

newItems := []*manifest.Object{}
var newItems []*manifest.Object
for _, obj := range objects.Items {

// Uses unsafe method?? Is it safe?
getOptions := metav1.GetOptions{}
gvk, _ := meta.UnsafeGuessKindToResource(obj.GroupVersionKind())
gvk := obj.GroupVersionKind()
restMapper, err := apiutil.NewDiscoveryRESTMapper(r.config)
if err != nil {
return reconcile.Result{}, fmt.Errorf("unable to create RESTMapper from config: %v", err)
}
mapping, err := restMapper.RESTMapping(obj.GroupKind(), gvk.Version)
if err != nil {
return reconcile.Result{}, fmt.Errorf("unable to get mapping for resource: %v", err)
}
ns := obj.UnstructuredObject().GetNamespace()
unstruct, err := dynamicClientset.Resource(gvk).Namespace(ns).Get(context.Background(),
unstruct, err := r.dynamicClient.Resource(mapping.Resource).Namespace(ns).Get(context.Background(),
obj.Name, getOptions)
if err != nil{
if err != nil && !errors.IsNotFound(err){
log.WithValues("name", obj.Name).Error(err, "Unable to get resource")
}
if unstruct != nil {
annotations := unstruct.GetAnnotations()
if ignoreAnnotation, ok := annotations["addons.operators.ignore"]; ok {
if ignoreAnnotation == "true" {
log.WithValues("kind", obj.Kind).WithValues("name", obj.Name).Info("Found ignore annotation on object, " +
"skipping object")
continue
}
if _, ok := annotations["addons.k8s.io/ignore"]; ok {
log.WithValues("kind", obj.Kind).WithValues("name", obj.Name).Info("Found ignore annotation on object, " +
"skipping object")
continue
}
}
newItems = append(newItems, obj)
Expand Down

0 comments on commit 0105582

Please sign in to comment.