This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
client.go
162 lines (122 loc) · 4.49 KB
/
client.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package client
import (
"io/ioutil"
"net"
"os"
clientset "pkg/aadsync/client/clientset/versioned"
aadgroupsyncv1 "pkg/aadsync/apis/aad.microsoft.com/v1"
v1 "pkg/aadsync/client/clientset/versioned/typed/aad.microsoft.com/v1"
logrus "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
rest "k8s.io/client-go/rest"
certutil "k8s.io/client-go/util/cert"
)
// Client contains the internal AAD Group Sync Client details
type Client struct {
Log *logrus.Entry
Config *rest.Config
Client v1.AADGroupSyncInterface
Namespace string
}
// NewClient creates a new AAD Group Sync Client with default incluster configuration. You need to be running
// incluster for this to be successful
func NewClient(namespace string, log *logrus.Entry) *Client {
config, err := rest.InClusterConfig()
if err != nil {
log.Fatal(err)
}
return NewClientForConfigAndNamespace(config, namespace, log)
}
// NewClientForConfigAndNamespace creates a new AAD Group Sync Client with the specified configuration and namespace
func NewClientForConfigAndNamespace(config *rest.Config, namespace string, log *logrus.Entry) *Client {
clientset, err := clientset.NewForConfig(config)
if err != nil {
log.Fatal(err)
}
client := &Client{
Log: log,
Config: config,
Client: clientset.AadV1().AADGroupSyncs(string(namespace)),
}
log.Info("Created aad group sync client")
log.Debugf("Host: %s", client.Config.Host)
return client
}
// NewClientForLocal creates a new AAD Group Sync Client from local copies of incluster resources. This is useful
// for testing
func NewClientForLocal(namespace string, log *logrus.Entry) *Client {
// Found incluster at /var/run/secrets/kubernetes.io/serviceaccount/token
tokenFile := os.Getenv("KUBERNETES_SERVICEACCOUNT_TOKENFILE")
// Found incluster at /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
rootCAFile := os.Getenv("KUBERNETES_SERVICEACCOUNT_ROOTCAFILE")
host := os.Getenv("KUBERNETES_SERVICE_HOST")
port := os.Getenv("KUBERNETES_SERVICE_PORT")
token, err := ioutil.ReadFile(tokenFile)
if err != nil {
log.Fatal(err)
}
tlsClientConfig := rest.TLSClientConfig{}
if _, err := certutil.NewPool(rootCAFile); err != nil {
log.Fatalf("Expected to load root CA config from %s, but got err: %v", rootCAFile, err)
} else {
tlsClientConfig.CAFile = rootCAFile
}
config := &rest.Config{
Host: "https://" + net.JoinHostPort(host, port),
TLSClientConfig: tlsClientConfig,
BearerToken: string(token),
BearerTokenFile: tokenFile,
}
return NewClientForConfigAndNamespace(config, namespace, log)
}
// Get returns an existing aadgroupsyncs.aad.microsoft.com CRD
func (c *Client) Get(aadGroupName string) (*aadgroupsyncv1.AADGroupSync, error) {
c.Log.Infof("Fetching aadgroupsyncs.aad.microsoft.com: %s", aadGroupName)
aadGroup, err := c.Client.Get(aadGroupName, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
c.Log.Infof("Not found - aadgroupsyncs.aad.microsoft.com: %s", aadGroupName)
return nil, nil
}
return nil, err
}
return aadGroup, nil
}
// Create creates a new aadgroupsyncs.aad.microsoft.com CRD
func (c *Client) Create(aadGroup *aadgroupsyncv1.AADGroupSync) (*aadgroupsyncv1.AADGroupSync, error) {
c.Log.Infof("Creating aadgroupsyncs.aad.microsoft.com: %s", aadGroup.ObjectMeta.Name)
aadGroup, err := c.Client.Create(aadGroup)
if err != nil {
return nil, err
}
return aadGroup, nil
}
// Update updates an existing aadgroupsyncs.aad.microsoft.com CRD
func (c *Client) Update(aadGroup *aadgroupsyncv1.AADGroupSync) (*aadgroupsyncv1.AADGroupSync, error) {
c.Log.Infof("Updating aadgroupsyncs.aad.microsoft.com: %s", aadGroup.ObjectMeta.Name)
aadGroup, err := c.Client.Update(aadGroup)
if err != nil {
return nil, err
}
return aadGroup, nil
}
// Delete deletes an existing aadgroupsyncs.aad.microsoft.com CRD
func (c *Client) Delete(aadGroupName string) error {
c.Log.Infof("Deleting aadgroupsyncs.aad.microsoft.com: %s", aadGroupName)
deletePolicy := metav1.DeletePropagationForeground
err := c.Client.Delete(aadGroupName, &metav1.DeleteOptions{PropagationPolicy: &deletePolicy})
if err != nil {
return err
}
return nil
}
// List returns a collection of existing aadgroupsyncs.aad.microsoft.com CRDs
func (c *Client) List() ([]aadgroupsyncv1.AADGroupSync, error) {
c.Log.Infof("Fetching all aadgroupsyncs.aad.microsoft.com")
aadGroupList, err := c.Client.List(metav1.ListOptions{})
if err != nil {
return nil, err
}
return aadGroupList.Items, nil
}