forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
aks_version_endpoint.go
165 lines (130 loc) · 4.14 KB
/
aks_version_endpoint.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
package capabilities
import (
"context"
"encoding/json"
"fmt"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2017-09-30/containerservice"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/mcuadros/go-version"
"net/http"
"sort"
)
func NewAKSVersionsHandler() *AKSVersionHandler {
return &AKSVersionHandler{}
}
type AKSVersionHandler struct {
}
type regionCapabilitiesRequestBody struct {
// credentials
ClientID string `json:"clientId"`
ClientSecret string `json:"clientSecret"`
SubscriptionID string `json:"subscriptionId"`
TenantID string `json:"tenantId"`
Region string `json:"region"`
}
func (g *AKSVersionHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
writer.WriteHeader(http.StatusMethodNotAllowed)
return
}
writer.Header().Set("Content-Type", "application/json")
var body regionCapabilitiesRequestBody
if err := extractRequestBody(writer, req, &body); err != nil {
handleErr(writer, err)
return
}
if err := validateRegionRequestBody(writer, &body); err != nil {
handleErr(writer, err)
return
}
region := body.Region
clientID := body.ClientID
clientSecret := body.ClientSecret
subscriptionID := body.SubscriptionID
tenantID := body.TenantID
oAuthConfig, err := adal.NewOAuthConfig(azure.PublicCloud.ActiveDirectoryEndpoint, tenantID)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
handleErr(writer, fmt.Errorf("failed to configure azure oaith: %v", err))
return
}
spToken, err := adal.NewServicePrincipalToken(*oAuthConfig, clientID, clientSecret, azure.PublicCloud.ResourceManagerEndpoint)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
handleErr(writer, fmt.Errorf("failed to create token: %v", err))
return
}
authorizer := autorest.NewBearerAuthorizer(spToken)
client := containerservice.NewContainerServicesClient(subscriptionID)
client.Authorizer = authorizer
orchestrators, err := client.ListOrchestrators(context.Background(), region, "managedClusters")
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
handleErr(writer, fmt.Errorf("failed to get orchestrators: %v", err))
return
}
if orchestrators.Orchestrators == nil {
writer.WriteHeader(http.StatusBadRequest)
handleErr(writer, fmt.Errorf("no version profiles returned: %v", err))
return
}
var kubernetesVersions []string
for _, profile := range *orchestrators.Orchestrators {
if profile.OrchestratorType == nil || profile.OrchestratorVersion == nil {
writer.WriteHeader(http.StatusInternalServerError)
handleErr(writer, fmt.Errorf("unexpected nil orchestrator type or version"))
return
}
if *profile.OrchestratorType == "Kubernetes" {
kubernetesVersions = append(kubernetesVersions, *profile.OrchestratorVersion)
}
}
sort.Sort(sortableVersion(kubernetesVersions))
serialized, err := json.Marshal(kubernetesVersions)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
handleErr(writer, err)
return
}
writer.Write(serialized)
}
type sortableVersion []string
func (s sortableVersion) Len() int {
return len(s)
}
func (s sortableVersion) Swap(a, b int) {
s[a], s[b] = s[b], s[a]
}
func (s sortableVersion) Less(a, b int) bool {
return version.Compare(s[a], s[b], "<")
}
func validateRegionRequestBody(writer http.ResponseWriter, body *regionCapabilitiesRequestBody) error {
region := body.Region
clientID := body.ClientID
clientSecret := body.ClientSecret
subscriptionID := body.SubscriptionID
tenantID := body.TenantID
if region == "" {
writer.WriteHeader(http.StatusBadRequest)
return fmt.Errorf("invalid region")
}
if clientID == "" {
writer.WriteHeader(http.StatusBadRequest)
return fmt.Errorf("invalid clientID")
}
if clientSecret == "" {
writer.WriteHeader(http.StatusBadRequest)
return fmt.Errorf("invalid clientSecret")
}
if subscriptionID == "" {
writer.WriteHeader(http.StatusBadRequest)
return fmt.Errorf("invalid subscriptionID")
}
if tenantID == "" {
writer.WriteHeader(http.StatusBadRequest)
return fmt.Errorf("invalid tenantID")
}
return nil
}