-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
utils.go
172 lines (150 loc) · 4.93 KB
/
utils.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
/*
Copyright 2022 The Kubernetes 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 scaleway
import (
"errors"
"fmt"
"net/http"
"os"
"strings"
"github.com/scaleway/scaleway-sdk-go/scw"
k8serrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi"
)
// isHTTPCodeError returns true if err is an http error with code statusCode
func isHTTPCodeError(err error, statusCode int) bool {
if err == nil {
return false
}
responseError := &scw.ResponseError{}
if errors.As(err, &responseError) && responseError.StatusCode == statusCode {
return true
}
return false
}
// is404Error returns true if err is an HTTP 404 error
func is404Error(err error) bool {
notFoundError := &scw.ResourceNotFoundError{}
return isHTTPCodeError(err, http.StatusNotFound) || errors.As(err, ¬FoundError)
}
func ParseZoneFromClusterSpec(clusterSpec kops.ClusterSpec) (scw.Zone, error) {
zone := ""
for _, subnet := range clusterSpec.Networking.Subnets {
if zone == "" {
zone = subnet.Zone
} else if zone != subnet.Zone {
return "", fmt.Errorf("scaleway currently only supports clusters in the same zone")
}
}
return scw.Zone(zone), nil
}
func ParseRegionFromZone(zone scw.Zone) (region scw.Region, err error) {
region, err = scw.ParseRegion(strings.TrimRight(string(zone), "-123"))
if err != nil {
return "", fmt.Errorf("could not determine region from zone %s: %w", zone, err)
}
return region, nil
}
func ClusterNameFromTags(tags []string) string {
for _, tag := range tags {
if strings.HasPrefix(tag, TagClusterName) {
return strings.TrimPrefix(tag, TagClusterName+"=")
}
}
return ""
}
func InstanceGroupNameFromTags(tags []string) string {
for _, tag := range tags {
if strings.HasPrefix(tag, TagInstanceGroup) {
return strings.TrimPrefix(tag, TagInstanceGroup+"=")
}
}
return ""
}
func InstanceRoleFromTags(tags []string) string {
for _, tag := range tags {
if strings.HasPrefix(tag, TagNameRolePrefix) {
return strings.TrimPrefix(tag, TagNameRolePrefix+"=")
}
}
return ""
}
func getScalewayProfile() (*scw.Profile, error) {
scwProfileName := os.Getenv("SCW_PROFILE")
if scwProfileName == "" {
return nil, nil
}
config, err := scw.LoadConfig()
if err != nil {
return nil, fmt.Errorf("loading Scaleway config file: %w", err)
}
profile, ok := config.Profiles[scwProfileName]
if !ok {
return nil, fmt.Errorf("could not find Scaleway profile %q", scwProfileName)
}
return profile, nil
}
func checkCredentials(accessKey, secretKey, projectID string) []error {
errList := []error(nil)
if accessKey == "" {
errList = append(errList, fmt.Errorf("SCW_ACCESS_KEY has to be set"))
}
if secretKey == "" {
errList = append(errList, fmt.Errorf("SCW_SECRET_KEY has to be set"))
}
if projectID == "" {
errList = append(errList, fmt.Errorf("SCW_DEFAULT_PROJECT_ID has to be set"))
}
return errList
}
func CreateValidScalewayProfile() (*scw.Profile, error) {
var profile scw.Profile
// If the profile is REDACTED, we're running integration tests so no need to return any credentials
if profileName := os.Getenv("SCW_PROFILE"); profileName == "REDACTED" {
return &profile, nil
}
// We load the credentials form the profile first
profileFromScwConfig, err := getScalewayProfile()
if err != nil {
return nil, err
}
// We load the credentials from the environment second
var profileFromEnv scw.Profile
if accessKey := os.Getenv("SCW_ACCESS_KEY"); accessKey != "" {
profileFromEnv.AccessKey = &accessKey
}
if secretKey := os.Getenv("SCW_SECRET_KEY"); secretKey != "" {
profileFromEnv.SecretKey = &secretKey
}
if projectID := os.Getenv("SCW_DEFAULT_PROJECT_ID"); projectID != "" {
profileFromEnv.DefaultProjectID = &projectID
}
// We merge the profiles: the environment will override the values from the profile
if profileFromScwConfig == nil {
profile = profileFromEnv
} else {
profile = *scw.MergeProfiles(profileFromScwConfig, &profileFromEnv)
}
// We check that the profile has an access key, a secret key and a default project ID
if errList := checkCredentials(fi.ValueOf(profile.AccessKey), fi.ValueOf(profile.SecretKey), fi.ValueOf(profile.DefaultProjectID)); errList != nil {
errMsg := k8serrors.NewAggregate(errList).Error()
if scwProfileName := os.Getenv("SCW_PROFILE"); scwProfileName != "" {
errMsg += fmt.Sprintf(" in profile %q", scwProfileName)
} else {
errMsg += " in a Scaleway profile or as an environment variable"
}
return nil, fmt.Errorf(errMsg)
}
return &profile, nil
}