-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathacm.go
More file actions
133 lines (114 loc) · 4.15 KB
/
acm.go
File metadata and controls
133 lines (114 loc) · 4.15 KB
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
/*
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers
// https://github.com/stolostron/cm-cli/blob/64e944330f6ca20c559abcd382d7712f10cb904f/pkg/cmd/cmd.go#L75
import (
"context"
"fmt"
"log"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)
func haveACMHub(r *PatternReconciler) bool {
gvrMCH := schema.GroupVersionResource{Group: "operator.open-cluster-management.io", Version: "v1", Resource: "multiclusterhubs"}
serverNamespace := ""
cms, err := r.fullClient.CoreV1().ConfigMaps("").List(context.TODO(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("%v = %v", "ocm-configmap-type", "image-manifest"),
})
if (err != nil || len(cms.Items) == 0) && serverNamespace != "" {
cms, err = r.fullClient.CoreV1().ConfigMaps(serverNamespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("%v = %v", "ocm-configmap-type", "image-manifest"),
})
}
if err != nil || len(cms.Items) == 0 {
cms, err = r.fullClient.CoreV1().ConfigMaps("open-cluster-management").List(context.TODO(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("%v = %v", "ocm-configmap-type", "image-manifest"),
})
}
if err != nil {
log.Printf("config map error: %s\n", err.Error())
return false
}
if len(cms.Items) == 0 {
log.Printf("No config map\n")
return false
}
ns := cms.Items[0].Namespace
umch, err := r.dynamicClient.Resource(gvrMCH).Namespace(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Printf("Error obtaining hub: %s\n", err)
return false
} else if len(umch.Items) == 0 {
log.Printf("No hub in %s\n", ns)
return false
}
return true
}
// listManagedClusters lists all ManagedCluster resources (excluding local-cluster)
// Returns a list of cluster names and an error
func (r *PatternReconciler) listManagedClusters(ctx context.Context) ([]string, error) {
gvrMC := schema.GroupVersionResource{
Group: "cluster.open-cluster-management.io",
Version: "v1",
Resource: "managedclusters",
}
// ManagedCluster is a cluster-scoped resource, so no namespace needed
mcList, err := r.dynamicClient.Resource(gvrMC).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("failed to list ManagedClusters: %w", err)
}
var clusterNames []string
for _, item := range mcList.Items {
name := item.GetName()
// Exclude local-cluster (hub cluster)
if name != "local-cluster" {
clusterNames = append(clusterNames, name)
}
}
return clusterNames, nil
}
// deleteManagedClusters deletes all ManagedCluster resources (excluding local-cluster)
// Returns the number of clusters deleted and an error
func (r *PatternReconciler) deleteManagedClusters(ctx context.Context) (int, error) {
gvrMC := schema.GroupVersionResource{
Group: "cluster.open-cluster-management.io",
Version: "v1",
Resource: "managedclusters",
}
// ManagedCluster is a cluster-scoped resource, so no namespace needed
mcList, err := r.dynamicClient.Resource(gvrMC).List(ctx, metav1.ListOptions{})
if err != nil {
return 0, fmt.Errorf("failed to list ManagedClusters: %w", err)
}
deletedCount := 0
for _, item := range mcList.Items {
name := item.GetName()
// Exclude local-cluster (hub cluster)
if name == "local-cluster" {
continue
}
// Delete the managed cluster
err := r.dynamicClient.Resource(gvrMC).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil {
// If already deleted, that's fine
if kerrors.IsNotFound(err) {
continue
}
return deletedCount, fmt.Errorf("failed to delete ManagedCluster %q: %w", name, err)
}
log.Printf("Deleted ManagedCluster: %q", name)
deletedCount++
}
return deletedCount, nil
}