Skip to content
This repository was archived by the owner on Jun 23, 2020. It is now read-only.

Commit 1846fb9

Browse files
authored
Merge pull request #45 from oracle/ol-resolve-regional-differences
Fix incorrect volume OCID returned for Frankfurt
2 parents 9300491 + 4250583 commit 1846fb9

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

pkg/oci/driver/driver.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ func (d OCIFlexvolumeDriver) Init() flexvolume.DriverStatus {
6767
return flexvolume.Succeed()
6868
}
6969

70+
// deriveVolumeOCID will figure out the correct OCID for a volume
71+
// based solely on the region key and volumeName. Because of differences
72+
// across regions we need to impose some awkward logic here to get the correct
73+
// OCID
74+
func deriveVolumeOCID(regionKey string, volumeName string) string {
75+
var volumeOCID string
76+
77+
if regionKey == "fra" {
78+
volumeOCID = fmt.Sprintf(volumeOCIDTemplate, "eu-frankfurt-1", volumeName)
79+
} else {
80+
volumeOCID = fmt.Sprintf(volumeOCIDTemplate, regionKey, volumeName)
81+
}
82+
83+
return volumeOCID
84+
}
85+
7086
// Attach initiates the attachment of the given OCI volume to the k8s worker
7187
// node.
7288
func (d OCIFlexvolumeDriver) Attach(opts flexvolume.Options, nodeName string) flexvolume.DriverStatus {
@@ -80,7 +96,7 @@ func (d OCIFlexvolumeDriver) Attach(opts flexvolume.Options, nodeName string) fl
8096
return flexvolume.Fail(err)
8197
}
8298

83-
volumeOCID := fmt.Sprintf(volumeOCIDTemplate, c.GetConfig().Auth.RegionKey, opts["kubernetes.io/pvOrVolumeName"])
99+
volumeOCID := deriveVolumeOCID(c.GetConfig().Auth.RegionKey, opts["kubernetes.io/pvOrVolumeName"])
84100

85101
log.Printf("Attaching volume %s -> instance %s", volumeOCID, instance.ID)
86102

@@ -122,7 +138,7 @@ func (d OCIFlexvolumeDriver) Detach(pvOrVolumeName, nodeName string) flexvolume.
122138
return flexvolume.Fail(err)
123139
}
124140

125-
volumeOCID := fmt.Sprintf(volumeOCIDTemplate, c.GetConfig().Auth.RegionKey, pvOrVolumeName)
141+
volumeOCID := deriveVolumeOCID(c.GetConfig().Auth.RegionKey, pvOrVolumeName)
126142
attachment, err := c.FindVolumeAttachment(volumeOCID)
127143
if err != nil {
128144
return flexvolume.Fail(err)
@@ -155,7 +171,7 @@ func (d OCIFlexvolumeDriver) IsAttached(opts flexvolume.Options, nodeName string
155171
return flexvolume.Fail(err)
156172
}
157173

158-
volumeOCID := fmt.Sprintf(volumeOCIDTemplate, c.GetConfig().Auth.RegionKey, opts["kubernetes.io/pvOrVolumeName"])
174+
volumeOCID := deriveVolumeOCID(c.GetConfig().Auth.RegionKey, opts["kubernetes.io/pvOrVolumeName"])
159175
attachment, err := c.FindVolumeAttachment(volumeOCID)
160176
if err != nil {
161177
return flexvolume.DriverStatus{

pkg/oci/driver/driver_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2017 The OCI Flexvolume Driver Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package driver
16+
17+
import "testing"
18+
19+
var volumeOCIDTests = []struct {
20+
regionKey string
21+
volumeName string
22+
expected string
23+
}{
24+
{"phx", "aaaaaa", "ocid1.volume.oc1.phx.aaaaaa"},
25+
{"iad", "aaaaaa", "ocid1.volume.oc1.iad.aaaaaa"},
26+
{"fra", "aaaaaa", "ocid1.volume.oc1.eu-frankfurt-1.aaaaaa"},
27+
}
28+
29+
func TestDeriveVolumeOCID(t *testing.T) {
30+
for _, tt := range volumeOCIDTests {
31+
result := deriveVolumeOCID(tt.regionKey, tt.volumeName)
32+
if result != tt.expected {
33+
t.Errorf("Failed to derive OCID. Expected %s got %s", tt.expected, result)
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)