-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
namespace.go
225 lines (195 loc) · 6.9 KB
/
namespace.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
package itest
import (
"bytes"
"context"
"fmt"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/datawire/dlib/dlog"
"github.com/telepresenceio/telepresence/v2/pkg/dos"
)
type NamespacePair interface {
Harness
ApplyApp(ctx context.Context, name, workload string)
ApplyEchoService(ctx context.Context, name string, port int)
ApplyTemplate(ctx context.Context, path string, values any)
DeleteTemplate(ctx context.Context, path string, values any)
AppNamespace() string
TelepresenceConnect(ctx context.Context, args ...string) string
DeleteSvcAndWorkload(ctx context.Context, workload, name string)
Kubectl(ctx context.Context, args ...string) error
KubectlOk(ctx context.Context, args ...string) string
KubectlOut(ctx context.Context, args ...string) (string, error)
ManagerNamespace() string
RollbackTM(ctx context.Context)
RolloutStatusWait(ctx context.Context, workload string) error
}
type Namespaces struct {
Namespace string `json:"namespace,omitempty"`
ManagedNamespaces []string `json:"managedNamespaces,omitempty"`
}
func (n *Namespaces) HelmString() string {
var sb strings.Builder
sb.WriteByte('{')
sb.WriteString(n.Namespace)
for _, m := range n.ManagedNamespaces {
if m != n.Namespace {
sb.WriteByte(',')
sb.WriteString(m)
}
}
sb.WriteByte('}')
return sb.String()
}
func (n *Namespaces) UniqueList() []string {
ul := make([]string, 0, len(n.ManagedNamespaces)+1)
ul = append(ul, n.Namespace)
for _, m := range n.ManagedNamespaces {
if m != n.Namespace {
ul = append(ul, m)
}
}
return ul
}
type namespacesContextKey struct{}
func WithNamespaces(ctx context.Context, namespaces *Namespaces) context.Context {
return context.WithValue(ctx, namespacesContextKey{}, namespaces)
}
func GetNamespaces(ctx context.Context) *Namespaces {
if namespaces, ok := ctx.Value(namespacesContextKey{}).(*Namespaces); ok {
return namespaces
}
return nil
}
// The namespaceSuite has no tests. It's sole purpose is to create and destroy the namespaces and
// any non-namespaced resources that we, ourselves, make nsPair specific, such as the
// mutating webhook configuration for the traffic-agent injection.
type nsPair struct {
Harness
Namespaces
}
// TelepresenceConnect connects using the AppNamespace and ManagerNamespace.
func (s *nsPair) TelepresenceConnect(ctx context.Context, args ...string) string {
return TelepresenceOk(ctx,
append(
[]string{"connect", "--namespace", s.AppNamespace(), "--manager-namespace", s.ManagerNamespace()},
args...)...)
}
func WithNamespacePair(ctx context.Context, suffix string, f func(NamespacePair)) {
s := &nsPair{}
var namespace string
namespace, s.Namespace = AppAndMgrNSName(suffix)
s.ManagedNamespaces = []string{namespace}
getT(ctx).Run(fmt.Sprintf("Test_Namespaces_%s", suffix), func(t *testing.T) {
ctx = WithT(ctx, t)
ctx = WithUser(ctx, s.Namespace+":"+TestUser)
ctx = WithNamespaces(ctx, &s.Namespaces)
s.Harness = NewContextHarness(ctx)
s.PushHarness(ctx, s.setup, s.tearDown)
defer s.PopHarness()
f(s)
})
}
const purposeLabel = "tp-cli-testing"
func (s *nsPair) setup(ctx context.Context) bool {
CreateNamespaces(ctx, s.AppNamespace(), s.Namespace)
t := getT(ctx)
if t.Failed() {
return false
}
err := Kubectl(ctx, s.Namespace, "apply", "-f", filepath.Join(GetOSSRoot(ctx), "testdata", "k8s", "client_sa.yaml"))
assert.NoError(t, err, "failed to create connect ServiceAccount")
return !t.Failed()
}
func AppAndMgrNSName(suffix string) (appNS, mgrNS string) {
mgrNS = fmt.Sprintf("ambassador-%s", suffix)
appNS = fmt.Sprintf("telepresence-%s", suffix)
return
}
func (s *nsPair) tearDown(ctx context.Context) {
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
DeleteNamespaces(ctx, s.AppNamespace(), s.Namespace)
}()
wg.Add(1)
go func() {
defer wg.Done()
_ = Kubectl(ctx, "", "delete", "--wait=false", "mutatingwebhookconfiguration", "agent-injector-webhook-"+s.Namespace)
}()
wg.Wait()
}
func (s *nsPair) RollbackTM(ctx context.Context) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
err := Command(ctx, "helm", "rollback", "--no-hooks", "--wait", "--namespace", s.ManagerNamespace(), "traffic-manager").Run()
t := getT(ctx)
require.NoError(t, err)
require.NoError(t, RolloutStatusWait(ctx, s.Namespace, "deploy/traffic-manager"))
s.CapturePodLogs(ctx, "traffic-manager", "", s.Namespace)
}
func (s *nsPair) AppNamespace() string {
return s.ManagedNamespaces[0]
}
func (s *nsPair) ManagerNamespace() string {
return s.Namespace
}
func (s *nsPair) ApplyEchoService(ctx context.Context, name string, port int) {
getT(ctx).Helper()
ApplyEchoService(ctx, name, s.AppNamespace(), port)
}
// ApplyApp calls kubectl apply -n <namespace> -f on the given app + .yaml found in testdata/k8s relative
// to the directory returned by GetCurrentDirectory.
func (s *nsPair) ApplyApp(ctx context.Context, name, workload string) {
getT(ctx).Helper()
ApplyApp(ctx, name, s.AppNamespace(), workload)
}
func (s *nsPair) RolloutStatusWait(ctx context.Context, workload string) error {
return RolloutStatusWait(ctx, s.AppNamespace(), workload)
}
func (s *nsPair) DeleteSvcAndWorkload(ctx context.Context, workload, name string) {
getT(ctx).Helper()
DeleteSvcAndWorkload(ctx, workload, name, s.AppNamespace())
}
func (s *nsPair) ApplyTemplate(ctx context.Context, path string, values any) {
s.doWithTemplate(ctx, "apply", path, values)
}
func (s *nsPair) DeleteTemplate(ctx context.Context, path string, values any) {
yml, err := ReadTemplate(ctx, path, values)
require.NoError(getT(ctx), err)
if err = s.Kubectl(dos.WithStdin(ctx, bytes.NewReader(yml)), "apply", "-f", "-"); err != nil {
dlog.Errorf(ctx, "unable to apply %q", string(yml))
getT(ctx).Fatal(err)
}
}
func (s *nsPair) doWithTemplate(ctx context.Context, action, path string, values any) {
yml, err := ReadTemplate(ctx, path, values)
require.NoError(getT(ctx), err)
if err = s.Kubectl(dos.WithStdin(ctx, bytes.NewReader(yml)), action, "-f", "-"); err != nil {
dlog.Errorf(ctx, "unable to %s %q", action, string(yml))
getT(ctx).Fatal(err)
}
}
// Kubectl runs kubectl with the default context and the application namespace.
func (s *nsPair) Kubectl(ctx context.Context, args ...string) error {
getT(ctx).Helper()
return Kubectl(ctx, s.AppNamespace(), args...)
}
// KubectlOk runs kubectl with the default context and the application namespace and returns its combined output
// and fails if an error occurred.
func (s *nsPair) KubectlOk(ctx context.Context, args ...string) string {
out, err := KubectlOut(ctx, s.AppNamespace(), args...)
require.NoError(getT(ctx), err)
return out
}
// KubectlOut runs kubectl with the default context and the application namespace and returns its combined output.
func (s *nsPair) KubectlOut(ctx context.Context, args ...string) (string, error) {
getT(ctx).Helper()
return KubectlOut(ctx, s.AppNamespace(), args...)
}