github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/kubetest/stage.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 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 http://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 package main 18 19 import ( 20 "fmt" 21 "os/exec" 22 "regexp" 23 "strings" 24 25 "k8s.io/test-infra/kubetest/util" 26 ) 27 28 type stageStrategy struct { 29 bucket string 30 ci bool 31 gcsSuffix string 32 versionSuffix string 33 dockerRegistry string 34 } 35 36 // Return something like gs://bucket/ci/suffix 37 func (s *stageStrategy) String() string { 38 p := "devel" 39 if s.ci { 40 p = "ci" 41 } 42 return fmt.Sprintf("%v%v%v", s.bucket, p, s.gcsSuffix) 43 } 44 45 // Parse bucket, ci, suffix from gs://BUCKET/ci/SUFFIX 46 func (s *stageStrategy) Set(value string) error { 47 re := regexp.MustCompile(`^(gs://[\w-]+)/(devel|ci)(/.*)?`) 48 mat := re.FindStringSubmatch(value) 49 if mat == nil { 50 return fmt.Errorf("Invalid stage location: %v. Use gs://bucket/ci/optional-suffix", value) 51 } 52 s.bucket = mat[1] 53 s.ci = mat[2] == "ci" 54 s.gcsSuffix = mat[3] 55 return nil 56 } 57 58 func (s *stageStrategy) Type() string { 59 return "stageStrategy" 60 } 61 62 // True when this kubetest invocation wants to stage the release 63 func (s *stageStrategy) Enabled() bool { 64 return s.bucket != "" 65 } 66 67 // Stage the release build to GCS. 68 // Essentially release/push-build.sh --bucket=B --ci? --gcs-suffix=S --federation? --noupdatelatest 69 func (s *stageStrategy) Stage(fed, noAllowDup bool) error { 70 name := util.K8s("release", "push-build.sh") 71 b := s.bucket 72 if strings.HasPrefix(b, "gs://") { 73 b = b[len("gs://"):] 74 } 75 args := []string{ 76 "--nomock", 77 "--verbose", 78 "--noupdatelatest", // we may need to expose control of this if build jobs start using kubetest 79 fmt.Sprintf("--bucket=%v", b), 80 } 81 if s.ci { 82 args = append(args, "--ci") 83 } 84 if len(s.gcsSuffix) > 0 { 85 args = append(args, fmt.Sprintf("--gcs-suffix=%v", s.gcsSuffix)) 86 } 87 if len(s.versionSuffix) > 0 { 88 args = append(args, fmt.Sprintf("--version-suffix=%s", s.versionSuffix)) 89 } 90 if len(s.dockerRegistry) > 0 { 91 args = append(args, fmt.Sprintf("--docker-registry=%s", s.dockerRegistry)) 92 } 93 if fed { 94 args = append(args, "--federation") 95 } 96 97 if !noAllowDup { 98 args = append(args, "--allow-dup") 99 } 100 101 cmd := exec.Command(name, args...) 102 cmd.Dir = util.K8s("kubernetes") 103 return control.FinishRunning(cmd) 104 } 105 106 type stageFederationStrategy struct { 107 stageStrategy 108 } 109 110 func (s *stageFederationStrategy) Type() string { 111 return "stageFederationStrategy" 112 } 113 114 // Stage the federation release build to GCS. 115 // Essentially release/push-build.sh --bucket=B --ci? --gcs-suffix=S --noupdatelatest 116 func (s *stageFederationStrategy) Stage() error { 117 name := util.K8s("release", "push-build.sh") 118 b := s.bucket 119 if strings.HasPrefix(b, "gs://") { 120 b = b[len("gs://"):] 121 } 122 args := []string{ 123 "--nomock", 124 "--verbose", 125 "--noupdatelatest", // we may need to expose control of this if build jobs start using kubetest 126 fmt.Sprintf("--bucket=%v", b), 127 "--release-kind=federation", 128 } 129 if s.ci { 130 args = append(args, "--ci") 131 } 132 if len(s.gcsSuffix) > 0 { 133 args = append(args, fmt.Sprintf("--gcs-suffix=%v", s.gcsSuffix)) 134 } 135 if len(s.versionSuffix) > 0 { 136 args = append(args, fmt.Sprintf("--version-suffix=%s", s.versionSuffix)) 137 } 138 if len(s.dockerRegistry) > 0 { 139 args = append(args, fmt.Sprintf("--docker-registry=%s", s.dockerRegistry)) 140 } 141 142 cmd := exec.Command(name, args...) 143 cmd.Dir = util.K8s("federation") 144 return control.FinishRunning(cmd) 145 }