-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_account_action.go
108 lines (98 loc) · 2.51 KB
/
service_account_action.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
108
package actions
import (
"context"
"github.com/andytechcastro/swiss-knife/errors"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
coreInterface "k8s.io/client-go/kubernetes/typed/core/v1"
)
// ServiceAccount for manager namespace actions
type ServiceAccount struct {
client coreInterface.CoreV1Interface
CurrentNamespace string
}
// NewServiceAccountAction get ServiceAccount action
func NewServiceAccountAction(client coreInterface.CoreV1Interface) *ServiceAccount {
return &ServiceAccount{
client: client,
CurrentNamespace: "",
}
}
// Namespace set namespace
func (sa *ServiceAccount) Namespace(namespace string) *ServiceAccount {
sa.CurrentNamespace = namespace
return sa
}
// Get get namespace
func (sa *ServiceAccount) Get(saName string) (*apiv1.ServiceAccount, error) {
if saName == "" {
return nil, errors.GetEmptyError("Name")
}
serviceAccount, err := sa.client.ServiceAccounts(sa.CurrentNamespace).Get(
context.TODO(),
saName,
metav1.GetOptions{})
if err != nil {
return nil, err
}
return serviceAccount, nil
}
// Create Create an ServiceAccount
func (sa *ServiceAccount) Create(serviceAccount *apiv1.ServiceAccount) error {
if serviceAccount == nil {
return errors.GetEmptyError("ServiceAccount")
}
_, err := sa.client.ServiceAccounts(sa.CurrentNamespace).Create(
context.TODO(),
serviceAccount,
metav1.CreateOptions{},
)
if err != nil {
return err
}
return nil
}
// Update Update a pods in the client
func (sa *ServiceAccount) Update(serviceAccount *apiv1.ServiceAccount) error {
if serviceAccount == nil {
return errors.GetEmptyError("ServiceAccount")
}
_, err := sa.client.ServiceAccounts(sa.CurrentNamespace).Update(
context.TODO(),
serviceAccount,
metav1.UpdateOptions{},
)
if err != nil {
return err
}
return nil
}
// Delete Delete a pod in the client
func (sa *ServiceAccount) Delete(saName string) error {
if saName == "" {
return errors.GetEmptyError("Name")
}
deletePolicy := metav1.DeletePropagationForeground
err := sa.client.ServiceAccounts(sa.CurrentNamespace).Delete(
context.TODO(),
saName,
metav1.DeleteOptions{
PropagationPolicy: &deletePolicy,
},
)
if err != nil {
return err
}
return nil
}
// List List all pods in a namespace
func (sa *ServiceAccount) List() (*apiv1.ServiceAccountList, error) {
serviceAccountList, err := sa.client.ServiceAccounts(sa.CurrentNamespace).List(
context.TODO(),
metav1.ListOptions{},
)
if err != nil {
return nil, err
}
return serviceAccountList, nil
}