Skip to content

Commit

Permalink
chore: Tidy up dnspolicy controller code and files
Browse files Browse the repository at this point in the history
Follow on from Kuadrant#937 to
cleanup controller files to bring them more inline with the current sotw
approach.

There are no logical changes in this commit, existing code is just being
moved.

Signed-off-by: Michael Nairn <mnairn@redhat.com>
  • Loading branch information
mikenairn committed Oct 31, 2024
1 parent 5fa1c90 commit 3dac1ed
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 265 deletions.
76 changes: 0 additions & 76 deletions controllers/dns_helper.go

This file was deleted.

42 changes: 0 additions & 42 deletions controllers/dnspolicy_controller.go

This file was deleted.

65 changes: 62 additions & 3 deletions controllers/dnspolicy_dnsrecords.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package controllers

import (
"fmt"
"net"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
externaldns "sigs.k8s.io/external-dns/endpoint"
Expand All @@ -13,11 +15,14 @@ import (
"github.com/kuadrant/kuadrant-operator/api/v1alpha1"
)

var (
ErrNoRoutes = fmt.Errorf("no routes attached to any gateway listeners")
ErrNoAddresses = fmt.Errorf("no valid status addresses to use on gateway")
const (
LabelListenerReference = "kuadrant.io/listener-name"
)

func dnsRecordName(gatewayName, listenerName string) string {
return fmt.Sprintf("%s-%s", gatewayName, listenerName)
}

func desiredDNSRecord(gateway *gatewayapiv1.Gateway, clusterID string, dnsPolicy *v1alpha1.DNSPolicy, targetListener gatewayapiv1.Listener) (*kuadrantdnsv1alpha1.DNSRecord, error) {
rootHost := string(*targetListener.Hostname)
var healthCheckSpec *kuadrantdnsv1alpha1.HealthCheckSpec
Expand Down Expand Up @@ -61,6 +66,60 @@ func desiredDNSRecord(gateway *gatewayapiv1.Gateway, clusterID string, dnsPolicy
return dnsRecord, nil
}

// GatewayWrapper is a wrapper for gateway to implement interface from the builder
type GatewayWrapper struct {
*gatewayapiv1.Gateway
excludedAddresses []string
}

func NewGatewayWrapper(gateway *gatewayapiv1.Gateway) *GatewayWrapper {
return &GatewayWrapper{Gateway: gateway}
}

func (g *GatewayWrapper) GetAddresses() []builder.TargetAddress {
addresses := make([]builder.TargetAddress, len(g.Status.Addresses))
for i, address := range g.Status.Addresses {
addresses[i] = builder.TargetAddress{
Type: builder.AddressType(*address.Type),
Value: address.Value,
}
}
return addresses
}

func (g *GatewayWrapper) RemoveExcludedStatusAddresses(p *v1alpha1.DNSPolicy) error {
g.excludedAddresses = p.Spec.ExcludeAddresses
newAddresses := []gatewayapiv1.GatewayStatusAddress{}
for _, address := range g.Gateway.Status.Addresses {
found := false
for _, exclude := range p.Spec.ExcludeAddresses {
//Only a CIDR will have / in the address so attempt to parse fail if not valid
if strings.Contains(exclude, "/") {
_, network, err := net.ParseCIDR(exclude)
if err != nil {
return fmt.Errorf("could not parse the CIDR from the excludeAddresses field %w", err)
}
ip := net.ParseIP(address.Value)
// only check addresses that are actually IPs
if ip != nil && network.Contains(ip) {
found = true
break
}
}
if exclude == address.Value {
found = true
break
}
}
if !found {
newAddresses = append(newAddresses, address)
}
}
// setting this in memory only won't be saved to actual gateway
g.Status.Addresses = newAddresses
return nil
}

func buildEndpoints(clusterID, hostname string, gateway *gatewayapiv1.Gateway, policy *v1alpha1.DNSPolicy) ([]*externaldns.Endpoint, error) {
gw := gateway.DeepCopy()
gatewayWrapper := NewGatewayWrapper(gw)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build unit

package controllers_test

import (
Expand All @@ -9,7 +11,7 @@ import (
"github.com/kuadrant/kuadrant-operator/controllers"
)

func TestRemoveExcludedStatusAddresses(t *testing.T) {
func TestGatewayWrapper_RemoveExcludedStatusAddresses(t *testing.T) {
ipaddress := gatewayapiv1.IPAddressType
hostaddress := gatewayapiv1.HostnameAddressType
testCases := []struct {
Expand Down
63 changes: 63 additions & 0 deletions controllers/dnspolicy_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2024.
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 controllers

import (
"github.com/prometheus/client_golang/prometheus"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/metrics"

kuadrantv1alpha1 "github.com/kuadrant/kuadrant-operator/api/v1alpha1"
)

const (
dnsPolicyNameLabel = "dns_policy_name"
dnsPolicyNamespaceLabel = "dns_policy_namespace"
dnsPolicyCondition = "dns_policy_condition"
)

var (
dnsPolicyReady = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "kuadrant_dns_policy_ready",
Help: "DNS Policy ready",
},
[]string{dnsPolicyNameLabel, dnsPolicyNamespaceLabel, dnsPolicyCondition})
)

func emitConditionMetrics(dnsPolicy *kuadrantv1alpha1.DNSPolicy) {
readyStatus := meta.FindStatusCondition(dnsPolicy.Status.Conditions, ReadyConditionType)
if readyStatus == nil {
dnsPolicyReady.WithLabelValues(dnsPolicy.Name, dnsPolicy.Namespace, "true").Set(0)
dnsPolicyReady.WithLabelValues(dnsPolicy.Name, dnsPolicy.Namespace, "false").Set(0)
dnsPolicyReady.WithLabelValues(dnsPolicy.Name, dnsPolicy.Namespace, "unknown").Set(1)
} else if readyStatus.Status != metav1.ConditionTrue {
dnsPolicyReady.WithLabelValues(dnsPolicy.Name, dnsPolicy.Namespace, "true").Set(0)
dnsPolicyReady.WithLabelValues(dnsPolicy.Name, dnsPolicy.Namespace, "false").Set(1)
dnsPolicyReady.WithLabelValues(dnsPolicy.Name, dnsPolicy.Namespace, "unknown").Set(0)
} else {
dnsPolicyReady.WithLabelValues(dnsPolicy.Name, dnsPolicy.Namespace, "true").Set(1)
dnsPolicyReady.WithLabelValues(dnsPolicy.Name, dnsPolicy.Namespace, "false").Set(0)
dnsPolicyReady.WithLabelValues(dnsPolicy.Name, dnsPolicy.Namespace, "unknown").Set(0)
}
}

func init() {
metrics.Registry.MustRegister(dnsPolicyReady)
}
Loading

0 comments on commit 3dac1ed

Please sign in to comment.