/
service.go
143 lines (116 loc) · 3.19 KB
/
service.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
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package discovery
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"sync"
"time"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1apply "k8s.io/client-go/applyconfigurations/core/v1"
)
type FakeService interface {
Run(ctx context.Context) error
Port() *int32
Name() string
}
// Creates and registers an in-process Service capable of communicating with the
// kubernetes integration test apiserver
type fakeService struct {
name string
client kubernetes.Interface
handler http.Handler
lock sync.RWMutex
activePort *int32
}
func NewFakeService(name string, client kubernetes.Interface, handler http.Handler) *fakeService {
return &fakeService{
name: name,
client: client,
handler: handler,
}
}
func (f *fakeService) Run(ctx context.Context) error {
aggregatedServer := httptest.NewUnstartedServer(f.handler)
aggregatedServer.StartTLS()
defer aggregatedServer.Close()
serverURL, err := url.Parse(aggregatedServer.URL)
if err != nil {
// This should never occur
panic(err)
}
serverPort, err := strconv.Atoi(serverURL.Port())
if err != nil {
// This should never occur
panic(err)
}
port := int32(serverPort)
// Install service into the cluster
service, err := f.client.CoreV1().Services("default").Apply(
ctx,
corev1apply.Service(f.name, "default").
WithSpec(corev1apply.ServiceSpec().
WithPorts(
corev1apply.ServicePort().
WithPort(port)).
WithType("ExternalName").
WithExternalName("localhost")),
metav1.ApplyOptions{
FieldManager: "test-manager",
},
)
if err != nil {
return err
}
f.lock.Lock()
f.activePort = &port
f.lock.Unlock()
<-ctx.Done()
f.lock.Lock()
f.activePort = nil
f.lock.Unlock()
// Uninstall service from the cluser
err = f.client.CoreV1().Services("default").Delete(ctx, service.Name, metav1.DeleteOptions{})
if errors.Is(err, context.Canceled) {
err = nil
}
return err
}
func (f *fakeService) WaitForReady(ctx context.Context) error {
err := wait.PollUntilContextTimeout(ctx, 200*time.Millisecond, time.Second, false, func(ctx context.Context) (done bool, err error) {
return f.Port() != nil, nil
})
if errors.Is(err, context.Canceled) {
err = nil
} else if err != nil {
err = fmt.Errorf("service should have come alive in a reasonable amount of time: %w", err)
}
return err
}
func (f *fakeService) Port() *int32 {
// Returns the port of the server if it is running or nil
// if it is not running
f.lock.RLock()
defer f.lock.RUnlock()
return f.activePort
}
func (f *fakeService) Name() string {
return f.name
}