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

K8s metadata #38

Merged
merged 2 commits into from
Dec 11, 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
45 changes: 29 additions & 16 deletions ocp-metadata/ocp-metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"regexp"

authenticationv1 "k8s.io/api/authentication/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -57,16 +58,18 @@ func (meta *Metadata) GetClusterMetadata() (ClusterMetadata, error) {
if err != nil {
return metadata, nil
}
metadata.ClusterName, metadata.Platform, metadata.Region = infra.Status.InfrastructureName, infra.Status.Platform, infra.Status.PlatformStatus.Aws.Region
metadata.ClusterType = "self-managed"
for _, v := range infra.Status.PlatformStatus.Aws.ResourceTags {
if v.Key == "red-hat-clustertype" {
metadata.ClusterType = v.Value
if infra != nil {
metadata.ClusterName, metadata.Platform, metadata.Region = infra.Status.InfrastructureName, infra.Status.Platform, infra.Status.PlatformStatus.Aws.Region
metadata.ClusterType = "self-managed"
for _, v := range infra.Status.PlatformStatus.Aws.ResourceTags {
if v.Key == "red-hat-clustertype" {
metadata.ClusterType = v.Value
}
}
metadata.SDNType, err = meta.getSDNInfo()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need SDNType as well to be populated only when there are infra nodes present?

Copy link
Member Author

@rsevilla87 rsevilla87 Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

infra does not refer to infra nodes but to the "infrastructure" object, which is only present in OpenShift clusters, same as config.openshift.io/v1/networks. for that reason when the infra object is not present, we don't try to get the sdnType

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I made this mistake too @vishnuchalla

if err != nil {
return metadata, err
}
}
metadata.SDNType, err = meta.getSDNInfo()
if err != nil {
return metadata, err
}
version, err := meta.getVersionInfo()
if err != nil {
Expand Down Expand Up @@ -154,20 +157,24 @@ func getBearerToken(clientset *kubernetes.Clientset) (string, error) {
return response.Status.Token, err
}

// getInfraDetails returns cluster name and platform
func (meta *Metadata) getInfraDetails() (infraObj, error) {
// getInfraDetails returns a pointer to an infrastructure object or nil
func (meta *Metadata) getInfraDetails() (*infraObj, error) {
var infraJSON infraObj
infra, err := meta.dynamicClient.Resource(schema.GroupVersionResource{
Group: "config.openshift.io",
Version: "v1",
Resource: "infrastructures",
}).Get(context.TODO(), "cluster", metav1.GetOptions{})
if err != nil {
return infraJSON, err
// If the infrastructure resource is not found we assume this is not an OCP cluster
if errors.IsNotFound(err) {
return nil, nil
}
return &infraJSON, err
}
infraData, _ := infra.MarshalJSON()
err = json.Unmarshal(infraData, &infraJSON)
return infraJSON, err
return &infraJSON, err
vishnuchalla marked this conversation as resolved.
Show resolved Hide resolved
}

// getVersionInfo obtains OCP and k8s version information
Expand All @@ -186,6 +193,10 @@ func (meta *Metadata) getVersionInfo() (versionObj, error) {
Resource: "clusterversions",
}).Get(context.TODO(), "version", metav1.GetOptions{})
if err != nil {
// If the clusterversion resource is not found we assume this is not an OCP cluster
if errors.IsNotFound(err) {
return versionInfo, nil
}
return versionInfo, err
}
clusterVersionBytes, _ := clusterVersion.MarshalJSON()
Expand All @@ -212,17 +223,19 @@ func (meta *Metadata) getNodesInfo(clusterMetadata *ClusterMetadata) error {
return err
}
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
// When the master label is found, the node is considered a master, regardless of other labels the node could have
for _, node := range nodes.Items {
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"]
if _, ok := node.Labels["node-role.kubernetes.io/worker"]; ok {
if len(node.Spec.Taints) == 0 { // When mastersSchedulable is true, master nodes have at least one taint
if len(node.Spec.Taints) == 0 { // When mastersSchedulable is false, master nodes have at least one taint
clusterMetadata.WorkerNodesCount++
}
}
} else if _, ok := node.Labels["node-role.kubernetes.io/control-plane"]; ok { // Check for control-plane 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"]
Expand Down
32 changes: 16 additions & 16 deletions ocp-metadata/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ type clusterVersion struct {
// Type to store cluster metadata
type ClusterMetadata struct {
MetricName string `json:"metricName,omitempty"`
Platform string `json:"platform"`
ClusterType string `json:"clusterType"`
OCPVersion string `json:"ocpVersion"`
OCPMajorVersion string `json:"ocpMajorVersion"`
K8SVersion string `json:"k8sVersion"`
MasterNodesType string `json:"masterNodesType"`
WorkerNodesType string `json:"workerNodesType"`
MasterNodesCount int `json:"masterNodesCount"`
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"`
Region string `json:"region"`
Platform string `json:"platform,omitempty"`
ClusterType string `json:"clusterType,omitempty"`
OCPVersion string `json:"ocpVersion,omitempty"`
OCPMajorVersion string `json:"ocpMajorVersion,omitempty"`
K8SVersion string `json:"k8sVersion,omitempty"`
MasterNodesType string `json:"masterNodesType,omitempty"`
WorkerNodesType string `json:"workerNodesType,omitempty"`
MasterNodesCount int `json:"masterNodesCount,omitempty"`
InfraNodesType string `json:"infraNodesType,omitempty"`
WorkerNodesCount int `json:"workerNodesCount,omitempty"`
InfraNodesCount int `json:"infraNodesCount,omitempty"`
OtherNodesCount int `json:"otherNodesCount,omitempty"`
TotalNodes int `json:"totalNodes,omitempty"`
SDNType string `json:"sdnType,omitempty"`
ClusterName string `json:"clusterName,omitempty"`
Region string `json:"region,omitempty"`
}
Loading