-
Notifications
You must be signed in to change notification settings - Fork 248
/
job_spec.go
103 lines (86 loc) · 2.32 KB
/
job_spec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package api
import (
"crypto/sha256"
"encoding/json"
"fmt"
"runtime/debug"
"github.com/sirupsen/logrus"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/test-infra/prow/pod-utils/downwardapi"
)
// JobSpec is a superset of the upstream spec.
// +k8s:deepcopy-gen=false
type JobSpec struct {
downwardapi.JobSpec `json:",inline"`
// rawSpec is the serialized form of the Spec
rawSpec string
// these fields allow the job to be targeted at a location
namespace string
BaseNamespace string
// if set, any new artifacts will be a child of this object
owner *meta.OwnerReference
Metadata Metadata
Target string
TargetAdditionalSuffix string
}
// Namespace returns the namespace of the job. Must not be evaluated
// at step construction time because its unset there
func (s *JobSpec) Namespace() string {
if s.namespace == "" {
logrus.Warn("Warning, namespace accessed before it was set, this is a bug in ci-operator. Stack:")
logrus.Warn(string(debug.Stack()))
}
return s.namespace
}
func (s *JobSpec) SetNamespace(namespace string) {
s.namespace = namespace
}
func (s *JobSpec) RawSpec() string {
return s.rawSpec
}
func (s *JobSpec) Owner() *meta.OwnerReference {
return s.owner
}
func (s *JobSpec) SetOwner(owner *meta.OwnerReference) {
s.owner = owner
}
// Inputs returns the definition of the job as an input to
// the execution graph.
func (s *JobSpec) Inputs() InputDefinition {
spec := &JobSpec{
JobSpec: downwardapi.JobSpec{
Refs: s.Refs,
},
}
raw, err := json.Marshal(spec)
if err != nil {
panic(err)
}
return InputDefinition{string(raw)}
}
func (s JobSpec) JobNameHash() string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(s.Job)))[:5]
}
func (s JobSpec) UniqueHash() string {
job := s.Job
if s.TargetAdditionalSuffix != "" {
job += fmt.Sprintf("-%s", s.TargetAdditionalSuffix)
}
return fmt.Sprintf("%x", sha256.Sum256([]byte(job)))[:5]
}
// ResolveSpecFromEnv will determine the Refs being
// tested in by parsing Prow environment variable contents
func ResolveSpecFromEnv() (*JobSpec, error) {
apiSpec, err := downwardapi.ResolveSpecFromEnv()
if err != nil {
return nil, fmt.Errorf("malformed $JOB_SPEC: %w", err)
}
raw, err := json.Marshal(apiSpec)
if err != nil {
panic(err)
}
return &JobSpec{
JobSpec: *apiSpec,
rawSpec: string(raw),
}, nil
}