Skip to content

Commit

Permalink
Create referenced ingress if GSLB is configured with Ingress annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Baptista Aguas <[email protected]>
  • Loading branch information
abaguas committed Aug 11, 2024
1 parent 737dda0 commit 494e215
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 23 deletions.
4 changes: 3 additions & 1 deletion chart/k8gb/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ spec:
- name: DNS_ZONE
value: {{ .Values.k8gb.dnsZone }}
- name: RECONCILE_REQUEUE_SECONDS
value: {{ quote .Values.k8gb.reconcileRequeueSeconds}}
value: {{ quote .Values.k8gb.reconcileRequeueSeconds }}
- name: ANNOTATION_CREATES_GSLB_WITH_EMBEDDED_INGRESS
value: {{ quote .Values.k8gb.annotationCreatesGSLBWithEmbeddedIngress }}
{{- if .Values.infoblox.enabled }}
- name: INFOBLOX_GRID_HOST
valueFrom:
Expand Down
3 changes: 3 additions & 0 deletions chart/k8gb/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@
},
"serviceMonitor": {
"$ref": "#/definitions/k8gbServiceMonitor"
},
"annotationCreatesGSLBWithEmbeddedIngress": {
"type": "boolean"
}
},
"required": [
Expand Down
2 changes: 2 additions & 0 deletions chart/k8gb/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ k8gb:
# -- enable ServiceMonitor
serviceMonitor:
enabled: false
# -- whether annotations on ingress create a GSLB with an embedded or a referenced ingress
annotationCreatesGSLBWithEmbeddedIngress: false

externaldns:
# -- `.spec.template.spec.dnsPolicy` for ExternalDNS deployment
Expand Down
2 changes: 2 additions & 0 deletions controllers/depresolver/depresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ type Config struct {
// OtelExporterOtlpEndpoint where the traces should be sent to (in case of otel collector deployed on the same pod as sidecar -> localhost:4318)
// otel collector itself can be configured via a configmap to send it somewhere else
OtelExporterOtlpEndpoint string `env:"OTEL_EXPORTER_OTLP_ENDPOINT, default=localhost:4318"`
// AnnotationCreatesGSLBWithEmbeddedIngress flag (will be deprecated in v1.1)
AnnotationCreatesGSLBWithEmbeddedIngress bool `env:"ANNOTATION_CREATES_GSLB_WITH_EMBEDDED_INGRESS, default=false"`
}

// DependencyResolver resolves configuration for GSLB
Expand Down
27 changes: 14 additions & 13 deletions controllers/depresolver/depresolver_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,20 @@ const (
InfobloxPortKey = "INFOBLOX_WAPI_PORT"
InfobloxUsernameKey = "INFOBLOX_WAPI_USERNAME"
// #nosec G101; ignore false positive gosec; see: https://securego.io/docs/rules/g101.html
InfobloxPasswordKey = "INFOBLOX_WAPI_PASSWORD"
InfobloxHTTPRequestTimeoutKey = "INFOBLOX_HTTP_REQUEST_TIMEOUT"
InfobloxHTTPPoolConnectionsKey = "INFOBLOX_HTTP_POOL_CONNECTIONS"
K8gbNamespaceKey = "POD_NAMESPACE"
CoreDNSExposedKey = "COREDNS_EXPOSED"
LogLevelKey = "LOG_LEVEL"
LogFormatKey = "LOG_FORMAT"
LogNoColorKey = "NO_COLOR"
SplitBrainCheckKey = "SPLIT_BRAIN_CHECK"
TracingEnabled = "TRACING_ENABLED"
OtelExporterOtlpEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT"
TracingSamplingRatio = "TRACING_SAMPLING_RATIO"
MetricsAddressKey = "METRICS_ADDRESS"
InfobloxPasswordKey = "INFOBLOX_WAPI_PASSWORD"
InfobloxHTTPRequestTimeoutKey = "INFOBLOX_HTTP_REQUEST_TIMEOUT"
InfobloxHTTPPoolConnectionsKey = "INFOBLOX_HTTP_POOL_CONNECTIONS"
K8gbNamespaceKey = "POD_NAMESPACE"
CoreDNSExposedKey = "COREDNS_EXPOSED"
LogLevelKey = "LOG_LEVEL"
LogFormatKey = "LOG_FORMAT"
LogNoColorKey = "NO_COLOR"
SplitBrainCheckKey = "SPLIT_BRAIN_CHECK"
TracingEnabled = "TRACING_ENABLED"
OtelExporterOtlpEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT"
TracingSamplingRatio = "TRACING_SAMPLING_RATIO"
MetricsAddressKey = "METRICS_ADDRESS"
AnnotationCreatesGSLBWithEmbeddedIngress = "ANNOTATION_CREATES_GSLB_WITH_EMBEDDED_INGRESS"
)

// Deprecated environment variables keys
Expand Down
3 changes: 2 additions & 1 deletion controllers/depresolver/depresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ func cleanup() {
ExtDNSEnabledKey, InfobloxGridHostKey, InfobloxVersionKey, InfobloxPortKey, InfobloxUsernameKey,
InfobloxPasswordKey, K8gbNamespaceKey, CoreDNSExposedKey, InfobloxHTTPRequestTimeoutKey,
InfobloxHTTPPoolConnectionsKey, LogLevelKey, LogFormatKey, LogNoColorKey, MetricsAddressKey, SplitBrainCheckKey, TracingEnabled,
TracingSamplingRatio, OtelExporterOtlpEndpoint} {
TracingSamplingRatio, OtelExporterOtlpEndpoint, AnnotationCreatesGSLBWithEmbeddedIngress} {
if os.Unsetenv(s) != nil {
panic(fmt.Errorf("cleanup %s", s))
}
Expand Down Expand Up @@ -1542,6 +1542,7 @@ func configureEnvVar(config Config) {
_ = os.Setenv(TracingEnabled, strconv.FormatBool(config.TracingEnabled))
_ = os.Setenv(TracingSamplingRatio, strconv.FormatFloat(config.TracingSamplingRatio, 'f', 2, 64))
_ = os.Setenv(OtelExporterOtlpEndpoint, config.OtelExporterOtlpEndpoint)
_ = os.Setenv(AnnotationCreatesGSLBWithEmbeddedIngress, strconv.FormatBool(config.AnnotationCreatesGSLBWithEmbeddedIngress))
}

func getTestContext(testData string) (client.Client, *k8gbv1beta1.Gslb) {
Expand Down
39 changes: 31 additions & 8 deletions controllers/gslb_controller_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,37 @@ func (r *GslbReconciler) createGSLBFromIngress(c client.Client, a client.Object,
Msg("Gslb already exists. Skipping Gslb creation...")
return
}
gslb := &k8gbv1beta1.Gslb{
ObjectMeta: metav1.ObjectMeta{
Namespace: a.GetNamespace(),
Name: a.GetName(),
},
Spec: k8gbv1beta1.GslbSpec{
Ingress: k8gbv1beta1.FromV1IngressSpec(ingressToReuse.Spec),
},

if len(ingressToReuse.Labels) == 0 {
log.Warn().
Str("ingress", a.GetName()).
Msg("Deprecated: Ingress does not have labels. From v1.1 every Ingress must have unique labels")
}
var gslb *k8gbv1beta1.Gslb
if r.Config.AnnotationCreatesGSLBWithEmbeddedIngress {
gslb = &k8gbv1beta1.Gslb{
ObjectMeta: metav1.ObjectMeta{
Namespace: a.GetNamespace(),
Name: a.GetName(),
},
Spec: k8gbv1beta1.GslbSpec{
Ingress: k8gbv1beta1.FromV1IngressSpec(ingressToReuse.Spec),
},
}
} else {
gslb = &k8gbv1beta1.Gslb{
ObjectMeta: metav1.ObjectMeta{
Namespace: a.GetNamespace(),
Name: a.GetName(),
},
Spec: k8gbv1beta1.GslbSpec{
ResourceRef: k8gbv1beta1.ResourceRef{
Ingress: metav1.LabelSelector{
MatchLabels: ingressToReuse.Labels,
},
},
},
}
}

gslb.Spec.Strategy, err = r.parseStrategy(a.GetAnnotations(), strategy)
Expand Down
2 changes: 2 additions & 0 deletions terratest/examples/broken-ingress-annotation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ metadata:
annotations:
k8gb.io/strategy: failover
k8gb.io/primary-geotag: eu
labels:
app: notfound-broken
name: broken-test-gslb-annotation-failover
spec:
ingressClassName: nginx
Expand Down
2 changes: 2 additions & 0 deletions terratest/examples/ingress-annotation-failover-simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ metadata:
k8gb.io/strategy: failover
k8gb.io/primary-geotag: "eu"
k8gb.io/dns-ttl-seconds: "5"
labels:
app: ingress-failover-simple
spec:
ingressClassName: nginx
rules:
Expand Down
2 changes: 2 additions & 0 deletions terratest/examples/ingress-annotation-failover.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ metadata:
k8gb.io/primary-geotag: "eu"
k8gb.io/dns-ttl-seconds: "5"
k8gb.io/splitbrain-threshold-seconds: "600"
labels:
app: ingress-failover
name: test-gslb-annotation-failover
spec:
ingressClassName: nginx
Expand Down
2 changes: 2 additions & 0 deletions terratest/examples/ingress-annotation-rr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ metadata:
annotations:
k8gb.io/strategy: roundRobin
k8gb.io/dns-ttl-seconds: "5"
labels:
app: ingress-rr
name: test-gslb-annotation
spec:
ingressClassName: nginx
Expand Down
2 changes: 2 additions & 0 deletions terratest/examples/ingress-annotation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ metadata:
annotations:
k8gb.io/strategy: failover
k8gb.io/primary-geotag: "eu"
labels:
app: test-ingress-annotation-failover
name: test-gslb
spec:
ingressClassName: nginx
Expand Down

0 comments on commit 494e215

Please sign in to comment.