forked from getsentry/sentry-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
31 lines (25 loc) · 965 Bytes
/
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
package main
import (
"context"
"fmt"
"k8s.io/client-go/kubernetes"
)
type clientsetCtxKey struct{}
// Note: we do not use kubernetes.Clientset type directly, to enable mocking via "k8s.io/client-go/kubernetes/fake"
// package. A type alias is introduced to avoid confusion: "kubernetes.Interface" is actually a clientset.Interface, and
// not a generic interface for Kubernetes resources.
type ClientsetInterface = kubernetes.Interface
func setClientsetOnContext(ctx context.Context, clientset ClientsetInterface) context.Context {
return context.WithValue(ctx, clientsetCtxKey{}, clientset)
}
func getClientsetFromContext(ctx context.Context) (ClientsetInterface, error) {
val := ctx.Value(clientsetCtxKey{})
if val == nil {
return nil, fmt.Errorf("no clientset present on context")
}
if clientset, ok := val.(ClientsetInterface); ok {
return clientset, nil
} else {
return nil, fmt.Errorf("cannot convert clientset value from context")
}
}