Skip to content

Commit 2e69972

Browse files
owainlewisprydie
authored andcommitted
Use instance metadata canonical region name in metadata (#273)
This change simplifies the canonical region name lookup by pulling directly from instance metadata.
1 parent 199d221 commit 2e69972

File tree

11 files changed

+31
-127
lines changed

11 files changed

+31
-127
lines changed

pkg/cloudprovider/providers/oci/ccm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func NewCloudProvider(config *providercfg.Config) (cloudprovider.Interface, erro
102102
if err != nil {
103103
return nil, err
104104
}
105-
config.CompartmentID = metadata.CompartmentOCID
105+
config.CompartmentID = metadata.CompartmentID
106106
}
107107

108108
if !config.LoadBalancer.Disabled && config.VCNID == "" {

pkg/cloudprovider/providers/oci/config/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestReadConfigShouldHaveNoDefaultRegionIfNoneSpecified(t *testing.T) {
126126
}
127127
}
128128

129-
func TestReadConfigShouldSetCompartmentOCIDWhenProvidedValidConfig(t *testing.T) {
129+
func TestReadConfigShouldSetCompartmentIDWhenProvidedValidConfig(t *testing.T) {
130130
cfg, err := ReadConfig(strings.NewReader(validConfig))
131131
if err != nil {
132132
t.Fatalf("expected no error but got '%+v'", err)

pkg/flexvolume/block/config.go

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"io/ioutil"
2222
"log"
2323
"os"
24-
"strings"
2524

2625
providercfg "github.com/oracle/oci-cloud-controller-manager/pkg/cloudprovider/providers/oci/config"
2726
"github.com/oracle/oci-cloud-controller-manager/pkg/oci/instance/metadata"
@@ -31,13 +30,6 @@ import (
3130
"k8s.io/apimachinery/pkg/util/validation/field"
3231
)
3332

34-
var ociRegions = map[string]string{
35-
"phx": "us-phoenix-1",
36-
"iad": "us-ashburn-1",
37-
"fra": "eu-frankfurt-1",
38-
"lhr": "uk-london-1",
39-
}
40-
4133
// Config holds the configuration for the OCI flexvolume driver.
4234
type Config struct {
4335
providercfg.Config `yaml:",inline"`
@@ -89,23 +81,23 @@ func ConfigFromFile(path string) (*Config, error) {
8981
}
9082

9183
func (c *Config) setDefaults() error {
92-
if c.Auth.Region == "" || c.Auth.CompartmentID == "" {
84+
if c.Auth.Region == "" || c.Auth.RegionKey == "" || c.Auth.CompartmentID == "" {
9385
meta, err := c.metadata.Get()
9486
if err != nil {
9587
return err
9688
}
9789

9890
if c.Auth.Region == "" {
99-
c.Auth.Region = meta.Region
91+
c.Auth.Region = meta.CanonicalRegionName
10092
}
101-
if c.Auth.CompartmentID == "" {
102-
c.Auth.CompartmentID = meta.CompartmentOCID
93+
94+
if c.Auth.RegionKey == "" {
95+
c.Auth.RegionKey = meta.Region
10396
}
104-
}
10597

106-
err := c.setRegionFields(c.Auth.Region)
107-
if err != nil {
108-
return fmt.Errorf("setting config region fields: %v", err)
98+
if c.Auth.CompartmentID == "" {
99+
c.Auth.CompartmentID = meta.CompartmentID
100+
}
109101
}
110102

111103
if c.Auth.Passphrase == "" && c.Auth.PrivateKeyPassphrase != "" {
@@ -116,34 +108,6 @@ func (c *Config) setDefaults() error {
116108
return nil
117109
}
118110

119-
// setRegionFields accepts either a region short name or a region long name and
120-
// sets both the Region and RegionKey fields.
121-
func (c *Config) setRegionFields(region string) error {
122-
input := region
123-
region = strings.ToLower(region)
124-
125-
var name, key string
126-
name, ok := ociRegions[region]
127-
if !ok {
128-
for key, name = range ociRegions {
129-
if name == region {
130-
ok = true
131-
break
132-
}
133-
}
134-
if !ok {
135-
return fmt.Errorf("tried to connect to unsupported OCI region %q", input)
136-
}
137-
} else {
138-
key = region
139-
}
140-
141-
c.Auth.Region = name
142-
c.Auth.RegionKey = key
143-
144-
return nil
145-
}
146-
147111
// validate checks that all required fields are populated.
148112
func (c *Config) validate() error {
149113
return ValidateConfig(c).ToAggregate()

pkg/flexvolume/block/config_test.go

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ import (
2424
)
2525

2626
func TestConfigDefaulting(t *testing.T) {
27-
expectedCompartmentOCID := "ocid1.compartment.oc1..aaaaaaaa3um2atybwhder4qttfhgon4j3hcxgmsvnyvx4flfjyewkkwfzwnq"
27+
expectedCompartmentID := "ocid1.compartment.oc1..aaaaaaaa3um2atybwhder4qttfhgon4j3hcxgmsvnyvx4flfjyewkkwfzwnq"
2828
expectedRegion := "us-phoenix-1"
2929
expectedRegionKey := "phx"
3030

3131
cfg := &Config{metadata: metadata.NewMock(
3232
&metadata.InstanceMetadata{
33-
CompartmentOCID: expectedCompartmentOCID,
34-
Region: expectedRegionKey, // instance metadata API only returns the region key
33+
CompartmentID: expectedCompartmentID,
34+
CanonicalRegionName: expectedRegion,
35+
Region: expectedRegionKey, // instance metadata API only returns the region key
3536
},
3637
)}
3738

@@ -43,59 +44,13 @@ func TestConfigDefaulting(t *testing.T) {
4344
if cfg.Auth.Region != expectedRegion {
4445
t.Fatalf("Expected cfg.Region = %q, got %q", cfg.Auth.Region, expectedRegion)
4546
}
47+
4648
if cfg.Auth.RegionKey != expectedRegionKey {
4749
t.Fatalf("Expected cfg.RegionKey = %q, got %q", cfg.Auth.RegionKey, expectedRegionKey)
4850
}
4951

50-
if cfg.Auth.CompartmentID != expectedCompartmentOCID {
51-
t.Fatalf("Expected cfg.CompartmentOCID = %q, got %q", cfg.Auth.CompartmentID, expectedCompartmentOCID)
52-
}
53-
}
54-
55-
func TestConfigSetRegion(t *testing.T) {
56-
var testCases = []struct {
57-
in string
58-
region string
59-
shortRegion string
60-
shouldErr bool
61-
}{
62-
{"us-phoenix-1", "us-phoenix-1", "phx", false},
63-
{"US-PHOENIX-1", "us-phoenix-1", "phx", false},
64-
{"phx", "us-phoenix-1", "phx", false},
65-
{"PHX", "us-phoenix-1", "phx", false},
66-
67-
{"us-ashburn-1", "us-ashburn-1", "iad", false},
68-
{"US-ASHBURN-1", "us-ashburn-1", "iad", false},
69-
{"iad", "us-ashburn-1", "iad", false},
70-
{"IAD", "us-ashburn-1", "iad", false},
71-
72-
{"eu-frankfurt-1", "eu-frankfurt-1", "fra", false},
73-
{"EU-FRANKFURT-1", "eu-frankfurt-1", "fra", false},
74-
{"fra", "eu-frankfurt-1", "fra", false},
75-
{"FRA", "eu-frankfurt-1", "fra", false},
76-
77-
// error cases
78-
{"us-east", "", "", true},
79-
{"", "", "", true},
80-
}
81-
82-
for _, tt := range testCases {
83-
t.Run(tt.in, func(t *testing.T) {
84-
cfg := &Config{}
85-
err := cfg.setRegionFields(tt.in)
86-
if err != nil {
87-
if !tt.shouldErr {
88-
t.Errorf("SetRegionFields(%q) unexpected error: %v", tt.in, err)
89-
}
90-
}
91-
92-
if cfg.Auth.Region != tt.region {
93-
t.Errorf("SetRegionFields(%q) => {Region: %q}; want {Region: %q}", tt.in, cfg.Auth.Region, tt.region)
94-
}
95-
if cfg.Auth.RegionKey != tt.shortRegion {
96-
t.Errorf("SetRegionFields(%q) => {RegionShortName: %q}; want {RegionShortName: %q}", tt.in, cfg.Auth.RegionKey, tt.shortRegion)
97-
}
98-
})
52+
if cfg.Auth.CompartmentID != expectedCompartmentID {
53+
t.Fatalf("Expected cfg.CompartmentID = %q, got %q", cfg.Auth.CompartmentID, expectedCompartmentID)
9954
}
10055
}
10156

pkg/flexvolume/block/driver.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,14 @@ func (d OCIFlexvolumeDriver) Init() flexvolume.DriverStatus {
158158
return flexvolume.Succeed()
159159
}
160160

161-
// deriveVolumeOCID will figure out the correct OCID for a volume
162-
// based solely on the region key and volumeName. Because of differences
163-
// across regions we need to impose some awkward logic here to get the correct
164-
// OCID or if it is already an OCID then return the OCID.
161+
// deriveVolumeOCID will expand a partial OCID to a full OCID
162+
// based on the region key and volume name.
165163
func deriveVolumeOCID(regionKey string, volumeName string) string {
166164
if strings.HasPrefix(volumeName, ocidPrefix) {
167165
return volumeName
168166
}
169167

170-
var volumeOCID string
171-
if regionKey == "fra" {
172-
volumeOCID = fmt.Sprintf(volumeOCIDTemplate, "eu-frankfurt-1", volumeName)
173-
} else {
174-
volumeOCID = fmt.Sprintf(volumeOCIDTemplate, regionKey, volumeName)
175-
}
176-
177-
return volumeOCID
168+
return fmt.Sprintf(volumeOCIDTemplate, regionKey, volumeName)
178169
}
179170

180171
// constructKubeClient uses a kubeconfig layed down by a secret via deploy.sh to return

pkg/flexvolume/block/driver_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ var volumeOCIDTests = []struct {
2626
}{
2727
{"phx", "aaaaaa", "ocid1.volume.oc1.phx.aaaaaa"},
2828
{"iad", "aaaaaa", "ocid1.volume.oc1.iad.aaaaaa"},
29-
{"fra", "aaaaaa", "ocid1.volume.oc1.eu-frankfurt-1.aaaaaa"},
29+
{"eu-frankfurt-1", "aaaaaa", "ocid1.volume.oc1.eu-frankfurt-1.aaaaaa"},
30+
{"uk-london-1", "aaaaaa", "ocid1.volume.oc1.uk-london-1.aaaaaa"},
3031
}
3132

3233
func TestDeriveVolumeOCID(t *testing.T) {

pkg/oci/instance/metadata/instance_metadata.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ const (
3030
// local OCI instance metadata API endpoint.
3131
// https://docs.us-phoenix-1.oraclecloud.com/Content/Compute/Tasks/gettingmetadata.htm
3232
type InstanceMetadata struct {
33-
CompartmentOCID string `json:"compartmentId"`
34-
Region string `json:"region"`
33+
CompartmentID string `json:"compartmentId"`
34+
Region string `json:"region"`
35+
CanonicalRegionName string `json:"canonicalRegionName"`
3536
}
3637

3738
// Interface defines how consumers access OCI instance metadata.

pkg/oci/instance/metadata/instance_metadata_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const exampleResponse = `{
3232
"ssh_authorized_keys" : "ssh-rsa some-key-data tlangfor@tlangfor-mac\n"
3333
},
3434
"region" : "phx",
35+
"canonicalRegionName" : "us-phoenix-1",
3536
"shape" : "VM.Standard1.1",
3637
"state" : "Provisioning",
3738
"timeCreated" : 1496415602152
@@ -49,8 +50,9 @@ func TestGetMetadata(t *testing.T) {
4950
}
5051

5152
expected := &InstanceMetadata{
52-
CompartmentOCID: "ocid1.compartment.oc1..aaaaaaaa3um2atybwhder4qttfhgon4j3hcxgmsvnyvx4flfjyewkkwfzwnq",
53-
Region: "phx",
53+
CompartmentID: "ocid1.compartment.oc1..aaaaaaaa3um2atybwhder4qttfhgon4j3hcxgmsvnyvx4flfjyewkkwfzwnq",
54+
Region: "phx",
55+
CanonicalRegionName: "us-phoenix-1",
5456
}
5557
if !reflect.DeepEqual(meta, expected) {
5658
t.Errorf("Get() => %+v, want %+v", meta, expected)

pkg/volume/provisioner/block/block_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,6 @@ func (p *MockProvisionerClient) Timeout() time.Duration {
272272
return 30 * time.Second
273273
}
274274

275-
// CompartmentOCID mocks client CompartmentOCID implementation
276-
func (p *MockProvisionerClient) CompartmentOCID() (compartmentOCID string) {
277-
return ""
278-
}
279-
280275
// TenancyOCID mocks client TenancyOCID implementation
281276
func (p *MockProvisionerClient) TenancyOCID() string {
282277
return "ocid1.tenancy.oc1..aaaaaaaatyn7scrtwtqedvgrxgr2xunzeo6uanvyhzxqblctwkrpisvke4kq"

pkg/volume/provisioner/core/provisioner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ func NewOCIProvisioner(
106106
if metadata == nil {
107107
return nil, errors.Wrap(mdErr, "unable to get compartment OCID")
108108
}
109-
logger.With("compartmentID", metadata.CompartmentOCID).Infof("'CompartmentID' not given. Using compartment OCID from instance metadata.")
110-
cfg.CompartmentID = metadata.CompartmentOCID
109+
logger.With("compartmentID", metadata.CompartmentID).Infof("'CompartmentID' not given. Using compartment OCID from instance metadata.")
110+
cfg.CompartmentID = metadata.CompartmentID
111111
}
112112

113113
cp, err := newConfigurationProvider(logger, cfg)

0 commit comments

Comments
 (0)