-
Notifications
You must be signed in to change notification settings - Fork 288
/
fake_client.go
231 lines (193 loc) · 5.53 KB
/
fake_client.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package k8s
import (
"bytes"
"context"
"io"
"sync"
"time"
"k8s.io/apimachinery/pkg/labels"
"github.com/windmilleng/tilt/internal/model"
"github.com/docker/distribution/reference"
"github.com/pkg/errors"
"github.com/windmilleng/tilt/internal/container"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/watch"
)
// A magic constant. If the docker client returns this constant, we always match
// even if the container doesn't have the correct image name.
const MagicTestContainerID = "tilt-testcontainer"
var _ Client = &FakeK8sClient{}
// For keying PodLogsByPodAndContainer
type PodAndCName struct {
PID PodID
CName container.Name
}
type FakeK8sClient struct {
Yaml string
DeletedYaml string
Lb LoadBalancerSpec
LastPodQueryNamespace Namespace
LastPodQueryImage reference.NamedTagged
PodLogsByPodAndContainer map[PodAndCName]BufferCloser
ContainerLogsError error
LastForwardPortPodID PodID
LastForwardPortRemotePort int
watcherMu sync.Mutex
watches []fakePodWatch
UpsertError error
Runtime container.Runtime
}
type fakePodWatch struct {
ls labels.Selector
ch chan *v1.Pod
}
func (c *FakeK8sClient) WatchServices(ctx context.Context, lps []model.LabelPair) (<-chan *v1.Service, error) {
return nil, nil
}
func (c *FakeK8sClient) WatchedSelectors() []labels.Selector {
c.watcherMu.Lock()
defer c.watcherMu.Unlock()
var ret []labels.Selector
for _, w := range c.watches {
ret = append(ret, w.ls)
}
return ret
}
func (c *FakeK8sClient) EmitPod(ls labels.Selector, p *v1.Pod) {
c.watcherMu.Lock()
defer c.watcherMu.Unlock()
for _, w := range c.watches {
if SelectorEqual(ls, w.ls) {
w.ch <- p
}
}
}
func (c *FakeK8sClient) WatchPods(ctx context.Context, ls labels.Selector) (<-chan *v1.Pod, error) {
c.watcherMu.Lock()
ch := make(chan *v1.Pod, 20)
c.watches = append(c.watches, fakePodWatch{ls, ch})
c.watcherMu.Unlock()
go func() {
// when ctx is canceled, remove the label selector from the list of watched label selectors
<-ctx.Done()
c.watcherMu.Lock()
var newWatches []fakePodWatch
for _, e := range c.watches {
if !SelectorEqual(e.ls, ls) {
newWatches = append(newWatches, e)
}
}
c.watches = newWatches
c.watcherMu.Unlock()
}()
return ch, nil
}
func NewFakeK8sClient() *FakeK8sClient {
return &FakeK8sClient{
PodLogsByPodAndContainer: make(map[PodAndCName]BufferCloser),
}
}
func (c *FakeK8sClient) ConnectedToCluster(ctx context.Context) error {
return nil
}
func (c *FakeK8sClient) Upsert(ctx context.Context, entities []K8sEntity) error {
if c.UpsertError != nil {
return c.UpsertError
}
yaml, err := SerializeYAML(entities)
if err != nil {
return errors.Wrap(err, "kubectl apply")
}
c.Yaml = yaml
return nil
}
func (c *FakeK8sClient) Delete(ctx context.Context, entities []K8sEntity) error {
yaml, err := SerializeYAML(entities)
if err != nil {
return errors.Wrap(err, "kubectl delete")
}
c.DeletedYaml = yaml
return nil
}
func (c *FakeK8sClient) WatchPod(ctx context.Context, pod *v1.Pod) (watch.Interface, error) {
return watch.NewEmptyWatch(), nil
}
func (c *FakeK8sClient) SetLogsForPodContainer(pID PodID, cName container.Name, logs string) {
c.PodLogsByPodAndContainer[PodAndCName{pID, cName}] = BufferCloser{Buffer: bytes.NewBufferString(logs)}
}
func (c *FakeK8sClient) ContainerLogs(ctx context.Context, pID PodID, cName container.Name, n Namespace, startTime time.Time) (io.ReadCloser, error) {
if c.ContainerLogsError != nil {
return nil, c.ContainerLogsError
}
// If we have specific logs for this pod/container combo, return those
if buf, ok := c.PodLogsByPodAndContainer[PodAndCName{pID, cName}]; ok {
return buf, nil
}
return BufferCloser{Buffer: bytes.NewBuffer(nil)}, nil
}
func (c *FakeK8sClient) PodByID(ctx context.Context, pID PodID, n Namespace) (*v1.Pod, error) {
return nil, nil
}
func FakePodStatus(image reference.NamedTagged, phase string) v1.PodStatus {
return v1.PodStatus{
Phase: v1.PodPhase(phase),
ContainerStatuses: []v1.ContainerStatus{
{
Name: "main",
ContainerID: "docker://" + MagicTestContainerID,
Image: image.String(),
Ready: true,
},
{
Name: "tilt-synclet",
ContainerID: "docker://tilt-testsynclet",
// can't use the constants in synclet because that would create a dep cycle
Image: "gcr.io/windmill-public-containers/tilt-synclet:latest",
Ready: true,
},
},
}
}
func FakePodSpec(image reference.NamedTagged) v1.PodSpec {
return v1.PodSpec{
Containers: []v1.Container{
{
Name: "main",
Image: image.String(),
Ports: []v1.ContainerPort{
{
ContainerPort: 8080,
},
},
},
{
Name: "tilt-synclet",
Image: "gcr.io/windmill-public-containers/tilt-synclet:latest",
},
},
}
}
func (c *FakeK8sClient) applyWasCalled() bool {
return c.Yaml != ""
}
func (c *FakeK8sClient) ForwardPort(ctx context.Context, namespace Namespace, podID PodID, optionalLocalPort, remotePort int) (int, func(), error) {
c.LastForwardPortPodID = podID
c.LastForwardPortRemotePort = remotePort
return optionalLocalPort, func() {}, nil
}
func (c *FakeK8sClient) ContainerRuntime(ctx context.Context) container.Runtime {
if c.Runtime != "" {
return c.Runtime
}
return container.RuntimeDocker
}
func (c *FakeK8sClient) Exec(ctx context.Context, podID PodID, cName container.Name, n Namespace, cmd []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
return nil
}
type BufferCloser struct {
*bytes.Buffer
}
func (b BufferCloser) Close() error {
return nil
}
var _ io.ReadCloser = BufferCloser{}