Skip to content

Commit

Permalink
refactor: expose sink types (#117)
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>
  • Loading branch information
TylerGillson committed Nov 16, 2023
1 parent 9fbfff0 commit f28d8af
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
11 changes: 6 additions & 5 deletions internal/controller/validationresult_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@ 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.
const ValidationResultHash = "validator/validation-result-hash"

var (
vr *v1alpha1.ValidationResult
vrKey types.NamespacedName
vrKey ktypes.NamespacedName
sinkState v1alpha1.SinkState
)

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions internal/sinks/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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}
Expand Down
8 changes: 5 additions & 3 deletions internal/sinks/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
},
{
Expand Down
7 changes: 7 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ type ValidationResult struct {
Condition *v1alpha1.ValidationCondition
State *v1alpha1.ValidationState
}

type SinkType string

const (
SinkTypeAlertmanager SinkType = "alertmanager"
SinkTypeSlack SinkType = "slack"
)

0 comments on commit f28d8af

Please sign in to comment.