k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/config/jobs/kubernetes/kops/helpers.py (about) 1 # Copyright 2020 The Kubernetes Authors. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 15 import zlib 16 17 import boto3 # pylint: disable=import-error 18 19 # We support rapid focus on a few tests of high concern 20 # This should be used for temporary tests we are evaluating, 21 # and ideally linked to a bug, and removed once the bug is fixed 22 run_hourly = [ 23 ] 24 25 run_daily = [ 26 ] 27 28 def simple_hash(s): 29 # & 0xffffffff avoids python2/python3 compatibility 30 return zlib.crc32(s.encode()) & 0xffffffff 31 32 def build_cron(key, runs_per_day): 33 runs_per_week = 0 34 minute = simple_hash("minutes:" + key) % 60 35 hour = simple_hash("hours:" + key) % 24 36 day_of_week = simple_hash("day_of_week:" + key) % 7 37 38 if runs_per_day > 0: 39 hour_denominator = 24 / runs_per_day 40 hour_offset = simple_hash("hours:" + key) % hour_denominator 41 return "%d %d-23/%d * * *" % (minute, hour_offset, hour_denominator), (runs_per_day * 7) 42 43 # run Ubuntu 20.04 (Focal) jobs more frequently 44 if "u2004" in key: 45 runs_per_week += 7 46 return "%d %d * * *" % (minute, hour), runs_per_week 47 48 # run hotlist jobs more frequently 49 if key in run_hourly: 50 runs_per_week += 24 * 7 51 return "%d * * * *" % (minute), runs_per_week 52 53 if key in run_daily: 54 runs_per_week += 7 55 return "%d %d * * *" % (minute, hour), runs_per_week 56 57 runs_per_week += 1 58 return "%d %d * * %d" % (minute, hour, day_of_week), runs_per_week 59 60 def replace_or_remove_line(s, pattern, new_str): 61 keep = [] 62 for line in s.split('\n'): 63 if pattern in line: 64 if new_str: 65 line = line.replace(pattern, new_str) 66 keep.append(line) 67 else: 68 keep.append(line) 69 return '\n'.join(keep) 70 71 def should_skip_newer_k8s(k8s_version, kops_version): 72 if kops_version is None: 73 return False 74 if k8s_version is None: 75 return True 76 return float(k8s_version) > float(kops_version) 77 78 def k8s_version_info(k8s_version): 79 test_package_url = '' 80 test_package_dir = '' 81 if k8s_version == 'latest': 82 marker = 'latest.txt' 83 k8s_deploy_url = "https://dl.k8s.io/release/latest.txt" 84 elif k8s_version == 'ci': 85 marker = 'latest.txt' 86 k8s_deploy_url = "https://storage.googleapis.com/k8s-release-dev/ci/latest.txt" 87 test_package_url = 'https://storage.googleapis.com/k8s-release-dev' 88 test_package_dir = 'ci' 89 elif k8s_version == 'stable': 90 marker = 'stable.txt' 91 k8s_deploy_url = "https://dl.k8s.io/release/stable.txt" 92 elif k8s_version: 93 marker = f"stable-{k8s_version}.txt" 94 k8s_deploy_url = f"https://dl.k8s.io/release/stable-{k8s_version}.txt" # pylint: disable=line-too-long 95 else: 96 raise Exception('missing required k8s_version') 97 return marker, k8s_deploy_url, test_package_url, test_package_dir 98 99 def create_args(kops_channel, networking, extra_flags, kops_image): 100 args = f"--channel={kops_channel} --networking=" + networking 101 102 image_overridden = False 103 if extra_flags: 104 for arg in extra_flags: 105 if "--image=" in arg: 106 image_overridden = True 107 args = args + " " + arg 108 if kops_image and not image_overridden: 109 args = f"--image='{kops_image}' {args}" 110 return args.strip() 111 112 def latest_aws_image(owner, name, arch='x86_64'): 113 client = boto3.client('ec2', region_name='us-east-1') 114 response = client.describe_images( 115 Owners=[owner], 116 Filters=[ 117 { 118 'Name': 'name', 119 'Values': [ 120 name, 121 ], 122 }, 123 { 124 'Name': 'architecture', 125 'Values': [ 126 arch 127 ], 128 }, 129 ], 130 ) 131 images = [] 132 for image in response['Images']: 133 images.append(image['ImageLocation'].replace('amazon', owner)) 134 images.sort(reverse=True) 135 return images[0] 136 137 distro_images = { 138 'al2023': latest_aws_image('137112412989', 'al2023-ami-2*-kernel-6.1-x86_64'), 139 'amzn2': latest_aws_image('137112412989', 'amzn2-ami-kernel-5.10-hvm-*-x86_64-gp2'), 140 'deb10': latest_aws_image('136693071363', 'debian-10-amd64-*'), 141 'deb11': latest_aws_image('136693071363', 'debian-11-amd64-*'), 142 'deb12': latest_aws_image('136693071363', 'debian-12-amd64-*'), 143 'flatcar': latest_aws_image('075585003325', 'Flatcar-beta-*-hvm'), 144 'flatcararm64': latest_aws_image('075585003325', 'Flatcar-beta-*-hvm', 'arm64'), 145 'rhel8': latest_aws_image('309956199498', 'RHEL-8.*_HVM-*-x86_64-*'), 146 'rhel9': latest_aws_image('309956199498', 'RHEL-9.*_HVM-*-x86_64-*'), 147 'rocky8': latest_aws_image('792107900819', 'Rocky-8-ec2-8.*.x86_64'), 148 'u2004': latest_aws_image('099720109477', 'ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*'), # pylint: disable=line-too-long 149 'u2004arm64': latest_aws_image('099720109477', 'ubuntu/images/hvm-ssd/ubuntu-focal-20.04-arm64-server-*', 'arm64'), # pylint: disable=line-too-long 150 'u2204': latest_aws_image('099720109477', 'ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*'), # pylint: disable=line-too-long 151 'u2204arm64': latest_aws_image('099720109477', 'ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-arm64-server-*', 'arm64'), # pylint: disable=line-too-long 152 'u2404': latest_aws_image('099720109477', 'ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*'), # pylint: disable=line-too-long 153 'u2404arm64': latest_aws_image('099720109477', 'ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-arm64-server-*', 'arm64'), # pylint: disable=line-too-long 154 } 155 156 distros_ssh_user = { 157 'al2023': 'ec2-user', 158 'amzn2': 'ec2-user', 159 'deb10': 'admin', 160 'deb11': 'admin', 161 'deb12': 'admin', 162 'flatcar': 'core', 163 'flatcararm64': 'core', 164 'rhel8': 'ec2-user', 165 'rhel9': 'ec2-user', 166 'rocky8': 'rocky', 167 'u2004': 'ubuntu', 168 'u2004arm64': 'ubuntu', 169 'u2204': 'ubuntu', 170 'u2204arm64': 'ubuntu', 171 'u2404': 'ubuntu', 172 'u2404arm64': 'ubuntu', 173 }