This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from flanksource/add-annotate-ingress-controller
Add annotate ingress controller
- Loading branch information
Showing
11 changed files
with
392 additions
and
2 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
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,73 @@ | ||
package v1 | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
utilsk8s "github.com/flanksource/platform-operator/pkg/controllers/utils/k8s" | ||
"k8s.io/api/extensions/v1beta1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
var ilog = logf.Log.WithName("ingress-annotator") | ||
|
||
func IngressAnnotatorMutateWebhook(client client.Client, svcName, svcNamespace, domain string) *admission.Webhook { | ||
return &admission.Webhook{ | ||
Handler: NewIngressAnnotatorHandler(client, svcName, svcNamespace, domain), | ||
} | ||
} | ||
|
||
type ingressAnnotatorHandler struct { | ||
Client client.Client | ||
decoder *admission.Decoder | ||
ingressAnnotator *utilsk8s.IngressAnnotator | ||
ingressAnnotatorEnabled bool | ||
} | ||
|
||
// +kubebuilder:webhook:path=/mutate-v1-ingress,mutating=true,failurePolicy=ignore,groups="extensions",resources=ingresses,verbs=create;update,versions=v1beta1,name=annotate-ingress-v1.platform.flanksource.com | ||
|
||
func NewIngressAnnotatorHandler(client client.Client, svcName, svcNamespace, domain string) *ingressAnnotatorHandler { | ||
var ingressAnnotator *utilsk8s.IngressAnnotator = nil | ||
var ingressAnnotatorEnabled = false | ||
|
||
if svcName != "" && svcNamespace != "" { | ||
ingressAnnotator = utilsk8s.NewIngressAnnotator(client, svcName, svcNamespace, domain) | ||
ingressAnnotatorEnabled = true | ||
} | ||
|
||
return &ingressAnnotatorHandler{Client: client, ingressAnnotator: ingressAnnotator, ingressAnnotatorEnabled: ingressAnnotatorEnabled} | ||
} | ||
|
||
func (a *ingressAnnotatorHandler) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
ingress := &v1beta1.Ingress{} | ||
err := a.decoder.Decode(req, ingress) | ||
if err != nil { | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
if !a.ingressAnnotatorEnabled { | ||
ilog.Info("ingress annotator is not enabled. All requests will be declared valid") | ||
return admission.Allowed("") | ||
} | ||
|
||
newIngress, changed, err := a.ingressAnnotator.Annotate(ctx, ingress) | ||
if err != nil { | ||
return admission.Errored(http.StatusInternalServerError, err) | ||
} else if !changed { | ||
return admission.Allowed("not changed") | ||
} | ||
|
||
marshaledIngress, err := json.Marshal(newIngress) | ||
if err != nil { | ||
return admission.Errored(http.StatusInternalServerError, err) | ||
} | ||
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledIngress) | ||
} | ||
|
||
func (a *ingressAnnotatorHandler) InjectDecoder(d *admission.Decoder) error { | ||
a.decoder = d | ||
return nil | ||
} |
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,38 @@ | ||
package ingress | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
const ( | ||
name = "ingress-annotator-controller" | ||
) | ||
|
||
var log = logf.Log.WithName(name) | ||
|
||
func Add(mgr manager.Manager, interval time.Duration, svcName, svcNamespace, domain string) error { | ||
if err := addIngressReconciler(mgr, newIngressReconciler(mgr, svcName, svcNamespace, domain)); err != nil { | ||
return errors.Wrap(err, "failed to add ingress reconciler") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var _ reconcile.Reconciler = &IngressReconciler{} | ||
|
||
// these functions are passed to the predicate | ||
// objects are queued only in case the label exists | ||
func onCreate(e event.CreateEvent) bool { | ||
return true | ||
} | ||
|
||
func onUpdate(e event.UpdateEvent) bool { | ||
return true | ||
} |
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,93 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
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 ingress | ||
|
||
import ( | ||
"context" | ||
|
||
utilsk8s "github.com/flanksource/platform-operator/pkg/controllers/utils/k8s" | ||
"k8s.io/api/extensions/v1beta1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
) | ||
|
||
type IngressReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
|
||
ingressAnnotator *utilsk8s.IngressAnnotator | ||
} | ||
|
||
func newIngressReconciler(mgr manager.Manager, svcName, svcNamespace, domain string) reconcile.Reconciler { | ||
client := mgr.GetClient() | ||
ingressAnnotator := utilsk8s.NewIngressAnnotator(client, svcName, svcNamespace, domain) | ||
|
||
return &IngressReconciler{ | ||
Client: client, | ||
Scheme: mgr.GetScheme(), | ||
ingressAnnotator: ingressAnnotator, | ||
} | ||
} | ||
|
||
func addIngressReconciler(mgr manager.Manager, r reconcile.Reconciler) error { | ||
c, err := controller.New(name, mgr, controller.Options{Reconciler: r}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := c.Watch( | ||
&source.Kind{Type: &v1beta1.Ingress{}}, &handler.EnqueueRequestForObject{}, | ||
predicate.Funcs{CreateFunc: onCreate, UpdateFunc: onUpdate}, | ||
); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// +kubebuilder:rbac:groups="extensions",resources=ingresses,verbs=get;list;update;watch | ||
|
||
func (r *IngressReconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) { | ||
ctx := context.Background() | ||
|
||
ingress := &v1beta1.Ingress{} | ||
if err := r.Get(ctx, request.NamespacedName, ingress); err != nil { | ||
if errors.IsNotFound(err) { | ||
return reconcile.Result{}, nil | ||
} | ||
return reconcile.Result{}, err | ||
} | ||
|
||
updatedIngress, changed, err := r.ingressAnnotator.Annotate(ctx, ingress) | ||
if err != nil || !changed { | ||
return reconcile.Result{}, err | ||
} | ||
|
||
if err := r.Client.Update(ctx, updatedIngress); err != nil { | ||
log.Error(err, "failed to update", "ingress", updatedIngress.Name, "namespace", updatedIngress.Namespace) | ||
return reconcile.Result{}, err | ||
} | ||
|
||
return reconcile.Result{}, nil | ||
} |
Oops, something went wrong.