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

VMI count #50

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 26 additions & 11 deletions ocp-metadata/ocp-metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (meta *Metadata) GetCurrentPodCount() (int, error) {
if err != nil {
return podCount, err
}
podList, err := meta.clientSet.CoreV1().Pods(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{FieldSelector: "status.phase=Running"})
podList, err := meta.clientSet.CoreV1().Pods(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{FieldSelector: "status.phase=" + running})
if err != nil {
return podCount, err
}
Expand All @@ -144,13 +144,32 @@ func (meta *Metadata) GetCurrentPodCount() (int, error) {
return podCount, nil
}

// Returns the number of current running VMIs in the cluster
func (meta *Metadata) GetCurrentVMICount() (int, error) {
var vmiCount int
vmis, err := meta.dynamicClient.Resource(vmiGVR).Namespace(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return vmiCount, err
}
for _, vmi := range vmis.Items {
status, found, err := unstructured.NestedString(vmi.UnstructuredContent(), "status", "phase")
if !found {
return vmiCount, fmt.Errorf("phase field not found in kubevirt.io/v1/namespaces/%s/virtualmachineinstances/%s status", vmi.GetNamespace(), vmi.GetName())
}
if err != nil {
return vmiCount, err
}
if status == running {
vmiCount++
}
}
return vmiCount, nil
}

// GetDefaultIngressDomain returns default ingress domain of the default ingress controller
func (meta *Metadata) GetDefaultIngressDomain() (string, error) {
ingressController, err := meta.dynamicClient.Resource(schema.GroupVersionResource{
Group: "operator.openshift.io",
Version: "v1",
Resource: "ingresscontrollers",
}).Namespace("openshift-ingress-operator").Get(context.TODO(), "default", metav1.GetOptions{})
ingressController, err := meta.dynamicClient.Resource(vmiGVR).
jtaleric marked this conversation as resolved.
Show resolved Hide resolved
Namespace("openshift-ingress-operator").Get(context.TODO(), "default", metav1.GetOptions{})
if err != nil {
return "", err
}
Expand All @@ -163,11 +182,7 @@ func (meta *Metadata) GetDefaultIngressDomain() (string, error) {

// getPrometheusURL Returns a valid prometheus endpoint from the openshift-monitoring/prometheus-k8s route
func getPrometheusURL(dynamicClient dynamic.Interface) (string, error) {
route, err := dynamicClient.Resource(schema.GroupVersionResource{
Group: routeGroup,
Version: routeVersion,
Resource: routeResource,
}).Namespace(monitoringNs).Get(context.TODO(), "prometheus-k8s", metav1.GetOptions{})
route, err := dynamicClient.Resource(routeGVR).Namespace(monitoringNs).Get(context.TODO(), "prometheus-k8s", metav1.GetOptions{})
if err != nil {
return "", err
}
Expand Down
22 changes: 18 additions & 4 deletions ocp-metadata/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,33 @@

package ocpmetadata

import "time"
import (
"time"

"k8s.io/apimachinery/pkg/runtime/schema"
)

// OCP specific constants
const (
routeGroup = "route.openshift.io"
routeVersion = "v1"
routeResource = "routes"
running = "Running"
completedUpdate = "Completed"
workerNodeSelector = "node-role.kubernetes.io/worker=,node-role.kubernetes.io/infra!=,node-role.kubernetes.io/workload!="
monitoringNs = "openshift-monitoring"
tokenExpiration = 10 * time.Hour
)

var routeGVR = schema.GroupVersionResource{
Group: "route.openshift.io",
Version: "v1",
Resource: "routes",
}

var vmiGVR = schema.GroupVersionResource{
Group: "kubevirt.io",
Version: "v1",
Resource: "virtualmachineinstances",
}

// infraObj
// TODO at the moment can be used to decode some AWS platform specific information from the infrastructure object
// like region and resourceTags (which is actually used to detect if this is a ROSA cluster)
Expand Down
Loading