-
Notifications
You must be signed in to change notification settings - Fork 69
/
mco_managedcluster.go
153 lines (141 loc) · 4.66 KB
/
mco_managedcluster.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) Red Hat, Inc.
// Copyright Contributors to the Open Cluster Management project
// Licensed under the Apache License 2.0
package utils
import (
"context"
"errors"
goversion "github.com/hashicorp/go-version"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func UpdateObservabilityFromManagedCluster(opt TestOptions, enableObservability bool) error {
clusterName := GetManagedClusterName(opt)
if clusterName != "" {
clientDynamic := GetKubeClientDynamic(opt, true)
cluster, err := clientDynamic.Resource(NewOCMManagedClustersGVR()).
Get(context.TODO(), clusterName, metav1.GetOptions{})
if err != nil {
return err
}
labels, ok := cluster.Object["metadata"].(map[string]interface{})["labels"].(map[string]interface{})
if !ok {
cluster.Object["metadata"].(map[string]interface{})["labels"] = map[string]interface{}{}
labels = cluster.Object["metadata"].(map[string]interface{})["labels"].(map[string]interface{})
}
if !enableObservability {
labels["observability"] = "disabled"
} else {
delete(labels, "observability")
}
_, updateErr := clientDynamic.Resource(NewOCMManagedClustersGVR()).
Update(context.TODO(), cluster, metav1.UpdateOptions{})
if updateErr != nil {
return updateErr
}
}
return nil
}
func ListManagedClusters(opt TestOptions) ([]string, error) {
clientDynamic := GetKubeClientDynamic(opt, true)
objs, err := clientDynamic.Resource(NewOCMManagedClustersGVR()).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
clusterNames := []string{}
for _, obj := range objs.Items {
metadata := obj.Object["metadata"].(map[string]interface{})
name := metadata["name"].(string)
labels := metadata["labels"].(map[string]interface{})
if labels != nil {
obsControllerStr := ""
if obsController, ok := labels["feature.open-cluster-management.io/addon-observability-controller"]; ok {
obsControllerStr = obsController.(string)
}
if obsControllerStr != "unreachable" {
clusterNames = append(clusterNames, name)
}
}
}
if len(clusterNames) == 0 {
return clusterNames, errors.New("no managedcluster found")
}
return clusterNames, nil
}
func ListOCPManagedClusterIDs(opt TestOptions, minVersionStr string) ([]string, error) {
minVersion, err := goversion.NewVersion(minVersionStr)
if err != nil {
return nil, err
}
clientDynamic := GetKubeClientDynamic(opt, true)
objs, err := clientDynamic.Resource(NewOCMManagedClustersGVR()).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
clusterIDs := []string{}
for _, obj := range objs.Items {
metadata := obj.Object["metadata"].(map[string]interface{})
labels := metadata["labels"].(map[string]interface{})
if labels != nil {
vendorStr := ""
if vendor, ok := labels["vendor"]; ok {
vendorStr = vendor.(string)
}
obsControllerStr := ""
if obsController, ok := labels["feature.open-cluster-management.io/addon-observability-controller"]; ok {
obsControllerStr = obsController.(string)
}
if vendorStr == "OpenShift" && obsControllerStr == "available" {
clusterVersionStr := ""
if clusterVersionVal, ok := labels["openshiftVersion"]; ok {
clusterVersionStr = clusterVersionVal.(string)
}
clusterVersion, err := goversion.NewVersion(clusterVersionStr)
if err != nil {
return nil, err
}
if clusterVersion.GreaterThanOrEqual(minVersion) {
clusterIDStr := ""
if clusterID, ok := labels["clusterID"]; ok {
clusterIDStr = clusterID.(string)
}
if len(clusterIDStr) > 0 {
clusterIDs = append(clusterIDs, clusterIDStr)
}
}
}
}
}
return clusterIDs, nil
}
func ListKSManagedClusterNames(opt TestOptions) ([]string, error) {
clientDynamic := GetKubeClientDynamic(opt, true)
objs, err := clientDynamic.Resource(NewOCMManagedClustersGVR()).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
clusterNames := []string{}
for _, obj := range objs.Items {
metadata := obj.Object["metadata"].(map[string]interface{})
labels := metadata["labels"].(map[string]interface{})
if labels != nil {
vendorStr := ""
if vendor, ok := labels["vendor"]; ok {
vendorStr = vendor.(string)
}
obsControllerStr := ""
if obsController, ok := labels["feature.open-cluster-management.io/addon-observability-controller"]; ok {
obsControllerStr = obsController.(string)
}
if vendorStr != "OpenShift" && obsControllerStr == "available" {
clusterNameStr := ""
if clusterNameVal, ok := labels["name"]; ok {
clusterNameStr = clusterNameVal.(string)
}
if len(clusterNameStr) > 0 {
clusterNames = append(clusterNames, clusterNameStr)
}
}
}
}
return clusterNames, nil
}