|
| 1 | +""" |
| 2 | +Copyright 2024 Google LLC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +import math |
| 18 | + |
| 19 | +from ..utils.console import xpk_exit, xpk_print |
| 20 | +from ..utils.file import write_tmp_file |
| 21 | +from ..core.kueue import ( |
| 22 | + MEMORY_SIZE_PER_VM, |
| 23 | + MIN_MEMORY_LIMIT_SIZE, |
| 24 | +) |
| 25 | +from .commands import ( |
| 26 | + run_command_for_value, |
| 27 | + run_command_with_updates_retry, |
| 28 | +) |
| 29 | + |
| 30 | +jobset_controller_manager_yml = """ |
| 31 | +apiVersion: apps/v1 |
| 32 | +kind: Deployment |
| 33 | +metadata: |
| 34 | + labels: |
| 35 | + app.kubernetes.io/component: manager |
| 36 | + app.kubernetes.io/created-by: jobset |
| 37 | + app.kubernetes.io/instance: controller-manager |
| 38 | + app.kubernetes.io/managed-by: kustomize |
| 39 | + app.kubernetes.io/name: deployment |
| 40 | + app.kubernetes.io/part-of: jobset |
| 41 | + control-plane: controller-manager |
| 42 | + name: jobset-controller-manager |
| 43 | + namespace: jobset-system |
| 44 | +spec: |
| 45 | + replicas: 1 |
| 46 | + selector: |
| 47 | + matchLabels: |
| 48 | + control-plane: controller-manager |
| 49 | + template: |
| 50 | + metadata: |
| 51 | + annotations: |
| 52 | + kubectl.kubernetes.io/default-container: manager |
| 53 | + labels: |
| 54 | + control-plane: controller-manager |
| 55 | + spec: |
| 56 | + containers: |
| 57 | + - args: |
| 58 | + - --config=/controller_manager_config.yaml |
| 59 | + - --zap-log-level=2 |
| 60 | + command: |
| 61 | + - /manager |
| 62 | + image: registry.k8s.io/jobset/jobset:v0.8.0 |
| 63 | + livenessProbe: |
| 64 | + httpGet: |
| 65 | + path: /healthz |
| 66 | + port: 8081 |
| 67 | + initialDelaySeconds: 15 |
| 68 | + periodSeconds: 20 |
| 69 | + name: manager |
| 70 | + ports: |
| 71 | + - containerPort: 9443 |
| 72 | + name: webhook-server |
| 73 | + protocol: TCP |
| 74 | + readinessProbe: |
| 75 | + httpGet: |
| 76 | + path: /readyz |
| 77 | + port: 8081 |
| 78 | + initialDelaySeconds: 5 |
| 79 | + periodSeconds: 10 |
| 80 | + resources: |
| 81 | + limits: |
| 82 | + memory: {memory_limit_size} |
| 83 | + requests: |
| 84 | + cpu: 500m |
| 85 | + memory: 128Mi |
| 86 | + securityContext: |
| 87 | + allowPrivilegeEscalation: false |
| 88 | + capabilities: |
| 89 | + drop: |
| 90 | + - ALL |
| 91 | + volumeMounts: |
| 92 | + - mountPath: /controller_manager_config.yaml |
| 93 | + name: manager-config |
| 94 | + subPath: controller_manager_config.yaml |
| 95 | + - mountPath: /tmp/k8s-webhook-server/serving-certs |
| 96 | + name: cert |
| 97 | + readOnly: true |
| 98 | + securityContext: |
| 99 | + runAsNonRoot: true |
| 100 | + serviceAccountName: jobset-controller-manager |
| 101 | + terminationGracePeriodSeconds: 10 |
| 102 | + volumes: |
| 103 | + - configMap: |
| 104 | + name: jobset-manager-config |
| 105 | + name: manager-config |
| 106 | + - name: cert |
| 107 | + secret: |
| 108 | + defaultMode: 420 |
| 109 | + secretName: jobset-webhook-server-cert |
| 110 | +""" |
| 111 | + |
| 112 | + |
| 113 | +def update_jobset_resources_if_necessary(args): |
| 114 | + """Update the jobset manifest to increase the resources for the jobset controller manager. |
| 115 | +
|
| 116 | + Args: |
| 117 | + args: user provided arguments for running the command. |
| 118 | +
|
| 119 | + Returns: |
| 120 | + 0 if successful and 1 otherwise. |
| 121 | + """ |
| 122 | + # Get total number of nodes |
| 123 | + cmd_total_node_num = 'kubectl get node --no-headers | wc -l' |
| 124 | + return_code, out = run_command_for_value( |
| 125 | + cmd_total_node_num, 'Count total nodes', args |
| 126 | + ) |
| 127 | + if return_code != 0: |
| 128 | + xpk_exit(1) |
| 129 | + # 1.2MiB per VM or 4GiB (whichever is greater). |
| 130 | + new_memory_limit = ( |
| 131 | + f'{max(math.ceil(int(out) * MEMORY_SIZE_PER_VM), MIN_MEMORY_LIMIT_SIZE)}Mi' |
| 132 | + ) |
| 133 | + yml_string = jobset_controller_manager_yml.format( |
| 134 | + memory_limit_size=new_memory_limit, |
| 135 | + ) |
| 136 | + tmp = write_tmp_file(yml_string) |
| 137 | + command = f'kubectl apply -f {str(tmp.file.name)}' |
| 138 | + |
| 139 | + task = 'Updating jobset Controller Manager resources' |
| 140 | + return_code = run_command_with_updates_retry(command, task, args) |
| 141 | + if return_code != 0: |
| 142 | + xpk_print(f'{task} returned ERROR {return_code}') |
| 143 | + return return_code |
0 commit comments