-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
installer.go
368 lines (309 loc) · 14.4 KB
/
installer.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
Copyright 2019 The Kubernetes 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 cluster
import (
"context"
"sort"
"strings"
"time"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/version"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/repository"
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/scheme"
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/util"
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
"sigs.k8s.io/cluster-api/util/contract"
)
// ProviderInstaller defines methods for enforcing consistency rules for provider installation.
type ProviderInstaller interface {
// Add adds a provider to the install queue.
// NB. By deferring the installation, the installer service can perform validation of the target state of the management cluster
// before actually starting the installation of new providers.
Add(repository.Components)
// Install performs the installation of the providers ready in the install queue.
Install(context.Context, InstallOptions) ([]repository.Components, error)
// Validate performs steps to validate a management cluster by looking at the current state and the providers in the queue.
// The following checks are performed in order to ensure a fully operational cluster:
// - There must be only one instance of the same provider
// - All the providers in must support the same API Version of Cluster API (contract)
// - All provider CRDs that are referenced in core Cluster API CRDs must comply with the CRD naming scheme,
// otherwise a warning is logged.
Validate(context.Context) error
// Images returns the list of images required for installing the providers ready in the install queue.
Images() []string
}
// InstallOptions defines the options used to configure installation.
type InstallOptions struct {
WaitProviders bool
WaitProviderTimeout time.Duration
}
// providerInstaller implements ProviderInstaller.
type providerInstaller struct {
configClient config.Client
repositoryClientFactory RepositoryClientFactory
proxy Proxy
providerComponents ComponentsClient
providerInventory InventoryClient
installQueue []repository.Components
}
var _ ProviderInstaller = &providerInstaller{}
func (i *providerInstaller) Add(components repository.Components) {
i.installQueue = append(i.installQueue, components)
// Ensure Providers are installed in the following order: Core, Bootstrap, ControlPlane, Infrastructure.
sort.Slice(i.installQueue, func(a, b int) bool {
return i.installQueue[a].Type().Order() < i.installQueue[b].Type().Order()
})
}
func (i *providerInstaller) Install(ctx context.Context, opts InstallOptions) ([]repository.Components, error) {
ret := make([]repository.Components, 0, len(i.installQueue))
for _, components := range i.installQueue {
if err := installComponentsAndUpdateInventory(ctx, components, i.providerComponents, i.providerInventory); err != nil {
return nil, err
}
ret = append(ret, components)
}
return ret, waitForProvidersReady(ctx, opts, i.installQueue, i.proxy)
}
func installComponentsAndUpdateInventory(ctx context.Context, components repository.Components, providerComponents ComponentsClient, providerInventory InventoryClient) error {
log := logf.Log
log.Info("Installing", "Provider", components.ManifestLabel(), "Version", components.Version(), "TargetNamespace", components.TargetNamespace())
inventoryObject := components.InventoryObject()
log.V(1).Info("Creating objects", "Provider", components.ManifestLabel(), "Version", components.Version(), "TargetNamespace", components.TargetNamespace())
if err := providerComponents.Create(ctx, components.Objs()); err != nil {
return err
}
log.V(1).Info("Creating inventory entry", "Provider", components.ManifestLabel(), "Version", components.Version(), "TargetNamespace", components.TargetNamespace())
return providerInventory.Create(ctx, inventoryObject)
}
// waitForProvidersReady waits till the installed components are ready.
func waitForProvidersReady(ctx context.Context, opts InstallOptions, installQueue []repository.Components, proxy Proxy) error {
// If we dont have to wait for providers to be installed
// return early.
if !opts.WaitProviders {
return nil
}
log := logf.Log
log.Info("Waiting for providers to be available...")
return waitManagerDeploymentsReady(ctx, opts, installQueue, proxy)
}
// waitManagerDeploymentsReady waits till the installed manager deployments are ready.
func waitManagerDeploymentsReady(ctx context.Context, opts InstallOptions, installQueue []repository.Components, proxy Proxy) error {
for _, components := range installQueue {
for _, obj := range components.Objs() {
if util.IsDeploymentWithManager(obj) {
if err := waitDeploymentReady(ctx, obj, opts.WaitProviderTimeout, proxy); err != nil {
return errors.Wrapf(err, "deployment %q is not ready after %s", obj.GetName(), opts.WaitProviderTimeout)
}
}
}
}
return nil
}
func waitDeploymentReady(ctx context.Context, deployment unstructured.Unstructured, timeout time.Duration, proxy Proxy) error {
return wait.PollUntilContextTimeout(ctx, 100*time.Millisecond, timeout, false, func(ctx context.Context) (bool, error) {
c, err := proxy.NewClient()
if err != nil {
return false, err
}
key := client.ObjectKey{
Namespace: deployment.GetNamespace(),
Name: deployment.GetName(),
}
dep := &appsv1.Deployment{}
if err := c.Get(ctx, key, dep); err != nil {
return false, err
}
for _, c := range dep.Status.Conditions {
if c.Type == appsv1.DeploymentAvailable && c.Status == corev1.ConditionTrue {
return true, nil
}
}
return false, nil
})
}
func (i *providerInstaller) Validate(ctx context.Context) error {
// Get the list of providers currently in the cluster.
providerList, err := i.providerInventory.List(ctx)
if err != nil {
return err
}
// Starts simulating what will be the resulting management cluster by adding to the list the providers in the installQueue.
// During this operation following checks are performed:
// - There must be only one instance of the same provider
for _, components := range i.installQueue {
if providerList, err = simulateInstall(providerList, components); err != nil {
return errors.Wrapf(err, "installing provider %q can lead to a non functioning management cluster", components.ManifestLabel())
}
}
// Gets the API Version of Cluster API (contract) all the providers in the management cluster must support,
// which is the same of the core provider.
providerInstanceContracts := map[string]string{}
coreProviders := providerList.FilterCore()
if len(coreProviders) != 1 {
return errors.Errorf("invalid management cluster: there should a core provider, found %d", len(coreProviders))
}
coreProvider := coreProviders[0]
managementClusterContract, err := i.getProviderContract(ctx, providerInstanceContracts, coreProvider)
if err != nil {
return err
}
// Checks if all the providers supports the same API Version of Cluster API (contract).
for _, components := range i.installQueue {
provider := components.InventoryObject()
// Gets the API Version of Cluster API (contract) the provider support and compare it with the management cluster contract.
providerContract, err := i.getProviderContract(ctx, providerInstanceContracts, provider)
if err != nil {
return err
}
if providerContract != managementClusterContract {
return errors.Errorf("installing provider %q can lead to a non functioning management cluster: the target version for the provider supports the %s API Version of Cluster API (contract), while the management cluster is using %s", components.ManifestLabel(), providerContract, managementClusterContract)
}
}
// Validate if provider CRDs comply with the naming scheme.
for _, components := range i.installQueue {
componentObjects := components.Objs()
for _, obj := range componentObjects {
// Continue if object is not a CRD.
if obj.GetKind() != customResourceDefinitionKind {
continue
}
gk, err := getCRDGroupKind(obj)
if err != nil {
return errors.Wrap(err, "failed to read group and kind from CustomResourceDefinition")
}
if err := validateCRDName(obj, gk); err != nil {
return err
}
}
}
return nil
}
func getCRDGroupKind(obj unstructured.Unstructured) (*schema.GroupKind, error) {
var group string
var kind string
version := obj.GroupVersionKind().Version
switch version {
case apiextensionsv1beta1.SchemeGroupVersion.Version:
crd := &apiextensionsv1beta1.CustomResourceDefinition{}
if err := scheme.Scheme.Convert(&obj, crd, nil); err != nil {
return nil, errors.Wrapf(err, "failed to convert %s CustomResourceDefinition %q", version, obj.GetName())
}
group = crd.Spec.Group
kind = crd.Spec.Names.Kind
case apiextensionsv1.SchemeGroupVersion.Version:
crd := &apiextensionsv1.CustomResourceDefinition{}
if err := scheme.Scheme.Convert(&obj, crd, nil); err != nil {
return nil, errors.Wrapf(err, "failed to convert %s CustomResourceDefinition %q", version, obj.GetName())
}
group = crd.Spec.Group
kind = crd.Spec.Names.Kind
default:
return nil, errors.Errorf("failed to read %s CustomResourceDefinition %q", version, obj.GetName())
}
return &schema.GroupKind{Group: group, Kind: kind}, nil
}
func validateCRDName(obj unstructured.Unstructured, gk *schema.GroupKind) error {
// Return if CRD has skip CRD name preflight check annotation.
if _, ok := obj.GetAnnotations()[clusterctlv1.SkipCRDNamePreflightCheckAnnotation]; ok {
return nil
}
correctCRDName := contract.CalculateCRDName(gk.Group, gk.Kind)
// Return if name is correct.
if obj.GetName() == correctCRDName {
return nil
}
return errors.Errorf("ERROR: CRD name %q is invalid for a CRD referenced in a core Cluster API CRD,"+
"it should be %q. Support for CRDs that are referenced in core Cluster API resources with invalid names has been "+
"dropped. Note: Please check if this CRD is actually referenced in core Cluster API "+
"CRDs. If not, this warning can be hidden by setting the %q' annotation.", obj.GetName(), correctCRDName, clusterctlv1.SkipCRDNamePreflightCheckAnnotation)
}
// getProviderContract returns the API Version of Cluster API (contract) for a provider instance.
func (i *providerInstaller) getProviderContract(ctx context.Context, providerInstanceContracts map[string]string, provider clusterctlv1.Provider) (string, error) {
// If the contract for the provider instance is already known, return it.
if contract, ok := providerInstanceContracts[provider.InstanceName()]; ok {
return contract, nil
}
// Otherwise get the contract for the providers instance.
// Gets the providers metadata.
configRepository, err := i.configClient.Providers().Get(provider.ProviderName, provider.GetProviderType())
if err != nil {
return "", err
}
providerRepository, err := i.repositoryClientFactory(ctx, configRepository, i.configClient)
if err != nil {
return "", err
}
latestMetadata, err := providerRepository.Metadata(provider.Version).Get(ctx)
if err != nil {
return "", err
}
// Gets the contract for the current release.
currentVersion, err := version.ParseSemantic(provider.Version)
if err != nil {
return "", errors.Wrapf(err, "failed to parse current version for the %s provider", provider.InstanceName())
}
releaseSeries := latestMetadata.GetReleaseSeriesForVersion(currentVersion)
if releaseSeries == nil {
return "", errors.Errorf("invalid provider metadata: version %s for the provider %s does not match any release series", provider.Version, provider.InstanceName())
}
if releaseSeries.Contract != clusterv1.GroupVersion.Version {
return "", errors.Errorf("current version of clusterctl is only compatible with %s providers, detected %s for provider %s", clusterv1.GroupVersion.Version, releaseSeries.Contract, provider.ManifestLabel())
}
providerInstanceContracts[provider.InstanceName()] = releaseSeries.Contract
return releaseSeries.Contract, nil
}
// simulateInstall adds a provider to the list of providers in a cluster (without installing it).
func simulateInstall(providerList *clusterctlv1.ProviderList, components repository.Components) (*clusterctlv1.ProviderList, error) {
provider := components.InventoryObject()
existingInstances := providerList.FilterByProviderNameAndType(provider.ProviderName, provider.GetProviderType())
if len(existingInstances) > 0 {
namespaces := func() string {
var namespaces []string
for _, provider := range existingInstances {
namespaces = append(namespaces, provider.Namespace)
}
return strings.Join(namespaces, ", ")
}()
return providerList, errors.Errorf("there is already an instance of the %q provider installed in the %q namespace", provider.ManifestLabel(), namespaces)
}
providerList.Items = append(providerList.Items, provider)
return providerList, nil
}
func (i *providerInstaller) Images() []string {
ret := sets.Set[string]{}
for _, components := range i.installQueue {
ret = ret.Insert(components.Images()...)
}
return sets.List(ret)
}
func newProviderInstaller(configClient config.Client, repositoryClientFactory RepositoryClientFactory, proxy Proxy, providerMetadata InventoryClient, providerComponents ComponentsClient) *providerInstaller {
return &providerInstaller{
configClient: configClient,
repositoryClientFactory: repositoryClientFactory,
proxy: proxy,
providerComponents: providerComponents,
providerInventory: providerMetadata,
}
}