-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ingress): Watch ingresses and create monitor when annotations match
- Loading branch information
Showing
15 changed files
with
391 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
/* | ||
Copyright 2024. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package controller | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"strings" | ||
|
||
uptimerobotv1 "github.com/clevyr/uptime-robot-operator/api/v1" | ||
"github.com/mitchellh/mapstructure" | ||
corev1 "k8s.io/api/core/v1" | ||
networkingv1 "k8s.io/api/networking/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/fields" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
const AnnotationPrefix = "uptime-robot.clevyr.com/" | ||
|
||
// IngressReconciler reconciles a Ingress object | ||
type IngressReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
} | ||
|
||
//+kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;create;update;patch;delete | ||
//+kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses/status,verbs=get;update;patch | ||
//+kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses/finalizers,verbs=update | ||
|
||
// Reconcile is part of the main kubernetes reconciliation loop which aims to | ||
// move the current state of the cluster closer to the desired state. | ||
// | ||
// For more details, check Reconcile and its Result here: | ||
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile | ||
func (r *IngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
_ = log.FromContext(ctx) | ||
|
||
ingress := &networkingv1.Ingress{} | ||
if err := r.Get(ctx, req.NamespacedName, ingress); err != nil { | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
|
||
list, err := r.findMonitors(ctx, ingress) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
const myFinalizerName = "uptime-robot.clevyr.com/finalizer" | ||
if !ingress.DeletionTimestamp.IsZero() { | ||
// Object is being deleted | ||
if controllerutil.ContainsFinalizer(ingress, myFinalizerName) { | ||
for _, monitor := range list.Items { | ||
if err := r.Delete(ctx, &monitor); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
|
||
controllerutil.RemoveFinalizer(ingress, myFinalizerName) | ||
if err := r.Update(ctx, ingress); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
annotationCount := r.countMatchingAnnotations(ingress) | ||
var create bool | ||
if annotationCount == 0 { | ||
if len(list.Items) != 0 { | ||
// Delete existing Monitor | ||
for _, monitor := range list.Items { | ||
if err := r.Delete(ctx, &monitor); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
|
||
if controllerutil.ContainsFinalizer(ingress, myFinalizerName) { | ||
for _, monitor := range list.Items { | ||
if err := r.Delete(ctx, &monitor); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
|
||
controllerutil.RemoveFinalizer(ingress, myFinalizerName) | ||
if err := r.Update(ctx, ingress); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
return ctrl.Result{}, nil | ||
} | ||
} else { | ||
if len(list.Items) == 0 { | ||
// Create new Monitor | ||
create = true | ||
list.Items = append(list.Items, uptimerobotv1.Monitor{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: ingress.Name, | ||
Namespace: req.Namespace, | ||
}, | ||
Spec: uptimerobotv1.MonitorSpec{ | ||
SourceRef: &corev1.TypedLocalObjectReference{ | ||
Kind: ingress.Kind, | ||
Name: ingress.Name, | ||
}, | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
annotations := r.getMatchingAnnotations(ingress) | ||
for _, monitor := range list.Items { | ||
if err := r.updateValues(ingress, &monitor, annotations); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
if create { | ||
if err := r.Create(ctx, &monitor); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} else { | ||
if err := r.Update(ctx, &monitor); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
} | ||
|
||
if !controllerutil.ContainsFinalizer(ingress, myFinalizerName) { | ||
controllerutil.AddFinalizer(ingress, myFinalizerName) | ||
if err := r.Update(ctx, ingress); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *IngressReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&networkingv1.Ingress{}). | ||
Complete(r) | ||
} | ||
|
||
func (r *IngressReconciler) findMonitors(ctx context.Context, ingress *networkingv1.Ingress) (*uptimerobotv1.MonitorList, error) { | ||
list := &uptimerobotv1.MonitorList{} | ||
err := r.Client.List(ctx, list, &client.ListOptions{ | ||
FieldSelector: fields.OneTermEqualSelector("spec.sourceRef", ingress.Kind+"/"+ingress.Name), | ||
}) | ||
if err != nil { | ||
return list, err | ||
} | ||
return list, nil | ||
} | ||
|
||
func (r *IngressReconciler) countMatchingAnnotations(ingress *networkingv1.Ingress) uint { | ||
var count uint | ||
for k := range ingress.Annotations { | ||
if strings.HasPrefix(k, AnnotationPrefix) { | ||
count++ | ||
} | ||
} | ||
return count | ||
} | ||
|
||
func (r *IngressReconciler) getMatchingAnnotations(ingress *networkingv1.Ingress) map[string]string { | ||
annotations := make(map[string]string, r.countMatchingAnnotations(ingress)) | ||
for k, v := range ingress.Annotations { | ||
if strings.HasPrefix(k, AnnotationPrefix) { | ||
annotations[strings.TrimPrefix(k, AnnotationPrefix)] = v | ||
} | ||
} | ||
return annotations | ||
} | ||
|
||
func (r *IngressReconciler) updateValues(ingress *networkingv1.Ingress, monitor *uptimerobotv1.Monitor, annotations map[string]string) error { | ||
monitor.Spec.Monitor.FriendlyName = ingress.Name | ||
if _, ok := annotations["monitor.url"]; !ok { | ||
if len(ingress.Spec.Rules) != 0 { | ||
var u url.URL | ||
if u.Scheme, ok = annotations["monitor.scheme"]; !ok { | ||
if len(ingress.Spec.TLS) == 0 { | ||
u.Scheme = "http" | ||
} else { | ||
u.Scheme = "https" | ||
} | ||
} | ||
rule := ingress.Spec.Rules[0] | ||
if u.Host, ok = annotations["monitor.host"]; !ok { | ||
u.Host = rule.Host | ||
} | ||
if u.Path, ok = annotations["monitor.path"]; !ok && len(rule.HTTP.Paths) != 0 { | ||
if path := rule.HTTP.Paths[0].Path; path != "/" { | ||
u.Path = path | ||
} | ||
} | ||
monitor.Spec.Monitor.URL = u.String() | ||
} | ||
} | ||
|
||
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ | ||
TagName: "json", | ||
WeaklyTypedInput: true, | ||
Result: &monitor.Spec, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return dec.Decode(annotations) | ||
} |
Oops, something went wrong.