Skip to content

Commit

Permalink
Merge pull request #169 from jabdoa2/generics
Browse files Browse the repository at this point in the history
Refactor, Cleanup Code and use Generics
  • Loading branch information
toelke authored Jun 3, 2024
2 parents 07c21df + cfe3509 commit e748c7e
Show file tree
Hide file tree
Showing 33 changed files with 1,167 additions and 2,512 deletions.
52 changes: 5 additions & 47 deletions pkg/controller/daemonset/daemonset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,9 @@ import (

"github.com/wave-k8s/wave/pkg/core"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

// +kubebuilder:rbac:groups=apps,resources=daemonsets,verbs=get;list;watch;update;patch
Expand All @@ -48,61 +42,25 @@ func Add(mgr manager.Manager) error {
func newReconciler(mgr manager.Manager) *ReconcileDaemonSet {
return &ReconcileDaemonSet{
scheme: mgr.GetScheme(),
handler: core.NewHandler(mgr.GetClient(), mgr.GetEventRecorderFor("wave")),
handler: core.NewHandler[*appsv1.DaemonSet](mgr.GetClient(), mgr.GetEventRecorderFor("wave")),
}
}

// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler, h *core.Handler) error {
// Create a new controller
c, err := controller.New("daemonset-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}

err = c.Watch(source.Kind(mgr.GetCache(), &appsv1.DaemonSet{}), &handler.EnqueueRequestForObject{}, predicate.Or(predicate.GenerationChangedPredicate{}, predicate.AnnotationChangedPredicate{}))
if err != nil {
return err
}

// Watch ConfigMaps owned by a DaemonSet
err = c.Watch(source.Kind(mgr.GetCache(), &corev1.ConfigMap{}), core.EnqueueRequestForWatcher(h.GetWatchedConfigmaps()))
if err != nil {
return err
}

// Watch Secrets owned by a DaemonSet
err = c.Watch(source.Kind(mgr.GetCache(), &corev1.Secret{}), core.EnqueueRequestForWatcher(h.GetWatchedSecrets()))
if err != nil {
return err
}

return nil
func add(mgr manager.Manager, r reconcile.Reconciler, h *core.Handler[*appsv1.DaemonSet]) error {
return core.AddController("daemonset-controller", &appsv1.DaemonSet{}, mgr, r, h)
}

var _ reconcile.Reconciler = &ReconcileDaemonSet{}

// ReconcileDaemonSet reconciles a DaemonSet object
type ReconcileDaemonSet struct {
scheme *runtime.Scheme
handler *core.Handler
handler *core.Handler[*appsv1.DaemonSet]
}

// Reconcile reads that state of the cluster for a DaemonSet object and
// updates its PodSpec based on mounted configuration
func (r *ReconcileDaemonSet) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
// Fetch the DaemonSet instance
instance := &appsv1.DaemonSet{}
err := r.handler.Get(ctx, request.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
r.handler.RemoveWatches(request.NamespacedName)
// Object not found, return. Created objects are automatically garbage collected.
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}

return r.handler.HandleDaemonSet(instance)
return r.handler.Handle(ctx, request.NamespacedName, &appsv1.DaemonSet{})
}
70 changes: 46 additions & 24 deletions pkg/controller/daemonset/daemonset_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/prometheus/client_golang/prometheus"
"github.com/wave-k8s/wave/pkg/core"
"github.com/wave-k8s/wave/test/utils"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/metrics"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/webhook"

admissionv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -47,6 +54,11 @@ var t *envtest.Environment

var testCtx, testCancel = context.WithCancel(context.Background())

var requestsStart <-chan reconcile.Request
var requests <-chan reconcile.Request

var m utils.Matcher

var _ = BeforeSuite(func() {
failurePolicy := admissionv1.Ignore
sideEffects := admissionv1.SideEffectClassNone
Expand Down Expand Up @@ -101,30 +113,40 @@ var _ = BeforeSuite(func() {
if cfg, err = t.Start(); err != nil {
log.Fatal(err)
}

// Reset the Prometheus Registry before each test to avoid errors
metrics.Registry = prometheus.NewRegistry()

mgr, err := manager.New(cfg, manager.Options{
Metrics: metricsserver.Options{
BindAddress: "0",
},
WebhookServer: webhook.NewServer(webhook.Options{
Host: (*t).WebhookInstallOptions.LocalServingHost,
Port: (*t).WebhookInstallOptions.LocalServingPort,
CertDir: (*t).WebhookInstallOptions.LocalServingCertDir,
}),
})
Expect(err).NotTo(HaveOccurred())

c, cerr := client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(cerr).NotTo(HaveOccurred())
m = utils.Matcher{Client: c}

var recFn reconcile.Reconciler
r := newReconciler(mgr)
recFn, requestsStart, requests = core.SetupControllerTestReconcile(r)
Expect(add(mgr, recFn, r.handler)).NotTo(HaveOccurred())

// register mutating pod webhook
err = AddDaemonSetWebhook(mgr)
Expect(err).ToNot(HaveOccurred())

testCtx, testCancel = context.WithCancel(context.Background())
go core.Run(testCtx, mgr)
})

var _ = AfterSuite(func() {
testCancel()
t.Stop()
})

// SetupTestReconcile returns a reconcile.Reconcile implementation that delegates to inner and
// writes the request to requests after Reconcile is finished.
func SetupTestReconcile(inner reconcile.Reconciler) (reconcile.Reconciler, chan reconcile.Request, chan reconcile.Request) {
requestsStart := make(chan reconcile.Request)
requests := make(chan reconcile.Request)
fn := reconcile.Func(func(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
requestsStart <- req
result, err := inner.Reconcile(ctx, req)
requests <- req
return result, err
})
return fn, requestsStart, requests
}

// Run runs the webhook server.
func Run(ctx context.Context, k8sManager ctrl.Manager) error {
if err := k8sManager.Start(ctx); err != nil {
return err
}
return nil
}
Loading

0 comments on commit e748c7e

Please sign in to comment.