-
Notifications
You must be signed in to change notification settings - Fork 286
/
kubernetes.go
79 lines (64 loc) · 2.53 KB
/
kubernetes.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
package clientutil
import (
"context"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/aws/eks-anywhere/pkg/clients/kubernetes"
"github.com/aws/eks-anywhere/pkg/utils/ptr"
)
// Kubeclient implements kubernetes.Client interface using a
// client.Client as the underlying implementation.
type KubeClient struct {
client client.Client
}
func NewKubeClient(client client.Client) *KubeClient {
return &KubeClient{
client: client,
}
}
// Get retrieves an obj for the given name and namespace from the Kubernetes Cluster.
func (c *KubeClient) Get(ctx context.Context, name, namespace string, obj kubernetes.Object) error {
return c.client.Get(ctx, client.ObjectKey{Name: name, Namespace: namespace}, obj)
}
// List retrieves list of objects. On a successful call, Items field
// in the list will be populated with the result returned from the server.
func (c *KubeClient) List(ctx context.Context, list kubernetes.ObjectList) error {
return c.client.List(ctx, list)
}
// Create saves the object obj in the Kubernetes cluster.
func (c *KubeClient) Create(ctx context.Context, obj kubernetes.Object) error {
return c.client.Create(ctx, obj)
}
// Update updates the given obj in the Kubernetes cluster.
func (c *KubeClient) Update(ctx context.Context, obj kubernetes.Object) error {
return c.client.Update(ctx, obj)
}
// ApplyServerSide creates or patches and object using server side logic.
func (c *KubeClient) ApplyServerSide(ctx context.Context, fieldManager string, obj kubernetes.Object, opts ...kubernetes.ApplyServerSideOption) error {
o := &kubernetes.ApplyServerSideOptions{}
for _, opt := range opts {
opt.ApplyToApplyServerSide(o)
}
patchOpts := &client.PatchOptions{
FieldManager: fieldManager,
}
if o.ForceOwnership {
patchOpts.Force = ptr.Bool(true)
}
return c.client.Patch(ctx, obj, client.Apply, patchOpts)
}
// Delete deletes the given obj from Kubernetes cluster.
func (c *KubeClient) Delete(ctx context.Context, obj kubernetes.Object) error {
return c.client.Delete(ctx, obj)
}
// DeleteAllOf deletes all objects of the given type matching the given options.
func (c *KubeClient) DeleteAllOf(ctx context.Context, obj kubernetes.Object, opts ...kubernetes.DeleteAllOfOption) error {
o := &kubernetes.DeleteAllOfOptions{}
for _, opt := range opts {
opt.ApplyToDeleteAllOf(o)
}
clientOptions := &client.DeleteAllOfOptions{}
clientOptions.LabelSelector = labels.SelectorFromValidatedSet(o.HasLabels)
clientOptions.Namespace = o.Namespace
return c.client.DeleteAllOf(ctx, obj, clientOptions)
}