forked from crossplane/crossplane
/
aks.go
203 lines (165 loc) · 7.3 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
Copyright 2018 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package azure
import (
"context"
"fmt"
"strings"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2018-03-31/containerservice"
"github.com/Azure/go-autorest/autorest/to"
computev1alpha1 "github.com/crossplaneio/crossplane/pkg/apis/azure/compute/v1alpha1"
"github.com/crossplaneio/crossplane/pkg/apis/azure/v1alpha1"
"k8s.io/client-go/kubernetes"
)
const (
// AgentPoolProfileNameFmt is a format string for the name of the automatically created cluster agent pool profile
AgentPoolProfileNameFmt = "%s-nodepool"
maxClusterNameLen = 31
)
//---------------------------------------------------------------------------------------------------------------------
// AKS Setup API interfaces, clients, factories
// AKSSetupClient is a type that implements all of the AKS setup interface
type AKSSetupClient struct {
AKSClusterAPI
ApplicationAPI
ServicePrincipalAPI
}
// AKSSetupAPIFactory is an interface that can create instances of the AKSSetupClient
type AKSSetupAPIFactory interface {
CreateSetupClient(*v1alpha1.Provider, kubernetes.Interface) (*AKSSetupClient, error)
}
// AKSSetupClientFactory implements the AKSSetupAPIFactory interface by returning real clients that talk to Azure APIs
type AKSSetupClientFactory struct {
}
// CreateSetupClient creates and returns an AKS setup client that is ready to talk to Azure APIs
func (f *AKSSetupClientFactory) CreateSetupClient(provider *v1alpha1.Provider, clientset kubernetes.Interface) (*AKSSetupClient, error) {
aksClusterClient, err := NewAKSClusterClient(provider, clientset)
if err != nil {
return nil, err
}
appClient, err := NewApplicationClient(provider, clientset)
if err != nil {
return nil, err
}
spClient, err := NewServicePrincipalClient(provider, clientset)
if err != nil {
return nil, err
}
return &AKSSetupClient{
AKSClusterAPI: aksClusterClient,
ApplicationAPI: appClient,
ServicePrincipalAPI: spClient,
}, nil
}
//---------------------------------------------------------------------------------------------------------------------
// AKS Cluster API interfaces and clients
// AKSClusterAPI represents the API interface for a AKS Cluster client
type AKSClusterAPI interface {
Get(ctx context.Context, instance computev1alpha1.AKSCluster) (containerservice.ManagedCluster, error)
CreateOrUpdateBegin(ctx context.Context, instance computev1alpha1.AKSCluster, clusterName, appID, spSecret string) ([]byte, error)
CreateOrUpdateEnd(op []byte) (bool, error)
Delete(ctx context.Context, instance computev1alpha1.AKSCluster) (containerservice.ManagedClustersDeleteFuture, error)
ListClusterAdminCredentials(ctx context.Context, instance computev1alpha1.AKSCluster) (containerservice.CredentialResults, error)
}
// AKSClusterClient is the concreate implementation of the AKSClusterAPI interface that calls Azure API.
type AKSClusterClient struct {
containerservice.ManagedClustersClient
}
// NewAKSClusterClient creates and initializes a AKSClusterClient instance.
func NewAKSClusterClient(provider *v1alpha1.Provider, clientset kubernetes.Interface) (*AKSClusterClient, error) {
client, err := NewClient(provider, clientset)
if err != nil {
return nil, fmt.Errorf("failed to create Azure client: %+v", err)
}
aksClustersClient := containerservice.NewManagedClustersClient(client.SubscriptionID)
aksClustersClient.Authorizer = client.Authorizer
aksClustersClient.AddToUserAgent(UserAgent)
return &AKSClusterClient{aksClustersClient}, nil
}
// Get returns the AKS cluster details for the given instance
func (c *AKSClusterClient) Get(ctx context.Context, instance computev1alpha1.AKSCluster) (containerservice.ManagedCluster, error) {
return c.ManagedClustersClient.Get(ctx, instance.Spec.ResourceGroupName, instance.Status.ClusterName)
}
// CreateOrUpdateBegin begins the create/update operation for a AKS Cluster with the given properties
func (c *AKSClusterClient) CreateOrUpdateBegin(ctx context.Context, instance computev1alpha1.AKSCluster, clusterName, appID, spSecret string) ([]byte, error) {
spec := instance.Spec
agentPoolProfileName := fmt.Sprintf(AgentPoolProfileNameFmt, clusterName)
enableRBAC := !spec.DisableRBAC
nodeCount := int32(computev1alpha1.DefaultNodeCount)
if spec.NodeCount != nil {
nodeCount = int32(*spec.NodeCount)
}
createParams := containerservice.ManagedCluster{
Name: &clusterName,
Location: &spec.Location,
ManagedClusterProperties: &containerservice.ManagedClusterProperties{
KubernetesVersion: &spec.Version,
DNSPrefix: &spec.DNSNamePrefix,
AgentPoolProfiles: &[]containerservice.ManagedClusterAgentPoolProfile{
{
Name: &agentPoolProfileName,
Count: &nodeCount,
VMSize: containerservice.VMSizeTypes(spec.NodeVMSize),
},
},
ServicePrincipalProfile: &containerservice.ManagedClusterServicePrincipalProfile{
ClientID: to.StringPtr(appID),
Secret: to.StringPtr(spSecret),
},
EnableRBAC: &enableRBAC,
},
}
createFuture, err := c.CreateOrUpdate(ctx, instance.Spec.ResourceGroupName, clusterName, createParams)
if err != nil {
return nil, err
}
// serialize the create operation
createFutureJSON, err := createFuture.MarshalJSON()
if err != nil {
return nil, err
}
return createFutureJSON, nil
}
// CreateOrUpdateEnd checks to see if the given create/update operation is completed and if any error has occurred.
func (c *AKSClusterClient) CreateOrUpdateEnd(op []byte) (done bool, err error) {
// unmarshal the given create complete data into a future object
future := &containerservice.ManagedClustersCreateOrUpdateFuture{}
if err = future.UnmarshalJSON(op); err != nil {
return false, err
}
// check if the operation is done yet
done, err = future.Done(c.Client)
if !done {
return false, err
}
// check the result of the completed operation
if _, err = future.Result(c.ManagedClustersClient); err != nil {
return true, err
}
return true, nil
}
// Delete begins the deletion operator for the given AKS cluster instance
func (c *AKSClusterClient) Delete(ctx context.Context, instance computev1alpha1.AKSCluster) (containerservice.ManagedClustersDeleteFuture, error) {
return c.ManagedClustersClient.Delete(ctx, instance.Spec.ResourceGroupName, instance.Status.ClusterName)
}
// ListClusterAdminCredentials will return the admin credentials used to connect to the given AKS cluster
func (c *AKSClusterClient) ListClusterAdminCredentials(ctx context.Context, instance computev1alpha1.AKSCluster) (containerservice.CredentialResults, error) {
return c.ManagedClustersClient.ListClusterAdminCredentials(ctx, instance.Spec.ResourceGroupName, instance.Status.ClusterName)
}
// SanitizeClusterName sanitizes the given AKS cluster name
func SanitizeClusterName(name string) string {
if len(name) > maxClusterNameLen {
name = name[:maxClusterNameLen]
}
return strings.TrimSuffix(name, "-")
}