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

feat: Support Karpenter nodes and nodepoolInfo grouping #24

Merged
merged 1 commit into from
Nov 4, 2023
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Nodepool related information
- Node list
- Topology info (Region & Zone)
- Instance type
- Nodepool provider (supported: EKS/AKS/GKE)
- Nodepool provider (supported: EKS/AKS/GKE/Karpenter)
<p align="center"><img src="/assets/nodegizmo-nodepool.png" alt="Nodegizmo node "/></p>

##### nodegizmo node exec nodeName
Expand Down
38 changes: 17 additions & 21 deletions pkg/cmd/nodepool/nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nodepool

import (
"context"
"strings"

"github.com/Kavinraja-G/node-gizmo/pkg/outputs"
"github.com/Kavinraja-G/node-gizmo/utils"
Expand Down Expand Up @@ -32,31 +31,25 @@ func NewCmdNodepoolInfo() *cobra.Command {

// showNodePoolInfo driver function for the 'nodepool' command
func showNodePoolInfo(cmd *cobra.Command, args []string) error {
var genericNodepoolInfos = make(map[string]pkg.GenericNodepoolInfo)
var genericNodepoolInfos []pkg.GenericNodepoolInfo

nodes, err := utils.Cfg.Clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}

for _, node := range nodes.Items {
var genericNodepoolInfo pkg.GenericNodepoolInfo

cloudProvider, nodepoolID := getNodepoolIDAndProvider(node.Labels)
if _, ok := genericNodepoolInfos[nodepoolID]; !ok {
genericNodepoolInfo.NodepoolID = nodepoolID
genericNodepoolInfo.Nodes = append(genericNodepoolInfo.Nodes, node.Name)
genericNodepoolInfo.Provider = cloudProvider
genericNodepoolInfo.InstanceType = getNodeInstanceType(node.Labels)
genericNodepoolInfo.Region, genericNodepoolInfo.Zone = pkg.GetNodeTopologyInfo(node.Labels)

// finally add the genericNodepoolInfo data to the genericNodepoolInfos
genericNodepoolInfos[nodepoolID] = genericNodepoolInfo
} else {
var currentNodepoolInfo = genericNodepoolInfos[nodepoolID]
currentNodepoolInfo.Nodes = append(currentNodepoolInfo.Nodes, node.Name)
genericNodepoolInfos[nodepoolID] = currentNodepoolInfo
}
region, zone := pkg.GetNodeTopologyInfo(node.Labels)

genericNodepoolInfos = append(genericNodepoolInfos, pkg.GenericNodepoolInfo{
NodepoolID: nodepoolID,
Node: node.Name,
Provider: cloudProvider,
InstanceType: getNodeInstanceType(node.Labels),
Region: region,
Zone: zone,
})
}

outputHeaders, outputData := generateNodepoolInfoData(genericNodepoolInfos)
Expand All @@ -66,7 +59,7 @@ func showNodePoolInfo(cmd *cobra.Command, args []string) error {
return nil
}

// getNodepoolIDAndProvider returns the cloud provider type for the nodepool (EKS, GKE, AKS, can be Unknown)
// getNodepoolIDAndProvider returns the cloud provider type for the nodepool (EKS/Karpenter, GKE, AKS, can be Unknown)
func getNodepoolIDAndProvider(labels map[string]string) (string, string) {
if id, ok := labels[pkg.AwsNodepoolLabel]; ok {
return "EKS", id
Expand All @@ -77,6 +70,9 @@ func getNodepoolIDAndProvider(labels map[string]string) (string, string) {
if id, ok := labels[pkg.AksNodepoolLabel]; ok {
return "AKS", id
}
if id, ok := labels[pkg.KarpenterNodepool]; ok {
return "Karpenter", id
}

return "Unknown", "Unknown"
}
Expand All @@ -91,7 +87,7 @@ func getNodeInstanceType(labels map[string]string) string {
}

// generateNodepoolInfoData generates the Nodepool info outputs and the required headers for table-writer
func generateNodepoolInfoData(genericNodepoolInfos map[string]pkg.GenericNodepoolInfo) ([]string, [][]string) {
func generateNodepoolInfoData(genericNodepoolInfos []pkg.GenericNodepoolInfo) ([]string, [][]string) {
var headers = []string{"NODEPOOL", "PROVIDER", "REGION", "ZONE", "INSTANCE-TYPE", "NODES"}
var outputData [][]string

Expand All @@ -102,7 +98,7 @@ func generateNodepoolInfoData(genericNodepoolInfos map[string]pkg.GenericNodepoo
nodepoolInfo.Region,
nodepoolInfo.Zone,
nodepoolInfo.InstanceType,
strings.Join(nodepoolInfo.Nodes, "\n"),
nodepoolInfo.Node,
}

outputData = append(outputData, lineItems)
Expand Down
7 changes: 4 additions & 3 deletions pkg/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package pkg

// required constants used across the CLI commands (mostly well-known labels, annotations etc.,)
const (
AwsNodepoolLabel = "eks.amazonaws.com/nodegroup"
GkeNodepoolLabel = "cloud.google.com/gke-nodepool"
AksNodepoolLabel = "kubernetes.azure.com/agentpool"
AwsNodepoolLabel = "eks.amazonaws.com/nodegroup"
GkeNodepoolLabel = "cloud.google.com/gke-nodepool"
AksNodepoolLabel = "kubernetes.azure.com/agentpool"
KarpenterNodepool = "karpenter.sh/provisioner-name"

NodeInstanceTypeLabel = "node.kubernetes.io/instance-type"
TopologyRegionLabel = "topology.kubernetes.io/region"
Expand Down
8 changes: 6 additions & 2 deletions pkg/outputs/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import (
func TableOutput(headers []string, outputData [][]string) {
table := tablewriter.NewWriter(os.Stdout)

// misc configs for the table-writer
// enables autoMerge only for nodepool infos
if headers[0] == "NODEPOOL" {
table.SetAutoMergeCells(true)
}

// default configs for the table-writer
table.SetRowLine(false)
table.SetBorder(false)
table.SetAutoWrapText(false)
Expand All @@ -23,7 +28,6 @@ func TableOutput(headers []string, outputData [][]string) {
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetTablePadding("\t")
table.SetNoWhiteSpace(true)

// set headers and add the outputData
table.SetHeader(headers)
Expand Down
2 changes: 1 addition & 1 deletion pkg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type NodeCapacities struct {
// GenericNodepoolInfo required node pool info which is used 'nodepool' command
type GenericNodepoolInfo struct {
NodepoolID string
Nodes []string
Node string
Provider string
InstanceType string
Region string
Expand Down
Loading