-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipeline_types.go
130 lines (105 loc) · 3.25 KB
/
pipeline_types.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package v1alpha3
import (
"encoding/json"
"fmt"
. "github.com/docker/distribution/reference"
. "github.com/sky-uk/kfp-operator/apis"
"github.com/sky-uk/kfp-operator/apis/pipelines"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"strings"
)
type PipelineSpec struct {
Image string `json:"image" yaml:"image"`
TfxComponents string `json:"tfxComponents" yaml:"tfxComponents"`
Env []NamedValue `json:"env,omitempty" yaml:"env"`
BeamArgs []NamedValue `json:"beamArgs,omitempty"`
}
func (ps Pipeline) ComputeHash() []byte {
oh := pipelines.NewObjectHasher()
oh.WriteStringField(ps.Spec.Image)
oh.WriteStringField(ps.Spec.TfxComponents)
pipelines.WriteKVListField(oh, ps.Spec.Env)
pipelines.WriteKVListField(oh, ps.Spec.BeamArgs)
return oh.Sum()
}
func (ps Pipeline) ComputeVersion() string {
hash := ps.ComputeHash()[0:3]
ref, err := ParseNormalizedNamed(ps.Spec.Image)
if err == nil {
if namedTagged, ok := TagNameOnly(ref).(NamedTagged); ok {
return fmt.Sprintf("%s-%x", namedTagged.Tag(), hash)
}
}
return fmt.Sprintf("%x", hash)
}
//+kubebuilder:object:root=true
//+kubebuilder:resource:shortName="mlp"
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="KfpId",type="string",JSONPath=".status.kfpId"
//+kubebuilder:printcolumn:name="SynchronizationState",type="string",JSONPath=".status.synchronizationState"
//+kubebuilder:printcolumn:name="Version",type="string",JSONPath=".status.version"
type Pipeline struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec PipelineSpec `json:"spec,omitempty"`
Status Status `json:"status,omitempty"`
}
func (p *Pipeline) GetStatus() Status {
return p.Status
}
func (p *Pipeline) SetStatus(status Status) {
p.Status = status
}
func (p Pipeline) GetNamespacedName() types.NamespacedName {
return types.NamespacedName{
Name: p.Name,
Namespace: p.Namespace,
}
}
func (p Pipeline) GetKind() string {
return "pipeline"
}
//+kubebuilder:object:root=true
type PipelineList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Pipeline `json:"items"`
}
// +kubebuilder:validation:Type=string
// +kubebuilder:validation:Pattern:=`^[\w-]+(?::[\w-]+)?$`
type PipelineIdentifier struct {
Name string `json:"-"`
Version string `json:"-"`
}
func (pid *PipelineIdentifier) String() string {
if pid.Version == "" {
return pid.Name
}
return strings.Join([]string{pid.Name, pid.Version}, ":")
}
func (pid *PipelineIdentifier) MarshalJSON() ([]byte, error) {
return json.Marshal(pid.String())
}
func (pid *PipelineIdentifier) UnmarshalJSON(bytes []byte) error {
var pidStr string
err := json.Unmarshal(bytes, &pidStr)
if err != nil {
return err
}
nameVersion := strings.Split(pidStr, ":")
pid.Name = nameVersion[0]
if len(nameVersion) == 2 {
pid.Version = nameVersion[1]
}
return nil
}
func (pipeline *Pipeline) UnversionedIdentifier() PipelineIdentifier {
return PipelineIdentifier{Name: pipeline.Name}
}
func (pipeline *Pipeline) VersionedIdentifier() PipelineIdentifier {
return PipelineIdentifier{Name: pipeline.Name, Version: pipeline.ComputeVersion()}
}
func init() {
SchemeBuilder.Register(&Pipeline{}, &PipelineList{})
}