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

Commit f5fab5e

Browse files
committed
Mark Host Target as Child Object of the Ingress
1 parent 21c28b0 commit f5fab5e

File tree

1 file changed

+64
-8
lines changed

1 file changed

+64
-8
lines changed

auto-discovery/kubernetes/controllers/ingress_scan_controller.go

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ import (
2424
targetsv1 "github.com/secureCodeBox/secureCodeBox-v2-alpha/operator/apis/targets/v1"
2525

2626
networking "k8s.io/api/networking/v1beta1"
27-
apierrors "k8s.io/apimachinery/pkg/api/errors"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/runtime"
29-
"k8s.io/apimachinery/pkg/types"
3029
ctrl "sigs.k8s.io/controller-runtime"
3130
"sigs.k8s.io/controller-runtime/pkg/client"
3231
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -40,6 +39,11 @@ type IngressScanReconciler struct {
4039
Scheme *runtime.Scheme
4140
}
4241

42+
var (
43+
ownerKey = ".metadata.controller"
44+
apiGVStr = targetsv1.GroupVersion.String()
45+
)
46+
4347
// +kubebuilder:rbac:groups=networking,resources=ingress,verbs=get;list;watch
4448
// +kubebuilder:rbac:groups=networking,resources=ingress/status,verbs=get
4549

@@ -74,22 +78,45 @@ func (r *IngressScanReconciler) CreateOrUpdateTlsForHosts(ingress networking.Ing
7478

7579
for _, tlsConfig := range ingress.Spec.TLS {
7680
for _, hostname := range tlsConfig.Hosts {
81+
82+
var hostTargets targetsv1.HostList
83+
7784
// Check if there is a target already, or create one
85+
r.List(
86+
context.Background(),
87+
&hostTargets,
88+
client.InNamespace(ingress.Namespace),
89+
client.MatchingField(ownerKey, ingress.Name),
90+
)
91+
r.Log.Info("Listed hosts", "Length", len(hostTargets.Items))
92+
7893
host := targetsv1.Host{}
79-
err := r.Get(context.Background(), types.NamespacedName{Name: hostname, Namespace: ingress.Namespace}, &host)
80-
if apierrors.IsNotFound(err) {
94+
95+
found := false
96+
// Check if the ingress has a child Host with a matching Hostname
97+
for _, hostItem := range hostTargets.Items {
98+
r.Log.Info("Comparing Hostnames", "LoopyHostname", hostItem.Spec.Hostname, "IngressHostname", hostname)
99+
if hostItem.Spec.Hostname == hostname {
100+
r.Log.Info("Found Host")
101+
found = true
102+
host = hostItem
103+
}
104+
}
105+
if found == false {
81106
host.GenerateName = fmt.Sprintf("%s-", ingress.Name)
82107
host.Namespace = ingress.Namespace
83108
host.Spec.Hostname = hostname
84109
host.Spec.Ports = make([]targetsv1.HostPort, 0)
85-
err = r.Create(context.Background(), &host)
110+
111+
if err := ctrl.SetControllerReference(&ingress, &host, r.Scheme); err != nil {
112+
return err
113+
}
114+
115+
err := r.Create(context.Background(), &host)
86116
if err != nil {
87117
r.Log.Error(err, "unable to create host")
88118
return err
89119
}
90-
} else if err != nil {
91-
r.Log.Error(err, "unable to get host")
92-
return err
93120
}
94121

95122
containsHTTPSPort := false
@@ -124,18 +151,47 @@ func (r *IngressScanReconciler) CreateOrUpdateTlsForHosts(ingress networking.Ing
124151

125152
// SetupWithManager sets up the controller and initializes every thing it needs
126153
func (r *IngressScanReconciler) SetupWithManager(mgr ctrl.Manager) error {
154+
if err := mgr.GetFieldIndexer().IndexField(&targetsv1.Host{}, ownerKey, func(rawObj runtime.Object) []string {
155+
// grab the job object, extract the owner...
156+
host := rawObj.(*targetsv1.Host)
157+
owner := metav1.GetControllerOf(host)
158+
if owner == nil {
159+
return nil
160+
}
161+
// ...make sure it's a Host...
162+
if owner.APIVersion != "networking.k8s.io/v1beta1" || owner.Kind != "Ingress" {
163+
return nil
164+
}
165+
166+
// ...and if so, return it
167+
return []string{owner.Name}
168+
}); err != nil {
169+
return err
170+
}
127171

128172
isInDemoNamespaceFilter := predicate.Funcs{
129173
CreateFunc: func(event event.CreateEvent) bool {
174+
if val, ok := event.Meta.GetAnnotations()["auto-discovery.experimental.securecodebox.io/ignore"]; ok && val == "true" {
175+
return false
176+
}
130177
return event.Meta.GetNamespace() == "juice-shop" || event.Meta.GetNamespace() == "bodgeit"
131178
},
132179
DeleteFunc: func(event event.DeleteEvent) bool {
180+
if val, ok := event.Meta.GetAnnotations()["auto-discovery.experimental.securecodebox.io/ignore"]; ok && val == "true" {
181+
return false
182+
}
133183
return event.Meta.GetNamespace() == "juice-shop" || event.Meta.GetNamespace() == "bodgeit"
134184
},
135185
UpdateFunc: func(event event.UpdateEvent) bool {
186+
if val, ok := event.MetaNew.GetAnnotations()["auto-discovery.experimental.securecodebox.io/ignore"]; ok && val == "true" {
187+
return false
188+
}
136189
return event.MetaNew.GetNamespace() == "juice-shop" || event.MetaNew.GetNamespace() == "bodgeit"
137190
},
138191
GenericFunc: func(event event.GenericEvent) bool {
192+
if val, ok := event.Meta.GetAnnotations()["auto-discovery.experimental.securecodebox.io/ignore"]; ok && val == "true" {
193+
return false
194+
}
139195
return event.Meta.GetNamespace() == "juice-shop" || event.Meta.GetNamespace() == "bodgeit"
140196
},
141197
}

0 commit comments

Comments
 (0)