-
Notifications
You must be signed in to change notification settings - Fork 91
/
context.go
69 lines (59 loc) 路 1.4 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
package client
import (
"context"
"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/rest"
)
type (
contextKey int
)
const (
clientContextKey contextKey = iota
configContextKey
rawConfigContextKey
)
func SetupContext(ctx context.Context, config *rest.Config, rawConfig *api.Config) (context.Context, error) {
if pkgClient, err := New(config); err != nil {
return nil, err
} else {
return SetupContextWithClient(ctx, config, rawConfig, pkgClient), nil
}
}
func SetupContextWithClient(
ctx context.Context,
config *rest.Config,
rawConfig *api.Config,
client PackageV1Alpha1Client,
) context.Context {
ctx = context.WithValue(ctx, clientContextKey, client)
ctx = context.WithValue(ctx, configContextKey, config)
ctx = context.WithValue(ctx, rawConfigContextKey, rawConfig)
return ctx
}
func FromContext(ctx context.Context) PackageV1Alpha1Client {
value := ctx.Value(clientContextKey)
if value != nil {
if client, ok := value.(PackageV1Alpha1Client); ok {
return client
}
}
return nil
}
func ConfigFromContext(ctx context.Context) *rest.Config {
value := ctx.Value(configContextKey)
if value != nil {
if config, ok := value.(*rest.Config); ok {
return config
}
}
return nil
}
func RawConfigFromContext(ctx context.Context) *api.Config {
value := ctx.Value(rawConfigContextKey)
if value != nil {
if config, ok := value.(*api.Config); ok {
return config
}
}
return nil
}