Skip to content

Commit

Permalink
Migrate library dependant code from csi-common to block library (#72)
Browse files Browse the repository at this point in the history
* Migrate library dependant code from csi-common to block library

Signed-off-by: Sameer Shaikh <[email protected]>

* Migrate library dependant code from csi-common to block library

Signed-off-by: Sameer Shaikh <[email protected]>

* Migrate library dependant code from csi-common to block library

Signed-off-by: Sameer Shaikh <[email protected]>

* Add test tomls

Signed-off-by: Sameer Shaikh <[email protected]>

* Migrate library dependant code from csi-common to block library

Signed-off-by: Sameer Shaikh <[email protected]>

---------

Signed-off-by: Sameer Shaikh <[email protected]>
  • Loading branch information
sameshai authored May 14, 2024
1 parent ac01934 commit 6319cae
Show file tree
Hide file tree
Showing 8 changed files with 483 additions and 160 deletions.
29 changes: 15 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ go 1.20

require (
github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.6.7
github.com/IBM/ibmcloud-volume-interface v1.2.2
github.com/IBM/ibm-csi-common v1.1.13
github.com/IBM/ibmcloud-volume-interface v1.2.4
github.com/IBM/secret-common-lib v1.1.8
github.com/IBM/secret-utils-lib v1.1.8
github.com/fatih/structs v1.1.0
github.com/gofrs/uuid v4.2.0+incompatible
github.com/golang-jwt/jwt/v4 v4.2.0
github.com/gofrs/uuid v4.4.0+incompatible
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/stretchr/testify v1.8.2
go.uber.org/zap v1.20.0
golang.org/x/net v0.19.0
Expand All @@ -20,10 +21,10 @@ require (
github.com/IBM/go-sdk-core/v5 v5.9.1 // indirect
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/errors v0.19.8 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
Expand All @@ -48,30 +49,30 @@ require (
github.com/kelseyhightower/envconfig v1.4.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.3.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.7.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.10.0 // indirect
github.com/prometheus/procfs v0.1.3 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
go.mongodb.org/mongo-driver v1.7.5 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210924002016-3dee208752a0 // indirect
google.golang.org/grpc v1.47.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.56.3 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/go-playground/validator.v9 v9.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
177 changes: 31 additions & 146 deletions go.sum

Large diffs are not rendered by default.

165 changes: 165 additions & 0 deletions pkg/ibmcloudprovider/volume_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* Copyright 2024 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 ibmcloudprovider ...
package ibmcloudprovider

import (
"fmt"
"os"
"strings"
"time"

cloudProvider "github.com/IBM/ibm-csi-common/pkg/ibmcloudprovider"
"github.com/IBM/ibmcloud-volume-interface/config"
"github.com/IBM/ibmcloud-volume-interface/lib/provider"
"github.com/IBM/ibmcloud-volume-interface/provider/local"
provider_util "github.com/IBM/ibmcloud-volume-vpc/block/utils"
vpcconfig "github.com/IBM/ibmcloud-volume-vpc/block/vpcconfig"
"github.com/IBM/ibmcloud-volume-vpc/common/registry"
utilsConfig "github.com/IBM/secret-utils-lib/pkg/config"
"github.com/IBM/secret-utils-lib/pkg/k8s_utils"
"go.uber.org/zap"
"golang.org/x/net/context"
)

// IBMCloudStorageProvider Provider
type IBMCloudStorageProvider struct {
ProviderName string
ProviderConfig *config.Config
Registry registry.Providers
ClusterID string
}

var _ cloudProvider.CloudProviderInterface = &IBMCloudStorageProvider{}

// NewIBMCloudStorageProvider ...
func NewIBMCloudStorageProvider(clusterVolumeLabel string, k8sClient *k8s_utils.KubernetesClient, logger *zap.Logger) (*IBMCloudStorageProvider, error) {
logger.Info("NewIBMCloudStorageProvider-Reading provider configuration...")
// Load config file
conf, err := config.ReadConfig(*k8sClient, logger)
if err != nil {
logger.Error("Error loading configuration")
return nil, err
}

// Get only VPC_API_VERSION, in "2019-07-02T00:00:00.000Z" case vpc need only 2019-07-02"
dateTime, err := time.Parse(time.RFC3339, conf.VPC.APIVersion)
if err == nil {
conf.VPC.APIVersion = fmt.Sprintf("%d-%02d-%02d", dateTime.Year(), dateTime.Month(), dateTime.Day())
} else {
logger.Warn("Failed to parse VPC_API_VERSION, setting default value")
conf.VPC.APIVersion = "2022-11-11" // setting default values
}

var clusterInfo utilsConfig.ClusterConfig
logger.Info("Fetching clusterInfo")
if conf.IKS != nil && conf.IKS.Enabled || os.Getenv("IKS_ENABLED") == "True" {
clusterInfo, err = utilsConfig.GetClusterInfo(*k8sClient, logger)
if err != nil {
logger.Error("Unable to load ClusterInfo", local.ZapError(err))
return nil, err
}
logger.Info("Fetched clusterInfo..")
}

//Initialize the clusterVolumeLabel once which will be used for tagging by the library.
conf.VPC.ClusterVolumeLabel = clusterVolumeLabel

vpcBlockConfig := &vpcconfig.VPCBlockConfig{
VPCConfig: conf.VPC,
IKSConfig: conf.IKS,
APIConfig: conf.API,
ServerConfig: conf.Server,
}
// Prepare provider registry
registry, err := provider_util.InitProviders(vpcBlockConfig, k8sClient, logger)
if err != nil {
logger.Error("Error configuring providers", local.ZapError(err))
return nil, err
}

var providerName string
if isRunningInIKS() && conf.IKS.Enabled {
providerName = conf.IKS.IKSBlockProviderName
} else if conf.VPC.Enabled {
providerName = conf.VPC.VPCBlockProviderName
}

cloudProvider := &IBMCloudStorageProvider{
ProviderName: providerName,
ProviderConfig: conf,
Registry: registry,
ClusterID: clusterInfo.ClusterID,
}
logger.Info("Successfully read provider configuration")
return cloudProvider, nil
}

func isRunningInIKS() bool {
return true //TODO Check the master KUBE version
}

// GetProviderSession ...
func (icp *IBMCloudStorageProvider) GetProviderSession(ctx context.Context, logger *zap.Logger) (provider.Session, error) {
logger.Info("IBMCloudStorageProvider-GetProviderSession...")
if icp.ProviderConfig.API == nil {
icp.ProviderConfig.API = &config.APIConfig{
PassthroughSecret: string([]byte{}), // // TODO~ Need to remove it
}
}

prov, err := icp.Registry.Get(icp.ProviderName)
if err != nil {
logger.Error("Not able to get the said provider, might be its not registered", local.ZapError(err))
return nil, err
}

// Populating vpcBlockConfig which is used to open session
vpcBlockConfig := &vpcconfig.VPCBlockConfig{
VPCConfig: icp.ProviderConfig.VPC,
IKSConfig: icp.ProviderConfig.IKS,
APIConfig: icp.ProviderConfig.API,
ServerConfig: icp.ProviderConfig.Server,
}

session, _, err := provider_util.OpenProviderSessionWithContext(ctx, prov, vpcBlockConfig, icp.ProviderName, logger)
if err == nil {
logger.Info("Successfully got the provider session", zap.Reflect("ProviderName", session.ProviderName()))
return session, nil
}
logger.Error("Failed to get provider session", zap.Reflect("Error", err))
return nil, err
}

// GetConfig ...
func (icp *IBMCloudStorageProvider) GetConfig() *config.Config {
return icp.ProviderConfig
}

// GetClusterID ...
func (icp *IBMCloudStorageProvider) GetClusterID() string {
return icp.ClusterID
}

// CorrectEndpointURL corrects endpoint url if it is of form "http://"
func getEndpointURL(url string, logger *zap.Logger) string {
if strings.Contains(url, "http://") {
logger.Warn("Token exchange endpoint URL is of the form 'http' instead 'https'. Correcting it for valid request.", zap.Reflect("Endpoint URL: ", url))
return strings.Replace(url, "http", "https", 1)
}
return url
}
Loading

0 comments on commit 6319cae

Please sign in to comment.