def gen_etc_hosts(self, client, net):
"""Generate /etc/hosts file containing all subnet hosts
Makes it possible to register k8s nodes by hostname.
Disgusting hack to make up for OVH's terrible DNS.
"""
from ipaddress import IPv4Network
subnet = client.get('/cloud/project/{}/network/private/{}/subnet'.format(client._project, net))[0]
hosts = IPv4Network(subnet['cidr']).hosts()
hosts_content = compress(
('127.0.0.1\tlocalhost\n' + '::1\t\tlocalhost\n' +
'\n'.join(['{}\t{}'.format(ip, 'host-'+str(ip).replace('.', '-')) for ip in hosts]) + '\n').encode()
)
self.add_files([
{
'filesystem': 'root',
'path': '/etc/hosts',
'mode': 420, # 0644
'contents': {
'source': 'data:,' + quote(hosts_content),
'compression': 'gzip'
}
}
])
评论列表
文章目录