Skip to content

Commit

Permalink
Update endpoint IP selection order based on address type found (#61)
Browse files Browse the repository at this point in the history
Signed-off-by: Rahul Ganesh <rahulgab@amazon.com>
Co-authored-by: Rahul Ganesh <rahulgab@amazon.com>
  • Loading branch information
rahulbabu95 and Rahul Ganesh committed Apr 20, 2024
1 parent b202824 commit a407200
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 15 deletions.
39 changes: 24 additions & 15 deletions controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func (r *EtcdadmClusterReconciler) cloneConfigsAndGenerateMachine(ctx context.Co
ClusterName: cluster.Name,
Labels: EtcdLabelsForCluster(cluster.Name, ec.Name),
})

if err != nil {
return ctrl.Result{}, fmt.Errorf("error cloning infrastructure template for etcd machine: %v", err)
}
Expand Down Expand Up @@ -158,24 +157,34 @@ func (r *EtcdadmClusterReconciler) generateMachine(ctx context.Context, ec *etcd
}

func getEtcdMachineAddress(machine *clusterv1.Machine) string {
var foundAddress bool
var machineAddress string
var internalIP, internalDNS, externalIP, externalDNS string

// Check and record all different address types set for the machine and return later according to precedence.
for _, address := range machine.Status.Addresses {
if address.Type == clusterv1.MachineInternalIP || address.Type == clusterv1.MachineInternalDNS {
machineAddress = address.Address
foundAddress = true
break
switch address.Type {
case clusterv1.MachineInternalIP:
internalIP = address.Address
case clusterv1.MachineInternalDNS:
internalDNS = address.Address
case clusterv1.MachineExternalIP:
externalIP = address.Address
case clusterv1.MachineExternalDNS:
externalDNS = address.Address
}
}
for _, address := range machine.Status.Addresses {
if !foundAddress {
if address.Type == clusterv1.MachineExternalIP || address.Type == clusterv1.MachineExternalDNS {
machineAddress = address.Address
break
}
}

// The order of these checks determines the precedence of the address to use
if externalDNS != "" {
return externalDNS
} else if externalIP != "" {
return externalIP
} else if internalDNS != "" {
return internalDNS
} else if internalIP != "" {
return internalIP
}
return machineAddress

return ""
}

func getMemberClientURL(address string) string {
Expand Down
92 changes: 92 additions & 0 deletions controllers/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package controllers

import (
"testing"

. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

func TestGetEtcdMachineAddress(t *testing.T) {
g := NewWithT(t)
type test struct {
machine clusterv1.Machine
AvailAddrTypes clusterv1.MachineAddresses
wantAddr string
}

capiMachine := clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "machine",
},
Spec: clusterv1.MachineSpec{
ClusterName: "test-cluster",
},
Status: clusterv1.MachineStatus{},
}
tests := []test{
{
machine: capiMachine,
AvailAddrTypes: clusterv1.MachineAddresses{
clusterv1.MachineAddress{
Type: clusterv1.MachineInternalIP,
Address: "1.1.1.1",
}, clusterv1.MachineAddress{
Type: clusterv1.MachineExternalIP,
Address: "2.2.2.2",
},
},
wantAddr: "2.2.2.2",
}, {
machine: capiMachine,
AvailAddrTypes: []clusterv1.MachineAddress{
{
Type: clusterv1.MachineInternalDNS,
Address: "1.1.1.1",
}, {
Type: clusterv1.MachineExternalIP,
Address: "2.2.2.2",
},
},
wantAddr: "2.2.2.2",
}, {
machine: capiMachine,
AvailAddrTypes: []clusterv1.MachineAddress{
{
Type: clusterv1.MachineInternalIP,
Address: "1.1.1.1",
}, {
Type: clusterv1.MachineInternalDNS,
Address: "2.2.2.2",
},
},
wantAddr: "2.2.2.2",
}, {
machine: capiMachine,
AvailAddrTypes: []clusterv1.MachineAddress{
{
Type: clusterv1.MachineExternalDNS,
Address: "1.1.1.1",
}, {
Type: clusterv1.MachineInternalDNS,
Address: "2.2.2.2",
},
},
wantAddr: "1.1.1.1",
}, {
machine: capiMachine,
AvailAddrTypes: []clusterv1.MachineAddress{
{
Type: clusterv1.MachineHostName,
Address: "1.1.1.1",
},
},
wantAddr: "",
},
}
for _, tc := range tests {
capiMachine.Status.Addresses = tc.AvailAddrTypes
g.Expect(getEtcdMachineAddress(&capiMachine)).To(Equal(tc.wantAddr))
}
}

0 comments on commit a407200

Please sign in to comment.