Skip to content

Commit fe4d60d

Browse files
clusterctl-hack
1 parent e4c46dd commit fe4d60d

File tree

3 files changed

+191
-5
lines changed

3 files changed

+191
-5
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,6 @@ vendor
5454
tilt.d
5555
tilt-settings.json
5656
.tiltbuild
57+
58+
# User-supplied clusterctl hacks settings
59+
clusterctl-settings.json
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2015 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
###################
18+
19+
# local-overrides.py takes in input a list of provider and, for each of them, generates the components YAML from the
20+
# local repositories (the GitHub repositories clone), and finally stores it in the clusterctl local override folder
21+
22+
# prerequisites:
23+
24+
# - the script should be executed from sigs.k8s.io/cluster-api/ by calling cmd/clusterctl/hack/local-overrides.py
25+
# - there should be a sigs.k8s.io/cluster-api/clusterctl-settings.json file with the list of provider for which
26+
# the local overrides should be generated and the list of provider repositories to be included (on top of cluster-api).
27+
# {
28+
# "providers": [ "cluster-api", "kubeadm", "aws"],
29+
# "provider_repos": ["../cluster-api-provider-aws"]
30+
# }
31+
# - for each additional provider repository there should be a sigs.k8s.io/<provider_repo>/clusterctl-settings.json file e.g.
32+
# {
33+
# "name": "aws",
34+
# "config": {
35+
# "componentsFile": "infrastructure-components.yaml",
36+
# "nextVersion": "v0.5.0",
37+
# "type": "InfrastructureProvider"
38+
# }
39+
40+
###################
41+
42+
import json
43+
import subprocess
44+
import os
45+
import errno
46+
47+
settings = {}
48+
49+
providers = {
50+
'cluster-api': {
51+
'componentsFile': 'core-components.yaml',
52+
'nextVersion': 'v0.3.0',
53+
'type': 'CoreProvider',
54+
},
55+
'kubeadm': {
56+
'componentsFile': 'bootstrap-components.yaml',
57+
'nextVersion': 'v0.3.0',
58+
'type': 'BootstrapProvider',
59+
'configFolder': '/bootstrap/kubeadm/config/default',
60+
},
61+
}
62+
63+
def load_settings():
64+
global settings
65+
try:
66+
settings = json.load(open('clusterctl-settings.json'))
67+
except Exception as e:
68+
raise Exception('failed to load clusterctl-settings.json: {}'.format(e))
69+
70+
def load_provider():
71+
provider_repos = settings.get('provider_repos', [])
72+
for repo in provider_repos:
73+
file = repo + '/clusterctl-settings.json'
74+
try:
75+
provider_details = json.load(open(file))
76+
provider_name = provider_details['name']
77+
provider_config = provider_details['config']
78+
provider_config['repo'] = repo
79+
providers[provider_name] = provider_config
80+
except Exception as e:
81+
raise Exception('failed to load clusterctl-settings.json from repo {}: {}'.format(repo, e))
82+
83+
def kustomize(args):
84+
try:
85+
out = subprocess.Popen(args,
86+
stdout=subprocess.PIPE,
87+
stderr=subprocess.STDOUT)
88+
89+
stdout, stderr = out.communicate()
90+
if stderr is not None:
91+
raise Exception('stderr contains: \n{}'.format(stderr))
92+
93+
return stdout
94+
except Exception as e:
95+
raise Exception('failed to run {}: {}'.format(args, e))
96+
97+
def get_home():
98+
return os.path.expanduser('~')
99+
100+
def write_local_override(provider, version, components_file, components_yaml):
101+
try:
102+
home = get_home()
103+
overrides_folder = os.path.join(home, 'cluster-api', 'overrides')
104+
provider_overrides_folder = os.path.join(overrides_folder, provider, version)
105+
try:
106+
os.makedirs(provider_overrides_folder)
107+
except OSError as e:
108+
if e.errno != errno.EEXIST:
109+
raise
110+
f = open(os.path.join(provider_overrides_folder, components_file), 'w')
111+
f.write(components_yaml)
112+
f.close()
113+
except Exception as e:
114+
raise Exception('failed to write {} to {}: {}'.format(components_file, components_folder, e))
115+
116+
def create_local_overrides():
117+
providerList = settings.get('providers', [])
118+
assert providerList is not None, 'invalid configuration: please define the list of providers to overrides'
119+
assert len(providerList)>0, 'invalid configuration: please define at least one provider to override'
120+
121+
for provider in providerList:
122+
p = providers.get(provider)
123+
assert p is not None, 'invalid configuration: please the configuration for the {} provider'.format(provider)
124+
125+
repo = '.'
126+
if 'repo' in p:
127+
repo = p.get('repo')
128+
129+
config_folder = '/config/default'
130+
if 'configFolder' in p:
131+
config_folder = p.get('configFolder')
132+
133+
next_version = p.get('nextVersion')
134+
assert next_version is not None, 'invalid configuration for provider {}: please provide nextVersion value'.format(provider)
135+
136+
type = p.get('type')
137+
assert type is not None, 'invalid configuration for provider {}: please provide type value'.format(provider)
138+
assert type in ['CoreProvider','BootstrapProvider','InfrastructureProvider'], 'invalid configuration for provider {}: please provide type value'.format(provider)
139+
140+
components_file = p.get('componentsFile')
141+
assert components_file is not None, 'invalid configuration for provider {}: please provide componentsFile value'.format(provider)
142+
143+
components_yaml = kustomize(['kustomize', 'build', repo + config_folder])
144+
write_local_override(provider, next_version, components_file, components_yaml)
145+
146+
yield provider, type, next_version
147+
148+
149+
def CoreProviderFlag():
150+
return '--core'
151+
152+
def BootstrapProviderFlag():
153+
return '--bootstrap'
154+
155+
def InfrastructureProviderFlag():
156+
return '--infrastructure'
157+
158+
def type_to_flag(type):
159+
switcher = {
160+
'CoreProvider': CoreProviderFlag,
161+
'BootstrapProvider': BootstrapProviderFlag,
162+
'InfrastructureProvider': InfrastructureProviderFlag
163+
}
164+
func = switcher.get(type, lambda: 'Invalid type')
165+
return func()
166+
167+
def print_instructions(overrides):
168+
providerList = settings.get('providers', [])
169+
print ('clusterctl local overrides generated from local repositories for the {} providers.'.format(', '.join(providerList)))
170+
print ('in order to use them, please run:')
171+
print
172+
cmd = 'clusterctl init'
173+
for provider, type, next_version in overrides:
174+
cmd += ' {} {}:{}'.format(type_to_flag(type), provider, next_version)
175+
print (cmd)
176+
print
177+
178+
load_settings()
179+
180+
load_provider()
181+
182+
overrides = create_local_overrides()
183+
184+
print_instructions(overrides)

cmd/clusterctl/pkg/client/config/providers_client.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ func (p *providersClient) defaults() []Provider {
7070
// cluster API core provider
7171
&provider{
7272
name: ClusterAPIName,
73-
url: "https://github.com/kubernetes-sigs/cluster-api/releases/latest/cluster-api-components.yaml",
73+
url: "https://github.com/kubernetes-sigs/cluster-api/releases/latest/core-components.yaml",
7474
providerType: clusterctlv1.CoreProviderType,
7575
},
7676

77-
// Infrastructure providersClient
77+
// Infrastructure providers
7878
&provider{
7979
name: "aws",
8080
url: "https://github.com/kubernetes-sigs/cluster-api-provider-aws/releases/latest/infrastructure-components.yaml",
@@ -91,11 +91,10 @@ func (p *providersClient) defaults() []Provider {
9191
providerType: clusterctlv1.InfrastructureProviderType,
9292
},
9393

94-
// Bootstrap providersClient
95-
// TODO: CABPK in v1alpha3 will be included into CAPI, so this entry can be removed as soon as v1alpha3 is ready for test
94+
// Bootstrap providers
9695
&provider{
9796
name: KubeadmBootstrapProviderName,
98-
url: "https://github.com/kubernetes-sigs/cluster-api-bootstrap-provider-kubeadm/releases/latest/bootstrap-components.yaml",
97+
url: "https://github.com/kubernetes-sigs/cluster-api/releases/latest/bootstrap-components.yaml",
9998
providerType: clusterctlv1.BootstrapProviderType,
10099
},
101100
}

0 commit comments

Comments
 (0)