Initialise receivers
This commit is contained in:
		
							parent
							
								
									99fc71c17a
								
							
						
					
					
						commit
						3c711cc5d9
					
				|  | @ -61,6 +61,9 @@ const ( | ||||||
| 	// is underway.
 | 	// is underway.
 | ||||||
| 	ProgressingReason string = "Progressing" | 	ProgressingReason string = "Progressing" | ||||||
| 
 | 
 | ||||||
|  | 	// TokenNotFound represents the fact that receiver token can't be found.
 | ||||||
|  | 	TokenNotFoundReason string = "TokenNotFound" | ||||||
|  | 
 | ||||||
| 	// SuspendedReason represents the fact that the resource reconciliation is suspended.
 | 	// SuspendedReason represents the fact that the resource reconciliation is suspended.
 | ||||||
| 	SuspendedReason string = "Suspended" | 	SuspendedReason string = "Suspended" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | @ -60,6 +60,35 @@ type ReceiverStatus struct { | ||||||
| 	URL string `json:"url,omitempty"` | 	URL string `json:"url,omitempty"` | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func ReceiverReady(receiver Receiver, reason, message, url string) Receiver { | ||||||
|  | 	receiver.Status.Conditions = []Condition{ | ||||||
|  | 		{ | ||||||
|  | 			Type:               ReadyCondition, | ||||||
|  | 			Status:             corev1.ConditionTrue, | ||||||
|  | 			LastTransitionTime: metav1.Now(), | ||||||
|  | 			Reason:             reason, | ||||||
|  | 			Message:            message, | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 	receiver.Status.URL = url | ||||||
|  | 
 | ||||||
|  | 	return receiver | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func ReceiverNotReady(receiver Receiver, reason, message string) Receiver { | ||||||
|  | 	receiver.Status.Conditions = []Condition{ | ||||||
|  | 		{ | ||||||
|  | 			Type:               ReadyCondition, | ||||||
|  | 			Status:             corev1.ConditionFalse, | ||||||
|  | 			LastTransitionTime: metav1.Now(), | ||||||
|  | 			Reason:             reason, | ||||||
|  | 			Message:            message, | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return receiver | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // +genclient
 | // +genclient
 | ||||||
| // +genclient:Namespaced
 | // +genclient:Namespaced
 | ||||||
| // +kubebuilder:object:root=true
 | // +kubebuilder:object:root=true
 | ||||||
|  |  | ||||||
|  | @ -2,7 +2,6 @@ apiVersion: notification.fluxcd.io/v1alpha1 | ||||||
| kind: Alert | kind: Alert | ||||||
| metadata: | metadata: | ||||||
|   name: on-call-webapp |   name: on-call-webapp | ||||||
|   namespace: gitops-system |  | ||||||
| spec: | spec: | ||||||
|   providerRef: |   providerRef: | ||||||
|     name: slack |     name: slack | ||||||
|  |  | ||||||
|  | @ -2,9 +2,15 @@ apiVersion: notification.fluxcd.io/v1alpha1 | ||||||
| kind: Provider | kind: Provider | ||||||
| metadata: | metadata: | ||||||
|   name: slack |   name: slack | ||||||
|   namespace: gitops-system |  | ||||||
| spec: | spec: | ||||||
|   type: slack |   type: slack | ||||||
|   channel: general |   channel: general | ||||||
|   secretRef: |   secretRef: | ||||||
|     name: slack-url |     name: slack-url | ||||||
|  | --- | ||||||
|  | apiVersion: v1 | ||||||
|  | kind: Secret | ||||||
|  | metadata: | ||||||
|  |   name: slack-url | ||||||
|  | data: | ||||||
|  |   address: aHR0cHM6Ly9ob29rcy5zbGFjay5jb20vc2VydmljZXMv | ||||||
|  |  | ||||||
|  | @ -1,7 +1,21 @@ | ||||||
| apiVersion: notification.fluxcd.io/v1alpha1 | apiVersion: notification.fluxcd.io/v1alpha1 | ||||||
| kind: Receiver | kind: Receiver | ||||||
| metadata: | metadata: | ||||||
|   name: receiver-sample |   name: github-receiver | ||||||
| spec: | spec: | ||||||
|   # Add fields here |   type: github | ||||||
|   foo: bar |   events: | ||||||
|  |     - ping | ||||||
|  |     - push | ||||||
|  |   resources: | ||||||
|  |     - kind: GitRepository | ||||||
|  |       name: podinfo | ||||||
|  |   secretRef: | ||||||
|  |     name: github-token | ||||||
|  | --- | ||||||
|  | apiVersion: v1 | ||||||
|  | kind: Secret | ||||||
|  | metadata: | ||||||
|  |   name: github-token | ||||||
|  | data: | ||||||
|  |   token: YUhSMGNITTZMeTlvYjI5cmN5NXpiR0ZqYXk1amIyMHZjMlZ5ZG1salpYTXY= | ||||||
|  |  | ||||||
|  | @ -18,12 +18,14 @@ package controllers | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
|  | 	"crypto/sha256" | ||||||
|  | 	"fmt" | ||||||
| 	"strings" | 	"strings" | ||||||
| 
 | 
 | ||||||
| 	"github.com/go-logr/logr" | 	"github.com/go-logr/logr" | ||||||
| 	corev1 "k8s.io/api/core/v1" | 	corev1 "k8s.io/api/core/v1" | ||||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |  | ||||||
| 	"k8s.io/apimachinery/pkg/runtime" | 	"k8s.io/apimachinery/pkg/runtime" | ||||||
|  | 	"k8s.io/apimachinery/pkg/types" | ||||||
| 	ctrl "sigs.k8s.io/controller-runtime" | 	ctrl "sigs.k8s.io/controller-runtime" | ||||||
| 	"sigs.k8s.io/controller-runtime/pkg/client" | 	"sigs.k8s.io/controller-runtime/pkg/client" | ||||||
| 
 | 
 | ||||||
|  | @ -57,23 +59,31 @@ func (r *ReceiverReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { | ||||||
| 			break | 			break | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  | 	if !init { | ||||||
|  | 		return ctrl.Result{}, nil | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	if init { | 	token, err := r.token(ctx, receiver) | ||||||
| 		receiver.Status.Conditions = []v1alpha1.Condition{ | 	if err != nil { | ||||||
| 			{ | 		receiver = v1alpha1.ReceiverNotReady(receiver, v1alpha1.TokenNotFoundReason, err.Error()) | ||||||
| 				Type:               v1alpha1.ReadyCondition, |  | ||||||
| 				Status:             corev1.ConditionTrue, |  | ||||||
| 				LastTransitionTime: metav1.Now(), |  | ||||||
| 				Reason:             v1alpha1.InitializedReason, |  | ||||||
| 				Message:            v1alpha1.InitializedReason, |  | ||||||
| 			}, |  | ||||||
| 		} |  | ||||||
| 		if err := r.Status().Update(ctx, &receiver); err != nil { | 		if err := r.Status().Update(ctx, &receiver); err != nil { | ||||||
|  | 			log.Error(err, "unable to update Receiver status") | ||||||
| 			return ctrl.Result{Requeue: true}, err | 			return ctrl.Result{Requeue: true}, err | ||||||
| 		} | 		} | ||||||
| 		log.Info("Provider initialised") |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	receiverURL := fmt.Sprintf("/hook/%s", sha256sum(token+receiver.Name+receiver.Namespace)) | ||||||
|  | 	receiver = v1alpha1.ReceiverReady(receiver, | ||||||
|  | 		v1alpha1.InitializedReason, | ||||||
|  | 		"Receiver initialised with URL: "+receiverURL, | ||||||
|  | 		receiverURL) | ||||||
|  | 	if err := r.Status().Update(ctx, &receiver); err != nil { | ||||||
|  | 		log.Error(err, "unable to update Receiver status") | ||||||
|  | 		return ctrl.Result{Requeue: true}, err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	log.Info("Receiver initialised") | ||||||
|  | 
 | ||||||
| 	return ctrl.Result{}, nil | 	return ctrl.Result{}, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -82,3 +92,31 @@ func (r *ReceiverReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||||||
| 		For(&v1alpha1.Receiver{}). | 		For(&v1alpha1.Receiver{}). | ||||||
| 		Complete(r) | 		Complete(r) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | // token extract the token value from the secret object
 | ||||||
|  | func (r *ReceiverReconciler) token(ctx context.Context, receiver v1alpha1.Receiver) (string, error) { | ||||||
|  | 	token := "" | ||||||
|  | 	secretName := types.NamespacedName{ | ||||||
|  | 		Namespace: receiver.GetNamespace(), | ||||||
|  | 		Name:      receiver.Spec.SecretRef.Name, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	var secret corev1.Secret | ||||||
|  | 	err := r.Client.Get(ctx, secretName, &secret) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return "", fmt.Errorf("unable to read token from secret '%s' error: %w", secretName, err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if val, ok := secret.Data["token"]; ok { | ||||||
|  | 		token = string(val) | ||||||
|  | 	} else { | ||||||
|  | 		return "", fmt.Errorf("invalid '%s' secret data: required fields 'token'", secretName) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return token, nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func sha256sum(val string) string { | ||||||
|  | 	digest := sha256.Sum256([]byte(val)) | ||||||
|  | 	return fmt.Sprintf("%x", digest) | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue