This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
/
cloud.go
184 lines (157 loc) 路 6.27 KB
/
cloud.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
/*
Copyright The Pharmer 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 aks
import (
"context"
"fmt"
"pharmer.dev/cloud/pkg/credential"
api "pharmer.dev/pharmer/apis/v1alpha1"
"pharmer.dev/pharmer/cloud"
ms "github.com/Azure/azure-sdk-for-go/profiles/latest/containerservice/mgmt/containerservice"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-03-01/compute"
cs "github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2019-06-01/containerservice"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-03-01/resources"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/appscode/go/types"
"github.com/appscode/go/wait"
"github.com/pkg/errors"
)
type cloudConnector struct {
*cloud.Scope
namer namer
availabilitySetsClient compute.AvailabilitySetsClient
groupsClient resources.GroupsClient
managedClient ms.ManagedClustersClient
}
func newconnector(cm *ClusterManager) (*cloudConnector, error) {
log := cm.Logger
cluster := cm.Cluster
cred, err := cm.StoreProvider.Credentials().Get(cluster.Spec.Config.CredentialName)
if err != nil {
log.Error(err, "failed to get credential from store")
return nil, err
}
typed := credential.Azure{CommonSpec: credential.CommonSpec(cred.Spec)}
if ok, err := typed.IsValid(); !ok {
return nil, errors.Wrapf(err, "credential %s is invalid", cluster.Spec.Config.CredentialName)
}
baseURI := azure.PublicCloud.ResourceManagerEndpoint
config, err := adal.NewOAuthConfig(azure.PublicCloud.ActiveDirectoryEndpoint, typed.TenantID())
if err != nil {
log.Error(err, "failed to get Oauth config")
return nil, err
}
spt, err := adal.NewServicePrincipalToken(*config, typed.ClientID(), typed.ClientSecret(), baseURI)
if err != nil {
log.Error(err, "failed to get principal token")
return nil, err
}
client := autorest.NewClientWithUserAgent(fmt.Sprintf("Azure-SDK-for-Go/%s", compute.Version()))
client.Authorizer = autorest.NewBearerAuthorizer(spt)
availabilitySetsClient := compute.NewAvailabilitySetsClientWithBaseURI(baseURI, typed.SubscriptionID())
availabilitySetsClient.Authorizer = autorest.NewBearerAuthorizer(spt)
groupsClient := resources.NewGroupsClientWithBaseURI(baseURI, typed.SubscriptionID())
groupsClient.Authorizer = autorest.NewBearerAuthorizer(spt)
managedClient := ms.NewManagedClustersClient(typed.SubscriptionID())
managedClient.Authorizer = autorest.NewBearerAuthorizer(spt)
return &cloudConnector{
Scope: cm.Scope,
namer: cm.namer,
availabilitySetsClient: availabilitySetsClient,
groupsClient: groupsClient,
managedClient: managedClient,
}, nil
}
func (conn *cloudConnector) getResourceGroup() (bool, error) {
_, err := conn.groupsClient.Get(context.TODO(), conn.namer.ResourceGroupName())
return err == nil, err
}
func (conn *cloudConnector) ensureResourceGroup() (resources.Group, error) {
req := resources.Group{
Name: types.StringP(conn.namer.ResourceGroupName()),
Location: types.StringP(conn.Cluster.Spec.Config.Cloud.Zone),
Tags: map[string]*string{
"KubernetesCluster": types.StringP(conn.Cluster.Name),
},
}
return conn.groupsClient.CreateOrUpdate(context.TODO(), conn.namer.ResourceGroupName(), req)
}
func (conn *cloudConnector) deleteResourceGroup() error {
log := conn.Logger
_, err := conn.groupsClient.Delete(context.TODO(), conn.namer.ResourceGroupName())
log.Info("Resource group deleted", "resourcegroup-name", conn.namer.ResourceGroupName())
return err
}
func (conn *cloudConnector) upsertAKS(agentPools []cs.ManagedClusterAgentPoolProfile) error {
log := conn.Logger
cred, err := conn.StoreProvider.Credentials().Get(conn.Cluster.Spec.Config.CredentialName)
if err != nil {
return err
}
typed := credential.Azure{CommonSpec: credential.CommonSpec(cred.Spec)}
if ok, err := typed.IsValid(); !ok {
return errors.Wrapf(err, "credential %s is invalid", conn.Cluster.Spec.Config.CredentialName)
}
container := cs.ManagedCluster{
Name: &conn.Cluster.Name,
Location: types.StringP(conn.Cluster.Spec.Config.Cloud.Zone),
ManagedClusterProperties: &cs.ManagedClusterProperties{
DNSPrefix: types.StringP(conn.Cluster.Name),
//Fqdn: types.StringP(conn.Cluster.Name),
KubernetesVersion: types.StringP(conn.Cluster.Spec.Config.KubernetesVersion),
ServicePrincipalProfile: &cs.ManagedClusterServicePrincipalProfile{
ClientID: types.StringP(typed.ClientID()),
Secret: types.StringP(typed.ClientSecret()),
},
AgentPoolProfiles: &agentPools,
LinuxProfile: &cs.LinuxProfile{
AdminUsername: types.StringP(conn.namer.AdminUsername()),
SSH: &cs.SSHConfiguration{
PublicKeys: &[]cs.SSHPublicKey{
{
KeyData: types.StringP(string(conn.Certs.SSHKey.PublicKey)),
},
},
},
},
},
}
_, err = conn.managedClient.CreateOrUpdate(context.Background(), conn.namer.ResourceGroupName(), conn.Cluster.Name, container)
if err != nil {
log.Error(err, "failed to create cluster")
return err
}
return conn.WaitForClusterOperation()
}
func (conn *cloudConnector) WaitForClusterOperation() error {
log := conn.Logger
attempt := 0
return wait.PollImmediate(api.RetryInterval, api.RetryTimeout, func() (bool, error) {
attempt++
r, err := conn.managedClient.Get(context.Background(), conn.namer.ResourceGroupName(), conn.Cluster.Name)
if err != nil {
return false, nil
}
log.Info("waiting for cluster operation", "attempt", attempt, "operation", *r.Name, "status", *r.ProvisioningState)
if *r.ProvisioningState == "Succeeded" {
return true, nil
}
return false, nil
})
}
func (conn *cloudConnector) deleteAKS() error {
_, err := conn.managedClient.Delete(context.Background(), conn.namer.ResourceGroupName(), conn.Cluster.Name)
return err
}