/
init.go
261 lines (215 loc) · 10.1 KB
/
init.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
/*
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 client
import (
"sort"
"github.com/pkg/errors"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/repository"
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
)
const NoopProvider = "-"
// InitOptions carries the options supported by Init.
type InitOptions struct {
// Kubeconfig defines the kubeconfig to use for accessing the management cluster. If empty,
// default rules for kubeconfig discovery will be used.
Kubeconfig Kubeconfig
// CoreProvider version (e.g. cluster-api:v0.3.0) to add to the management cluster. If unspecified, the
// cluster-api core provider's latest release is used.
CoreProvider string
// BootstrapProviders and versions (e.g. kubeadm:v0.3.0) to add to the management cluster.
// If unspecified, the kubeadm bootstrap provider's latest release is used.
BootstrapProviders []string
// InfrastructureProviders and versions (e.g. aws:v0.5.0) to add to the management cluster.
InfrastructureProviders []string
// ControlPlaneProviders and versions (e.g. kubeadm:v0.3.0) to add to the management cluster.
// If unspecified, the kubeadm control plane provider latest release is used.
ControlPlaneProviders []string
// TargetNamespace defines the namespace where the providers should be deployed. If unspecified, each provider
// will be installed in a provider's default namespace.
TargetNamespace string
// WatchingNamespace defines the namespace the providers should watch to reconcile Cluster API objects.
// If unspecified, the providers watches for Cluster API objects across all namespaces.
WatchingNamespace string
// LogUsageInstructions instructs the init command to print the usage instructions in case of first run.
LogUsageInstructions bool
// skipVariables skips variable parsing in the provider components yaml.
// It is set to true for listing images of provider components.
skipVariables bool
}
// Init initializes a management cluster by adding the requested list of providers.
func (c *clusterctlClient) Init(options InitOptions) ([]Components, error) {
log := logf.Log
// gets access to the management cluster
cluster, err := c.clusterClientFactory(ClusterClientFactoryInput{kubeconfig: options.Kubeconfig})
if err != nil {
return nil, err
}
// ensure the custom resource definitions required by clusterctl are in place
if err := cluster.ProviderInventory().EnsureCustomResourceDefinitions(); err != nil {
return nil, err
}
// checks if the cluster already contains a Core provider.
// if not we consider this the first time init is executed, and thus we enforce the installation of a core provider,
// a bootstrap provider and a control-plane provider (if not already explicitly requested by the user)
log.Info("Fetching providers")
firstRun := c.addDefaultProviders(cluster, &options)
// create an installer service, add the requested providers to the install queue and then perform validation
// of the target state of the management cluster before starting the installation.
installer, err := c.setupInstaller(cluster, options)
if err != nil {
return nil, err
}
// Before installing the providers, validates the management cluster resulting by the planned installation. The following checks are performed:
// - There should be only one instance of the same provider per namespace.
// - Instances of the same provider should not be fighting for objects (no watching overlap).
// - Providers combines in valid management groups
// - All the providers should belong to one/only one management groups
// - All the providers in a management group must support the same API Version of Cluster API (contract)
if err := installer.Validate(); err != nil {
return nil, err
}
// Before installing the providers, ensure the cert-manager Webhook is in place.
if err := cluster.CertManager().EnsureWebhook(); err != nil {
return nil, err
}
components, err := installer.Install()
if err != nil {
return nil, err
}
// If this is the firstRun, then log the usage instructions.
if firstRun && options.LogUsageInstructions {
log.Info("")
log.Info("Your management cluster has been initialized successfully!")
log.Info("")
log.Info("You can now create your first workload cluster by running the following:")
log.Info("")
log.Info(" clusterctl config cluster [name] --kubernetes-version [version] | kubectl apply -f -")
log.Info("")
}
// Components is an alias for repository.Components; this makes the conversion from the two types
aliasComponents := make([]Components, len(components))
for i, components := range components {
aliasComponents[i] = components
}
return aliasComponents, nil
}
// Init returns the list of images required for init.
func (c *clusterctlClient) InitImages(options InitOptions) ([]string, error) {
// gets access to the management cluster
cluster, err := c.clusterClientFactory(ClusterClientFactoryInput{kubeconfig: options.Kubeconfig})
if err != nil {
return nil, err
}
// checks if the cluster already contains a Core provider.
// if not we consider this the first time init is executed, and thus we enforce the installation of a core provider,
// a bootstrap provider and a control-plane provider (if not already explicitly requested by the user)
c.addDefaultProviders(cluster, &options)
// skip variable parsing when listing images
options.skipVariables = true
// create an installer service, add the requested providers to the install queue and then perform validation
// of the target state of the management cluster before starting the installation.
installer, err := c.setupInstaller(cluster, options)
if err != nil {
return nil, err
}
// Gets the list of container images required for the cert-manager (if not already installed).
images, err := cluster.CertManager().Images()
if err != nil {
return nil, err
}
// Appends the list of container images required for the selected providers.
images = append(images, installer.Images()...)
sort.Strings(images)
return images, nil
}
func (c *clusterctlClient) setupInstaller(cluster cluster.Client, options InitOptions) (cluster.ProviderInstaller, error) {
installer := cluster.ProviderInstaller()
addOptions := addToInstallerOptions{
installer: installer,
targetNamespace: options.TargetNamespace,
watchingNamespace: options.WatchingNamespace,
skipVariables: options.skipVariables,
}
if options.CoreProvider != "" {
if err := c.addToInstaller(addOptions, clusterctlv1.CoreProviderType, options.CoreProvider); err != nil {
return nil, err
}
}
if err := c.addToInstaller(addOptions, clusterctlv1.BootstrapProviderType, options.BootstrapProviders...); err != nil {
return nil, err
}
if err := c.addToInstaller(addOptions, clusterctlv1.ControlPlaneProviderType, options.ControlPlaneProviders...); err != nil {
return nil, err
}
if err := c.addToInstaller(addOptions, clusterctlv1.InfrastructureProviderType, options.InfrastructureProviders...); err != nil {
return nil, err
}
return installer, nil
}
func (c *clusterctlClient) addDefaultProviders(cluster cluster.Client, options *InitOptions) bool {
firstRun := false
// Check if there is already a core provider installed in the cluster
// Nb. we are ignoring the error so this operation can support listing images even if there is no an existing management cluster;
// in case there is no an existing management cluster, we assume there are no core providers installed in the cluster.
currentCoreProvider, _ := cluster.ProviderInventory().GetDefaultProviderName(clusterctlv1.CoreProviderType)
// If there are no core providers installed in the cluster, consider this a first run and add default providers to the list
// of providers to be installed.
if currentCoreProvider == "" {
firstRun = true
if options.CoreProvider == "" {
options.CoreProvider = config.ClusterAPIProviderName
}
if len(options.BootstrapProviders) == 0 {
options.BootstrapProviders = append(options.BootstrapProviders, config.KubeadmBootstrapProviderName)
}
if len(options.ControlPlaneProviders) == 0 {
options.ControlPlaneProviders = append(options.ControlPlaneProviders, config.KubeadmControlPlaneProviderName)
}
}
return firstRun
}
type addToInstallerOptions struct {
installer cluster.ProviderInstaller
targetNamespace string
watchingNamespace string
skipVariables bool
}
// addToInstaller adds the components to the install queue and checks that the actual provider type match the target group
func (c *clusterctlClient) addToInstaller(options addToInstallerOptions, providerType clusterctlv1.ProviderType, providers ...string) error {
for _, provider := range providers {
// It is possible to opt-out from automatic installation of bootstrap/control-plane providers using '-' as a provider name (NoopProvider).
if provider == NoopProvider {
if providerType == clusterctlv1.CoreProviderType {
return errors.New("the '-' value can not be used for the core provider")
}
continue
}
componentsOptions := repository.ComponentsOptions{
TargetNamespace: options.targetNamespace,
WatchingNamespace: options.watchingNamespace,
SkipVariables: options.skipVariables,
}
components, err := c.getComponentsByName(provider, providerType, componentsOptions)
if err != nil {
return errors.Wrapf(err, "failed to get provider components for the %q provider", provider)
}
if components.Type() != providerType {
return errors.Errorf("can't use %q provider as an %q, it is a %q", provider, providerType, components.Type())
}
options.installer.Add(components)
}
return nil
}