Skip to content

declarative: add WithPreserveNamespace option #1

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

Open
wants to merge 3 commits into
base: integration
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func Add(mgr manager.Manager) error {
declarative.WithOwner(declarative.SourceAsOwner),
declarative.WithLabels(declarative.SourceLabel),
declarative.WithStatus(status.NewBasic(mgr.GetClient())),
declarative.WithPreserveNamespace(),
)

c, err := controller.New("dashboard-controller", mgr, controller.Options{Reconciler: r})
Expand Down
2 changes: 1 addition & 1 deletion alpha/patterns/addon/pkg/status/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (a *aggregator) Reconciled(ctx context.Context, src declarative.Declarative
case "extensions/Deployment", "apps/Deployment":
healthy, err = a.deployment(ctx, instance, o.Name)
default:
log.WithValues("type", gk).Info("type not implemented for status aggregation, skipping")
log.WithValues("type", gk).V(2).Info("type not implemented for status aggregation, skipping")
}

status.Healthy = status.Healthy && healthy
Expand Down
11 changes: 11 additions & 0 deletions alpha/patterns/declarative/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type reconcilerParams struct {
manifestController ManifestController

//prune bool
preserveNamespace bool

sink Sink
ownerFn OwnerSelector
labelMaker LabelMaker
Expand Down Expand Up @@ -168,4 +170,13 @@ func WithStatus(status Status) reconcilerOption {
}
}

// WithPreserveNamespace preserves the namespaces defined in the deployment manifest
// instead of matching the namespace of the DeclarativeObject
func WithPreserveNamespace() reconcilerOption {
return func(p reconcilerParams) reconcilerParams {
p.preserveNamespace = true
return p
}
}

type reconcilerOption func(params reconcilerParams) reconcilerParams
8 changes: 4 additions & 4 deletions alpha/patterns/declarative/pkg/watch/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,25 @@ func (dw *dynamicWatch) Add(trigger schema.GroupVersionKind, options metav1.List

client, err := dw.newDynamicClient(trigger)
if err != nil {
fmt.Errorf("creating client for (%s): %v", trigger.String(), err)
return fmt.Errorf("creating client for (%s): %v", trigger.String(), err)
}

events, err := client.Watch(options)

if err != nil {
log.WithValues("kind", trigger.String()).WithValues("namespace", target.Namespace).WithValues("options", options.String()).Error(err, "adding watch to dynamic client")
log.WithValues("kind", trigger.String()).WithValues("namespace", target.Namespace).WithValues("labels", options.LabelSelector).Error(err, "adding watch to dynamic client")
return fmt.Errorf("adding watch to dynamic client: %v", err)
}

log.WithValues("kind", trigger.String()).WithValues("namespace", target.Namespace).WithValues("options", options.String()).Info("watch registered")
log.WithValues("kind", trigger.String()).WithValues("namespace", target.Namespace).WithValues("options", options.String()).V(2).Info("watch registered")
eventChan := events.ResultChan()

go func() {
for clientEvent := range eventChan {
log.WithValues("event", fmt.Sprintf("%#v", clientEvent)).WithValues("kind", trigger.String()).Info("event recieved")
if clientEvent.Object == nil || clientEvent.Type == watch.Added {
continue
}
log.WithValues("type", clientEvent.Type).WithValues("kind", trigger.String()).Info("broadcasting event")
dw.events <- event.GenericEvent{Object: clientEvent.Object, Meta: &target}
}
}()
Expand Down
10 changes: 8 additions & 2 deletions alpha/patterns/declarative/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ func (r *Reconciler) reconcileExists(ctx context.Context, name types.NamespacedN
}
*/

if err := r.kubectl.Apply(ctx, name.Namespace, m, extraArgs...); err != nil {
ns := ""
if !r.options.preserveNamespace {
ns = name.Namespace
}

if err := r.kubectl.Apply(ctx, ns, m, extraArgs...); err != nil {
log.Error(err, "applying manifest")
return reconcile.Result{}, fmt.Errorf("error applying manifest: %v", err)
}
Expand Down Expand Up @@ -197,6 +202,7 @@ func (r *Reconciler) BuildDeploymentObjects(ctx context.Context, name types.Name
if r.options.labelMaker != nil {
transforms = append(transforms, AddLabels(r.options.labelMaker(ctx, instance)))
}
// TODO(jrjohnson): apply namespace here
for _, t := range transforms {
err := t(ctx, instance, objects)
if err != nil {
Expand Down Expand Up @@ -252,7 +258,7 @@ func (r *Reconciler) injectOwnerRef(ctx context.Context, instance DeclarativeObj
}

log := log.Log
log.WithValues("object", instance).Info("injecting owner references")
log.WithValues("object", fmt.Sprintf("%s/%s", instance.GetName(), instance.GetNamespace())).Info("injecting owner references")

for _, o := range objects.Items {
owner, err := r.options.ownerFn(ctx, instance, *o, *objects)
Expand Down
5 changes: 2 additions & 3 deletions alpha/patterns/declarative/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func DefaultObjectOrder(ctx context.Context) func(o *manifest.Object) int {
return 100

// Create the pods after we've created other things they might be waiting for
case "extensions/Deployment", "app/Deployment":
case "extensions/Deployment", "apps/Deployment":
return 1000

// Autoscalers typically act on a deployment
Expand All @@ -43,8 +43,7 @@ func DefaultObjectOrder(ctx context.Context) func(o *manifest.Object) int {
return 10000

default:
// TODO: Downgrade to V(2) once we're more comfortable with the ordering
log.WithValues("group", o.Group).WithValues("kind", o.Kind).Info("unknown group / kind")
log.WithValues("group", o.Group).WithValues("kind", o.Kind).V(2).Info("unknown group / kind")
return 1000
}
}
Expand Down