Skip to content

Commit

Permalink
added support to restore cross account snapshot (#198)
Browse files Browse the repository at this point in the history
* added support to restore cross account snapshot

Signed-off-by: Arashad Ahamad <arahamad@in.ibm.com>

* fixed lint issue

Signed-off-by: Arashad Ahamad <arahamad@in.ibm.com>

* fixed travis fmt and unit tests related with snapshot

Signed-off-by: Arashad Ahamad <arahamad@in.ibm.com>

* fixed unit tests code coverage

Signed-off-by: Arashad Ahamad <arahamad@in.ibm.com>

---------

Signed-off-by: Arashad Ahamad <arahamad@in.ibm.com>
  • Loading branch information
arahamad authored Sep 13, 2024
1 parent 088d0a7 commit 47fa964
Show file tree
Hide file tree
Showing 87 changed files with 6,296 additions and 498 deletions.
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/kubernetes-sigs/ibm-vpc-block-csi-driver
go 1.22.0

require (
github.com/IBM/ibm-csi-common v1.1.15
github.com/IBM/ibm-csi-common v1.1.18
github.com/IBM/ibmcloud-volume-interface v1.2.6
github.com/IBM/ibmcloud-volume-vpc v1.1.11
github.com/IBM/secret-utils-lib v1.1.11
Expand All @@ -14,8 +14,8 @@ require (
github.com/prometheus/client_golang v1.19.1
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.26.0
golang.org/x/net v0.26.0
golang.org/x/sys v0.21.0
golang.org/x/net v0.28.0
golang.org/x/sys v0.23.0
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
k8s.io/klog/v2 v2.130.1
Expand Down Expand Up @@ -82,10 +82,10 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
go.mongodb.org/mongo-driver v1.14.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
32 changes: 16 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.6.7 h1:eHgfQl6IeSmzWUyiSi13CvoFYsovoyq
github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.6.7/go.mod h1:RiUvKuHKTBmBApDMUQzBL14pQUGKcx/IioKQPIcRQjs=
github.com/IBM/go-sdk-core/v5 v5.17.4 h1:VGb9+mRrnS2HpHZFM5hy4J6ppIWnwNrw0G+tLSgcJLc=
github.com/IBM/go-sdk-core/v5 v5.17.4/go.mod h1:KsAAI7eStAWwQa4F96MLy+whYSh39JzNjklZRbN/8ns=
github.com/IBM/ibm-csi-common v1.1.15 h1:oJ0NsrVBqfXKCZXSc/EI+s7/RlUK/TpzTI+jcdoAqpI=
github.com/IBM/ibm-csi-common v1.1.15/go.mod h1:oKYsmovJ4HAmbHfhQvVn1dJ+xeQHqaa3hQsN/Z5itlc=
github.com/IBM/ibm-csi-common v1.1.18 h1:CbzoONFN6vdmPLoXGxygiq3sbr2xtAcVUb0Vqj3/HNI=
github.com/IBM/ibm-csi-common v1.1.18/go.mod h1:bDs9CLfr09kFpSMcR35e9AmyR2pSydx8goHjagFPrHs=
github.com/IBM/ibmcloud-volume-interface v1.2.6 h1:OLumrSQ0XTOp6gW+k0z2X53uTYOIt1wWSkTCXzK/vAM=
github.com/IBM/ibmcloud-volume-interface v1.2.6/go.mod h1:sDeQiPuN8k9yTRl9FbE2GZCXPNg4cV3oldUfL8wwGNA=
github.com/IBM/ibmcloud-volume-vpc v1.1.11 h1:HnDPi7ZN+iAPVLZ29sLeQnJ7y0lvorc8/q4csDnxht4=
Expand Down Expand Up @@ -121,8 +121,8 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/pprof v0.0.0-20240525223248-4bfdf5a9a2af h1:kmjWCqn2qkEml422C2Rrd27c3VGxi6a/6HNq8QmHRKM=
github.com/google/pprof v0.0.0-20240525223248-4bfdf5a9a2af/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo=
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 h1:FKHo8hFI3A+7w0aUQuYXQ+6EN5stWmeY/AZqtM8xk9k=
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -231,8 +231,8 @@ go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
Expand All @@ -251,8 +251,8 @@ golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.21.0 h1:tsimM75w1tF/uws5rbeHzIWxEqElMehnc+iW793zsZs=
golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
Expand All @@ -276,17 +276,17 @@ golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand All @@ -299,8 +299,8 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
49 changes: 32 additions & 17 deletions pkg/ibmcsidriver/controller.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Kubernetes Authors.
Copyright 2024 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.
Expand Down Expand Up @@ -527,7 +527,7 @@ func (csiCS *CSIControllerServer) DeleteSnapshot(ctx context.Context, req *csi.D
}

snapshot := &provider.Snapshot{}
snapshot.SnapshotID = getSnapshotIDFromCRN(snapshotID)
snapshot.SnapshotID, _ = getSnapshotAndAccountIDsFromCRN(snapshotID)

err = session.DeleteSnapshot(snapshot)
if err != nil {
Expand Down Expand Up @@ -561,24 +561,39 @@ func (csiCS *CSIControllerServer) ListSnapshots(ctx context.Context, req *csi.Li

entries := []*csi.ListSnapshotsResponse_Entry{}
snapshotID := req.GetSnapshotId()
if len(snapshotID) != 0 {
snapshot, err := session.GetSnapshot(snapshotID)
if snapshot == nil {
return &csi.ListSnapshotsResponse{}, nil
snapID, snapshotAccountID := getSnapshotAndAccountIDsFromCRN(snapshotID)
if len(snapID) != 0 {
if csiCS.Driver.accountID == snapshotAccountID { // in case snapshotID's account and cluster account ID is same
snapshot, err := session.GetSnapshot(snapID)
if snapshot == nil {
return &csi.ListSnapshotsResponse{}, nil
}
if providerError.RetrivalFailed == providerError.GetErrorType(err) {
ctxLogger.Info("Snapshot not found. Returning success ...")
return &csi.ListSnapshotsResponse{}, nil
}
return &csi.ListSnapshotsResponse{
Entries: append(entries, &csi.ListSnapshotsResponse_Entry{
Snapshot: createCSISnapshotResponse(*snapshot).Snapshot,
}),
NextToken: "",
}, nil
} else { // In case of cross account volume snapshot
return &csi.ListSnapshotsResponse{
Entries: append(entries, &csi.ListSnapshotsResponse_Entry{
Snapshot: &csi.Snapshot{
SnapshotId: snapshotID,
SourceVolumeId: "",
ReadyToUse: true,
},
}),
NextToken: "",
}, nil
}
if providerError.RetrivalFailed == providerError.GetErrorType(err) {
ctxLogger.Info("Snapshot not found. Returning success ...")
return &csi.ListSnapshotsResponse{}, nil
}
return &csi.ListSnapshotsResponse{
Entries: append(entries, &csi.ListSnapshotsResponse_Entry{
Snapshot: createCSISnapshotResponse(*snapshot).Snapshot,
}),
NextToken: "",
}, nil

}

maxEntries := int(req.MaxEntries)
maxEntries := int(req.GetMaxEntries())
tags := map[string]string{}
sourceVolumeID := req.GetSourceVolumeId()
if len(sourceVolumeID) != 0 {
Expand Down
23 changes: 19 additions & 4 deletions pkg/ibmcsidriver/controller_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,28 @@ func createCSIVolumeResponse(vol provider.Volume, capBytes int64, zones []string
return volResp
}

// getSnapshotIDFromCRN ...
func getSnapshotIDFromCRN(crn string) string {
// getAccountID ...
func getAccountID(input string) string {
tokens := strings.Split(input, "/")

if len(tokens) > 1 {
return tokens[1]
} else {
return ""
}
}

// getSnapshotAndAccountIDsFromCRN ...
func getSnapshotAndAccountIDsFromCRN(crn string) (string, string) {
// This method will be able to handle either crn is actual crn or caller passed snapshot ID also
// expected CRN -> crn:v1:service:public:is:us-south:a/c468d8642937fecd8a0860fe0f379bf9::snapshot:r006-1234fe0c-3d9b-4c95-a6d1-8e0d4bcb6ecb
// or crn passed as sanpshotID like r006-1234fe0c-3d9b-4c95-a6d1-8e0d4bcb6ecb
crnParts := strings.Split(crn, ":")
return crnParts[len(crnParts)-1]
crnTokens := strings.Split(crn, ":")

if len(crnTokens) > 9 {
return crnTokens[len(crnTokens)-1], getAccountID(crnTokens[len(crnTokens)-4])
}
return crn, "" // assuming that crn will contain only snapshotID
}

// createCSISnapshotResponse ...
Expand Down
8 changes: 8 additions & 0 deletions pkg/ibmcsidriver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,14 @@ func TestListSnapshots(t *testing.T) {
expErrCode: codes.OK,
libSnapshotError: nil,
},
{
name: "List snapshot with snapshotID as CRN",
snapshotID: "crn:v1:staging:public:is:us-south:a/77f2bcedd73fe82c1c::snapshot:r134-1ad4-4852-b24a-b65050e42429",
expectedEntries: 1,
libGetSnapshotErr: false,
expErrCode: codes.OK,
libSnapshotError: nil,
},
}
timeNow := time.Now()
// Creating test logger
Expand Down
4 changes: 3 additions & 1 deletion pkg/ibmcsidriver/ibm_csi_driver.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Kubernetes Authors.
Copyright 2024 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.
Expand Down Expand Up @@ -35,6 +35,7 @@ type IBMCSIDriver struct {
vendorVersion string
logger *zap.Logger
region string
accountID string
ids *CSIIdentityServer
ns *CSINodeServer
cs *CSIControllerServer
Expand Down Expand Up @@ -111,6 +112,7 @@ func (icDriver *IBMCSIDriver) SetupIBMCSIDriver(provider cloudProvider.CloudProv
return fmt.Errorf("Controller_Helper: Failed to initialize node metadata: error: %v", err)
}
icDriver.region = regionMetadata.GetRegion()
icDriver.accountID = regionMetadata.GetAccountID()

return nil
}
Expand Down
13 changes: 6 additions & 7 deletions pkg/ibmcsidriver/server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Kubernetes Authors.
Copyright 2024 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.
Expand All @@ -19,18 +19,17 @@ package ibmcsidriver

import (
"errors"
csi "github.com/container-storage-interface/spec/lib/go/csi"
"github.com/golang/glog"
"go.uber.org/zap"
"golang.org/x/net/context"
"google.golang.org/grpc"
"net"
"net/url"
"os"
"os/signal"
"sync"
"syscall"

csi "github.com/container-storage-interface/spec/lib/go/csi"
"github.com/golang/glog"
"go.uber.org/zap"
"golang.org/x/net/context"
"google.golang.org/grpc"
)

// NonBlockingGRPCServer Defines Non blocking GRPC server interfaces
Expand Down
Loading

0 comments on commit 47fa964

Please sign in to comment.