diff --git a/cmd/aro/operator.go b/cmd/aro/operator.go index f1475a85704..f0bbfa045d5 100644 --- a/cmd/aro/operator.go +++ b/cmd/aro/operator.go @@ -211,7 +211,7 @@ func operator(ctx context.Context, log *logrus.Entry) error { } if err = (clusterdnschecker.NewReconciler( log.WithField("controller", clusterdnschecker.ControllerName), - client, role)).SetupWithManager(mgr); err != nil { + client, metricsClient, role)).SetupWithManager(mgr); err != nil { return fmt.Errorf("unable to create controller %s: %v", clusterdnschecker.ControllerName, err) } if err = (ingresscertificatechecker.NewReconciler( diff --git a/pkg/operator/controllers/checkers/clusterdnschecker/checker.go b/pkg/operator/controllers/checkers/clusterdnschecker/checker.go index 40162b7dc8b..87d80ffd804 100644 --- a/pkg/operator/controllers/checkers/clusterdnschecker/checker.go +++ b/pkg/operator/controllers/checkers/clusterdnschecker/checker.go @@ -8,6 +8,7 @@ import ( "fmt" "strings" + "github.com/Azure/ARO-RP/pkg/operator/metrics" operatorv1 "github.com/openshift/api/operator/v1" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" @@ -18,12 +19,14 @@ type clusterDNSChecker interface { } type checker struct { - client client.Client + client client.Client + metricsClient metrics.Client } -func newClusterDNSChecker(client client.Client) *checker { +func newClusterDNSChecker(client client.Client, metricsClient metrics.Client) *checker { return &checker{ - client: client, + client: client, + metricsClient: metricsClient, } } @@ -31,6 +34,7 @@ func (r *checker) Check(ctx context.Context) error { dns := &operatorv1.DNS{} err := r.client.Get(ctx, types.NamespacedName{Name: "default"}, dns) if err != nil { + r.metricsClient.UpdateDnsConfigurationValid(false) return err } @@ -41,6 +45,7 @@ func (r *checker) Check(ctx context.Context) error { // If "." is set as a zone, bail out and warn about the // malformed config, as this will prevent CoreDNS from rolling // out + r.metricsClient.UpdateDnsConfigurationValid(false) return fmt.Errorf("malformed config: %q in zones", z) } } @@ -49,8 +54,10 @@ func (r *checker) Check(ctx context.Context) error { } if len(upstreams) > 0 { + r.metricsClient.UpdateDnsConfigurationValid(false) return fmt.Errorf("custom upstream DNS servers in use: %s", strings.Join(upstreams, ", ")) } + r.metricsClient.UpdateDnsConfigurationValid(true) return nil } diff --git a/pkg/operator/controllers/checkers/clusterdnschecker/checker_test.go b/pkg/operator/controllers/checkers/clusterdnschecker/checker_test.go index 61e8d0f7dc1..4caf720dff6 100644 --- a/pkg/operator/controllers/checkers/clusterdnschecker/checker_test.go +++ b/pkg/operator/controllers/checkers/clusterdnschecker/checker_test.go @@ -7,10 +7,12 @@ import ( "context" "testing" + "github.com/golang/mock/gomock" operatorv1 "github.com/openshift/api/operator/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrlfake "sigs.k8s.io/controller-runtime/pkg/client/fake" + mock_metrics "github.com/Azure/ARO-RP/pkg/util/mocks/operator/metrics" utilerror "github.com/Azure/ARO-RP/test/util/error" ) @@ -18,9 +20,10 @@ func TestCheck(t *testing.T) { ctx := context.Background() for _, tt := range []struct { - name string - DNS *operatorv1.DNS - wantErr string + name string + DNS *operatorv1.DNS + wantErr string + wantMetricValid bool }{ { name: "valid dns config", @@ -29,6 +32,7 @@ func TestCheck(t *testing.T) { Name: "default", }, }, + wantMetricValid: true, }, { name: "invalid config: malformed dns config", @@ -44,7 +48,8 @@ func TestCheck(t *testing.T) { }, }, }, - wantErr: `malformed config: "." in zones`, + wantErr: `malformed config: "." in zones`, + wantMetricValid: false, }, { name: "invalid config: forward plugin upstream is", @@ -67,11 +72,13 @@ func TestCheck(t *testing.T) { }, }, }, - wantErr: `custom upstream DNS servers in use: first-fake.io, second-fake.io, third-fake.io`, + wantErr: `custom upstream DNS servers in use: first-fake.io, second-fake.io, third-fake.io`, + wantMetricValid: false, }, { - name: "default config not found", - wantErr: `dnses.operator.openshift.io "default" not found`, + name: "default config not found", + wantErr: `dnses.operator.openshift.io "default" not found`, + wantMetricValid: false, }, } { t.Run(tt.name, func(t *testing.T) { @@ -80,8 +87,15 @@ func TestCheck(t *testing.T) { clientBuilder = clientBuilder.WithObjects(tt.DNS) } + controller := gomock.NewController(t) + defer controller.Finish() + + metricsClientFake := mock_metrics.NewMockClient(controller) + metricsClientFake.EXPECT().UpdateDnsConfigurationValid(tt.wantMetricValid) + sp := &checker{ - client: clientBuilder.Build(), + client: clientBuilder.Build(), + metricsClient: metricsClientFake, } err := sp.Check(ctx) diff --git a/pkg/operator/controllers/checkers/clusterdnschecker/controller.go b/pkg/operator/controllers/checkers/clusterdnschecker/controller.go index d761aeba612..7b3d2d8bc87 100644 --- a/pkg/operator/controllers/checkers/clusterdnschecker/controller.go +++ b/pkg/operator/controllers/checkers/clusterdnschecker/controller.go @@ -20,6 +20,7 @@ import ( "github.com/Azure/ARO-RP/pkg/operator" arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1" + "github.com/Azure/ARO-RP/pkg/operator/metrics" "github.com/Azure/ARO-RP/pkg/util/conditions" ) @@ -43,12 +44,12 @@ type Reconciler struct { client client.Client } -func NewReconciler(log *logrus.Entry, client client.Client, role string) *Reconciler { +func NewReconciler(log *logrus.Entry, client client.Client, metricsClient metrics.Client, role string) *Reconciler { return &Reconciler{ log: log, role: role, - checker: newClusterDNSChecker(client), + checker: newClusterDNSChecker(client, metricsClient), client: client, }