/
hub.go
203 lines (173 loc) · 6.49 KB
/
hub.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright Red Hat
package helpers
import (
"context"
"errors"
"os"
"strconv"
singaporev1alpha1 "github.com/stolostron/cluster-registration-operator/api/singapore/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
clusterapiv1 "open-cluster-management.io/api/cluster/v1"
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
manifestworkv1 "open-cluster-management.io/api/work/v1"
clusteradmapply "open-cluster-management.io/clusteradm/pkg/helpers/apply"
authv1alpha1 "open-cluster-management.io/managed-serviceaccount/api/v1alpha1"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/cluster"
)
var (
scheme = runtime.NewScheme()
)
func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = clusterapiv1.AddToScheme(scheme)
_ = clusterv1beta1.AddToScheme(scheme)
_ = addonv1alpha1.AddToScheme(scheme)
_ = authv1alpha1.AddToScheme(scheme)
_ = manifestworkv1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}
type HubInstance struct {
HubConfig *singaporev1alpha1.HubConfig
Cluster cluster.Cluster
Client client.Client
KubeClient kubernetes.Interface
DynamicClient dynamic.Interface
APIExtensionClient apiextensionsclient.Interface
HubApplier clusteradmapply.Applier
}
// GetConditionStatus returns the status for a given condition type and whether the condition was found
func GetConditionStatus(conditions []metav1.Condition, t string) (status metav1.ConditionStatus, ok bool) {
log := ctrl.Log.WithName("GetConditionStatus")
for i := range conditions {
condition := conditions[i]
if condition.Type == t {
log.V(1).Info("condition found", "type", condition.Type, "status", condition.Status)
return condition.Status, true
}
}
log.V(1).Info("condition not found", "type", t)
return "", false
}
func GetHubCluster(workspace string, hubInstances []HubInstance) (HubInstance, error) {
// For now, we always assume there is only one hub cluster. //TODO Later we will replace this with a lookup.
if len(hubInstances) == 0 {
return HubInstance{}, errors.New("hub cluster is not configured")
}
return hubInstances[0], nil
}
func GetHubClusters(mgr ctrl.Manager) ([]HubInstance, error) {
setupLog := ctrl.Log.WithName("setup")
setupLog.Info("setup registeredCluster manager")
setupLog.Info("create dynamic client")
dynamicClient, err := dynamic.NewForConfig(mgr.GetConfig())
if err != nil {
return nil, err
}
setupLog.Info("create kube client")
kubeClient, err := kubernetes.NewForConfig(mgr.GetConfig())
if err != nil {
return nil, err
}
setupLog.Info("retrieve POD namespace")
namespace := os.Getenv("POD_NAMESPACE")
if len(namespace) == 0 {
err := errors.New("POD_NAMESPACE not defined")
return nil, err
}
gvr := schema.GroupVersionResource{
Group: "singapore.open-cluster-management.io",
Version: "v1alpha1",
Resource: "hubconfigs"}
setupLog.Info("retrieve list of hubConfig")
hubConfigListU, err := dynamicClient.Resource(gvr).Namespace(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
setupLog.Info("nb of hubConfig unstructured found", "sze", len(hubConfigListU.Items))
hubInstances := make([]HubInstance, 0)
for _, hubConfigU := range hubConfigListU.Items {
setupLog.Info("convert to hubConfig structure", "name", hubConfigU.GetName())
hubConfig := &singaporev1alpha1.HubConfig{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(hubConfigU.Object, hubConfig); err != nil {
return nil, err
}
setupLog.Info("get config secret", "name", hubConfig.Spec.KubeConfigSecretRef.Name)
configSecret, err := kubeClient.CoreV1().Secrets(hubConfig.Namespace).Get(context.TODO(),
hubConfig.Spec.KubeConfigSecretRef.Name,
metav1.GetOptions{})
if err != nil {
setupLog.Error(err, "unable to read kubeconfig secret for MCE cluster",
"HubConfig Name", hubConfig.GetName(),
"HubConfig Secret Name", hubConfig.Spec.KubeConfigSecretRef.Name)
return nil, err
}
kubeConfigData, ok := configSecret.Data["kubeconfig"]
if !ok {
setupLog.Error(err, "HubConfig secret missing kubeconfig data",
"HubConfig Name", hubConfig.GetName(),
"HubConfig Secret Name", hubConfig.Spec.KubeConfigSecretRef.Name)
return nil, errors.New("HubConfig secret missing kubeconfig data")
}
setupLog.Info("generate hubKubeConfig")
hubKubeconfig, err := clientcmd.RESTConfigFromKubeConfig(kubeConfigData)
if err != nil {
setupLog.Error(err, "unable to create REST config for MCE cluster")
return nil, err
}
if hubConfig.Spec.QPS != "" {
qps, err := strconv.ParseFloat(hubConfig.Spec.QPS, 32);
if err != nil {
return nil, err
}
hubKubeconfig.QPS = float32(qps)
}
hubKubeconfig.Burst = hubConfig.Spec.Burst
if hubConfig.Spec.Burst == 0 {
hubKubeconfig.Burst = 200
}
if hubConfig.Spec.QPS == "" {
hubKubeconfig.QPS = 100.0
}
// Add MCE cluster
hubCluster, err := cluster.New(hubKubeconfig,
func(o *cluster.Options) {
o.Scheme = scheme // Explicitly set the scheme which includes ManagedCluster
},
)
if err != nil {
setupLog.Error(err, "unable to setup MCE cluster. For \"Unauthorized\" error message, the HubConfig secret is expired.",
"HubConfig Secret Name", hubConfig.Spec.KubeConfigSecretRef.Name)
return nil, err
}
// Add MCE cluster to manager
if err := mgr.Add(hubCluster); err != nil {
setupLog.Error(err, "unable to add MCE cluster")
return nil, err
}
kubeClient := kubernetes.NewForConfigOrDie(hubKubeconfig)
dynamicClient := dynamic.NewForConfigOrDie(hubKubeconfig)
apiExtensionClient := apiextensionsclient.NewForConfigOrDie(hubKubeconfig)
hubApplier := clusteradmapply.NewApplierBuilder().WithClient(kubeClient, apiExtensionClient, dynamicClient).Build()
hubInstance := HubInstance{
HubConfig: hubConfig,
Cluster: hubCluster,
Client: hubCluster.GetClient(),
KubeClient: kubeClient,
DynamicClient: dynamicClient,
APIExtensionClient: apiExtensionClient,
HubApplier: hubApplier,
}
hubInstances = append(hubInstances, hubInstance)
}
return hubInstances, nil
}