/
cluster.go
308 lines (261 loc) Β· 9.21 KB
/
cluster.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
Copyright 2022 The KubeSphere Authors.
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 scope
import (
"context"
"github.com/go-logr/logr"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2/klogr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
capiutil "sigs.k8s.io/cluster-api/util"
capicollections "sigs.k8s.io/cluster-api/util/collections"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
infrav1 "github.com/kubesphere/kubekey/v3/api/v1beta1"
"github.com/kubesphere/kubekey/v3/pkg/rootfs"
)
// ClusterScopeParams defines the input parameters used to create a new Scope.
type ClusterScopeParams struct {
Client client.Client
Logger *logr.Logger
Cluster *clusterv1.Cluster
KKCluster *infrav1.KKCluster
ControllerName string
RootFsBasePath string
}
// NewClusterScope creates a new Scope from the supplied parameters.
// This is meant to be called for each reconcile iteration.
func NewClusterScope(params ClusterScopeParams) (*ClusterScope, error) {
if params.Cluster == nil {
return nil, errors.New("failed to generate new scope from nil Cluster")
}
if params.KKCluster == nil {
return nil, errors.New("failed to generate new scope from nil KKCluster")
}
if params.Logger == nil {
log := klogr.New()
params.Logger = &log
}
clusterScope := &ClusterScope{
Logger: *params.Logger,
client: params.Client,
Cluster: params.Cluster,
KKCluster: params.KKCluster,
controllerName: params.ControllerName,
rootFs: rootfs.NewLocalRootFs(params.KKCluster.Name, params.RootFsBasePath),
}
helper, err := patch.NewHelper(params.KKCluster, params.Client)
if err != nil {
return nil, errors.Wrap(err, "failed to init patch helper")
}
clusterScope.patchHelper = helper
return clusterScope, nil
}
// ClusterScope defines the basic context for an actuator to operate upon.
type ClusterScope struct {
logr.Logger
client client.Client
patchHelper *patch.Helper
Cluster *clusterv1.Cluster
KKCluster *infrav1.KKCluster
controllerName string
rootFs rootfs.Interface
}
// Name returns the CAPI cluster name.
func (s *ClusterScope) Name() string {
return s.Cluster.Name
}
// Namespace returns the cluster namespace.
func (s *ClusterScope) Namespace() string {
return s.Cluster.Namespace
}
// InfraClusterName returns the KK cluster name.
func (s *ClusterScope) InfraClusterName() string {
return s.KKCluster.Name
}
// KubernetesClusterName is the name of the Kubernetes cluster.
func (s *ClusterScope) KubernetesClusterName() string {
return s.Cluster.Name
}
// GetKKMachines returns the list of KKMachines for a KKCluster.
func (s *ClusterScope) GetKKMachines(ctx context.Context) (*infrav1.KKMachineList, error) {
kkml := &infrav1.KKMachineList{}
if err := s.client.List(
ctx,
kkml,
client.InNamespace(s.KKCluster.Namespace),
client.MatchingLabels{
infrav1.KKClusterLabelName: s.KKCluster.Name,
},
); err != nil {
return nil, errors.Wrap(err, "failed to list KKMachines")
}
return kkml, nil
}
// GetMachines returns the collections of machines for a KKCluster.
func (s *ClusterScope) GetMachines(ctx context.Context, filters ...capicollections.Func) (capicollections.Machines, error) {
kkml, err := s.GetKKMachines(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to get KKMachines")
}
machines := make(capicollections.Machines, len(kkml.Items))
for i := range kkml.Items {
machine, err := capiutil.GetOwnerMachine(ctx, s.client, kkml.Items[i].ObjectMeta)
if err != nil {
return nil, errors.Wrap(err, "failed to get owner machine")
}
machines.Insert(machine)
}
return machines.Filter(filters...), nil
}
// ControlPlaneEndpoint returns the control plane endpoint.
func (s *ClusterScope) ControlPlaneEndpoint() clusterv1.APIEndpoint {
return s.KKCluster.Spec.ControlPlaneEndpoint
}
// GlobalRegistry returns the global registry spec.
func (s *ClusterScope) GlobalRegistry() *infrav1.Registry {
return &s.KKCluster.Spec.Registry
}
// GlobalAuth returns the global auth spec.
func (s *ClusterScope) GlobalAuth() *infrav1.Auth {
return &s.KKCluster.Spec.Nodes.Auth
}
// AllInstancesInfo returns the all instance specs.
func (s *ClusterScope) AllInstancesInfo() []infrav1.InstanceInfo {
return s.KKCluster.Spec.Nodes.Instances
}
// GetInstancesSpecByRole returns the KKInstance spec for the given role.
func (s *ClusterScope) GetInstancesSpecByRole(role infrav1.Role) []infrav1.KKInstanceSpec {
var arr []infrav1.KKInstanceSpec
for i := range s.KKCluster.Spec.Nodes.Instances {
instance := s.KKCluster.Spec.Nodes.Instances[i]
for _, r := range instance.Roles {
if r == role {
spec := infrav1.KKInstanceSpec{}
err := copier.Copy(&spec, &instance)
if err != nil {
s.Logger.Info("Failed to copy instance spec", "instance", instance, "error", err)
continue
}
arr = append(arr, spec)
}
}
}
return arr
}
// AllInstances returns all existing KKInstances in the cluster.
func (s *ClusterScope) AllInstances() ([]*infrav1.KKInstance, error) {
// Get all KKInstances linked to this KKCluster.
allInstances := &infrav1.KKInstanceList{}
err := s.client.List(
context.TODO(),
allInstances,
client.InNamespace(s.KKCluster.Namespace),
client.MatchingLabels(s.KKCluster.Labels),
)
if err != nil {
return nil, errors.Wrap(err, "failed to list kkInstances")
}
// Filter out irrelevant instances (deleting/mismatch labels) and claim orphaned instances.
filteredInstances := make([]*infrav1.KKInstance, 0, len(allInstances.Items))
for idx := range allInstances.Items {
instance := &allInstances.Items[idx]
if shouldExcludeInstance(s.KKCluster, instance) {
continue
}
filteredInstances = append(filteredInstances, instance)
}
return filteredInstances, nil
}
// shouldExcludeInstance returns true if the instance should be filtered out, false otherwise.
func shouldExcludeInstance(cluster *infrav1.KKCluster, instance *infrav1.KKInstance) bool {
if metav1.GetControllerOf(instance) != nil && !capiutil.IsOwnedByObject(instance, cluster) {
return true
}
return false
}
// PatchObject persists the cluster configuration and status.
func (s *ClusterScope) PatchObject() error {
// Always update the readyCondition by summarizing the state of other conditions.
// A step counter is added to represent progress during the provisioning process (instead we are hiding during the deletion process).
applicableConditions := []clusterv1.ConditionType{
infrav1.HostReadyCondition,
infrav1.ExternalLoadBalancerReadyCondition,
}
conditions.SetSummary(s.KKCluster,
conditions.WithConditions(applicableConditions...),
conditions.WithStepCounterIf(s.KKCluster.ObjectMeta.DeletionTimestamp.IsZero()),
conditions.WithStepCounter(),
)
return s.patchHelper.Patch(
context.TODO(),
s.KKCluster,
patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
clusterv1.ReadyCondition,
infrav1.HostReadyCondition,
infrav1.ExternalLoadBalancerReadyCondition,
infrav1.PrincipalPreparedCondition,
}})
}
// Close closes the current scope persisting the cluster configuration and status.
func (s *ClusterScope) Close() error {
return s.PatchObject()
}
// ControllerName returns the name of the controller that
// created the ClusterScope.
func (s *ClusterScope) ControllerName() string {
return s.controllerName
}
// Distribution returns Kubernetes distribution of the cluster.
func (s *ClusterScope) Distribution() string {
return s.KKCluster.Spec.Distribution
}
// RootFs returns the CAPKK rootfs interface.
func (s *ClusterScope) RootFs() rootfs.Interface {
return s.rootFs
}
// ComponentZone returns the KKCluster binaries zone.
func (s *ClusterScope) ComponentZone() string {
if s.KKCluster.Spec.Component == nil {
return ""
}
return s.KKCluster.Spec.Component.ZONE
}
// ComponentHost returns the KKCluster binaries host.
func (s *ClusterScope) ComponentHost() string {
if s.KKCluster.Spec.Component == nil {
return ""
}
return s.KKCluster.Spec.Component.Host
}
// ComponentOverrides returns the KKCluster binaries overrides.
func (s *ClusterScope) ComponentOverrides() []infrav1.Override {
if s.KKCluster.Spec.Component == nil {
return []infrav1.Override{}
}
return s.KKCluster.Spec.Component.Overrides
}
// ControlPlaneLoadBalancer returns the KKLoadBalancerSpec.
func (s *ClusterScope) ControlPlaneLoadBalancer() *infrav1.KKLoadBalancerSpec {
return s.KKCluster.Spec.ControlPlaneLoadBalancer
}
// APIServerPort returns the APIServerPort to use when creating the load balancer.
func (s *ClusterScope) APIServerPort() int32 {
if s.Cluster.Spec.ClusterNetwork != nil && s.Cluster.Spec.ClusterNetwork.APIServerPort != nil {
return *s.Cluster.Spec.ClusterNetwork.APIServerPort
}
return 6443
}