Add validation for providers and alerts
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
This commit is contained in:
parent
5a50b19489
commit
6c855a22bc
|
@ -26,12 +26,14 @@ import (
|
|||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/client-go/tools/reference"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
|
||||
"github.com/fluxcd/pkg/apis/meta"
|
||||
"github.com/fluxcd/pkg/runtime/metrics"
|
||||
"github.com/fluxcd/pkg/runtime/predicates"
|
||||
|
||||
"github.com/fluxcd/notification-controller/api/v1beta1"
|
||||
)
|
||||
|
@ -67,6 +69,23 @@ func (r *AlertReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
|||
defer r.MetricsRecorder.RecordDuration(*objRef, reconcileStart)
|
||||
}
|
||||
|
||||
// validate alert spec and provider
|
||||
if err := r.validate(ctx, alert); err != nil {
|
||||
alert.Status.Conditions = []meta.Condition{
|
||||
{
|
||||
Type: meta.ReadyCondition,
|
||||
Status: corev1.ConditionFalse,
|
||||
LastTransitionTime: metav1.Now(),
|
||||
Reason: meta.ReconciliationFailedReason,
|
||||
Message: err.Error(),
|
||||
},
|
||||
}
|
||||
if err := r.Status().Update(ctx, &alert); err != nil {
|
||||
return ctrl.Result{Requeue: true}, err
|
||||
}
|
||||
return ctrl.Result{Requeue: true}, err
|
||||
}
|
||||
|
||||
init := true
|
||||
if c := meta.GetCondition(alert.Status.Conditions, meta.ReadyCondition); c != nil {
|
||||
if c.Status == corev1.ConditionTrue {
|
||||
|
@ -98,9 +117,19 @@ func (r *AlertReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
|||
func (r *AlertReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
For(&v1beta1.Alert{}).
|
||||
WithEventFilter(predicates.ChangePredicate{}).
|
||||
Complete(r)
|
||||
}
|
||||
|
||||
func (r *AlertReconciler) validate(ctx context.Context, alert v1beta1.Alert) error {
|
||||
var provider v1beta1.Provider
|
||||
providerName := types.NamespacedName{Namespace: alert.Namespace, Name: alert.Spec.ProviderRef.Name}
|
||||
if err := r.Get(ctx, providerName, &provider); err != nil {
|
||||
return fmt.Errorf("failed to get provider %s, error: %w", providerName.String(), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *AlertReconciler) recordReadiness(alert v1beta1.Alert, deleted bool) {
|
||||
if r.MetricsRecorder == nil {
|
||||
return
|
||||
|
|
|
@ -26,14 +26,17 @@ import (
|
|||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/client-go/tools/reference"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
|
||||
"github.com/fluxcd/pkg/apis/meta"
|
||||
"github.com/fluxcd/pkg/runtime/metrics"
|
||||
"github.com/fluxcd/pkg/runtime/predicates"
|
||||
|
||||
"github.com/fluxcd/notification-controller/api/v1beta1"
|
||||
"github.com/fluxcd/notification-controller/internal/notifier"
|
||||
)
|
||||
|
||||
// ProviderReconciler reconciles a Provider object
|
||||
|
@ -67,6 +70,23 @@ func (r *ProviderReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
|||
defer r.MetricsRecorder.RecordDuration(*objRef, reconcileStart)
|
||||
}
|
||||
|
||||
// validate provider spec and credentials
|
||||
if err := r.validate(ctx, provider); err != nil {
|
||||
provider.Status.Conditions = []meta.Condition{
|
||||
{
|
||||
Type: meta.ReadyCondition,
|
||||
Status: corev1.ConditionFalse,
|
||||
LastTransitionTime: metav1.Now(),
|
||||
Reason: meta.ReconciliationFailedReason,
|
||||
Message: err.Error(),
|
||||
},
|
||||
}
|
||||
if err := r.Status().Update(ctx, &provider); err != nil {
|
||||
return ctrl.Result{Requeue: true}, err
|
||||
}
|
||||
return ctrl.Result{Requeue: true}, err
|
||||
}
|
||||
|
||||
init := true
|
||||
if c := meta.GetCondition(provider.Status.Conditions, meta.ReadyCondition); c != nil {
|
||||
if c.Status == corev1.ConditionTrue {
|
||||
|
@ -96,9 +116,42 @@ func (r *ProviderReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
|
|||
func (r *ProviderReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
For(&v1beta1.Provider{}).
|
||||
WithEventFilter(predicates.ChangePredicate{}).
|
||||
Complete(r)
|
||||
}
|
||||
|
||||
func (r *ProviderReconciler) validate(ctx context.Context, provider v1beta1.Provider) error {
|
||||
address := provider.Spec.Address
|
||||
token := ""
|
||||
if provider.Spec.SecretRef != nil {
|
||||
var secret corev1.Secret
|
||||
secretName := types.NamespacedName{Namespace: provider.Namespace, Name: provider.Spec.SecretRef.Name}
|
||||
|
||||
if err := r.Get(ctx, secretName, &secret); err != nil {
|
||||
return fmt.Errorf("failed to read secret, error: %w", err)
|
||||
}
|
||||
|
||||
if a, ok := secret.Data["address"]; ok {
|
||||
address = string(a)
|
||||
}
|
||||
|
||||
if t, ok := secret.Data["token"]; ok {
|
||||
token = string(t)
|
||||
}
|
||||
}
|
||||
|
||||
if address == "" {
|
||||
return fmt.Errorf("no address found in 'spec.address' nor in `spec.secretRef`")
|
||||
}
|
||||
|
||||
factory := notifier.NewFactory(address, provider.Spec.Proxy, provider.Spec.Username, provider.Spec.Channel, token)
|
||||
if _, err := factory.Notifier(provider.Spec.Type); err != nil {
|
||||
return fmt.Errorf("failed to initialise provider, error: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ProviderReconciler) recordReadiness(provider v1beta1.Provider, deleted bool) {
|
||||
if r.MetricsRecorder == nil {
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue