Skip to content

Commit

Permalink
Remove comments. Handle the error. Add a new column to show the list …
Browse files Browse the repository at this point in the history
…of nodes

Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Nov 28, 2024
1 parent 68bc257 commit 9fc54e2
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions pkg/cmd/get/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func populateClusterList() ([]Cluster, error) {

detectOpt, err := util.DetectKindNodeProvider()
if err != nil {
//logger.Error(err, "failed to detect the provider.")
return nil, err
}

Expand All @@ -94,7 +93,6 @@ func populateClusterList() ([]Cluster, error) {
// TODO: Check if we need it or not like also if the new code handle the kubeconfig path passed as parameter
_, err = helpers.GetKubeClient(kubeConfig)
if err != nil {
//logger.Error(err, "failed to create the kube client.")
return nil, err
}

Expand All @@ -111,33 +109,32 @@ func populateClusterList() ([]Cluster, error) {
provider := cluster.NewProvider(cluster.ProviderWithLogger(kind.KindLoggerFromLogr(&logger)), detectOpt)
clusters, err := provider.List()
if err != nil {
//logger.Error(err, "failed to list clusters.")
return nil, err
}

// Populate a list of Kube client for each cluster/context matching a idpbuilder cluster
manager, _ := CreateKubeClientForEachIDPCluster(config, clusters)
// Populate a list of Kube client for each cluster/context matching an idpbuilder cluster
manager, err := CreateKubeClientForEachIDPCluster(config, clusters)
if err != nil {
return nil, err
}

for _, cluster := range clusters {
aCluster := Cluster{Name: cluster}

// Search about the idp cluster within the kubeconfig file and show information
c, found := findClusterByName(config, "kind-"+cluster)
if !found {
//logger.Error(nil, fmt.Sprintf("Cluster not found: %s within kube config file\n", cluster))
logger.Info(fmt.Sprintf("Cluster not found: %s within kube config file\n", cluster))
} else {
cli, err := GetClientForCluster(manager, cluster)
if err != nil {
//logger.Error(err, fmt.Sprintf("failed to get the context for the cluster: %s.", cluster))
return nil, err
}
logger.V(1).Info(fmt.Sprintf("Got the context for the cluster: %s.", cluster))

// Print the external port mounted on the container and available also as ingress host port
targetPort, err := findExternalHTTPSPort(cli)
if err != nil {
//logger.Error(err, "failed to get the kubernetes ingress service.")
return nil, err
} else {
aCluster.ExternalPort = targetPort
Expand All @@ -146,10 +143,9 @@ func populateClusterList() ([]Cluster, error) {
aCluster.URLKubeApi = c.Server
aCluster.TlsCheck = c.InsecureSkipTLSVerify

// Print the internal port running the Kuber API service
// Print the internal port running the Kube API service
kubeApiPort, err := findInternalKubeApiPort(cli)
if err != nil {
//logger.Error(err, "failed to get the kubernetes default service.")
return nil, err
} else {
aCluster.KubePort = kubeApiPort
Expand All @@ -159,7 +155,6 @@ func populateClusterList() ([]Cluster, error) {
var nodeList corev1.NodeList
err = cli.List(context.TODO(), &nodeList)
if err != nil {
//logger.Error(err, "failed to list nodes for the current kube cluster.")
return nil, err
}

Expand Down Expand Up @@ -194,11 +189,13 @@ func populateClusterList() ([]Cluster, error) {
// Get Node Allocated resources
allocated, err := printAllocatedResources(context.Background(), cli, node.Name)
if err != nil {
//logger.Error(err, "failed to get the allocated resources.")
return nil, err
}
aNode.Allocated = allocated

aCluster.Nodes = append(aCluster.Nodes, aNode)
}

}
clusterList = append(clusterList, aCluster)
}
Expand All @@ -214,6 +211,7 @@ func generateClusterTable(clusterTable []Cluster) metav1.Table {
{Name: "Kube-Api", Type: "string"},
{Name: "TLS", Type: "string"},
{Name: "Kube-Port", Type: "string"},
{Name: "Nodes", Type: "string"},
}
for _, cluster := range clusterTable {
row := metav1.TableRow{
Expand All @@ -223,6 +221,7 @@ func generateClusterTable(clusterTable []Cluster) metav1.Table {
cluster.URLKubeApi,
cluster.TlsCheck,
cluster.KubePort,
generateNodeData(cluster.Nodes),
},
}
table.Rows = append(table.Rows, row)
Expand All @@ -242,6 +241,17 @@ func printTable(opts printers.PrintOptions, table metav1.Table) {
fmt.Println(out.String())
}

func generateNodeData(nodes []Node) string {
var result string
for i, aNode := range nodes {
result += aNode.Name
if i < len(nodes)-1 {
result += ","
}
}
return result
}

func printAllocatedResources(ctx context.Context, k8sClient client.Client, nodeName string) (Allocated, error) {
// List all pods on the specified node
var podList corev1.PodList
Expand All @@ -265,10 +275,6 @@ func printAllocatedResources(ctx context.Context, k8sClient client.Client, nodeN
}
}

// Display the total allocated resources
//fmt.Printf(" CPU Requests: %s\n", totalCPU.String())
//fmt.Printf(" Memory Requests: %s\n", totalMemory.String())

allocated := Allocated{
Memory: totalMemory.String(),
Cpu: totalCPU.String(),
Expand Down

0 comments on commit 9fc54e2

Please sign in to comment.