-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
init.go
152 lines (126 loc) · 6.83 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
/*
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 cmd
import (
"context"
"time"
"github.com/spf13/cobra"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client"
)
type initOptions struct {
kubeconfig string
kubeconfigContext string
coreProvider string
bootstrapProviders []string
controlPlaneProviders []string
infrastructureProviders []string
ipamProviders []string
runtimeExtensionProviders []string
addonProviders []string
targetNamespace string
validate bool
waitProviders bool
waitProviderTimeout int
}
var initOpts = &initOptions{}
var initCmd = &cobra.Command{
Use: "init",
GroupID: groupManagement,
Short: "Initialize a management cluster",
Long: LongDesc(`
Initialize a management cluster.
Installs Cluster API core components, the kubeadm bootstrap provider,
and the selected bootstrap and infrastructure providers.
The management cluster must be an existing Kubernetes cluster, make sure
to have enough privileges to install the desired components.
Use 'clusterctl config repositories' to get a list of available providers and their configuration; if
necessary, edit $XDG_CONFIG_HOME/cluster-api/clusterctl.yaml file to add new provider or to customize existing ones.
Some providers require environment variables to be set before running clusterctl init.
Refer to the provider documentation, or use 'clusterctl generate provider --infrastructure [name] --describe'
to get a list of required variables.
See https://cluster-api.sigs.k8s.io for more details.`),
Example: Examples(`
# Initialize a management cluster, by installing the given infrastructure provider.
#
# Note: when this command is executed on an empty management cluster,
# it automatically triggers the installation of the Cluster API core provider.
clusterctl init --infrastructure=aws
# Initialize a management cluster with a specific version of the given infrastructure provider.
clusterctl init --infrastructure=aws:v0.4.1
# Initialize a management cluster with a custom kubeconfig path and the given infrastructure provider.
clusterctl init --kubeconfig=foo.yaml --infrastructure=aws
# Initialize a management cluster with multiple infrastructure providers.
clusterctl init --infrastructure=aws,vsphere
# Initialize a management cluster with a custom target namespace for the provider resources.
clusterctl init --infrastructure aws --target-namespace foo`),
Args: cobra.NoArgs,
RunE: func(*cobra.Command, []string) error {
return runInit()
},
}
func init() {
initCmd.PersistentFlags().StringVar(&initOpts.kubeconfig, "kubeconfig", "",
"Path to the kubeconfig for the management cluster. If unspecified, default discovery rules apply.")
initCmd.PersistentFlags().StringVar(&initOpts.kubeconfigContext, "kubeconfig-context", "",
"Context to be used within the kubeconfig file. If empty, current context will be used.")
initCmd.PersistentFlags().StringVar(&initOpts.coreProvider, "core", "",
"Core provider version (e.g. cluster-api:v1.1.5) to add to the management cluster. If unspecified, Cluster API's latest release is used.")
initCmd.PersistentFlags().StringSliceVarP(&initOpts.infrastructureProviders, "infrastructure", "i", nil,
"Infrastructure providers and versions (e.g. aws:v0.5.0) to add to the management cluster.")
initCmd.PersistentFlags().StringSliceVarP(&initOpts.bootstrapProviders, "bootstrap", "b", nil,
"Bootstrap providers and versions (e.g. kubeadm:v1.1.5) to add to the management cluster. If unspecified, Kubeadm bootstrap provider's latest release is used.")
initCmd.PersistentFlags().StringSliceVarP(&initOpts.controlPlaneProviders, "control-plane", "c", nil,
"Control plane providers and versions (e.g. kubeadm:v1.1.5) to add to the management cluster. If unspecified, the Kubeadm control plane provider's latest release is used.")
initCmd.PersistentFlags().StringSliceVar(&initOpts.ipamProviders, "ipam", nil,
"IPAM providers and versions (e.g. in-cluster:v0.1.0) to add to the management cluster.")
initCmd.PersistentFlags().StringSliceVar(&initOpts.runtimeExtensionProviders, "runtime-extension", nil,
"Runtime extension providers and versions to add to the management cluster; please note that clusterctl doesn't include any default runtime extensions and thus it is required to use custom configuration files to register runtime extensions.")
initCmd.PersistentFlags().StringSliceVar(&initOpts.addonProviders, "addon", nil,
"Add-on providers and versions (e.g. helm:v0.1.0) to add to the management cluster.")
initCmd.Flags().StringVarP(&initOpts.targetNamespace, "target-namespace", "n", "",
"The target namespace where the providers should be deployed. If unspecified, the provider components' default namespace is used.")
initCmd.Flags().BoolVar(&initOpts.waitProviders, "wait-providers", false,
"Wait for providers to be installed.")
initCmd.Flags().IntVar(&initOpts.waitProviderTimeout, "wait-provider-timeout", 5*60,
"Wait timeout per provider installation in seconds. This value is ignored if --wait-providers is false")
initCmd.Flags().BoolVar(&initOpts.validate, "validate", true,
"If true, clusterctl will validate that the deployments will succeed on the management cluster.")
initCmd.AddCommand(initListImagesCmd)
RootCmd.AddCommand(initCmd)
}
func runInit() error {
ctx := context.Background()
c, err := client.New(ctx, cfgFile)
if err != nil {
return err
}
options := client.InitOptions{
Kubeconfig: client.Kubeconfig{Path: initOpts.kubeconfig, Context: initOpts.kubeconfigContext},
CoreProvider: initOpts.coreProvider,
BootstrapProviders: initOpts.bootstrapProviders,
ControlPlaneProviders: initOpts.controlPlaneProviders,
InfrastructureProviders: initOpts.infrastructureProviders,
IPAMProviders: initOpts.ipamProviders,
RuntimeExtensionProviders: initOpts.runtimeExtensionProviders,
AddonProviders: initOpts.addonProviders,
TargetNamespace: initOpts.targetNamespace,
LogUsageInstructions: true,
WaitProviders: initOpts.waitProviders,
WaitProviderTimeout: time.Duration(initOpts.waitProviderTimeout) * time.Second,
IgnoreValidationErrors: !initOpts.validate,
}
if _, err := c.Init(ctx, options); err != nil {
return err
}
return nil
}