jobspec.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package downwardapi
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "k8s.io/test-infra/prow/kube"
  20. )
  21. // JobSpec is the full downward API that we expose to
  22. // jobs that realize a ProwJob. We will provide this
  23. // data to jobs with environment variables in two ways:
  24. // - the full spec, in serialized JSON in one variable
  25. // - individual fields of the spec in their own variables
  26. type JobSpec struct {
  27. Type kube.ProwJobType `json:"type,omitempty"`
  28. Job string `json:"job,omitempty"`
  29. BuildID string `json:"buildid,omitempty"`
  30. ProwJobID string `json:"prowjobid,omitempty"`
  31. Refs kube.Refs `json:"refs,omitempty"`
  32. // we need to keep track of the agent until we
  33. // migrate everyone away from using the $BUILD_NUMBER
  34. // environment variable
  35. agent kube.ProwJobAgent
  36. }
  37. // NewJobSpec converts a kube.ProwJobSpec invocation into a JobSpec
  38. func NewJobSpec(spec kube.ProwJobSpec, buildID, prowJobID string) JobSpec {
  39. refs := kube.Refs{}
  40. if spec.Refs != nil {
  41. refs = *spec.Refs
  42. }
  43. return JobSpec{
  44. Type: spec.Type,
  45. Job: spec.Job,
  46. BuildID: buildID,
  47. ProwJobID: prowJobID,
  48. Refs: refs,
  49. agent: spec.Agent,
  50. }
  51. }
  52. // ResolveSpecFromEnv will determine the Refs being
  53. // tested in by parsing Prow environment variable contents
  54. func ResolveSpecFromEnv() (*JobSpec, error) {
  55. specEnv, ok := os.LookupEnv(JobSpecEnv)
  56. if !ok {
  57. return nil, fmt.Errorf("$%s unset", JobSpecEnv)
  58. }
  59. spec := &JobSpec{}
  60. if err := json.Unmarshal([]byte(specEnv), spec); err != nil {
  61. return nil, fmt.Errorf("malformed $%s: %v", JobSpecEnv, err)
  62. }
  63. return spec, nil
  64. }
  65. const (
  66. // JobSpecEnv is the name that contains JobSpec marshaled into a string.
  67. JobSpecEnv = "JOB_SPEC"
  68. jobNameEnv = "JOB_NAME"
  69. jobTypeEnv = "JOB_TYPE"
  70. prowJobIDEnv = "PROW_JOB_ID"
  71. buildIDEnv = "BUILD_ID"
  72. prowBuildIDEnv = "BUILD_NUMBER" // Deprecated, will be removed in the future.
  73. repoOwnerEnv = "REPO_OWNER"
  74. repoNameEnv = "REPO_NAME"
  75. pullBaseRefEnv = "PULL_BASE_REF"
  76. pullBaseShaEnv = "PULL_BASE_SHA"
  77. pullRefsEnv = "PULL_REFS"
  78. pullNumberEnv = "PULL_NUMBER"
  79. pullPullShaEnv = "PULL_PULL_SHA"
  80. )
  81. // EnvForSpec returns a mapping of environment variables
  82. // to their values that should be available for a job spec
  83. func EnvForSpec(spec JobSpec) (map[string]string, error) {
  84. env := map[string]string{
  85. jobNameEnv: spec.Job,
  86. buildIDEnv: spec.BuildID,
  87. prowJobIDEnv: spec.ProwJobID,
  88. jobTypeEnv: string(spec.Type),
  89. }
  90. // for backwards compatibility, we provide the build ID
  91. // in both $BUILD_ID and $BUILD_NUMBER for Prow agents
  92. // and in both $buildId and $BUILD_NUMBER for Jenkins
  93. if spec.agent == kube.KubernetesAgent {
  94. env[prowBuildIDEnv] = spec.BuildID
  95. }
  96. raw, err := json.Marshal(spec)
  97. if err != nil {
  98. return env, fmt.Errorf("failed to marshal job spec: %v", err)
  99. }
  100. env[JobSpecEnv] = string(raw)
  101. if spec.Type == kube.PeriodicJob {
  102. return env, nil
  103. }
  104. env[repoOwnerEnv] = spec.Refs.Org
  105. env[repoNameEnv] = spec.Refs.Repo
  106. env[pullBaseRefEnv] = spec.Refs.BaseRef
  107. env[pullBaseShaEnv] = spec.Refs.BaseSHA
  108. env[pullRefsEnv] = spec.Refs.String()
  109. if spec.Type == kube.PostsubmitJob || spec.Type == kube.BatchJob {
  110. return env, nil
  111. }
  112. env[pullNumberEnv] = strconv.Itoa(spec.Refs.Pulls[0].Number)
  113. env[pullPullShaEnv] = spec.Refs.Pulls[0].SHA
  114. return env, nil
  115. }
  116. // EnvForType returns the slice of environment variables to export for jobType
  117. func EnvForType(jobType kube.ProwJobType) []string {
  118. baseEnv := []string{jobNameEnv, JobSpecEnv, jobTypeEnv, prowJobIDEnv, buildIDEnv, prowBuildIDEnv}
  119. refsEnv := []string{repoOwnerEnv, repoNameEnv, pullBaseRefEnv, pullBaseShaEnv, pullRefsEnv}
  120. pullEnv := []string{pullNumberEnv, pullPullShaEnv}
  121. switch jobType {
  122. case kube.PeriodicJob:
  123. return baseEnv
  124. case kube.PostsubmitJob, kube.BatchJob:
  125. return append(baseEnv, refsEnv...)
  126. case kube.PresubmitJob:
  127. return append(append(baseEnv, refsEnv...), pullEnv...)
  128. default:
  129. return []string{}
  130. }
  131. }