diff --git a/internal/controller/validationresult_controller.go b/internal/controller/validationresult_controller.go index 56152e10..a11f19cb 100644 --- a/internal/controller/validationresult_controller.go +++ b/internal/controller/validationresult_controller.go @@ -24,13 +24,14 @@ import ( corev1 "k8s.io/api/core/v1" apierrs "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/types" + ktypes "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" v1alpha1 "github.com/spectrocloud-labs/validator/api/v1alpha1" "github.com/spectrocloud-labs/validator/internal/sinks" "github.com/spectrocloud-labs/validator/pkg/constants" + "github.com/spectrocloud-labs/validator/pkg/types" ) // ValidationResultHash is used to determine whether to re-emit updates to a validation result sink. @@ -38,7 +39,7 @@ const ValidationResultHash = "validator/validation-result-hash" var ( vr *v1alpha1.ValidationResult - vrKey types.NamespacedName + vrKey ktypes.NamespacedName sinkState v1alpha1.SinkState ) @@ -59,7 +60,7 @@ func (r *ValidationResultReconciler) Reconcile(ctx context.Context, req ctrl.Req r.Log.V(0).Info("Reconciling ValidationResult", "name", req.Name, "namespace", req.Namespace) vc := &v1alpha1.ValidatorConfig{} - vcKey := types.NamespacedName{Namespace: r.Namespace, Name: constants.ValidatorConfig} + vcKey := ktypes.NamespacedName{Namespace: r.Namespace, Name: constants.ValidatorConfig} if err := r.Get(ctx, vcKey, vc); err != nil { if !apierrs.IsNotFound(err) { r.Log.Error(err, "failed to fetch ValidatorConfig", "key", req) @@ -110,13 +111,13 @@ func (r *ValidationResultReconciler) Reconcile(ctx context.Context, req ctrl.Req // emissions will occur during the 1st reconciliation, where N is the number of rules in the validator. if vc.Spec.Sink != nil && len(vr.Status.Conditions) == vr.Spec.ExpectedResults && (!ok || prevHash != currHash) { - sink := sinks.NewSink(vc.Spec.Sink.Type, r.Log) + sink := sinks.NewSink(types.SinkType(vc.Spec.Sink.Type), r.Log) sinkState = v1alpha1.SinkEmitFailed var sinkConfig map[string][]byte if vc.Spec.Sink.SecretName != "" { sinkSecret := &corev1.Secret{} - sinkConfigKey := types.NamespacedName{Namespace: r.Namespace, Name: vc.Spec.Sink.SecretName} + sinkConfigKey := ktypes.NamespacedName{Namespace: r.Namespace, Name: vc.Spec.Sink.SecretName} if err := r.Client.Get(ctx, sinkConfigKey, sinkSecret); err != nil { r.Log.Error(err, "failed to fetch sink configuration secret") return ctrl.Result{}, err diff --git a/internal/sinks/sink.go b/internal/sinks/sink.go index ed217c31..a0d14325 100644 --- a/internal/sinks/sink.go +++ b/internal/sinks/sink.go @@ -8,6 +8,7 @@ import ( "github.com/pkg/errors" "github.com/spectrocloud-labs/validator/api/v1alpha1" + "github.com/spectrocloud-labs/validator/pkg/types" ) var SinkEmissionFailed = errors.New("sink emission failed") @@ -17,11 +18,11 @@ type Sink interface { Emit(result v1alpha1.ValidationResult) error } -func NewSink(sinkType string, log logr.Logger) Sink { +func NewSink(sinkType types.SinkType, log logr.Logger) Sink { switch sinkType { - case "alertmanager": + case types.SinkTypeAlertmanager: return &AlertmanagerSink{log: log} - case "slack": + case types.SinkTypeSlack: return &SlackSink{log: log} default: return &SlackSink{log: log} diff --git a/internal/sinks/sink_test.go b/internal/sinks/sink_test.go index b5563544..163677e8 100644 --- a/internal/sinks/sink_test.go +++ b/internal/sinks/sink_test.go @@ -5,22 +5,24 @@ import ( "testing" "github.com/go-logr/logr" + + "github.com/spectrocloud-labs/validator/pkg/types" ) func TestNewSink(t *testing.T) { cs := []struct { name string - sinkType string + sinkType types.SinkType expected Sink }{ { name: "Pass (slack)", - sinkType: "slack", + sinkType: types.SinkTypeSlack, expected: &SlackSink{}, }, { name: "Pass (alertmanager)", - sinkType: "alertmanager", + sinkType: types.SinkTypeAlertmanager, expected: &AlertmanagerSink{}, }, { diff --git a/pkg/types/types.go b/pkg/types/types.go index cd7f5236..319878ce 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -7,3 +7,10 @@ type ValidationResult struct { Condition *v1alpha1.ValidationCondition State *v1alpha1.ValidationState } + +type SinkType string + +const ( + SinkTypeAlertmanager SinkType = "alertmanager" + SinkTypeSlack SinkType = "slack" +)