Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit cf7bf49

Browse files
committed
Create inital target based for ingress tls hosts
1 parent 2d31ffa commit cf7bf49

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

cloud-integrations/kubernetes/controllers/ingress_scan_controller.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ import (
2020
"context"
2121

2222
"github.com/go-logr/logr"
23+
targetsv1 "github.com/secureCodeBox/secureCodeBox-v2-alpha/operator/apis/targets/v1"
24+
2325
networking "k8s.io/api/networking/v1beta1"
26+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2427
"k8s.io/apimachinery/pkg/runtime"
28+
"k8s.io/apimachinery/pkg/types"
2529
ctrl "sigs.k8s.io/controller-runtime"
2630
"sigs.k8s.io/controller-runtime/pkg/client"
2731
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -40,14 +44,58 @@ type IngressScanReconciler struct {
4044

4145
// Reconcile compares the Ingress object against the state of the cluster and updates both if needed
4246
func (r *IngressScanReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
43-
_ = context.Background()
47+
ctx := context.Background()
4448
log := r.Log
4549

4650
log.Info("Something happened to a ingress", "ingress", req.Name, "namespace", req.Namespace)
4751

52+
var ingress networking.Ingress
53+
if err := r.Get(ctx, req.NamespacedName, &ingress); err != nil {
54+
// we'll ignore not-found errors, since they can't be fixed by an immediate
55+
// requeue (we'll need to wait for a new notification), and we can get them
56+
// on deleted requests.
57+
log.V(7).Info("Unable to fetch Ingress")
58+
return ctrl.Result{}, client.IgnoreNotFound(err)
59+
}
60+
61+
err := r.CreateOrUpdateTlsForHosts(ingress)
62+
if err != nil {
63+
return ctrl.Result{}, err
64+
}
65+
4866
return ctrl.Result{}, nil
4967
}
5068

69+
func (r *IngressScanReconciler) CreateOrUpdateTlsForHosts(ingress networking.Ingress) error {
70+
if ingress.Spec.TLS == nil {
71+
return nil
72+
}
73+
74+
for _, tlsConfig := range ingress.Spec.TLS {
75+
for _, hostname := range tlsConfig.Hosts {
76+
// Check if there is a target already, or create one
77+
host := targetsv1.Host{}
78+
err := r.Get(context.Background(), types.NamespacedName{Name: hostname, Namespace: ingress.Namespace}, &host)
79+
if apierrors.IsNotFound(err) {
80+
host.Name = hostname
81+
host.Namespace = ingress.Namespace
82+
host.Spec.Hostname = hostname
83+
host.Spec.Ports = make([]targetsv1.HostPort, 0)
84+
err = r.Create(context.Background(), &host)
85+
if err != nil {
86+
r.Log.Error(err, "unable to create host")
87+
return err
88+
}
89+
} else if err != nil {
90+
r.Log.Error(err, "unable to get host")
91+
return err
92+
}
93+
}
94+
}
95+
96+
return nil
97+
}
98+
5199
// SetupWithManager sets up the controller and initializes every thing it needs
52100
func (r *IngressScanReconciler) SetupWithManager(mgr ctrl.Manager) error {
53101

0 commit comments

Comments
 (0)