github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/scenarios/kubernetes_verify.py (about) 1 #!/usr/bin/env python 2 3 # Copyright 2016 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 # Need to figure out why this only fails on travis 18 # pylint: disable=bad-continuation 19 20 """Runs verify/test-go checks for kubernetes/kubernetes.""" 21 22 import argparse 23 import os 24 import re 25 import subprocess 26 import sys 27 28 29 VERSION_TAG = { 30 '1.7': '1.7-v20170713-c28e0556', 31 '1.8': '1.8-v20170906-3a1c5ae5', 32 '1.9': '1.9-v20171018-6ddbad97', 33 '1.10': '1.10-v20180221-3655e24ae', 34 # this is master, feature branches... 35 'default': '1.11-v20180504-30872f7f6' 36 } 37 38 39 def check_output(*cmd): 40 """Log and run the command, return output, raising on errors.""" 41 print >>sys.stderr, 'Run:', cmd 42 return subprocess.check_output(cmd) 43 44 45 def check(*cmd): 46 """Log and run the command, raising on errors.""" 47 print >>sys.stderr, 'Run:', cmd 48 subprocess.check_call(cmd) 49 50 51 def retry(func, times=5): 52 """call func until it returns true at most times times""" 53 success = False 54 for _ in range(0, times): 55 success = func() 56 if success: 57 return success 58 return success 59 60 61 def try_call(cmds): 62 """returns true if check(cmd) does not throw an exception 63 over all cmds where cmds = [[cmd, arg, arg2], [cmd2, arg]]""" 64 try: 65 for cmd in cmds: 66 check(*cmd) 67 return True 68 # pylint: disable=bare-except 69 except: 70 return False 71 72 def get_git_cache(k8s): 73 git = os.path.join(k8s, ".git") 74 if not os.path.isfile(git): 75 return None 76 with open(git) as git_file: 77 return git_file.read().replace("gitdir: ", "").rstrip("\n") 78 79 80 def branch_to_tag(branch): 81 verify_branch = re.match(r'release-(\d+\.\d+)', branch) 82 key = 'default' 83 if verify_branch and verify_branch.group(1) in VERSION_TAG: 84 key = verify_branch.group(1) 85 return VERSION_TAG[key] 86 87 88 def main(branch, script, force, on_prow): 89 """Test branch using script, optionally forcing verify checks.""" 90 tag = branch_to_tag(branch) 91 92 force = 'y' if force else 'n' 93 artifacts = '%s/_artifacts' % os.environ['WORKSPACE'] 94 k8s = os.getcwd() 95 if not os.path.basename(k8s) == 'kubernetes': 96 raise ValueError(k8s) 97 98 check('rm', '-rf', '.gsutil') 99 remote = 'bootstrap-upstream' 100 uri = 'https://github.com/kubernetes/kubernetes.git' 101 102 current_remotes = check_output('git', 'remote') 103 if re.search('^%s$' % remote, current_remotes, flags=re.MULTILINE): 104 check('git', 'remote', 'remove', remote) 105 check('git', 'remote', 'add', remote, uri) 106 check('git', 'remote', 'set-url', '--push', remote, 'no_push') 107 # If .git is cached between runs this data may be stale 108 check('git', 'fetch', remote) 109 110 if not os.path.isdir(artifacts): 111 os.makedirs(artifacts) 112 113 if on_prow: 114 # TODO(bentheelder): on prow REPO_DIR should be /go/src/k8s.io/kubernetes 115 # however these paths are brittle enough as is... 116 git_cache = get_git_cache(k8s) 117 cmd = [ 118 'docker', 'run', '--rm=true', '--privileged=true', 119 '-v', '/var/run/docker.sock:/var/run/docker.sock', 120 '-v', '/etc/localtime:/etc/localtime:ro', 121 '-v', '%s:/go/src/k8s.io/kubernetes' % k8s, 122 ] 123 if git_cache is not None: 124 cmd.extend(['-v', '%s:%s' % (git_cache, git_cache)]) 125 cmd.extend([ 126 '-v', '/workspace/k8s.io/:/workspace/k8s.io/', 127 '-v', '%s:/workspace/artifacts' % artifacts, 128 '-e', 'KUBE_FORCE_VERIFY_CHECKS=%s' % force, 129 '-e', 'KUBE_VERIFY_GIT_BRANCH=%s' % branch, 130 '-e', 'REPO_DIR=%s' % k8s, # hack/lib/swagger.sh depends on this 131 '--tmpfs', '/tmp:exec,mode=1777', 132 'gcr.io/k8s-testimages/kubekins-test:%s' % tag, 133 'bash', '-c', 'cd kubernetes && %s' % script, 134 ]) 135 check(*cmd) 136 else: 137 check( 138 'docker', 'run', '--rm=true', '--privileged=true', 139 '-v', '/var/run/docker.sock:/var/run/docker.sock', 140 '-v', '/etc/localtime:/etc/localtime:ro', 141 '-v', '%s:/go/src/k8s.io/kubernetes' % k8s, 142 '-v', '%s:/workspace/artifacts' % artifacts, 143 '-e', 'KUBE_FORCE_VERIFY_CHECKS=%s' % force, 144 '-e', 'KUBE_VERIFY_GIT_BRANCH=%s' % branch, 145 '-e', 'REPO_DIR=%s' % k8s, # hack/lib/swagger.sh depends on this 146 'gcr.io/k8s-testimages/kubekins-test:%s' % tag, 147 'bash', '-c', 'cd kubernetes && %s' % script, 148 ) 149 150 151 152 if __name__ == '__main__': 153 PARSER = argparse.ArgumentParser( 154 'Runs verification checks on the kubernetes repo') 155 PARSER.add_argument( 156 '--branch', default='master', help='Upstream target repo') 157 PARSER.add_argument( 158 '--force', action='store_true', help='Force all verify checks') 159 PARSER.add_argument( 160 '--script', 161 default='./hack/jenkins/test-dockerized.sh', 162 help='Script in kubernetes/kubernetes that runs checks') 163 PARSER.add_argument( 164 '--prow', action='store_true', help='Force Prow mode' 165 ) 166 ARGS = PARSER.parse_args() 167 main(ARGS.branch, ARGS.script, ARGS.force, ARGS.prow)