forked from goharbor/harbor-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
90 lines (65 loc) · 2.32 KB
/
context.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
package test
import (
"context"
"github.com/plotly/harbor-operator/pkg/factories/application"
"github.com/plotly/harbor-operator/pkg/factories/logger"
"github.com/plotly/harbor-operator/pkg/scheme"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
var k8sClientKey = true
func GetClient(ctx context.Context) client.Client {
return ctx.Value(&k8sClientKey).(client.Client)
}
func WithClient(ctx context.Context, c client.Client) context.Context {
return context.WithValue(ctx, &k8sClientKey, c)
}
var k8sSchemeKey = true
func GetScheme(ctx context.Context) *runtime.Scheme {
return ctx.Value(&k8sSchemeKey).(*runtime.Scheme)
}
func WithScheme(ctx context.Context, s *runtime.Scheme) context.Context {
return context.WithValue(ctx, &k8sSchemeKey, s)
}
var environmentKey = true
func GetEnvironment(ctx context.Context) *envtest.Environment {
return ctx.Value(&environmentKey).(*envtest.Environment)
}
func WithEnvironment(ctx context.Context, env *envtest.Environment) context.Context {
return context.WithValue(ctx, &environmentKey, env)
}
var managerKey = true
func GetManager(ctx context.Context) manager.Manager {
return ctx.Value(&managerKey).(manager.Manager)
}
func WithManager(ctx context.Context, mgr manager.Manager) context.Context {
return context.WithValue(ctx, &managerKey, mgr)
}
var restConfigKey = true
func GetRestConfig(ctx context.Context) *rest.Config {
return ctx.Value(&restConfigKey).(*rest.Config)
}
func WithRestConfig(ctx context.Context, cfg *rest.Config) context.Context {
return context.WithValue(ctx, &restConfigKey, cfg)
}
func NewContext() context.Context {
log := zap.New(zap.WriteTo(ginkgo.GinkgoWriter), zap.UseDevMode(true))
logf.SetLogger(log)
ctx := logger.Context(log)
application.SetName(&ctx, NewName("app"))
application.SetVersion(&ctx, NewName("version"))
application.SetGitCommit(&ctx, NewName("commit"))
s, err := scheme.New(ctx)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
ctx = WithScheme(ctx, s)
testEnv := &envtest.Environment{}
ctx = WithEnvironment(ctx, testEnv)
return ctx
}