Skip to content

Commit

Permalink
Added cluster zones methods
Browse files Browse the repository at this point in the history
Signed-off-by: Vicente Zepeda Mas <[email protected]>
  • Loading branch information
chentex committed Jul 21, 2023
1 parent eec044e commit 2374dfb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions k8s-commons/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import (

type KClient interface {
GetVersion() string
// Pods
GetPod(ctx context.Context, namespace string, name string, version string) (*apiv1.Pod, error)
GetPods(ctx context.Context, namespace string, podParams PodParams) (*apiv1.PodList, error)
// Zones
GetZones(ctx context.Context) (map[string]int, error)
IsClusterMultiAZ(ctx context.Context) (bool, error)
GetZoneForNode(ctx context.Context, nodeName string) (string, error)
}

func NewKClient(configOverrides clientcmd.ConfigOverrides) (KClient, error) {
Expand Down
52 changes: 52 additions & 0 deletions k8s-commons/zones.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package k8scommons

import (
"context"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// GetZone returns a map with all the zones an the number of
// worker nodes on each one
func (c *K8SClient) GetZones(ctx context.Context) (map[string]int, error) {
return c.getZones(ctx)
}

// IsClusterMultiAZ returns true if the cluster has more than
// one zone configured in their worker nodes
func (c *K8SClient) IsClusterMultiAZ(ctx context.Context) (bool, error) {
zones, err := c.getZones(ctx)
if err != nil {
return false, err
}
return len(zones) > 1, nil
}

func (c *K8SClient) getZones(ctx context.Context) (map[string]int, error) {
zones := make(map[string]int)

n, err := c.client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{LabelSelector: "node-role.kubernetes.io/worker="})
if err != nil {
return zones, err
}

for _, l := range n.Items {
if len(l.GetLabels()["topology.kubernetes.io/zone"]) < 1 {
return zones, fmt.Errorf("no zone label on Node: %s", l.Name)
}
zones[l.GetLabels()["topology.kubernetes.io/zone"]]++
}
return zones, nil
}

func (c *K8SClient) GetZoneForNode(ctx context.Context, nodeName string) (string, error) {
n, err := c.client.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{})
if err != nil {
return "", err
}
if len(n.GetLabels()["topology.kubernetes.io/zone"]) < 1 {
return "", fmt.Errorf("no zone label on Node: %s", nodeName)
}
return n.GetLabels()["topology.kubernetes.io/zone"], nil
}

0 comments on commit 2374dfb

Please sign in to comment.