Skip to content

Commit

Permalink
connectivity: refactor initClients
Browse files Browse the repository at this point in the history
Refactor ConnectivityTest.initClients function to no longer detect
single node clusters.

The method's sole responsibility is now setting the ConnectivityTest's
k8s clients.

Signed-off-by: Louis DeLosSantos <[email protected]>
  • Loading branch information
ldelossa authored and michi-covalent committed Jun 24, 2024
1 parent 8546c13 commit 854a230
Showing 1 changed file with 9 additions and 45 deletions.
54 changes: 9 additions & 45 deletions connectivity/check/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ func (ct *ConnectivityTest) SetupAndValidate(ctx context.Context, extra SetupHoo
if err := ctx.Err(); err != nil {
return err
}
if err := ct.detectSingleNode(ctx); err != nil {
return err
}
if err := ct.initClients(ctx); err != nil {
return err
}
Expand Down Expand Up @@ -863,58 +866,20 @@ func (ct *ConnectivityTest) detectSingleNode(ctx context.Context) error {
return nil
}

// initClients checks if Cilium is installed on the cluster, whether the cluster
// has multiple nodes, and whether or not monitor aggregation is enabled.
// TODO(timo): Split this up, it does a lot.
// initClients assigns the k8s clients used for connectivity tests.
// in the event that this is a multi-cluster test scenario the destination k8s
// client is set to the cluster provided in the MultiCluster parameter.
func (ct *ConnectivityTest) initClients(ctx context.Context) error {
c := &deploymentClients{
src: ct.client,
dst: ct.client,
}

if ct.params.MultiCluster != "" && ct.params.SingleNode {
return fmt.Errorf("single-node test can not be enabled with multi-cluster test")
if ctx.Err() != nil {
return ctx.Err()
}

// In single-cluster environment, automatically detect a single-node
// environment so we can skip deploying tests which depend on multiple
// nodes.
if ct.params.MultiCluster == "" && !ct.params.SingleNode {
daemonSet, err := ct.client.GetDaemonSet(ctx, ct.params.CiliumNamespace, ct.params.AgentDaemonSetName, metav1.GetOptions{})
if err != nil {
ct.Fatal("Unable to determine status of Cilium DaemonSet. Run \"cilium status\" for more details")
return fmt.Errorf("unable to determine status of Cilium DaemonSet: %w", err)
}

isSingleNode := false
if daemonSet.Status.DesiredNumberScheduled == 1 {
isSingleNode = true
} else {
nodes, err := ct.client.ListNodes(ctx, metav1.ListOptions{})
if err != nil {
ct.Fatal("Unable to list nodes.")
return fmt.Errorf("unable to list nodes: %w", err)
}

numWorkerNodes := len(nodes.Items)
for _, n := range nodes.Items {
for _, t := range n.Spec.Taints {
// cannot schedule connectivity test pods on
// master node.
if t.Key == "node-role.kubernetes.io/master" {
numWorkerNodes--
}
}
}

isSingleNode = numWorkerNodes == 1
}

if isSingleNode {
ct.Info("Single-node environment detected, enabling single-node connectivity test")
ct.params.SingleNode = true
}
} else if ct.params.MultiCluster != "" {
if ct.params.MultiCluster != "" {
multiClusterClientLock.Lock()
defer multiClusterClientLock.Unlock()
dst, err := k8s.NewClient(ct.params.MultiCluster, "", ct.params.CiliumNamespace)
Expand All @@ -923,7 +888,6 @@ func (ct *ConnectivityTest) initClients(ctx context.Context) error {
}

c.dst = dst

}

ct.clients = c
Expand Down

0 comments on commit 854a230

Please sign in to comment.