/
config.go
61 lines (48 loc) · 1.67 KB
/
config.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
package kubeconfig
import (
"context"
"errors"
"k8s.io/client-go/rest"
)
var (
// ErrContextValueMissing is an error that's returned
// when trying to fetch a Config from a context that doesn't have one.
ErrContextValueMissing = errors.New("context does not contain the kubeconfig value")
)
type ctxKey struct{}
// NamespacedConfig is the configuration for a Kubernetes client.
// It contains a rest.Config, as well as the Kubernetes namespace to use when interacting with resources.
type NamespacedConfig struct {
// CRC32 is used for equality checks.
CRC32 uint32
// RestConfig contains the Kubernetes rest.Config for creating API clients.
RestConfig rest.Config
// Namespace contains the namespace in which the resources should be stored and retrieved from.
Namespace string
}
// Equals returns true if Config is equal to other.
func (c NamespacedConfig) Equals(other NamespacedConfig) bool {
return c.CRC32 == other.CRC32
}
// WithContext returns a new Context that contains Config inside.
func WithContext(ctx context.Context, val NamespacedConfig) context.Context {
return context.WithValue(ctx, ctxKey{}, val)
}
// FromContext extracts a Config from provided Context.
// If the Config is missing an error will be returned.
func FromContext(ctx context.Context) (NamespacedConfig, error) {
val, ok := ctx.Value(ctxKey{}).(NamespacedConfig)
if !ok {
return NamespacedConfig{}, ErrContextValueMissing
}
return val, nil
}
// MustFromContext extracts a Config from provided Context.
// If the Config is missing the call will panic.
func MustFromContext(ctx context.Context) NamespacedConfig {
val, err := FromContext(ctx)
if err != nil {
panic(err)
}
return val
}