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

Refactor existing controller to use controller factory #291

Merged
merged 4 commits into from Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
92 changes: 15 additions & 77 deletions pkg/controllers/routercerts/controller.go
Expand Up @@ -10,11 +10,7 @@ import (
configv1listers "github.com/openshift/client-go/config/listers/config/v1"
corev1listers "k8s.io/client-go/listers/core/v1"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
corev1informers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog"

operatorv1 "github.com/openshift/api/operator/v1"
Expand All @@ -23,20 +19,17 @@ import (
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/management"
"github.com/openshift/library-go/pkg/operator/v1helpers"

"github.com/openshift/library-go/pkg/controller/factory"
)

const (
controllerWorkQueueKey = "key"
conditionRouterCertsDegradedType = "RouterCertsDegraded"
)

// RouterCertsDomainValidationController validates that router certs match the ingress domain
type RouterCertsDomainValidationController struct {
operatorClient v1helpers.OperatorClient
cachesToSync []cache.InformerSynced
queue workqueue.RateLimitingInterface
eventRecorder events.Recorder

// routerCertsDomainValidationController validates that router certs match the ingress domain
type routerCertsDomainValidationController struct {
operatorClient v1helpers.OperatorClient
ingressLister configv1listers.IngressLister
secretLister corev1listers.SecretLister
targetNamespace string
Expand All @@ -54,31 +47,25 @@ func NewRouterCertsDomainValidationController(
targetNamespace string,
secretName string,
routeName string,
) *RouterCertsDomainValidationController {
controller := &RouterCertsDomainValidationController{
) factory.Controller {
controller := &routerCertsDomainValidationController{
operatorClient: operatorClient,
cachesToSync: nil,
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "RouterCertsDomainValidationController"),
eventRecorder: eventRecorder,
ingressLister: ingressInformer.Lister(),
secretLister: secretInformer.Lister(),
targetNamespace: targetNamespace,
secretName: secretName,
routeName: routeName,
systemCertPool: x509.SystemCertPool,
}
operatorClient.Informer().AddEventHandler(controller.newEventHandler())
ingressInformer.Informer().AddEventHandler(controller.newEventHandler())
secretInformer.Informer().AddEventHandler(controller.newEventHandler())
controller.cachesToSync = append(controller.cachesToSync,
operatorClient.Informer().HasSynced,
ingressInformer.Informer().HasSynced,
secretInformer.Informer().HasSynced,
)
return controller

return factory.New().
WithInformers(operatorClient.Informer(), ingressInformer.Informer(), secretInformer.Informer()).
WithSync(controller.sync).
ResyncEvery(30*time.Second).
ToController("RouterCertsDomainValidationController", eventRecorder)
}

func (c *RouterCertsDomainValidationController) sync() error {
func (c *routerCertsDomainValidationController) sync(ctx context.Context, syncCtx factory.SyncContext) error {
spec, _, _, err := c.operatorClient.GetOperatorState()
if err != nil {
return err
Expand All @@ -95,7 +82,7 @@ func (c *RouterCertsDomainValidationController) sync() error {
return nil
}

func (c *RouterCertsDomainValidationController) validateRouterCertificates() operatorv1.OperatorCondition {
func (c *routerCertsDomainValidationController) validateRouterCertificates() operatorv1.OperatorCondition {
// get ingress
ingress, err := c.ingressLister.Get("cluster")
if err != nil {
Expand Down Expand Up @@ -197,52 +184,3 @@ func verifyWithAnyCertificate(serverCerts []*x509.Certificate, options x509.Veri
// no certificate was able to verify dns name, return last error
return err
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\o/

func (c *RouterCertsDomainValidationController) Run(ctx context.Context, workers int) {
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()

klog.Infof("Starting RouterCertsDomainValidationController")
defer klog.Infof("Shutting down RouterCertsDomainValidationController")
if !cache.WaitForCacheSync(ctx.Done(), c.cachesToSync...) {
return
}

// doesn't matter what workers say, only start one.
go wait.Until(c.runWorker, time.Second, ctx.Done())

<-ctx.Done()
}

func (c *RouterCertsDomainValidationController) runWorker() {
for c.processNextWorkItem() {
}
}

func (c *RouterCertsDomainValidationController) processNextWorkItem() bool {
dsKey, quit := c.queue.Get()
if quit {
return false
}
defer c.queue.Done(dsKey)

err := c.sync()
if err == nil {
c.queue.Forget(dsKey)
return true
}

utilruntime.HandleError(fmt.Errorf("%v failed with : %v", dsKey, err))
c.queue.AddRateLimited(dsKey)

return true
}

// newEventHandler returns an event handler that queues the operator to check spec and status
func (c *RouterCertsDomainValidationController) newEventHandler() cache.ResourceEventHandler {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { c.queue.Add(controllerWorkQueueKey) },
UpdateFunc: func(old, new interface{}) { c.queue.Add(controllerWorkQueueKey) },
DeleteFunc: func(obj interface{}) { c.queue.Add(controllerWorkQueueKey) },
}
}
7 changes: 3 additions & 4 deletions pkg/controllers/routercerts/controller_test.go
Expand Up @@ -2,6 +2,7 @@ package routercerts

import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
Expand All @@ -24,7 +25,6 @@ import (
configv1 "github.com/openshift/api/config/v1"
operatorv1 "github.com/openshift/api/operator/v1"
configv1listers "github.com/openshift/client-go/config/listers/config/v1"
"github.com/openshift/library-go/pkg/operator/events/eventstesting"
"github.com/openshift/library-go/pkg/operator/v1helpers"
)

Expand Down Expand Up @@ -189,17 +189,16 @@ func TestValidateRouterCertificates(t *testing.T) {
if tc.systemCertPool == nil {
tc.systemCertPool = x509.SystemCertPool
}
controller := RouterCertsDomainValidationController{
controller := routerCertsDomainValidationController{
operatorClient: operatorClient,
eventRecorder: eventstesting.NewTestingEventRecorder(t),
ingressLister: configv1listers.NewIngressLister(ingresses),
secretLister: corev1listers.NewSecretLister(secrets),
targetNamespace: "target",
secretName: "router-certs",
routeName: "test-route",
systemCertPool: tc.systemCertPool,
}
err = controller.sync()
err = controller.sync(context.TODO(), nil)
require.NoError(t, err)
_, s, _, _ := operatorClient.GetOperatorState()
require.Len(t, s.Conditions, 1)
Expand Down