Skip to content

Commit

Permalink
Merge pull request #714 from Miciah/status-watch-clusteroperators
Browse files Browse the repository at this point in the history
Bug 2093462: status: Watch clusteroperators
  • Loading branch information
openshift-ci[bot] committed Jun 24, 2022
2 parents 84d7e67 + a21bde2 commit de9a92f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
2 changes: 2 additions & 0 deletions manifests/00-cluster-role.yaml
Expand Up @@ -120,6 +120,8 @@ rules:
verbs:
- create
- get
- list
- watch

- apiGroups:
- config.openshift.io
Expand Down
8 changes: 4 additions & 4 deletions pkg/manifests/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion pkg/operator/controller/status/controller.go
Expand Up @@ -30,6 +30,7 @@ import (
"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 All @@ -54,7 +55,9 @@ var clock utilclock.Clock = utilclock.RealClock{}
// the logic for creating the ClusterOperator operator and updating its status.
//
// The controller watches IngressController resources in the manager namespace
// and uses them to compute the operator status.
// and uses them to compute the operator status. It also watches the
// clusteroperators resource so that it reconciles the ingress clusteroperator
// in case something else updates or deletes it.
func New(mgr manager.Manager, config Config) (controller.Controller, error) {
reconciler := &reconciler{
config: config,
Expand All @@ -65,9 +68,32 @@ func New(mgr manager.Manager, config Config) (controller.Controller, error) {
if err != nil {
return nil, err
}

if err := c.Watch(&source.Kind{Type: &operatorv1.IngressController{}}, &handler.EnqueueRequestForObject{}); err != nil {
return nil, err
}

isIngressClusterOperator := func(o client.Object) bool {
return o.GetName() == operatorcontroller.IngressClusterOperatorName().Name
}
toDefaultIngressController := func(_ client.Object) []reconcile.Request {
return []reconcile.Request{{
NamespacedName: types.NamespacedName{
Namespace: config.Namespace,
Name: manifests.DefaultIngressControllerName,
},
}}
}
if err := c.Watch(
&source.Kind{Type: &configv1.ClusterOperator{}},
// The status controller doesn't care which ingresscontroller it
// is reconciling, so just enqueue a request to reconcile the
// default ingresscontroller.
handler.EnqueueRequestsFromMapFunc(toDefaultIngressController),
predicate.NewPredicateFuncs(isIngressClusterOperator),
); err != nil {
return nil, err
}
return c, nil
}

Expand Down
1 change: 1 addition & 0 deletions test/e2e/all_test.go
Expand Up @@ -76,6 +76,7 @@ func TestAll(t *testing.T) {
t.Run("TestConfigurableRouteRBAC", TestConfigurableRouteRBAC)
t.Run("TestDefaultIngressCertificate", TestDefaultIngressCertificate)
t.Run("TestDefaultIngressClass", TestDefaultIngressClass)
t.Run("TestOperatorRecreatesItsClusterOperator", TestOperatorRecreatesItsClusterOperator)
t.Run("TestHstsPolicyWorks", TestHstsPolicyWorks)
t.Run("TestIngressControllerCustomEndpoints", TestIngressControllerCustomEndpoints)
t.Run("TestIngressStatus", TestIngressStatus)
Expand Down
35 changes: 35 additions & 0 deletions test/e2e/operator_test.go
Expand Up @@ -299,6 +299,41 @@ func TestCustomIngressClass(t *testing.T) {
}
}

// TestOperatorRecreatesItsClusterOperator verifies that the ingress operator
// recreates the "ingress" clusteroperator if the clusteroperator is deleted.
//
// This is a serial test because it depends on modifying a shared resource: the
// clusteroperator object (which means that this test could interfere with other
// tests) and then verifying that the operator reconciles the resource
// automatically (which means that other tests could interfere with this one).
func TestOperatorRecreatesItsClusterOperator(t *testing.T) {
co := &configv1.ClusterOperator{}
coName := controller.IngressClusterOperatorName()
if err := kclient.Get(context.TODO(), coName, co); err != nil {
t.Fatalf("failed to get clusteroperator %q: %v", coName.Name, err)
}
oldUid := co.UID
if err := kclient.Delete(context.TODO(), co); err != nil {
t.Fatalf("failed to delete clusteroperator %q: %v", coName.Name, err)
}
err := wait.PollImmediate(1*time.Second, 30*time.Second, func() (bool, error) {
if err := kclient.Get(context.TODO(), coName, co); err != nil {
t.Logf("failed to get clusteroperator %q: %v", coName.Name, err)
return false, nil
}
return true, nil
})
if err != nil {
t.Fatalf("failed to observe recreation of clusteroperator %q: %v", coName.Name, err)
}
if co.DeletionTimestamp != nil {
t.Fatalf("expected clusteroperator %q not to be marked for deletion, but its deletion timestamp is set: %v", coName.Name, co.DeletionTimestamp)
}
if co.UID == oldUid {
t.Fatalf("expected clusteroperator %q to have a new uid after deletion, but it still has the old uid: %v", coName.Name, co.UID)
}
}

func TestUserDefinedIngressController(t *testing.T) {
t.Parallel()
name := types.NamespacedName{Namespace: operatorNamespace, Name: "testuserdefinedingresscontroller"}
Expand Down

0 comments on commit de9a92f

Please sign in to comment.