-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_actions.go
107 lines (96 loc) · 2.61 KB
/
custom_actions.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package actions
import (
"context"
"github.com/andytechcastro/swiss-knife/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
)
// Custom struct for custom actions
type Custom struct {
client dynamic.Interface
CurrentNamespace string
GroupVersionResource schema.GroupVersionResource
}
// NewCustomActions return a custom actions
func NewCustomActions(client dynamic.Interface) *Custom {
return &Custom{
client: client,
}
}
// Namespace set namespace for custom actions
func (c *Custom) Namespace(namespace string) *Custom {
c.CurrentNamespace = namespace
return c
}
// SetGroupVersionResource set GroupVersionResource for this actions
func (c *Custom) SetGroupVersionResource(resource schema.GroupVersionResource) *Custom {
c.GroupVersionResource = resource
return c
}
// Get get custom resource
func (c *Custom) Get(name string) (*unstructured.Unstructured, error) {
if name == "" {
return nil, errors.GetEmptyError("Name")
}
custom, err := c.client.Resource(c.GroupVersionResource).Namespace(c.CurrentNamespace).Get(
context.TODO(),
name,
metav1.GetOptions{},
)
if err != nil {
return nil, err
}
return custom, nil
}
// Create create a custom resource
func (c *Custom) Create(resource *unstructured.Unstructured) error {
if resource == nil {
return errors.GetEmptyError("Resource")
}
_, err := c.client.Resource(c.GroupVersionResource).Namespace(c.CurrentNamespace).Create(
context.TODO(),
resource,
metav1.CreateOptions{},
)
return err
}
// Update update a custome resource
func (c *Custom) Update(resource *unstructured.Unstructured) error {
if resource == nil {
return errors.GetEmptyError("Resource")
}
_, err := c.client.Resource(c.GroupVersionResource).Namespace(c.CurrentNamespace).Update(
context.TODO(),
resource,
metav1.UpdateOptions{},
)
return err
}
// Delete delete a custom resource
func (c *Custom) Delete(name string) error {
if name == "" {
return errors.GetEmptyError("Name")
}
deletePolicy := metav1.DeletePropagationForeground
err := c.client.Resource(c.GroupVersionResource).Namespace(c.CurrentNamespace).Delete(
context.TODO(),
name,
metav1.DeleteOptions{
PropagationPolicy: &deletePolicy,
},
)
return err
}
// List get list of custom resources
func (c *Custom) List() (*unstructured.UnstructuredList, error) {
customList, err := c.client.Resource(c.GroupVersionResource).Namespace(c.CurrentNamespace).List(
context.TODO(),
metav1.ListOptions{},
)
if err != nil {
return nil, err
}
return customList, nil
}