-
Notifications
You must be signed in to change notification settings - Fork 5
/
init_provider.go
120 lines (103 loc) · 4.61 KB
/
init_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
/**
* Copyright 2021 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 utils ...
package utils
import (
"errors"
"go.uber.org/zap"
"golang.org/x/net/context"
"github.com/IBM/ibmcloud-volume-file-vpc/common/registry"
vpc_provider "github.com/IBM/ibmcloud-volume-file-vpc/file/provider"
vpcfileconfig "github.com/IBM/ibmcloud-volume-file-vpc/file/vpcconfig"
iks_vpc_provider "github.com/IBM/ibmcloud-volume-file-vpc/iks/provider"
"github.com/IBM/ibmcloud-volume-interface/lib/provider"
util "github.com/IBM/ibmcloud-volume-interface/lib/utils"
"github.com/IBM/ibmcloud-volume-interface/provider/local"
"github.com/IBM/secret-utils-lib/pkg/k8s_utils"
)
// InitProviders initialization for all providers as per configurations
func InitProviders(conf *vpcfileconfig.VPCFileConfig, k8sClient *k8s_utils.KubernetesClient, logger *zap.Logger) (registry.Providers, error) {
var haveProviders bool
providerRegistry := ®istry.ProviderRegistry{}
// VPC provider registration
if conf.VPCConfig != nil && conf.VPCConfig.Enabled {
logger.Info("Configuring VPC File Provider")
prov, err := vpc_provider.NewProvider(conf, k8sClient, logger)
if err != nil {
logger.Info("VPC file provider error!")
return nil, err
}
providerRegistry.Register(conf.VPCConfig.VPCVolumeType, prov)
haveProviders = true
}
// IKS provider registration
if conf.IKSConfig != nil && conf.IKSConfig.Enabled {
logger.Info("Configuring IKS-VPC FILE Provider")
prov, err := iks_vpc_provider.NewProvider(conf, k8sClient, logger)
if err != nil {
logger.Error("VPC file provider error!")
return nil, err
}
providerRegistry.Register(conf.IKSConfig.IKSFileProviderName, prov)
haveProviders = true
}
if haveProviders {
logger.Info("Provider registration done!!!")
return providerRegistry, nil
}
return nil, errors.New("no providers registered")
}
// OpenProviderSession ...
func OpenProviderSession(prov local.Provider, vpcfileconf *vpcfileconfig.VPCFileConfig, providers registry.Providers, providerID string, ctxLogger *zap.Logger) (session provider.Session, fatal bool, err error) {
return OpenProviderSessionWithContext(context.TODO(), prov, vpcfileconf, providerID, ctxLogger)
}
// OpenProviderSessionWithContext ...
func OpenProviderSessionWithContext(ctx context.Context, prov local.Provider, vpcfileconf *vpcfileconfig.VPCFileConfig, providerID string, ctxLogger *zap.Logger) (provider.Session, bool, error) {
ctxLogger.Info("Fetching provider session")
ccf, err := prov.ContextCredentialsFactory(nil)
if err != nil {
ctxLogger.Error("Unable to fetch credentials", local.ZapError(err))
return nil, true, err
}
ctxLogger.Info("Calling provider/utils/init_provider.go GenerateContextCredentials")
contextCredentials, err := GenerateContextCredentials(vpcfileconf, providerID, ccf, ctxLogger)
if err != nil {
ctxLogger.Error("Unable to generate credentials", local.ZapError(err))
return nil, true, err
}
session, err := prov.OpenSession(ctx, contextCredentials, ctxLogger)
if err != nil {
ctxLogger.Error("Failed to open provider session", local.ZapError(err))
return nil, true, err
}
ctxLogger.Info("Successfully fetched provider session")
return session, false, nil
}
// GenerateContextCredentials ...
func GenerateContextCredentials(conf *vpcfileconfig.VPCFileConfig, providerID string, contextCredentialsFactory local.ContextCredentialsFactory, ctxLogger *zap.Logger) (provider.ContextCredentials, error) {
ctxLogger.Info("Generating generateContextCredentials for ", zap.String("Provider ID", providerID))
// Select appropriate authentication strategy
switch {
case (conf.VPCConfig != nil && providerID == conf.VPCConfig.VPCVolumeType):
ctxLogger.Info("Calling provider/init_provider.go ForIAMAccessToken")
return contextCredentialsFactory.ForIAMAccessToken(conf.VPCConfig.G2APIKey, ctxLogger)
case (conf.IKSConfig != nil && providerID == conf.IKSConfig.IKSFileProviderName):
return provider.ContextCredentials{}, nil // Get credentials in OpenSession method
default:
return provider.ContextCredentials{}, util.NewError("ErrorInsufficientAuthentication",
"Insufficient authentication credentials")
}
}