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
package component
import (
"context"
"errors"
"fmt"
"log/slog"
"path/filepath"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/bus"
contract "github.com/johnandersen777/socialweb-computer-kcp/internal/contract/component"
)
const ConditionReady = "Ready"
const (
ProtocolHTTP = contract.ProtocolHTTP
ProtocolHTTPS = contract.ProtocolHTTPS
ProtocolWS = contract.ProtocolWS
ProtocolWSS = contract.ProtocolWSS
ProtocolTCP = contract.ProtocolTCP
ProtocolSSH = contract.ProtocolSSH
)
type Service = contract.Service
type Readiness = contract.Readiness
type ReadySignal = contract.ReadySignal
type Instance struct {
Self commonv1.ObjectRef
UID types.UID
Namespace string
Labels map[string]string
Log *slog.Logger
StateDir string
Bus *bus.Bus
}
func (i Instance) Key() Key {
return Key{Workspace: i.Self.Workspace, Kind: i.Self.Kind, Name: i.Self.Name}
}
func (i Instance) Caller(ctx context.Context) context.Context {
return bus.WithCallerLabels(ctx, i.Self, i.Labels)
}
type NetworkHosts = contract.NetworkHosts
type Endpoint = contract.Endpoint
type RawSpec = contract.RawSpec
type RestartPolicy = contract.RestartPolicy
const (
RestartAlways = contract.RestartAlways
RestartOnFailure = contract.RestartOnFailure
RestartNever = contract.RestartNever
)
type ComponentSpec struct {
UID types.UID
Group string
Kind string
Name string
Namespace string
Workspace string
Generation int64
Suspend bool
Deleting bool
RestartPolicy RestartPolicy
StateDir string
Spec RawSpec
Annotations map[string]string
Labels map[string]string
}
func (s ComponentSpec) Check() error {
switch {
case s.UID == "":
return fmt.Errorf("component: the %s/%s spec has no uid", s.Kind, s.Name)
case s.Kind == "":
return errors.New("component: the spec has no kind")
case s.Name == "":
return fmt.Errorf("component: the %s spec has no name", s.Kind)
}
return nil
}
func (s ComponentSpec) Self() commonv1.ObjectRef {
return commonv1.ObjectRef{Group: s.Group, Kind: s.Kind, Name: s.Name, Workspace: s.Workspace}
}
func (s ComponentSpec) ComponentStateDir(base string) string {
if s.StateDir != "" {
return s.StateDir
}
if base == "" {
return ""
}
return filepath.Join(base, "components", s.Kind+"-"+s.Name)
}
type ComponentPhase = contract.Phase
const (
PhasePending = contract.PhasePending
PhaseStarting = contract.PhaseStarting
PhaseRunning = contract.PhaseRunning
PhaseReady = contract.PhaseReady
PhaseFailed = contract.PhaseFailed
PhaseStopping = contract.PhaseStopping
PhaseSuspended = contract.PhaseSuspended
)
type ComponentStatus struct {
ObservedGeneration int64
Phase ComponentPhase
Ready bool
Message string
Endpoints []Endpoint
Restarts int32
StartedAt time.Time
Conditions []metav1.Condition
}
func (s ComponentStatus) Endpoint(name string) (Endpoint, bool) {
for _, e := range s.Endpoints {
if e.Name == name {
return e, true
}
}
return Endpoint{}, false
}