Skip to content

Commit

Permalink
Move http init to Reconcile
Browse files Browse the repository at this point in the history
Signed-off-by: ivinokur <[email protected]>
  • Loading branch information
vinokurig committed May 7, 2024
1 parent 1db4598 commit 429ec8a
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 51 deletions.
16 changes: 4 additions & 12 deletions controllers/workspace/devworkspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package controllers
import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -115,6 +114,8 @@ func (r *DevWorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request
Ctx: ctx,
}

setupHttpClients(r.Client, r.Log)

// Fetch the Workspace instance
rawWorkspace := &dw.DevWorkspace{}
err = r.Get(ctx, req.NamespacedName, rawWorkspace)
Expand Down Expand Up @@ -669,25 +670,17 @@ func (r *DevWorkspaceReconciler) getWorkspaceId(ctx context.Context, workspace *
}

func (r *DevWorkspaceReconciler) SetupWithManager(mgr ctrl.Manager) error {
setupHttpClients(mgr.GetClient(), r.Log)
maxConcurrentReconciles, err := wkspConfig.GetMaxConcurrentReconciles()
if err != nil {
return err
}

var certificatesHandler = func(obj client.Object) []reconcile.Request {
certs, ok := readCertificates(mgr.GetClient(), r.Log)
if ok {
for _, certsPem := range certs {
injectCertificates([]byte(certsPem), httpClient.Transport.(*http.Transport))
}
}
var emptyMapper = func(obj client.Object) []reconcile.Request {
return []reconcile.Request{}
}

configWatcher := builder.WithPredicates(wkspConfig.Predicates())
automountWatcher := builder.WithPredicates(automountPredicates)
certificateWatcher := builder.WithPredicates(certificatePredicates)

// TODO: Set up indexing https://book.kubebuilder.io/cronjob-tutorial/controller-implementation.html#setup
return ctrl.NewControllerManagedBy(mgr).
Expand All @@ -707,9 +700,8 @@ func (r *DevWorkspaceReconciler) SetupWithManager(mgr ctrl.Manager) error {
Watches(&source.Kind{Type: &corev1.PersistentVolumeClaim{}}, handler.EnqueueRequestsFromMapFunc(r.dwPVCHandler)).
Watches(&source.Kind{Type: &corev1.Secret{}}, handler.EnqueueRequestsFromMapFunc(r.runningWorkspacesHandler), automountWatcher).
Watches(&source.Kind{Type: &corev1.ConfigMap{}}, handler.EnqueueRequestsFromMapFunc(r.runningWorkspacesHandler), automountWatcher).
Watches(&source.Kind{Type: &corev1.ConfigMap{}}, handler.EnqueueRequestsFromMapFunc(r.certificateHandler), certificateWatcher).
Watches(&source.Kind{Type: &corev1.PersistentVolumeClaim{}}, handler.EnqueueRequestsFromMapFunc(r.runningWorkspacesHandler), automountWatcher).
Watches(&source.Kind{Type: &controllerv1alpha1.DevWorkspaceOperatorConfig{}}, handler.EnqueueRequestsFromMapFunc(certificatesHandler), configWatcher).
Watches(&source.Kind{Type: &controllerv1alpha1.DevWorkspaceOperatorConfig{}}, handler.EnqueueRequestsFromMapFunc(emptyMapper), configWatcher).
WithEventFilter(devworkspacePredicates).
WithEventFilter(podPredicates).
Complete(r)
Expand Down
9 changes: 0 additions & 9 deletions controllers/workspace/eventhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ package controllers

import (
"context"
corev1 "k8s.io/api/core/v1"
"net/http"

dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
wkspConfig "github.com/devfile/devworkspace-operator/pkg/config"
Expand Down Expand Up @@ -108,10 +106,3 @@ func (r *DevWorkspaceReconciler) runningWorkspacesHandler(obj client.Object) []r
}
return reconciles
}

func (r *DevWorkspaceReconciler) certificateHandler(obj client.Object) []reconcile.Request {
for _, certsPem := range obj.(*corev1.ConfigMap).Data {
injectCertificates([]byte(certsPem), httpClient.Transport.(*http.Transport))
}
return []reconcile.Request{}
}
23 changes: 0 additions & 23 deletions controllers/workspace/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package controllers

import (
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/devworkspace-operator/pkg/config"
"github.com/devfile/devworkspace-operator/pkg/constants"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -100,19 +99,6 @@ var automountPredicates = predicate.Funcs{
GenericFunc: func(_ event.GenericEvent) bool { return false },
}

var certificatePredicates = predicate.Funcs{
CreateFunc: func(ev event.CreateEvent) bool {
return objectIsCertificateConfigmap(ev.Object)
},
DeleteFunc: func(ev event.DeleteEvent) bool {
return objectIsCertificateConfigmap(ev.Object)
},
UpdateFunc: func(ev event.UpdateEvent) bool {
return objectIsCertificateConfigmap(ev.ObjectNew)
},
GenericFunc: func(_ event.GenericEvent) bool { return false },
}

func objectIsAutomountResource(obj client.Object) bool {
labels := obj.GetLabels()
switch {
Expand All @@ -126,12 +112,3 @@ func objectIsAutomountResource(obj client.Object) bool {
}

}

func objectIsCertificateConfigmap(obj client.Object) bool {
routing := config.GetGlobalConfig().Routing
if routing == nil || routing.TLSCertificateConfigmapRef == nil {
return false
} else {
return routing.TLSCertificateConfigmapRef.Name == obj.GetName() && routing.TLSCertificateConfigmapRef.Namespace == obj.GetNamespace()
}
}
9 changes: 2 additions & 7 deletions pkg/config/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,8 @@ import (
func Predicates() predicate.Predicate {
return predicate.Funcs{
UpdateFunc: func(evt event.UpdateEvent) bool {
if newConfig, ok := evt.ObjectNew.(*dw.DevWorkspaceOperatorConfig); ok {
syncConfigFrom(newConfig)
if oldConfig, ok := evt.ObjectOld.(*dw.DevWorkspaceOperatorConfig); ok {
if *oldConfig.Config.Routing.TLSCertificateConfigmapRef != *newConfig.Config.Routing.TLSCertificateConfigmapRef {
return true
}
}
if config, ok := evt.ObjectNew.(*dw.DevWorkspaceOperatorConfig); ok {
syncConfigFrom(config)
}
return false
},
Expand Down

0 comments on commit 429ec8a

Please sign in to comment.