forked from fluxcd/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflux.go
178 lines (147 loc) · 3.33 KB
/
flux.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package flux
import (
"fmt"
"net/http"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/weaveworks/flux/ssh"
)
var (
ErrInvalidServiceID = errors.New("invalid service ID")
)
type Token string
func (t Token) Set(req *http.Request) {
if string(t) != "" {
req.Header.Set("Authorization", fmt.Sprintf("Scope-Probe token=%s", t))
}
}
// (User) Service identifiers
type ServiceID string // "default/helloworld"
func (id ServiceID) String() string {
return string(id)
}
func ParseServiceID(s string) (ServiceID, error) {
toks := strings.SplitN(s, "/", 2)
if len(toks) != 2 {
return "", errors.Wrap(ErrInvalidServiceID, "parsing "+s)
}
return ServiceID(s), nil
}
func MakeServiceID(namespace, service string) ServiceID {
return ServiceID(namespace + "/" + service)
}
func (id ServiceID) Components() (namespace, service string) {
toks := strings.SplitN(string(id), "/", 2)
if len(toks) != 2 {
panic("invalid service spec")
}
return toks[0], toks[1]
}
type ServiceIDSet map[ServiceID]struct{}
func (s ServiceIDSet) String() string {
var ids []string
for id, _ := range s {
ids = append(ids, string(id))
}
return "{" + strings.Join(ids, ", ") + "}"
}
func (s ServiceIDSet) Add(ids []ServiceID) {
for _, id := range ids {
s[id] = struct{}{}
}
}
func (s ServiceIDSet) Without(others ServiceIDSet) ServiceIDSet {
if s == nil || len(s) == 0 || others == nil || len(others) == 0 {
return s
}
res := ServiceIDSet{}
for id, _ := range s {
if !others.Contains(id) {
res[id] = struct{}{}
}
}
return res
}
func (s ServiceIDSet) Contains(id ServiceID) bool {
if s == nil {
return false
}
_, ok := s[id]
return ok
}
func (s ServiceIDSet) Intersection(others ServiceIDSet) ServiceIDSet {
if s == nil {
return others
}
if others == nil {
return s
}
result := ServiceIDSet{}
for id := range s {
if _, ok := others[id]; ok {
result[id] = struct{}{}
}
}
return result
}
func (s ServiceIDSet) ToSlice() ServiceIDs {
i := 0
keys := make(ServiceIDs, len(s))
for k := range s {
keys[i] = k
i++
}
return keys
}
type ServiceIDs []ServiceID
func (p ServiceIDs) Len() int { return len(p) }
func (p ServiceIDs) Less(i, j int) bool { return string(p[i]) < string(p[j]) }
func (p ServiceIDs) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p ServiceIDs) Sort() { sort.Sort(p) }
func (ids ServiceIDs) Without(others ServiceIDSet) (res ServiceIDs) {
for _, id := range ids {
if !others.Contains(id) {
res = append(res, id)
}
}
return res
}
func (ids ServiceIDs) Contains(id ServiceID) bool {
set := ServiceIDSet{}
set.Add(ids)
return set.Contains(id)
}
func (ids ServiceIDs) Intersection(others ServiceIDSet) ServiceIDSet {
set := ServiceIDSet{}
set.Add(ids)
return set.Intersection(others)
}
// -- types used in API
type ImageStatus struct {
ID ServiceID
Containers []Container
}
type ServiceStatus struct {
ID ServiceID
Containers []Container
Status string
Automated bool
Locked bool
Ignore bool
}
type Container struct {
Name string
Current Image
Available []Image
}
// --- config types
type GitRemoteConfig struct {
URL string `json:"url"`
Branch string `json:"branch"`
Path string `json:"path"`
}
type GitConfig struct {
Remote GitRemoteConfig `json:"remote"`
PublicSSHKey ssh.PublicKey `json:"publicSSHKey"`
}