-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathfind.go
93 lines (73 loc) · 3 KB
/
find.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package find
import (
"fmt"
"strings"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/openshift-kni/eco-goinfra/pkg/hive"
"github.com/openshift-kni/eco-goinfra/pkg/pod"
"github.com/openshift-kni/eco-gotests/tests/internal/cluster"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ClusterVersion returns the Major.Minor part of a cluster's OCP version.
func ClusterVersion(clusterObj cluster.APIClientGetter) (string, error) {
clusterVersion, err := cluster.GetOCPClusterVersion(clusterObj)
if err != nil {
return "", err
}
if !clusterVersion.Exists() {
return "", fmt.Errorf("cluster version not found")
}
splitVersion := strings.Split(clusterVersion.Object.Status.Desired.Version, ".")
return fmt.Sprintf("%s.%s", splitVersion[0], splitVersion[1]), nil
}
// SpokeClusterName returns the spoke cluster name based on hub and spoke cluster apiclients.
func SpokeClusterName(hubAPIClient, spokeAPIClient *clients.Settings) (string, error) {
spokeClusterVersion, err := cluster.GetOCPClusterVersion(spokeAPIClient)
if err != nil {
return "", err
}
if !spokeClusterVersion.Exists() {
return "", fmt.Errorf("spoke cluster version not found")
}
spokeClusterID := spokeClusterVersion.Object.Spec.ClusterID
clusterDeployments, err := hive.ListClusterDeploymentsInAllNamespaces(hubAPIClient)
if err != nil {
return "", err
}
for _, clusterDeploymentBuilder := range clusterDeployments {
if clusterDeploymentBuilder.Object.Spec.ClusterMetadata != nil &&
clusterDeploymentBuilder.Object.Spec.ClusterMetadata.ClusterID == string(spokeClusterID) {
return clusterDeploymentBuilder.Object.Spec.ClusterName, nil
}
}
return "", fmt.Errorf("could not find ClusterDeployment from provided API clients")
}
// AssistedServicePod returns pod running assisted-service.
func AssistedServicePod(apiClient *clients.Settings) (*pod.Builder, error) {
return getPodBuilder(apiClient, "app=assisted-service")
}
// AssistedImageServicePod returns pod running assisted-image-service.
func AssistedImageServicePod(apiClient *clients.Settings) (*pod.Builder, error) {
return getPodBuilder(apiClient, "app=assisted-image-service")
}
// InfrastructureOperatorPod returns pod running infrastructure-operator.
func InfrastructureOperatorPod(apiClient *clients.Settings) (*pod.Builder, error) {
return getPodBuilder(apiClient, "control-plane=infrastructure-operator")
}
// getPodBuilder returns a podBuilder of a pod based on provided label.
func getPodBuilder(apiClient *clients.Settings, label string) (*pod.Builder, error) {
if apiClient == nil {
return nil, fmt.Errorf("apiClient is nil")
}
podList, err := pod.ListInAllNamespaces(apiClient, metav1.ListOptions{LabelSelector: label})
if err != nil {
return nil, fmt.Errorf("failed to list pods on cluster: %w", err)
}
if len(podList) == 0 {
return nil, fmt.Errorf("pod with label '%s' not currently running", label)
}
if len(podList) > 1 {
return nil, fmt.Errorf("got unexpected pods when checking for pods with label '%s'", label)
}
return podList[0], nil
}