forked from kubesphere/kubekey
/
instance.go
238 lines (200 loc) Β· 7.82 KB
/
instance.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
/*
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/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2/klogr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
"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"
)
// InstanceScopeParams defines the input parameters used to create a new InstanceScope.
type InstanceScopeParams struct {
Client client.Client
Logger *logr.Logger
Cluster *clusterv1.Cluster
InfraCluster *ClusterScope
Machine *clusterv1.Machine
KKMachine *infrav1.KKMachine
KKInstance *infrav1.KKInstance
}
// NewInstanceScope creates a new InstanceScope from the supplied parameters.
// This is meant to be called for each reconcile iteration.
func NewInstanceScope(params InstanceScopeParams) (*InstanceScope, error) {
if params.Client == nil {
return nil, errors.New("client is required when creating a InstanceScope")
}
if params.Cluster == nil {
return nil, errors.New("cluster is required when creating a InstanceScope")
}
if params.Machine == nil {
return nil, errors.New("machine is required when creating a InstanceScope")
}
if params.InfraCluster == nil {
return nil, errors.New("KKCluster is required when creating a InstanceScope")
}
if params.KKMachine == nil {
return nil, errors.New("KKMachine is required when creating a InstanceScope")
}
if params.KKInstance == nil {
return nil, errors.New("KKInstance is required when creating a InstanceScope")
}
if params.Logger == nil {
log := klogr.New().WithValues("cluster", params.Cluster.Name, "machine", params.Machine.Name,
"kkInstance", params.KKInstance.Name)
params.Logger = &log
}
helper, err := patch.NewHelper(params.KKInstance, params.Client)
if err != nil {
return nil, errors.Wrap(err, "failed to init patch helper")
}
return &InstanceScope{
Logger: *params.Logger,
client: params.Client,
patchHelper: helper,
Cluster: params.Cluster,
Machine: params.Machine,
InfraCluster: params.InfraCluster,
KKMachine: params.KKMachine,
KKInstance: params.KKInstance,
}, nil
}
// InstanceScope defines a scope defined around a machine instance and its cluster.
type InstanceScope struct {
logr.Logger
client client.Client
patchHelper *patch.Helper
Cluster *clusterv1.Cluster
InfraCluster pkg.ClusterScoper
Machine *clusterv1.Machine
KKMachine *infrav1.KKMachine
KKInstance *infrav1.KKInstance
}
// Name returns the name of the KKInstance.
func (i *InstanceScope) Name() string {
return i.KKInstance.Name
}
// HostName returns the hostname of the KKInstance corresponding remote host.
func (i *InstanceScope) HostName() string {
return i.KKInstance.Spec.Name
}
// Namespace returns the namespace of the KKInstance.
func (i *InstanceScope) Namespace() string {
return i.KKInstance.Namespace
}
// InternalAddress returns the internal address of the KKInstance.
func (i *InstanceScope) InternalAddress() string {
return i.KKInstance.Spec.InternalAddress
}
// Arch returns the architecture of the KKInstance.
func (i *InstanceScope) Arch() string {
return i.KKInstance.Spec.Arch
}
// ContainerManager returns the ContainerManager struct of the KKInstance.
func (i *InstanceScope) ContainerManager() *infrav1.ContainerManager {
return &i.KKInstance.Spec.ContainerManager
}
// RepositoryEnabled returns whether the repository is enabled.
func (i *InstanceScope) RepositoryEnabled() bool {
return i.KKInstance.Spec.Repository != nil && i.KKInstance.Spec.Repository.ISO != ""
}
// RepositoryUseISO returns whether the repository uses ISO.
func (i *InstanceScope) RepositoryUseISO() bool {
return i.RepositoryEnabled() && i.KKInstance.Spec.Repository.ISO != infrav1.NONE
}
// Repository returns the repository of the KKInstance.
func (i *InstanceScope) Repository() *infrav1.Repository {
return i.KKInstance.Spec.Repository
}
// KubernetesVersion returns the Kubernetes version of the KKInstance.
func (i *InstanceScope) KubernetesVersion() string {
return *i.Machine.Spec.Version
}
// InPlaceUpgradeVersion returns the in-place upgrade version of the KKInstance.
func (i *InstanceScope) InPlaceUpgradeVersion() string {
return i.KKInstance.GetAnnotations()[infrav1.InPlaceUpgradeVersionAnnotation]
}
// IsControlPlane returns whether the KKInstance is a control plane node.
func (i *InstanceScope) IsControlPlane() bool {
if _, ok := i.Machine.GetLabels()[clusterv1.MachineControlPlaneLabelName]; ok {
return true
}
return false
}
// GetRawBootstrapDataWithFormat returns the raw bootstrap data from the corresponding machine.spec.bootstrap.
func (i *InstanceScope) GetRawBootstrapDataWithFormat(ctx context.Context) ([]byte, bootstrapv1.Format, error) {
if i.Machine.Spec.Bootstrap.DataSecretName == nil {
return nil, "", errors.New("error retrieving bootstrap data: linked Machine's bootstrap.dataSecretName is nil")
}
secret := &corev1.Secret{}
key := types.NamespacedName{Namespace: i.Machine.Namespace, Name: *i.Machine.Spec.Bootstrap.DataSecretName}
if err := i.client.Get(ctx, key, secret); err != nil {
return nil, "", errors.Wrapf(err, "failed to retrieve bootstrap data secret for KKInstance %s/%s", i.Namespace(), i.Name())
}
value, ok := secret.Data["value"]
if !ok {
return nil, "", errors.New("error retrieving bootstrap data: secret value key is missing")
}
format := secret.Data["format"]
if string(format) == "" {
format = []byte(bootstrapv1.CloudConfig)
}
return value, bootstrapv1.Format(format), nil
}
// PatchObject persists the machine spec and status.
func (i *InstanceScope) 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.KKInstanceBootstrappedCondition,
infrav1.KKInstanceBinariesReadyCondition,
infrav1.KKInstanceCRIReadyCondition,
infrav1.KKInstanceProvisionedCondition,
}
conditions.SetSummary(i.KKInstance,
conditions.WithConditions(applicableConditions...),
conditions.WithStepCounterIf(i.KKInstance.ObjectMeta.DeletionTimestamp.IsZero()),
conditions.WithStepCounter(),
)
return i.patchHelper.Patch(
context.TODO(),
i.KKInstance,
patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
clusterv1.ReadyCondition,
infrav1.KKInstanceBootstrappedCondition,
infrav1.KKInstanceBinariesReadyCondition,
infrav1.KKInstanceCRIReadyCondition,
infrav1.KKInstanceProvisionedCondition,
infrav1.KKInstanceDeletingBootstrapCondition,
}})
}
// Close the InstanceScope by updating the instance spec, instance status.
func (i *InstanceScope) Close() error {
return i.PatchObject()
}
// HasFailed returns the failure state of the instance scope.
func (i *InstanceScope) HasFailed() bool {
return i.KKInstance.Status.FailureReason != nil || i.KKInstance.Status.FailureMessage != nil
}
// SetState sets the state of the KKInstance.
func (i *InstanceScope) SetState(state infrav1.InstanceState) {
i.KKInstance.Status.State = state
}