Add configuration file for user to edit

config.yaml.sample should be copied to config.yaml to be of use.

Kubernetes nodes IP addresses, ssh credentials, and dns info is stored
there.

Fixed a bug in adding node. (node data wasn't saved on added nodes)

Updated README
This commit is contained in:
Maciej Kwiek 2016-04-14 17:01:54 +02:00
parent 6a93ca6881
commit 5b607e0886
4 changed files with 83 additions and 40 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
config.yaml

View File

@ -16,10 +16,11 @@ Vagrant setup:
5. vagrant up
6. Copy, link or clone this repo to solar-dev VM into k8s folder
8. solar repo import -l k8s
9. ./setup-k8s.py
10. solar changes stage
11. solar changes process
12. solar orch run-once
13. watch solar orch report
14. vagrant ssh solar-dev1
15. kubectl get pods (it works!)
9. cp config.yaml.sample config.yaml
10. ./setup-k8s.py deploy
11. solar changes stage
12. solar changes process
13. solar orch run-once
14. watch solar orch report
15. vagrant ssh solar-dev1
16. kubectl get pods (it works!)

21
config.yaml.sample Normal file
View File

@ -0,0 +1,21 @@
---
kube_master:
username: vagrant
password: vagrant
ssh_key: null
ip: 10.0.0.3
dns:
ip: 10.254.0.10
domain: cluster.local
kube_slaves:
username: vagrant
password: vagrant
ssh_key: null
ips:
- 10.0.0.4
- 10.0.0.5
- 10.0.0.6
- 10.0.0.7
- 10.0.0.8
- 10.0.0.9
- 10.0.0.10

View File

@ -4,6 +4,7 @@ import argparse
import re
from netaddr import IPAddress
import yaml
from solar.core.resource import composer as cr
from solar.core.resource import resource as rs
@ -11,20 +12,36 @@ from solar.events.api import add_event
from solar.events.controls import Dep
def create_config():
def create_config(dns_config):
return cr.create('kube-config', 'k8s/global_config',
{'cluster_dns': '10.254.0.10',
'cluster_domain': 'cluster.local'}
{'cluster_dns': dns_config['ip'],
'cluster_domain': dns_config['domain']}
)[0]
def setup_master(config):
def get_slave_nodes():
kube_nodes = rs.load_all(startswith='kube-node-')
p = re.compile('^kube-node-\d+$')
return [node for node in kube_nodes if p.match(node.name)]
def get_free_slave_ip(available_ips):
used_ips = [node.ip() for node in get_slave_nodes()]
free_ips = set(available_ips) - set(used_ips)
if len(free_ips) == 0:
raise ValueError('No free IP addresses available. '
'Did you edit config.yaml?')
return list(free_ips)[0]
def setup_master(config, user_config):
master = cr.create('kube-node-master', 'k8s/node',
{'name': 'kube-node-master',
'ip': '10.0.0.3',
'ssh_user': 'vagrant',
'ssh_password': 'vagrant',
'ssh_key': None})['kube-node-master']
'ip': user_config['ip'],
'ssh_user': user_config['username'],
'ssh_password': user_config['password'],
'ssh_key': user_config['ssh_key']})['kube-node-master']
master.connect(config, {})
docker = cr.create('kube-docker-master',
@ -52,16 +69,15 @@ def setup_master(config):
add_event(Dep(kubelet.name, 'run', 'success', calico.name, 'run'))
def setup_nodes(config, num=1):
def setup_nodes(config, user_config, num=1):
kube_nodes = []
kubernetes_master = rs.load('kubelet-master')
calico_master = rs.load('calico-master')
network = IPAddress('10.0.0.0')
internal_network = IPAddress(config.args['network'])
kube_nodes = [
setup_slave_node(config, kubernetes_master, calico_master,
network, internal_network, i)
setup_slave_node(config, user_config, kubernetes_master, calico_master,
internal_network, i)
for i in xrange(num)]
kube_master = rs.load('kube-node-master')
@ -75,17 +91,17 @@ def setup_nodes(config, num=1):
})
def setup_slave_node(config, kubernetes_master, calico_master,
network, internal_network, i):
def setup_slave_node(config, user_config, kubernetes_master, calico_master,
internal_network, i):
j = i + 1
kube_node = cr.create(
'kube-node-%d' % j,
'k8s/node',
{'name': 'kube-node-%d' % j,
'ip': str(network + j + 3),
'ssh_user': 'vagrant',
'ssh_password': 'vagrant',
'ssh_key': None}
'ip': get_free_slave_ip(user_config['ips']),
'ssh_user': user_config['username'],
'ssh_password': user_config['password'],
'ssh_key': user_config['ssh_key']}
)['kube-node-%d' % j]
iface_node = cr.create(
@ -133,7 +149,7 @@ def setup_slave_node(config, kubernetes_master, calico_master,
return kube_node
def add_dashboard(args):
def add_dashboard(args, *_):
kube_master = rs.load('kube-node-master')
master = rs.load('kubelet-master')
dashboard = cr.create('kubernetes-dashboard', 'k8s/dashboard', {})[0]
@ -141,7 +157,7 @@ def add_dashboard(args):
kube_master.connect(dashboard, {'ip': 'api_host'})
def add_dns(args):
def add_dns(args, *_):
config = rs.load('kube-config')
kube_master = rs.load('kube-node-master')
master = rs.load('kubelet-master')
@ -152,27 +168,25 @@ def add_dns(args):
'cluster_dns': 'cluster_dns'})
def add_node(args):
def add_node(args, user_config):
config = rs.load('kube-config')
kubernetes_master = rs.load('kubelet-master')
calico_master = rs.load('calico-master')
network = IPAddress('10.0.0.0')
internal_network = IPAddress(config.args['network'])
def get_node_id(n):
return n.name.split('-')[-1]
kube_nodes = rs.load_all(startswith='kube-node-')
p = re.compile('^kube-node-\d+$')
kube_nodes = [node for node in kube_nodes if p.match(node.name)]
kube_nodes = get_slave_nodes()
newest_id = int(get_node_id(max(kube_nodes, key=get_node_id)))
new_nodes = [setup_slave_node(config, kubernetes_master, calico_master,
network, internal_network, i)
new_nodes = [setup_slave_node(config, user_config['kube_slaves'],
kubernetes_master, calico_master,
internal_network, i)
for i in xrange(newest_id, newest_id + args.nodes)]
kube_master = rs.load('kube-node-master')
all_nodes = kube_nodes[:] + [kube_master]
all_nodes = new_nodes[:] + [kube_master]
hosts_files = rs.load_all(startswith='hosts_file_node_kube-')
for node in all_nodes:
for host_file in hosts_files:
@ -182,10 +196,10 @@ def add_node(args):
})
def deploy_k8s(args):
config = create_config()
setup_master(config)
setup_nodes(config, args.nodes)
def deploy_k8s(args, user_config):
config = create_config(user_config['dns'])
setup_master(config, user_config['kube_master'])
setup_nodes(config, user_config['kube_slaves'], args.nodes)
if args.dashboard:
add_dashboard(args)
@ -219,6 +233,12 @@ def get_args():
return parser.parse_args()
def get_user_config():
with open('config.yaml') as conf:
return yaml.load(conf)
if __name__ == "__main__":
args = get_args()
commands[args.command](args)
user_config = get_user_config()
commands[args.command](args, user_config)