Skip to content

Commit

Permalink
Fix worker node count logic (#25)
Browse files Browse the repository at this point in the history
* Fix worker node count logic

Signed-off-by: Raul Sevilla <[email protected]>

* Apply suggestion

Signed-off-by: Raul Sevilla <[email protected]>

---------

Signed-off-by: Raul Sevilla <[email protected]>
  • Loading branch information
rsevilla87 authored Jul 12, 2023
1 parent ec9da22 commit 97b6154
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 43 deletions.
50 changes: 18 additions & 32 deletions ocp-metadata/ocp-metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,9 @@ func (meta *Metadata) GetClusterMetadata() (ClusterMetadata, error) {
return metadata, err
}
metadata.OCPVersion, metadata.OCPMajorVersion, metadata.K8SVersion = version.ocpVersion, version.ocpMajorVersion, version.k8sVersion
nodeInfo, err := meta.getNodesInfo()
if err != nil {
if meta.getNodesInfo(&metadata) != nil {
return metadata, err
}
metadata.TotalNodes = nodeInfo.totalNodes
metadata.MasterNodesCount = nodeInfo.masterCount
metadata.WorkerNodesCount = nodeInfo.workerCount
metadata.InfraNodesCount = nodeInfo.infraCount
metadata.MasterNodesType = nodeInfo.masterType
metadata.WorkerNodesType = nodeInfo.workerType
metadata.InfraNodesType = nodeInfo.infraType
return metadata, err
}

Expand Down Expand Up @@ -212,35 +204,29 @@ func (meta *Metadata) getVersionInfo() (versionObj, error) {
}

// getNodesInfo returns node information
func (meta *Metadata) getNodesInfo() (nodeInfo, error) {
var nodeInfoData nodeInfo
func (meta *Metadata) getNodesInfo(clusterMetadata *ClusterMetadata) error {
nodes, err := meta.clientSet.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nodeInfoData, err
return err
}
nodeInfoData.totalNodes = len(nodes.Items)
clusterMetadata.TotalNodes = len(nodes.Items)
// When the master label is found, the node is considered a master, regarldess of other labels the node could have
// similar logic happens with the infra nodes
for _, node := range nodes.Items {
for k := range node.Labels {
switch k {
case "node-role.kubernetes.io/master":
nodeInfoData.masterCount++
nodeInfoData.masterType = node.Labels["node.kubernetes.io/instance-type"]
case "node-role.kubernetes.io/worker":
// Discard nodes with infra or workload label
for k := range node.Labels {
if k != "node-role.kubernetes.io/infra" && k != "node-role.kubernetes.io/workload" {
nodeInfoData.workerCount++
nodeInfoData.workerType = node.Labels["node.kubernetes.io/instance-type"]
break
}
}
case "node-role.kubernetes.io/infra":
nodeInfoData.infraCount++
nodeInfoData.infraType = node.Labels["node.kubernetes.io/instance-type"]
}
if _, ok := node.Labels["node-role.kubernetes.io/master"]; ok { // Check for master role
clusterMetadata.MasterNodesCount++
clusterMetadata.MasterNodesType = node.Labels["node.kubernetes.io/instance-type"]
} else if _, ok := node.Labels["node-role.kubernetes.io/infra"]; ok { // Check for infra role
clusterMetadata.InfraNodesCount++
clusterMetadata.InfraNodesType = node.Labels["node.kubernetes.io/instance-type"]
} else if _, ok := node.Labels["node-role.kubernetes.io/worker"]; ok { // Check for worker role
clusterMetadata.WorkerNodesCount++
clusterMetadata.WorkerNodesType = node.Labels["node.kubernetes.io/instance-type"]
} else {
clusterMetadata.OtherNodesCount++
}
}
return nodeInfoData, err
return err
}

// getSDNInfo returns SDN type
Expand Down
12 changes: 1 addition & 11 deletions ocp-metadata/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,6 @@ type clusterVersion struct {
} `json:"status"`
}

// Type to store node info
type nodeInfo struct {
workerCount int
infraCount int
masterCount int
totalNodes int
masterType string
workerType string
infraType string
}

// Type to store cluster metadata
type ClusterMetadata struct {
MetricName string `json:"metricName,omitempty"`
Expand All @@ -92,6 +81,7 @@ type ClusterMetadata struct {
InfraNodesType string `json:"infraNodesType"`
WorkerNodesCount int `json:"workerNodesCount"`
InfraNodesCount int `json:"infraNodesCount"`
OtherNodesCount int `json:"otherNodesCount"`
TotalNodes int `json:"totalNodes"`
SDNType string `json:"sdnType"`
ClusterName string `json:"clusterName"`
Expand Down

0 comments on commit 97b6154

Please sign in to comment.