Skip to content

Commit

Permalink
Refactor how we get etcd endpoints in CAPI clusters (#438)
Browse files Browse the repository at this point in the history
* Refactor how we get etcd endpoint in CAPI clusters

* Added tier label to CAPI etcd filters.

---------

Co-authored-by: Nick Jüttner <nick@juni.io>
  • Loading branch information
weseven and njuettner committed Dec 7, 2023
1 parent 7ce6fb5 commit 796bc3c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
11 changes: 4 additions & 7 deletions pkg/giantnetes/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ const (
)

const (
componentETCD = "etcd"

LabelCAPIControlPlaneNode = "node-role.kubernetes.io/control-plane=''"
EtcdLabelComponentKey = "component"
EtcdLabelComponentValue = "etcd"
EtcdLabelTierKey = "tier"
EtcdLabelTierValue = "control-plane"
)

var azureSupportFrom *semver.Version = semver.Must(semver.NewVersion("0.2.0"))
Expand All @@ -33,7 +34,3 @@ func AzureEtcdEndpoint(etcdDomain string) string {
func KVMEtcdEndpoint(etcdDomain string) string {
return fmt.Sprintf("https://%s:443", etcdDomain)
}

func CAPIEtcdEndpoint(component string, nodeName string) string {
return fmt.Sprintf("%s-%s", component, nodeName)
}
19 changes: 10 additions & 9 deletions pkg/giantnetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,7 @@ func (u *Utils) getEtcdEndpoint(ctx context.Context, cluster Cluster) (string, e
}
case CAPI:
{
// for CAPI endpoint we need to fetch workload cluster k8s client and look for controlplane nodes
var nodeList v1.NodeList

// for CAPI endpoint we need to fetch workload cluster k8s client and look for etcd pods
targetClusterRESTConfig, err := key.RESTConfig(ctx, crdClient, cluster.clusterKey)
if err != nil {
return "", microerror.Maskf(executionFailedError, "error fetching CAPI cluster rest config for cluster %#q with error %#q", cluster.clusterKey.Name, err)
Expand All @@ -307,18 +305,21 @@ func (u *Utils) getEtcdEndpoint(ctx context.Context, cluster Cluster) (string, e
return "", microerror.Maskf(executionFailedError, "error creating CAPI cluster kubernetes client for cluster %#q with error %#q", cluster.clusterKey.Name, err)
}

err = targetCtrlClient.List(ctx, &nodeList, ctrl.HasLabels{LabelCAPIControlPlaneNode})
// Specify the label for etcd pods
labelSelector := client.MatchingLabels(map[string]string{EtcdLabelComponentKey: EtcdLabelComponentValue, EtcdLabelTierKey: EtcdLabelTierValue})

// List pods with the specified label
podList := v1.PodList{}
err = targetCtrlClient.List(ctx, &podList, labelSelector)
if err != nil {
return "", microerror.Maskf(executionFailedError, "error creating CAPI cluster kubernetes client for cluster %#q with error %#q", cluster.clusterKey.Name, err)
}

if len(nodeList.Items) == 0 {
return "", microerror.Maskf(executionFailedError, "error getting etcd endpoint , no control plane nodes found, cluster %#q with error %#q", cluster.clusterKey.Name, err)
if len(podList.Items) == 0 {
return "", microerror.Maskf(executionFailedError, "error getting etcd endpoint, no etcd pods found in cluster %#q with error %#q", cluster.clusterKey.Name, err)
}

nodeName := nodeList.Items[0].Name

etcdEndpoint = CAPIEtcdEndpoint(componentETCD, nodeName)
etcdEndpoint = podList.Items[0].Name
break
}
}
Expand Down

0 comments on commit 796bc3c

Please sign in to comment.