Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checks for annotation for objects in-cluster #111

Merged
merged 3 commits into from
Aug 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 59 additions & 3 deletions pkg/patterns/declarative/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (
"path/filepath"
"strings"

"k8s.io/apimachinery/pkg/api/meta"

"k8s.io/client-go/dynamic"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -54,9 +58,11 @@ type Reconciler struct {
mgr manager.Manager

// recorder is the EventRecorder for creating k8s events
recorder recorder.EventRecorder
recorder recorder.EventRecorder
dynamicClient dynamic.Interface

options reconcilerParams
restMapper meta.RESTMapper
options reconcilerParams
}

type kubectlClient interface {
Expand Down Expand Up @@ -84,7 +90,19 @@ func (r *Reconciler) Init(mgr manager.Manager, prototype DeclarativeObject, opts
r.mgr = mgr
globalObjectTracker.mgr = mgr

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

restMapper, err := apiutil.NewDiscoveryRESTMapper(r.config)
if err != nil {
return err
}
r.restMapper = restMapper

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

Expand Down Expand Up @@ -184,6 +202,26 @@ func (r *Reconciler) reconcileExists(ctx context.Context, name types.NamespacedN
if err != nil {
return reconcile.Result{}, err
}

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

unstruct, err := getObjectFromCluster(obj, r)
if err != nil && !apierrors.IsNotFound(err) {
log.WithValues("name", obj.Name).Error(err, "Unable to get resource")
justinsb marked this conversation as resolved.
Show resolved Hide resolved
}
if unstruct != nil {
annotations := unstruct.GetAnnotations()
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)
}
objects.Items = newItems

var manifestStr string

m, err := objects.JSONManifest()
Expand Down Expand Up @@ -514,3 +552,21 @@ func parseListKind(infos *manifest.Objects) (*manifest.Objects, error) {
func (r *Reconciler) CollectMetrics() bool {
return r.options.metrics
}

func getObjectFromCluster(obj *manifest.Object, r *Reconciler) (*unstructured.
Unstructured, error) {
getOptions := metav1.GetOptions{}
gvk := obj.GroupVersionKind()

mapping, err := r.restMapper.RESTMapping(obj.GroupKind(), gvk.Version)
if err != nil {
return nil, fmt.Errorf("unable to get resource: %v", err)
}
ns := obj.UnstructuredObject().GetNamespace()
unstruct, err := r.dynamicClient.Resource(mapping.Resource).Namespace(ns).Get(context.Background(),
obj.Name, getOptions)
if err != nil {
return nil, fmt.Errorf("unable to get mapping for resource: %v", err)
}
return unstruct, nil
}