-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84f6a59
commit a7155a8
Showing
16 changed files
with
732 additions
and
12 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"crypto/md5" | ||
"fmt" | ||
"io" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
externaldns "sigs.k8s.io/external-dns/endpoint" | ||
|
||
"github.com/kuadrant/dns-operator/api/v1alpha1" | ||
"github.com/kuadrant/dns-operator/internal/provider" | ||
) | ||
|
||
// healthChecksConfig represents the user configuration for the health checks | ||
type healthChecksConfig struct { | ||
Endpoint string | ||
Port *int64 | ||
FailureThreshold *int64 | ||
Protocol *provider.HealthCheckProtocol | ||
} | ||
|
||
func (r *DNSRecordReconciler) ReconcileHealthChecks(ctx context.Context, dnsRecord *v1alpha1.DNSRecord) error { | ||
var results []provider.HealthCheckResult | ||
var err error | ||
|
||
dnsProvider, err := r.getDNSProvider(ctx, dnsRecord) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
healthCheckReconciler := dnsProvider.HealthCheckReconciler() | ||
|
||
// Get the configuration for the health checks. If no configuration is | ||
// set, ensure that the health checks are deleted | ||
config := getHealthChecksConfig(dnsRecord) | ||
|
||
if config == nil { | ||
// deleting | ||
for _, endpoint := range dnsRecord.Spec.Endpoints { | ||
addresses := provider.GetExternalAddresses(endpoint, dnsRecord) | ||
for _, address := range addresses { | ||
probeStatus := r.getProbeStatus(address, dnsRecord) | ||
if probeStatus == nil { | ||
continue | ||
} | ||
result, err := healthCheckReconciler.Delete(ctx, endpoint, probeStatus) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
results = append(results, result) | ||
} | ||
} | ||
} else { | ||
for _, dnsEndpoint := range dnsRecord.Spec.Endpoints { | ||
addresses := provider.GetExternalAddresses(dnsEndpoint, dnsRecord) | ||
for _, address := range addresses { | ||
endpointId, err := idForEndpoint(dnsRecord, dnsEndpoint, address) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
spec := provider.HealthCheckSpec{ | ||
Id: endpointId, | ||
Name: fmt.Sprintf("%s-%s-%s", *dnsRecord.Spec.RootHost, dnsEndpoint.DNSName, address), | ||
Host: dnsRecord.Spec.RootHost, | ||
Path: config.Endpoint, | ||
Port: config.Port, | ||
Protocol: config.Protocol, | ||
FailureThreshold: config.FailureThreshold, | ||
} | ||
|
||
probeStatus := r.getProbeStatus(address, dnsRecord) | ||
|
||
result, err := healthCheckReconciler.Reconcile(ctx, spec, dnsEndpoint, probeStatus, address) | ||
if err != nil { | ||
return err | ||
} | ||
results = append(results, result) | ||
} | ||
} | ||
} | ||
|
||
result := r.reconcileHealthCheckStatus(results, dnsRecord) | ||
return result | ||
} | ||
|
||
func (r *DNSRecordReconciler) getProbeStatus(address string, dnsRecord *v1alpha1.DNSRecord) *v1alpha1.HealthCheckStatusProbe { | ||
if dnsRecord.Status.HealthCheck == nil || dnsRecord.Status.HealthCheck.Probes == nil { | ||
return nil | ||
} | ||
for _, probeStatus := range dnsRecord.Status.HealthCheck.Probes { | ||
if probeStatus.IPAddress == address { | ||
return &probeStatus | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *DNSRecordReconciler) reconcileHealthCheckStatus(results []provider.HealthCheckResult, dnsRecord *v1alpha1.DNSRecord) error { | ||
probesCondition := metav1.Condition{ | ||
ObservedGeneration: dnsRecord.Generation, | ||
Status: metav1.ConditionTrue, | ||
Reason: "AllProbesSynced", | ||
LastTransitionTime: metav1.Now(), | ||
Message: fmt.Sprintf("all %v probes synced successfully", len(results)), | ||
Type: "healthProbesSynced", | ||
} | ||
|
||
var allSynced = metav1.ConditionTrue | ||
|
||
if dnsRecord.Status.HealthCheck == nil { | ||
dnsRecord.Status.HealthCheck = &v1alpha1.HealthCheckStatus{ | ||
Conditions: []metav1.Condition{}, | ||
Probes: []v1alpha1.HealthCheckStatusProbe{}, | ||
} | ||
} | ||
|
||
for _, c := range dnsRecord.Status.HealthCheck.Conditions { | ||
if c.Type == "healthProbesSynced" { | ||
probesCondition = c | ||
} | ||
} | ||
if probesCondition.ObservedGeneration != dnsRecord.Generation { | ||
probesCondition.ObservedGeneration = dnsRecord.Generation | ||
probesCondition.LastTransitionTime = metav1.Now() | ||
} | ||
|
||
dnsRecord.Status.HealthCheck.Probes = []v1alpha1.HealthCheckStatusProbe{} | ||
|
||
for _, result := range results { | ||
status := true | ||
if result.Result == provider.HealthCheckFailed { | ||
status = false | ||
allSynced = metav1.ConditionFalse | ||
} | ||
|
||
dnsRecord.Status.HealthCheck.Probes = append(dnsRecord.Status.HealthCheck.Probes, v1alpha1.HealthCheckStatusProbe{ | ||
ID: result.ID, | ||
IPAddress: result.IPAddress, | ||
Host: result.Host, | ||
Synced: status, | ||
}) | ||
} | ||
|
||
if probesCondition.Status != allSynced { | ||
probesCondition.Status = allSynced | ||
probesCondition.LastTransitionTime = metav1.Now() | ||
if allSynced == metav1.ConditionTrue { | ||
probesCondition.Reason = "AllProbesSynced" | ||
} else { | ||
probesCondition.Reason = "UnsyncedProbes" | ||
probesCondition.Message = "some probes have not yet successfully synced to the DNS Provider" | ||
} | ||
} | ||
|
||
dnsRecord.Status.HealthCheck.Conditions = []metav1.Condition{probesCondition} | ||
|
||
return nil | ||
} | ||
|
||
func getHealthChecksConfig(policy *v1alpha1.DNSRecord) *healthChecksConfig { | ||
if policy.Spec.HealthCheck == nil { | ||
return nil | ||
} | ||
|
||
return &healthChecksConfig{ | ||
Endpoint: policy.Spec.HealthCheck.Endpoint, | ||
Port: valueAs(toInt64, policy.Spec.HealthCheck.Port), | ||
FailureThreshold: valueAs(toInt64, policy.Spec.HealthCheck.FailureThreshold), | ||
Protocol: (*provider.HealthCheckProtocol)(policy.Spec.HealthCheck.Protocol), | ||
} | ||
} | ||
|
||
func valueAs[T, R any](f func(T) R, original *T) *R { | ||
if original == nil { | ||
return nil | ||
} | ||
|
||
value := f(*original) | ||
return &value | ||
} | ||
|
||
func toInt64(original int) int64 { | ||
return int64(original) | ||
} | ||
|
||
// idForEndpoint returns a unique identifier for an endpoint | ||
func idForEndpoint(dnsRecord *v1alpha1.DNSRecord, endpoint *externaldns.Endpoint, address string) (string, error) { | ||
hash := md5.New() | ||
if _, err := io.WriteString(hash, fmt.Sprintf("%s/%s@%s:%s", dnsRecord.Name, endpoint.SetIdentifier, endpoint.DNSName, address)); err != nil { | ||
return "", fmt.Errorf("unexpected error creating ID for endpoint %s", endpoint.SetIdentifier) | ||
} | ||
return fmt.Sprintf("%x", hash.Sum(nil)), 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
Oops, something went wrong.