Skip to content

Commit

Permalink
Merge branch 'main' into new-metadata-extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
rsevilla87 authored Feb 22, 2024
2 parents d6b6c68 + 0140b10 commit 4859eab
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 44 deletions.
45 changes: 29 additions & 16 deletions ocp-metadata/ocp-metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"gopkg.in/yaml.v3"
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 @@ -62,16 +63,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()
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 @@ -184,20 +187,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
}

// getVersionInfo obtains OCP and k8s version information
Expand All @@ -216,6 +223,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 @@ -242,17 +253,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
44 changes: 22 additions & 22 deletions ocp-metadata/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,26 @@ 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"`
Fips bool `json:"fips"`
Publish string `json:"publish"`
WorkerArch string `json:"workerArch"`
ControlPlaneArch string `json:"controlPlaneArch"`
Ipsec bool `json:"ipsec"`
IpsecMode string `json:"ipsecMode"`
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"`
Fips bool `json:"fips,omitempty"`
Publish string `json:"publish,omitempty"`
WorkerArch string `json:"workerArch,omitempty"`
ControlPlaneArch string `json:"controlPlaneArch,omitempty"`
Ipsec bool `json:"ipsec,omitempty"`
IpsecMode string `json:"ipsecMode,omitempty"`
}
7 changes: 2 additions & 5 deletions prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ func (p *Prometheus) QueryRange(query string, start, end time.Time, step time.Du

// Verifies prometheus connection
func (p *Prometheus) verifyConnection() error {
_, err := p.api.Runtimeinfo(context.TODO())
if err != nil {
return err
}
return nil
_, err := p.Query("up{}", time.Now().UTC().UTC())
return err
}
2 changes: 1 addition & 1 deletion prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var _ = Describe("Tests for Prometheus", func() {
_, err := NewClient(url, token, username, password, tlsSkipVerify)
//Asserting no of times mocks are called
Expect(count).To(BeEquivalentTo(0))
Expect(err.Error()).To(ContainSubstring("Get \"/api/v1/status/runtimeinfo\": unsupported protocol scheme \"\""))
Expect(err.Error()).To(ContainSubstring("Post \"/api/v1/query\": unsupported protocol scheme \"\""))
})

It("Test2 passing not valid url", func() {
Expand Down

0 comments on commit 4859eab

Please sign in to comment.