Skip to content

Commit

Permalink
kube: set default cluster name earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
karmab committed Nov 18, 2024
1 parent bf2da31 commit 30edffa
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 75 deletions.
4 changes: 2 additions & 2 deletions kvirt/baseconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@ def deploy_web_service(self, port=8000, ssl=False, ipv6=False):
cmd = "systemctl restart kweb" if update else "systemctl enable --now kweb"
call(cmd, shell=True)

def get_vip_from_confpool(self, cluster, confpool, overrides):
def get_vip_from_confpool(self, confpool, cluster, overrides):
if confpool not in self.confpools:
error("Confpool {confpool} not found")
sys.exit(1)
Expand Down Expand Up @@ -1484,7 +1484,7 @@ def get_vip_from_confpool(self, cluster, confpool, overrides):
error(f"No available ip in confpool {confpool}")
sys.exit(1)

def get_baremetal_hosts_from_confpool(self, cluster, confpool, overrides):
def get_baremetal_hosts_from_confpool(self, confpool, cluster, overrides):
if confpool not in self.confpools:
error("Confpool {confpool} not found")
sys.exit(1)
Expand Down
28 changes: 13 additions & 15 deletions kvirt/cluster/aks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,10 @@ def create(config, cluster, overrides, dnsconfig=None):
else:
msg = f"Publickey file {pub_key} not found"
return {'result': 'failure', 'reason': msg}
clustervalue = overrides.get('cluster') or cluster or 'myaks'
plan = cluster if cluster is not None else clustervalue
tags = {'plan': clustervalue, 'kube': clustervalue, 'kubetype': 'aks'}
cluster_data = {'name': clustervalue, 'tags': tags}
clusterdir = os.path.expanduser(f"~/.kcli/clusters/{clustervalue}")
plan = cluster
tags = {'plan': cluster, 'kube': cluster, 'kubetype': 'aks'}
cluster_data = {'name': cluster, 'tags': tags}
clusterdir = os.path.expanduser(f"~/.kcli/clusters/{cluster}")
if os.path.exists(clusterdir):
return {'result': 'failure', 'reason': f"Remove existing directory {clusterdir} or use --force"}
else:
Expand All @@ -108,20 +107,20 @@ def create(config, cluster, overrides, dnsconfig=None):
with open(f"{clusterdir}/kcli_parameters.yml", 'w') as p:
installparam = overrides.copy()
installparam['plan'] = plan
installparam['cluster'] = clustervalue
installparam['cluster'] = cluster
installparam['kubetype'] = 'aks'
installparam['client'] = config.client
yaml.safe_dump(installparam, p, default_flow_style=False, encoding='utf-8', allow_unicode=True)
tags = {'plan': clustervalue, 'kube': clustervalue, 'kubetype': 'aks'}
tags = {'plan': cluster, 'kube': cluster, 'kubetype': 'aks'}
admin_username, location, subscription_id, app_id, tenant_id, secret = project_init(config)
agent_pool = {'name': clustervalue, 'vm_size': flavor, 'count': workers, 'min_count': workers, 'max_count': 30,
agent_pool = {'name': cluster, 'vm_size': flavor, 'count': workers, 'min_count': workers, 'max_count': 30,
'enable_auto_scaling': autoscaling, 'mode': 'System', 'osDiskSizeGB': disk_size,
'enableFIPS': fips}
if network is not None:
agent_pool['vnetSubnetID'] = network
service_principal_profile = {'client_id': app_id, 'secret': secret}
linux_profile = {'admin_username': admin_username, 'ssh': {'public_keys': [{'key_data': pub_key}]}}
cluster_data = {'location': location, 'dns_prefix': clustervalue, 'tags': tags,
cluster_data = {'location': location, 'dns_prefix': cluster, 'tags': tags,
'service_principal_profile': service_principal_profile,
'agent_pool_profiles': [agent_pool],
'linux_profile': linux_profile, 'enable_rbac': True}
Expand All @@ -136,15 +135,14 @@ def create(config, cluster, overrides, dnsconfig=None):
cluster_data['network_profile'] = network_profile
credential = ClientSecretCredential(tenant_id=tenant_id, client_id=app_id, client_secret=secret)
containerservice_client = ContainerServiceClient(credential, subscription_id)
response = containerservice_client.managed_clusters.begin_create_or_update(resource_group, clustervalue,
cluster_data)
response = containerservice_client.managed_clusters.begin_create_or_update(resource_group, cluster, cluster_data)
if config.debug:
print(response.result())
pprint(f"Waiting for cluster {clustervalue} to be created")
pprint(f"Waiting for cluster {cluster} to be created")
response.wait()
get_kubeconfig(config, clustervalue)
success(f"Kubernetes cluster {clustervalue} deployed!!!")
info2(f"export KUBECONFIG=$HOME/.kcli/clusters/{clustervalue}/auth/kubeconfig")
get_kubeconfig(config, cluster)
success(f"Kubernetes cluster {cluster} deployed!!!")
info2(f"export KUBECONFIG=$HOME/.kcli/clusters/{cluster}/auth/kubeconfig")
info2("export PATH=$PWD:$PATH")
return {'result': 'success'}

Expand Down
27 changes: 13 additions & 14 deletions kvirt/cluster/eks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,16 @@ def create(config, cluster, overrides, dnsconfig=None):
if not extra_networks:
return {'result': 'failure', 'reason': 'You must define extra_networks'}
sgid = data['security_group']
clustervalue = overrides.get('cluster') or cluster or 'myeks'
plan = cluster if cluster is not None else clustervalue
tags = {'plan': clustervalue, 'kube': clustervalue, 'kubetype': 'eks'}
cluster_data = {'name': clustervalue, 'tags': tags}
plan = cluster
tags = {'plan': cluster, 'kube': cluster, 'kubetype': 'eks'}
cluster_data = {'name': cluster, 'tags': tags}
if version is not None:
version = str(version)
if version not in supported_versions:
msg = f"Version needs to be one of those: {(',').join(supported_versions)}"
return {'result': 'failure', 'reason': msg}
cluster_data['version'] = version
clusterdir = os.path.expanduser(f"~/.kcli/clusters/{clustervalue}")
clusterdir = os.path.expanduser(f"~/.kcli/clusters/{cluster}")
if os.path.exists(clusterdir):
return {'result': 'failure', 'reason': f"Remove existing directory {clusterdir} or use --force"}
else:
Expand All @@ -137,7 +136,7 @@ def create(config, cluster, overrides, dnsconfig=None):
with open(f"{clusterdir}/kcli_parameters.yml", 'w') as p:
installparam = overrides.copy()
installparam['plan'] = plan
installparam['cluster'] = clustervalue
installparam['cluster'] = cluster
installparam['kubetype'] = 'eks'
installparam['client'] = config.client
yaml.safe_dump(installparam, p, default_flow_style=False, encoding='utf-8', allow_unicode=True)
Expand Down Expand Up @@ -174,15 +173,15 @@ def create(config, cluster, overrides, dnsconfig=None):
cluster_data['resourcesVpcConfig'] = {'subnetIds': subnetids, 'securityGroupIds': [sgid]}
eks = boto3.client('eks', aws_access_key_id=access_key_id, aws_secret_access_key=access_key_secret,
region_name=region, aws_session_token=session_token)
pprint(f"Creating cluster {clustervalue}")
pprint(f"Creating cluster {cluster}")
response = eks.create_cluster(**cluster_data)
if config.debug:
print(response)
pprint("Waiting for cluster to be created")
waiter = eks.get_waiter("cluster_active")
waiter.wait(name=clustervalue)
get_kubeconfig(config, clustervalue)
nodegroup_data = {'clusterName': clustervalue, 'nodegroupName': clustervalue, 'scalingConfig':
waiter.wait(name=cluster)
get_kubeconfig(config, cluster)
nodegroup_data = {'clusterName': cluster, 'nodegroupName': cluster, 'scalingConfig':
{'minSize': workers, 'maxSize': 50, 'desiredSize': workers}, 'subnets': subnetids, 'tags': tags,
'nodeRole': worker_role}
keypair = config.options.get('keypair')
Expand All @@ -198,14 +197,14 @@ def create(config, cluster, overrides, dnsconfig=None):
nodegroup_data['amiType'] = ami_type
if capacity_type is not None:
nodegroup_data['capacityType'] = capacity_type
pprint(f"Creating nodegroup {clustervalue}")
pprint(f"Creating nodegroup {cluster}")
response = eks.create_nodegroup(**nodegroup_data)
if config.debug:
print(response)
waiter = eks.get_waiter("cluster_active")
waiter.wait(name=clustervalue)
success(f"Kubernetes cluster {clustervalue} deployed!!!")
info2(f"export KUBECONFIG=$HOME/.kcli/clusters/{clustervalue}/auth/kubeconfig")
waiter.wait(name=cluster)
success(f"Kubernetes cluster {cluster} deployed!!!")
info2(f"export KUBECONFIG=$HOME/.kcli/clusters/{cluster}/auth/kubeconfig")
info2("export PATH=$PWD:$PATH")
return {'result': 'success'}

Expand Down
3 changes: 1 addition & 2 deletions kvirt/cluster/gke/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ def create(config, cluster, overrides, dnsconfig=None):
confidential = data['confidential']
preemptible = data['preemptible']
disk_type = data['disk_type']
clustervalue = overrides.get('cluster') or cluster or 'mygke'
plan = cluster if cluster is not None else clustervalue
plan = cluster
clusterdir = os.path.expanduser(f"~/.kcli/clusters/{cluster}")
if os.path.exists(clusterdir):
msg = f"Remove existing directory {clusterdir} or use --force"
Expand Down
12 changes: 3 additions & 9 deletions kvirt/cluster/hypershift/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,18 +355,12 @@ def create(config, plandir, cluster, overrides):
data.update(overrides)
fix_typos(data)
retries = data.get('retries')
if 'cluster' in overrides:
clustervalue = overrides.get('cluster')
elif cluster is not None:
clustervalue = cluster
else:
clustervalue = 'myhypershift'
data['cluster'] = clustervalue
data['cluster'] = cluster
data['kube'] = data['cluster']
data['kubetype'] = 'hypershift'
ignore_hosts = data['ignore_hosts'] or data['sslip']
pprint(f"Deploying cluster {clustervalue}")
plan = cluster if cluster is not None else clustervalue
pprint(f"Deploying cluster {cluster}")
plan = cluster
upstream = data['upstream']
platform = data.get('platform')
assisted = platform == 'assisted'
Expand Down
4 changes: 2 additions & 2 deletions kvirt/cluster/k3s/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def create(config, plandir, cluster, overrides):
cloud_lb = data['cloud_lb']
cloud_storage = data['cloud_storage']
cloud_native = data['cloud_native']
data['cluster'] = overrides.get('cluster') or cluster or 'myk3s'
plan = cluster if cluster is not None else data['cluster']
data['cluster'] = cluster
plan = cluster
data['kube'] = data['cluster']
data['kubetype'] = 'k3s'
autoscale = data['autoscale']
Expand Down
4 changes: 2 additions & 2 deletions kvirt/cluster/kubeadm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def create(config, plandir, cluster, overrides):
if 'keys' not in overrides and get_ssh_pub_key() is None:
msg = "No usable public key found, which is required for the deployment. Create one using ssh-keygen"
return {'result': 'failure', 'reason': msg}
data['cluster'] = overrides.get('cluster') or cluster or 'mykube'
plan = cluster if cluster is not None else data['cluster']
data['cluster'] = cluster
plan = cluster
data['kube'] = data['cluster']
data['kubetype'] = 'generic'
cloud_lb = data['cloud_lb']
Expand Down
4 changes: 2 additions & 2 deletions kvirt/cluster/microshift/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def create(config, plandir, cluster, overrides, dnsconfig=None):
if 'keys' not in overrides and get_ssh_pub_key() is None:
msg = "No usable public key found, which is required for the deployment. Create one using ssh-keygen"
return {'result': 'failure', 'reason': msg}
data['cluster'] = overrides.get('cluster') or cluster or 'mymicroshift'
plan = cluster if cluster is not None else data['cluster']
data['cluster'] = cluster
plan = cluster
data['kube'] = data['cluster']
data['kubetype'] = 'microshift'
cluster = data.get('cluster')
Expand Down
11 changes: 5 additions & 6 deletions kvirt/cluster/openshift/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,13 +724,12 @@ def create(config, plandir, cluster, overrides, dnsconfig=None):
no_proxy = os.environ.get('NO_PROXY') or os.environ.get('no_proxy')
if 'no_proxy' not in data and no_proxy is not None:
data['no_proxy'] = no_proxy
clustervalue = overrides.get('cluster') or cluster or 'myopenshift'
if data['ctlplanes'] == 1 and data['workers'] == 0\
and 'ctlplane_memory' not in overrides and 'memory' not in overrides:
overrides['ctlplane_memory'] = 32768
warning("Forcing memory of single ctlplane vm to 32G")
retries = data['retries']
data['cluster'] = clustervalue
data['cluster'] = cluster
domain = data['domain']
dns_k = dnsconfig.k if dnsconfig is not None else k
if provider in cloud_providers:
Expand All @@ -748,14 +747,14 @@ def create(config, plandir, cluster, overrides, dnsconfig=None):
baremetal_hosts = data['baremetal_hosts']
notify = data['notify']
postscripts = data['postscripts']
pprint(f"Deploying cluster {clustervalue}")
plan = cluster if cluster is not None else clustervalue
pprint(f"Deploying cluster {cluster}")
plan = cluster
overrides['kubetype'] = 'openshift'
apps = overrides.get('apps', [])
disks = overrides.get('disks', [30])
overrides['kube'] = data['cluster']
installparam = overrides.copy()
installparam['cluster'] = clustervalue
installparam['cluster'] = cluster
baremetal_sno = workers == 0 and len(baremetal_hosts) == 1
baremetal_ctlplane = data['workers'] == 0 and len(baremetal_hosts) > 1
sno_vm = data['sno_vm']
Expand Down Expand Up @@ -1071,7 +1070,7 @@ def create(config, plandir, cluster, overrides, dnsconfig=None):
static_networking_ctlplane, static_networking_worker = False, False
macentries = []
custom_names = {}
vmrules = overrides.get('vmrules', [])
vmrules = [] if config.k.type == 'kvm' else overrides.get('vmrules', [])
for entry in vmrules:
if isinstance(entry, dict):
hostname = list(entry.keys())[0]
Expand Down
4 changes: 2 additions & 2 deletions kvirt/cluster/rke2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def create(config, plandir, cluster, overrides):
if data['sdn'].lower() not in valid_sdns:
msg = f"Invalid sdn, Choose between {','.join(valid_sdns)}"
return {'result': 'failure', 'reason': msg}
data['cluster'] = overrides.get('cluster') or cluster or 'mykube'
plan = cluster if cluster is not None else data['cluster']
data['cluster'] = cluster
plan = cluster
data['kube'] = data['cluster']
data['kubetype'] = 'rke2'
autolabel = data['autolabel']
Expand Down
49 changes: 30 additions & 19 deletions kvirt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,9 +942,18 @@ def create_vm(self, name, profile=None, overrides={}, customprofile={}, k=None,
if confpool is None:
continue
if confpool not in self.confpools:
error(f"{confpool} is not a valid confpool")
sys.exit(1)
warning(f"{confpool} is not a valid confpool")
continue
currentconfpool = self.confpools[confpool]
netmask = net.get('netmask') or net.get('prefix') or currentconfpool.get('netmask')\
or currentconfpool.get('prefix')
gateway = net.get('gateway') or currentconfpool.get('gateway')
if netmask is None:
warning(f"Ignoring confpool {confpool} as netmask isnt specified")
continue
elif gateway is None:
warning(f"Ignoring confpool {confpool} as gateway isnt specified")
continue
ip_reservations = currentconfpool.get('ip_reservations', {})
reserved_ips = list(ip_reservations.values())
if 'ips' in currentconfpool:
Expand All @@ -962,8 +971,8 @@ def create_vm(self, name, profile=None, overrides={}, customprofile={}, k=None,
if not onlyassets:
self.update_confpool(confpool, {'ip_reservations': ip_reservations})
else:
error(f"No available ip in confpool {confpool}")
sys.exit(1)
warning(f"No available ip in confpool {confpool}")
continue
if onlyassets:
image = image or profilename
result = {'result': 'success'}
Expand Down Expand Up @@ -2601,44 +2610,46 @@ def clean_tempkey(self, name, identityfile=None):
os.popen(sshcmd).read()

def threaded_create_kube(self, cluster, kubetype, kube_overrides):
cluster = kube_overrides.get('cluster') or cluster or f"my{kubetype}"
ippool = kube_overrides.get('ippool') or kube_overrides.get('confpool')
baremetalpool = kube_overrides.get('ippool') or kube_overrides.get('confpool')
if ippool is not None:
self.get_vip_from_confpool(cluster, ippool, overrides=kube_overrides)
self.get_vip_from_confpool(ippool, cluster, kube_overrides)
if baremetalpool is not None:
self.get_baremetal_hosts_from_confpool(cluster, baremetalpool, overrides=kube_overrides)
self.get_baremetal_hosts_from_confpool(baremetalpool, cluster, kube_overrides)
self.create_kube(cluster, kubetype, kube_overrides)

def create_kube(self, cluster, kubetype, overrides={}):
cluster = overrides.get('cluster') or cluster or f"my{kubetype}"
if self.type == 'web' and self.k.localkube:
return self.k.create_kube(cluster, kubetype, overrides=overrides)
return self.k.create_kube(cluster, kubetype, overrides)
ippool = overrides.get('ippool') or overrides.get('confpool')
baremetalpool = overrides.get('ippool') or overrides.get('confpool')
if ippool is not None:
self.get_vip_from_confpool(cluster, ippool, overrides=overrides)
self.get_vip_from_confpool(ippool, cluster, overrides)
if baremetalpool is not None:
self.get_baremetal_hosts_from_confpool(cluster, baremetalpool, overrides=overrides)
self.get_baremetal_hosts_from_confpool(baremetalpool, cluster, overrides)
if kubetype == 'openshift':
result = self.create_kube_openshift(cluster, overrides=overrides)
result = self.create_kube_openshift(cluster, overrides)
elif kubetype == 'openshift-sno':
overrides['sno'] = True
result = self.create_kube_openshift(cluster, overrides=overrides)
result = self.create_kube_openshift(cluster, overrides)
elif kubetype == 'hypershift':
result = self.create_kube_hypershift(cluster, overrides=overrides)
result = self.create_kube_hypershift(cluster, overrides)
elif kubetype == 'microshift':
result = self.create_kube_microshift(cluster, overrides=overrides)
result = self.create_kube_microshift(cluster, overrides)
elif kubetype == 'k3s':
result = self.create_kube_k3s(cluster, overrides=overrides)
result = self.create_kube_k3s(cluster, overrides)
elif kubetype == 'rke2':
result = self.create_kube_rke2(cluster, overrides=overrides)
result = self.create_kube_rke2(cluster, overrides)
elif kubetype == 'gke':
result = self.create_kube_gke(cluster, overrides=overrides)
result = self.create_kube_gke(cluster, overrides)
elif kubetype == 'eks':
result = self.create_kube_eks(cluster, overrides=overrides)
result = self.create_kube_eks(cluster, overrides)
elif kubetype == 'aks':
result = self.create_kube_aks(cluster, overrides=overrides)
result = self.create_kube_aks(cluster, overrides)
else:
result = self.create_kube_generic(cluster, overrides=overrides)
result = self.create_kube_generic(cluster, overrides)
return result

def create_kube_aks(self, cluster, overrides={}):
Expand Down
2 changes: 2 additions & 0 deletions kvirt/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,5 @@

VSPHERE = {'force_pool': False, 'dvs': True, 'restricted': False, 'import_network': 'VM Network',
'filtervms': False, 'filteruser': False, 'category': 'kcli', 'timeout': 3600, 'serial': False}

CLUSTERNAMES = {'openshift': 'myopenshift'}

0 comments on commit 30edffa

Please sign in to comment.