-
Notifications
You must be signed in to change notification settings - Fork 117
/
freight_types.go
133 lines (121 loc) · 4.87 KB
/
freight_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
131
132
133
package v1alpha1
import (
"crypto/sha1"
"fmt"
"path"
"sort"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name=Alias,type=string,JSONPath=`.metadata.labels.kargo\.akuity\.io/alias`
//+kubebuilder:printcolumn:name=Age,type=date,JSONPath=`.metadata.creationTimestamp`
// Freight represents a collection of versioned artifacts.
type Freight struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
// ID is a system-assigned value that is derived deterministically from the
// contents of the Freight. i.e. Two pieces of Freight can be compared for
// equality by comparing their IDs.
ID string `json:"id,omitempty"`
// Commits describes specific Git repository commits.
Commits []GitCommit `json:"commits,omitempty"`
// Images describes specific versions of specific container images.
Images []Image `json:"images,omitempty"`
// Charts describes specific versions of specific Helm charts.
Charts []Chart `json:"charts,omitempty"`
// Status describes the current status of this Freight.
Status FreightStatus `json:"status,omitempty"`
}
func (f *Freight) GetStatus() *FreightStatus {
return &f.Status
}
// UpdateID deterministically calculates a piece of Freight's ID based on its
// contents and assigns it to the ID field.
func (f *Freight) UpdateID() {
size := len(f.Commits) + len(f.Images) + len(f.Charts)
artifacts := make([]string, 0, size)
for _, commit := range f.Commits {
artifacts = append(
artifacts,
fmt.Sprintf("%s:%s", commit.RepoURL, commit.ID),
)
}
for _, image := range f.Images {
artifacts = append(
artifacts,
// Note: This isn't the usual image representation using EITHER :<tag> OR @<digest>.
// It is possible to have found an image with a tag that is already known, but with a
// new digest -- as in the case of "mutable" tags like "latest". It is equally possible to
// have found an image with a digest that is already known, but has been re-tagged.
// To cover both cases, we incorporate BOTH tag and digest into the canonical
// representation of an image used when calculating Freight ID.
fmt.Sprintf("%s:%s@%s", image.RepoURL, image.Tag, image.Digest),
)
}
for _, chart := range f.Charts {
artifacts = append(
artifacts,
fmt.Sprintf(
"%s:%s",
// path.Join accounts for the possibility that chart.Name is empty
path.Join(chart.RepoURL, chart.Name),
chart.Version,
),
)
}
sort.Strings(artifacts)
f.ID = fmt.Sprintf(
"%x",
sha1.Sum([]byte(strings.Join(artifacts, "|"))),
)
}
// GitCommit describes a specific commit from a specific Git repository.
type GitCommit struct {
// RepoURL is the URL of a Git repository.
RepoURL string `json:"repoURL,omitempty"`
// ID is the ID of a specific commit in the Git repository specified by
// RepoURL.
ID string `json:"id,omitempty"`
// Branch denotes the branch of the repository where this commit was found.
Branch string `json:"branch,omitempty"`
// Tag denotes a tag in the repository that matched selection criteria and
// resolved to this commit.
Tag string `json:"tag,omitempty"`
// HealthCheckCommit is the ID of a specific commit. When specified,
// assessments of Stage health will used this value (instead of ID) when
// determining if applicable sources of Argo CD Application resources
// associated with the Stage are or are not synced to this commit. Note that
// there are cases (as in that of Kargo Render being utilized as a promotion
// mechanism) wherein the value of this field may differ from the commit ID
// found in the ID field.
HealthCheckCommit string `json:"healthCheckCommit,omitempty"`
// Message is the git commit message
Message string `json:"message,omitempty"`
// Author is the git commit author
Author string `json:"author,omitempty"`
}
// FreightStatus describes a piece of Freight's most recently observed state.
type FreightStatus struct {
// VerifiedIn describes the Stages in which this Freight has been verified
// through promotion and subsequent health checks.
VerifiedIn map[string]VerifiedStage `json:"verifiedIn,omitempty"`
// ApprovedFor describes the Stages for which this Freight has been approved
// preemptively/manually by a user. This is useful for hotfixes, where one
// might wish to promote a piece of Freight to a given Stage without
// transiting the entire pipeline.
ApprovedFor map[string]ApprovedStage `json:"approvedFor,omitempty"`
}
// VerifiedStage describes a Stage in which Freight has been verified.
type VerifiedStage struct{}
// ApprovedStage describes a Stage for which Freight has been (manually)
// approved.
type ApprovedStage struct{}
//+kubebuilder:object:root=true
// FreightList is a list of Freight resources.
type FreightList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Freight `json:"items"`
}