Skip to content

Commit

Permalink
Emit custom metrics for clusterdnschecker
Browse files Browse the repository at this point in the history
  • Loading branch information
tsatam committed Feb 23, 2024
1 parent f423d3b commit c2559c8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/aro/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 10 additions & 3 deletions pkg/operator/controllers/checkers/clusterdnschecker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -18,19 +19,22 @@ 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,
}
}

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
}

Expand All @@ -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)
}
}
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ 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"
)

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",
Expand All @@ -29,6 +32,7 @@ func TestCheck(t *testing.T) {
Name: "default",
},
},
wantMetricValid: true,
},
{
name: "invalid config: malformed dns config",
Expand All @@ -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",
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
}
Expand Down

0 comments on commit c2559c8

Please sign in to comment.