Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update endpoint IP selection order based on address type found #61

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
}
Loading