-
Notifications
You must be signed in to change notification settings - Fork 9
/
provider.go
266 lines (235 loc) · 9.34 KB
/
provider.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* Copyright 2020 IBM Corp.
*
* 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 provider ...
package provider
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/IBM/ibmcloud-volume-interface/config"
"github.com/IBM/ibmcloud-volume-interface/lib/metrics"
"github.com/IBM/ibmcloud-volume-interface/lib/provider"
util "github.com/IBM/ibmcloud-volume-interface/lib/utils"
"github.com/IBM/ibmcloud-volume-interface/provider/iam"
"github.com/IBM/ibmcloud-volume-interface/provider/local"
vpcconfig "github.com/IBM/ibmcloud-volume-vpc/block/vpcconfig"
vpcauth "github.com/IBM/ibmcloud-volume-vpc/common/auth"
"github.com/IBM/ibmcloud-volume-vpc/common/messages"
userError "github.com/IBM/ibmcloud-volume-vpc/common/messages"
"github.com/IBM/ibmcloud-volume-vpc/common/vpcclient/riaas"
"go.uber.org/zap"
)
const (
// VPCClassic ...
VPCClassic = "gc"
// VPCNextGen ...
VPCNextGen = "g2"
// PrivatePrefix ...
PrivatePrefix = "private-"
// BasePrivateURL ...
BasePrivateURL = "https://" + PrivatePrefix
// HTTPSLength ...
HTTPSLength = 8
// NEXTGenProvider ...
NEXTGenProvider = 2
)
// VPCBlockProvider implements provider.Provider
type VPCBlockProvider struct {
timeout time.Duration
Config *vpcconfig.VPCBlockConfig
tokenGenerator *tokenGenerator
ContextCF local.ContextCredentialsFactory
ClientProvider riaas.RegionalAPIClientProvider
httpClient *http.Client
APIConfig riaas.Config
}
var _ local.Provider = &VPCBlockProvider{}
// NewProvider initialises an instance of an IaaS provider.
func NewProvider(conf *vpcconfig.VPCBlockConfig, logger *zap.Logger) (local.Provider, error) {
logger.Info("Entering NewProvider")
if conf.VPCConfig == nil {
return nil, errors.New("incomplete config for VPCBlockProvider")
}
//Do config validation and enable only one generationType (i.e VPC-Classic | VPC-NG)
gcConfigFound := (conf.VPCConfig.EndpointURL != "" || conf.VPCConfig.PrivateEndpointURL != "") && (conf.VPCConfig.TokenExchangeURL != "" || conf.VPCConfig.IKSTokenExchangePrivateURL != "") && (conf.VPCConfig.APIKey != "") && (conf.VPCConfig.ResourceGroupID != "")
g2ConfigFound := (conf.VPCConfig.G2EndpointPrivateURL != "" || conf.VPCConfig.G2EndpointURL != "") && (conf.VPCConfig.IKSTokenExchangePrivateURL != "" || conf.VPCConfig.G2TokenExchangeURL != "") && (conf.VPCConfig.G2APIKey != "") && (conf.VPCConfig.G2ResourceGroupID != "")
//if both config found, look for VPCTypeEnabled, otherwise default to GC
//Incase of NG configurations, override the base properties.
if (gcConfigFound && g2ConfigFound && conf.VPCConfig.VPCTypeEnabled == VPCNextGen) || (!gcConfigFound && g2ConfigFound) {
// overwrite the common variable in case of g2 i.e gen2, first preferences would be private endpoint
if conf.VPCConfig.G2EndpointPrivateURL != "" {
conf.VPCConfig.EndpointURL = conf.VPCConfig.G2EndpointPrivateURL
} else {
conf.VPCConfig.EndpointURL = conf.VPCConfig.G2EndpointURL
}
// update iam based public toke exchange endpoint
conf.VPCConfig.TokenExchangeURL = conf.VPCConfig.G2TokenExchangeURL
conf.VPCConfig.APIKey = conf.VPCConfig.G2APIKey
conf.VPCConfig.ResourceGroupID = conf.VPCConfig.G2ResourceGroupID
//Set API Generation As 2 (if unspecified in config/ENV-VAR)
if conf.VPCConfig.G2VPCAPIGeneration <= 0 {
conf.VPCConfig.G2VPCAPIGeneration = NEXTGenProvider
}
conf.VPCConfig.VPCAPIGeneration = conf.VPCConfig.G2VPCAPIGeneration
//Set the APIVersion Date, it can be different in GC and NG
if conf.VPCConfig.G2APIVersion != "" {
conf.VPCConfig.APIVersion = conf.VPCConfig.G2APIVersion
}
//set provider-type (this usually comes from the secret)
if conf.VPCConfig.VPCBlockProviderType != VPCNextGen {
conf.VPCConfig.VPCBlockProviderType = VPCNextGen
}
//Mark this as enabled/active
if conf.VPCConfig.VPCTypeEnabled != VPCNextGen {
conf.VPCConfig.VPCTypeEnabled = VPCNextGen
}
} else { //This is GC, no-override required
conf.VPCConfig.VPCBlockProviderType = VPCClassic //incase of gc, i dont see its being set in slclient.toml, but NG cluster has this
// For backward compatibility as some of the cluster storage secret may not have private gc endpoint url
if conf.VPCConfig.PrivateEndpointURL != "" {
conf.VPCConfig.EndpointURL = conf.VPCConfig.PrivateEndpointURL
}
}
contextCF, err := vpcauth.NewVPCContextCredentialsFactory(conf)
if err != nil {
return nil, err
}
timeoutString := conf.VPCConfig.VPCTimeout
if timeoutString == "" || timeoutString == "0s" {
logger.Info("Using VPC default timeout")
timeoutString = "120s"
}
timeout, err := time.ParseDuration(timeoutString)
if err != nil {
return nil, err
}
httpClient, err := config.GeneralCAHttpClientWithTimeout(timeout)
if err != nil {
logger.Error("Failed to prepare HTTP client", util.ZapError(err))
return nil, err
}
// SetRetryParameters sets the retry logic parameters
SetRetryParameters(conf.VPCConfig.MaxRetryAttempt, conf.VPCConfig.MaxRetryGap)
provider := &VPCBlockProvider{
timeout: timeout,
Config: conf,
tokenGenerator: &tokenGenerator{config: conf.VPCConfig},
ContextCF: contextCF,
httpClient: httpClient,
APIConfig: riaas.Config{
BaseURL: conf.VPCConfig.EndpointURL,
HTTPClient: httpClient,
APIVersion: conf.VPCConfig.APIVersion,
APIGeneration: conf.VPCConfig.VPCAPIGeneration,
ResourceGroup: conf.VPCConfig.ResourceGroupID,
},
}
// Update VPC config for IKS deployment
provider.Config.VPCConfig.IsIKS = conf.IKSConfig != nil && conf.IKSConfig.Enabled
userError.MessagesEn = messages.InitMessages()
return provider, nil
}
// ContextCredentialsFactory ...
func (vpcp *VPCBlockProvider) ContextCredentialsFactory(zone *string) (local.ContextCredentialsFactory, error) {
// Datacenter name not required by VPC provider implementation
return vpcp.ContextCF, nil
}
// OpenSession opens a session on the provider
func (vpcp *VPCBlockProvider) OpenSession(ctx context.Context, contextCredentials provider.ContextCredentials, ctxLogger *zap.Logger) (provider.Session, error) {
ctxLogger.Info("Entering OpenSession")
defer metrics.UpdateDurationFromStart(ctxLogger, "OpenSession", time.Now())
defer func() {
ctxLogger.Debug("Exiting OpenSession")
}()
// validate that we have what we need - i.e. valid credentials
if contextCredentials.Credential == "" {
return nil, util.NewError("Error Insufficient Authentication", "No authentication credential provided")
}
if vpcp.Config.ServerConfig.DebugTrace {
vpcp.APIConfig.DebugWriter = os.Stdout
}
if vpcp.ClientProvider == nil {
vpcp.ClientProvider = riaas.DefaultRegionalAPIClientProvider{}
}
ctxLogger.Debug("", zap.Reflect("apiConfig.BaseURL", vpcp.APIConfig.BaseURL))
if ctx != nil && ctx.Value(provider.RequestID) != nil {
// set ContextID only of speicifed in the context
vpcp.APIConfig.ContextID = fmt.Sprintf("%v", ctx.Value(provider.RequestID))
ctxLogger.Info("", zap.Reflect("apiConfig.ContextID", vpcp.APIConfig.ContextID))
}
client, err := vpcp.ClientProvider.New(vpcp.APIConfig)
if err != nil {
return nil, err
}
// Create a token for all other API calls
token, err := getAccessToken(contextCredentials, ctxLogger)
if err != nil {
return nil, err
}
ctxLogger.Debug("", zap.Reflect("Token", token.Token))
err = client.Login(token.Token)
if err != nil {
return nil, err
}
// Update retry logic default values
if vpcp.Config.VPCConfig.MaxRetryAttempt > 0 {
ctxLogger.Debug("", zap.Reflect("MaxRetryAttempt", vpcp.Config.VPCConfig.MaxRetryAttempt))
maxRetryAttempt = vpcp.Config.VPCConfig.MaxRetryAttempt
}
if vpcp.Config.VPCConfig.MaxRetryGap > 0 {
ctxLogger.Debug("", zap.Reflect("MaxRetryGap", vpcp.Config.VPCConfig.MaxRetryGap))
maxRetryGap = vpcp.Config.VPCConfig.MaxRetryGap
}
vpcSession := &VPCSession{
VPCAccountID: contextCredentials.IAMAccountID,
Config: vpcp.Config,
ContextCredentials: contextCredentials,
VolumeType: "vpc-block",
Provider: VPC,
Apiclient: client,
APIClientVolAttachMgr: client.VolumeAttachService(),
Logger: ctxLogger,
APIRetry: NewFlexyRetryDefault(),
SessionError: nil,
}
return vpcSession, nil
}
// getAccessToken ...
func getAccessToken(creds provider.ContextCredentials, logger *zap.Logger) (token *iam.AccessToken, err error) {
switch creds.AuthType {
case provider.IAMAccessToken:
token = &iam.AccessToken{Token: creds.Credential}
default:
err = errors.New("unknown AuthType")
}
return
}
// getPrivateEndpoint ...
func getPrivateEndpoint(logger *zap.Logger, publicEndPoint string) string {
logger.Info("In getPrivateEndpoint, RIaaS public endpoint", zap.Reflect("URL", publicEndPoint))
if !strings.Contains(publicEndPoint, PrivatePrefix) {
if len(publicEndPoint) > HTTPSLength {
return BasePrivateURL + publicEndPoint[HTTPSLength:]
}
} else {
return publicEndPoint
}
return ""
}