/
kube_lock.go
69 lines (57 loc) · 1.66 KB
/
kube_lock.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 kubelock
import (
"github.com/giantswarm/microerror"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
)
type Config struct {
DynClient dynamic.Interface
// GVR defines a resource that the lock will be
// created on with use of the dynamic Kubernetes client. This object
// will be passed directly in the dynamic client API calls.
//
// E.g. for Namespace resource it can be instantiated like:
//
// schema.GroupVersionResource{
// Group: "",
// Version: "v1",
// Resource: "namespaces",
// }
//
// E.g. for CR defined by a CRD defined in
// https://github.com/giantswarm/apiextensions/ repository it can be
// instantiated like:
//
// schema.GroupVersionResource{
// Group: v1alpha1.NewAWSConfigCRD().Spec.Group,
// Version: v1alpha1.NewAWSConfigCRD().Spec.Version,
// Resource: v1alpha1.NewAWSConfigCRD().Spec.Names.Plural,
// }
GVR schema.GroupVersionResource
}
type KubeLock struct {
dynClient dynamic.Interface
gvr schema.GroupVersionResource
}
func New(config Config) (*KubeLock, error) {
if config.DynClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.DynClient must not be empty", config)
}
if config.GVR.Version == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.GVR.Version must not be empty", config)
}
if config.GVR.Resource == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.GVR.Resource must not be empty", config)
}
k := &KubeLock{
dynClient: config.DynClient,
gvr: config.GVR,
}
return k, nil
}
func (k *KubeLock) Lock(name string) NamespaceableLock {
return &namespaceableLock{
resource: k.dynClient.Resource(k.gvr),
lockName: name,
}
}