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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package component
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
)
const (
ProtocolHTTP = "http"
ProtocolHTTPS = "https"
ProtocolWS = "ws"
ProtocolWSS = "wss"
ProtocolTCP = "tcp"
ProtocolSSH = "ssh"
)
type Key struct {
Workspace string
Kind string
Name string
}
func (k Key) Ref() commonv1.ObjectRef {
return commonv1.ObjectRef{Workspace: k.Workspace, Kind: k.Kind, Name: k.Name}
}
func KeyOf(ref commonv1.ObjectRef) Key {
return Key{Workspace: strings.TrimSpace(ref.Workspace), Kind: strings.TrimSpace(ref.Kind), Name: strings.TrimSpace(ref.Name)}
}
type Endpoint struct {
Name string
Protocol string
URL string
Address string
}
func PrimaryEndpoint(endpoints []Endpoint) (Endpoint, bool) {
for _, endpoint := range endpoints {
if endpoint.Name == ProtocolHTTP {
return endpoint, true
}
}
for _, endpoint := range endpoints {
if endpoint.URL != "" {
return endpoint, true
}
}
if len(endpoints) > 0 {
return endpoints[0], true
}
return Endpoint{}, false
}
type Service interface {
Start(ctx context.Context) error
Ready() Readiness
Endpoints() []Endpoint
Stop(ctx context.Context) error
}
type Readiness interface {
Done() <-chan struct{}
Err() error
}
var ErrNotReady = errors.New("component: the service failed to become ready")
type ReadySignal struct {
mu sync.Mutex
done chan struct{}
err error
set bool
}
var _ Readiness = (*ReadySignal)(nil)
func (r *ReadySignal) Done() <-chan struct{} {
r.mu.Lock()
defer r.mu.Unlock()
return r.doneLocked()
}
func (r *ReadySignal) Err() error {
r.mu.Lock()
defer r.mu.Unlock()
return r.err
}
func (r *ReadySignal) Signal() {
r.resolve(nil)
}
func (r *ReadySignal) Fail(err error) {
if err == nil {
err = ErrNotReady
}
r.resolve(err)
}
func (r *ReadySignal) resolve(err error) {
r.mu.Lock()
defer r.mu.Unlock()
if r.set {
return
}
r.set = true
r.err = err
close(r.doneLocked())
}
func (r *ReadySignal) doneLocked() chan struct{} {
if r.done == nil {
r.done = make(chan struct{})
}
return r.done
}
type NetworkHosts interface {
Address(hostport string) (string, bool)
Hosts() map[string]string
}
type RawSpec json.RawMessage
func (s RawSpec) Bytes() []byte {
return []byte(s)
}
func (s RawSpec) Decode(v any) error {
if len(s) == 0 {
return nil
}
if err := json.Unmarshal(s, v); err != nil {
return fmt.Errorf("component: decoding the raw spec: %w", err)
}
return nil
}
func (s RawSpec) Map() (map[string]any, error) {
out := map[string]any{}
if err := s.Decode(&out); err != nil {
return nil, err
}
if out == nil {
out = map[string]any{}
}
return out, nil
}
func (s RawSpec) MarshalJSON() ([]byte, error) {
if len(s) == 0 {
return []byte("null"), nil
}
if !json.Valid(s) {
return nil, errors.New("component: the raw spec is not valid JSON")
}
return s, nil
}
func (s *RawSpec) UnmarshalJSON(b []byte) error {
if s == nil {
return errors.New("component: raw spec unmarshal into nil")
}
*s = append((*s)[:0], b...)
return nil
}
type RestartPolicy string
const (
RestartAlways RestartPolicy = "Always"
RestartOnFailure RestartPolicy = "OnFailure"
RestartNever RestartPolicy = "Never"
)
func (p RestartPolicy) Valid() bool {
switch p {
case RestartAlways, RestartOnFailure, RestartNever:
return true
default:
return false
}
}
func (p RestartPolicy) WithDefault() RestartPolicy {
if p.Valid() {
return p
}
return RestartAlways
}
type Phase string
const (
PhasePending Phase = "Pending"
PhaseStarting Phase = "Starting"
PhaseRunning Phase = "Running"
PhaseReady Phase = "Ready"
PhaseFailed Phase = "Failed"
PhaseStopping Phase = "Stopping"
PhaseSuspended Phase = "Suspended"
)