/
serviceaccount.generated.go
124 lines (98 loc) · 3.59 KB
/
serviceaccount.generated.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package kubernetes
// Code generated by stub-gen; DO NOT EDIT.
import (
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/rosskirkpat/revvy/pkg/client"
)
// ServiceAccount wraps a Kubernetes ServiceAccount.
type ServiceAccount struct {
corev1.ServiceAccount
client client.Client
}
// NewServiceAccount creates a ServiceAccount from its Kubernetes ServiceAccount.
func NewServiceAccount(client client.Client, serviceaccount corev1.ServiceAccount) (ServiceAccount, error) {
createdServiceAccount, err := client.Kubernetes.
CoreV1().
ServiceAccounts(serviceaccount.Namespace).
Create(client.Ctx, &serviceaccount, metav1.CreateOptions{})
if err != nil {
return ServiceAccount{}, fmt.Errorf("failed to create serviceaccount %s in namespace %s: %w", serviceaccount.Name, serviceaccount.Namespace, err)
}
return ServiceAccount{
ServiceAccount: *createdServiceAccount,
client: client,
}, nil
}
// GetServiceAccount gets a serviceaccount in a namespace.
func GetServiceAccount(client client.Client, name string, namespace string) (ServiceAccount, error) {
options := metav1.GetOptions{}
serviceaccount, err := client.Kubernetes.
CoreV1().
ServiceAccounts(namespace).
Get(client.Ctx, name, options)
if err != nil {
return ServiceAccount{}, fmt.Errorf("failed to get serviceaccount %s in namespace %s: %w", name, namespace, err)
}
return ServiceAccount{
ServiceAccount: *serviceaccount,
client: client,
}, nil
}
// ListServiceAccounts lists all serviceaccounts in a namespace.
func ListServiceAccounts(client client.Client, namespace string) ([]ServiceAccount, error) {
options := metav1.ListOptions{}
list, err := client.Kubernetes.
CoreV1().
ServiceAccounts(namespace).
List(client.Ctx, options)
if err != nil {
return nil, fmt.Errorf("failed to list serviceaccounts in namespace %s: %w", namespace, err)
}
serviceaccounts := make([]ServiceAccount, 0, len(list.Items))
for _, item := range list.Items {
serviceaccounts = append(serviceaccounts, ServiceAccount{
ServiceAccount: item,
client: client,
})
}
return serviceaccounts, nil
}
// Delete deletes a ServiceAccount from the Kubernetes cluster.
func (serviceaccount ServiceAccount) Delete() error {
options := metav1.DeleteOptions{}
err := serviceaccount.client.Kubernetes.
CoreV1().
ServiceAccounts(serviceaccount.Namespace).
Delete(serviceaccount.client.Ctx, serviceaccount.Name, options)
if err != nil {
return fmt.Errorf("failed to delete serviceaccount %s in namespace %s: %w", serviceaccount.Name, serviceaccount.Namespace, err)
}
return nil
}
// Update gets the current ServiceAccount status.
func (serviceaccount *ServiceAccount) Update() error {
options := metav1.GetOptions{}
update, err := serviceaccount.client.Kubernetes.
CoreV1().
ServiceAccounts(serviceaccount.Namespace).
Get(serviceaccount.client.Ctx, serviceaccount.Name, options)
if err != nil {
return fmt.Errorf("failed to update serviceaccount %s in namespace %s: %w", serviceaccount.Name, serviceaccount.Namespace, err)
}
serviceaccount.ServiceAccount = *update
return nil
}
// Save saves the current ServiceAccount.
func (serviceaccount *ServiceAccount) Save() error {
update, err := serviceaccount.client.Kubernetes.
CoreV1().
ServiceAccounts(serviceaccount.Namespace).
Update(serviceaccount.client.Ctx, &serviceaccount.ServiceAccount, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("failed to save serviceaccount %s in namespace %s: %w", serviceaccount.Name, serviceaccount.Namespace, err)
}
serviceaccount.ServiceAccount = *update
return nil
}