/
aks.go
76 lines (58 loc) · 2.85 KB
/
aks.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
// Package aks is a wrapper for the connection to the Azure Kubernetes API
package aks
import (
"log"
aibv1 "github.com/Azure/aad-pod-identity/pkg/apis/aadpodidentity"
"github.com/probr/probr-sdk/providers/kubernetes/connection"
)
// AKS implements the Azure Kubernetes Service wrapper
type AKS struct {
conn connection.Connection
}
// NewAKS creates a new AKS instance taking a Connection instance as argument.
func NewAKS(connection connection.Connection) *AKS {
aks := &AKS{}
aks.conn = connection
// Guard clause: Check valid connection instance
if connection == nil {
log.Fatal("Connection instance cannot be nil")
}
return aks
}
// CreateAIB creates an AzureIdentityBinding in the cluster, 409 error if it already exists
func (aks *AKS) CreateAIB(namespace, aibName, aiName string) (resource connection.APIResource, err error) {
aib := aibv1.AzureIdentityBinding{}
aib.TypeMeta.Kind = "AzureIdentityBinding"
aib.TypeMeta.APIVersion = "aadpodidentity.k8s.io/v1"
aib.ObjectMeta.Namespace = namespace
aib.ObjectMeta.Name = aibName
aib.Spec.AzureIdentity = aiName
aib.Spec.Selector = "aadpodidbinding"
// Copy into a runtime.Object which is required for the api request
runtimeAib := aib.DeepCopyObject()
// set the api path for the aadpodidentity package which include the azureidentitybindings custom resource definition
apiPath := "apis/aadpodidentity.k8s.io/v1"
resource, err = aks.conn.PostRawResource(apiPath, namespace, "azureidentitybindings", runtimeAib)
log.Printf("Resource %v", resource)
return
}
// GetIdentityByNameAndNamespace queries cluster and returns resource, 404 error if not found
func (aks *AKS) GetIdentityByNameAndNamespace(azureIdentityName, namespace string) (resource connection.APIResource, err error) {
// Azure Identities are implemented as K8s Custom Resource Definition
// Need to make a 'raw' call to the corresponding K8s endpoint
// The K8s api endpoint for Azure Indentity is: "apis/aadpodidentity.k8s.io/v1/azureidentities"
apiEndPoint := "apis/aadpodidentity.k8s.io/v1"
resourceType := "azureidentities"
resource, err = aks.conn.GetRawResourceByName(apiEndPoint, namespace, resourceType, azureIdentityName)
return
}
// GetIdentityBindingByNameAndNamespace queries cluster and returns resource, 404 eror if not found
func (aks *AKS) GetIdentityBindingByNameAndNamespace(azureIdentityBindingName, namespace string) (resource connection.APIResource, err error) {
// Azure Identity Bindings are implemented as K8s Custom Resource Definition
// Need to make a 'raw' call to the corresponding K8s endpoint
// The K8s api endpoint for Azure Indentity Binding is: "apis/aadpodidentity.k8s.io/v1/azureidentitybindings"
apiEndPoint := "apis/aadpodidentity.k8s.io/v1"
resourceType := "azureidentitybindings"
resource, err = aks.conn.GetRawResourceByName(apiEndPoint, namespace, resourceType, azureIdentityBindingName)
return
}