Skip to content

Commit

Permalink
status: Watch clusteroperators
Browse files Browse the repository at this point in the history
Add a watch on clusteroperators in the status controller so that it updates
or recreates the dns clusteroperator as necessary should something else
update or delete it.

* pkg/operator/controller/status/controller.go (New): Watch
clusteroperators with a map function and predicate to reconcile
dnses.operator.openshift.io/default if
clusteroperators.config.openshift.io/dns is updated.
* test/e2e/operator_test.go (TestOperatorRecreatesItsClusterOperator): New
test.  Verify that the operator recreates the "dns" clusteroperator if the
clusteroperator is deleted.
  • Loading branch information
Miciah committed Apr 8, 2021
1 parent a5ea3fc commit f8b0031
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
25 changes: 24 additions & 1 deletion pkg/operator/controller/status/controller.go
Expand Up @@ -21,13 +21,15 @@ import (

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"

"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"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"
)
Expand Down Expand Up @@ -60,7 +62,9 @@ type reconciler struct {
// the logic for creating the ClusterOperator operator and updating its status.
//
// The controller watches DNS resources in the manager namespace and uses them
// to compute the operator status.
// to compute the operator status. It also watches the clusteroperators
// resource so that it reconciles the dns clusteroperator in case something else
// updates or deletes it.
func New(mgr manager.Manager, config operatorconfig.Config) (controller.Controller, error) {
reconciler := &reconciler{
Config: config,
Expand All @@ -74,6 +78,25 @@ func New(mgr manager.Manager, config operatorconfig.Config) (controller.Controll
if err := c.Watch(&source.Kind{Type: &operatorv1.DNS{}}, &handler.EnqueueRequestForObject{}); err != nil {
return nil, err
}
isDNSClusterOperator := func(meta metav1.Object, object runtime.Object) bool {
return meta.GetName() == operatorcontroller.DefaultOperatorName
}
clusteroperatorToDNS := func(a handler.MapObject) []reconcile.Request {
return []reconcile.Request{{
NamespacedName: types.NamespacedName{
Name: operatorcontroller.DefaultDNSName,
},
}}
}
if err := c.Watch(
&source.Kind{Type: &configv1.ClusterOperator{}},
&handler.EnqueueRequestsFromMapFunc{
ToRequests: handler.ToRequestsFunc(clusteroperatorToDNS),
},
predicate.NewPredicateFuncs(isDNSClusterOperator),
); err != nil {
return nil, err
}
return c, nil
}

Expand Down
27 changes: 27 additions & 0 deletions test/e2e/operator_test.go
Expand Up @@ -238,6 +238,33 @@ func TestCoreDNSDaemonSetReconciliation(t *testing.T) {
}
}

// TestOperatorRecreatesItsClusterOperator verifies that the DNS operator
// recreates the "dns" clusteroperator if the clusteroperator is deleted.
func TestOperatorRecreatesItsClusterOperator(t *testing.T) {
cl, err := getClient()
if err != nil {
t.Fatal(err)
}

co := &configv1.ClusterOperator{}
if err := cl.Get(context.TODO(), opName, co); err != nil {
t.Fatalf("failed to get clusteroperator %q: %v", opName.Name, err)
}
if err := cl.Delete(context.TODO(), co); err != nil {
t.Fatalf("failed to delete clusteroperator %q: %v", opName.Name, err)
}
err = wait.PollImmediate(1*time.Second, 30*time.Second, func() (bool, error) {
if err := cl.Get(context.TODO(), opName, co); err != nil {
t.Logf("failed to get clusteroperator %q: %v", opName.Name, err)
return false, nil
}
return true, nil
})
if err != nil {
t.Fatalf("failed to observe recreation clusteroperator %q: %v", opName.Name, err)
}
}

func TestDNSForwarding(t *testing.T) {
cl, err := getClient()
if err != nil {
Expand Down

0 comments on commit f8b0031

Please sign in to comment.