-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We cannot definitively determine whether a node exists within our zone without the provider ID string.
- Loading branch information
1 parent
2f186d6
commit 68fbb88
Showing
5 changed files
with
221 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright 2014 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. | ||
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 openstack | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// If Instances.InstanceID or cloudprovider.GetInstanceProviderID is changed, the regexp should be changed too. | ||
var providerIDRegexp = regexp.MustCompile(`^` + ProviderName + `://([^/]*)/([^/]+)$`) | ||
|
||
// instanceIDFromProviderID splits a provider's id and return instanceID. | ||
// A providerID is build out of '${ProviderName}:///${instance-id}' which contains ':///'. | ||
// or '${ProviderName}://${region}/${instance-id}' which contains '://'. | ||
// See cloudprovider.GetInstanceProviderID and Instances.InstanceID. | ||
func instanceIDFromProviderID(providerID string) (instanceID string, region string, err error) { | ||
|
||
// https://github.com/kubernetes/kubernetes/issues/85731 | ||
if providerID != "" && !strings.Contains(providerID, "://") { | ||
providerID = ProviderName + "://" + providerID | ||
} | ||
|
||
matches := providerIDRegexp.FindStringSubmatch(providerID) | ||
if len(matches) != 3 { | ||
return "", "", fmt.Errorf("ProviderID \"%s\" didn't match expected format \"openstack://region/InstanceID\"", providerID) | ||
} | ||
return matches[2], matches[1], nil | ||
} | ||
|
||
// Return true if instance is not openstack. | ||
func isNodeUnmanaged(providerID string) bool { | ||
if providerID != "" { | ||
return strings.Contains(providerID, "://") && !strings.HasPrefix(providerID, ProviderName+"://") | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
Copyright 2022 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. | ||
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 openstack | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_instanceIDFromProviderID(t *testing.T) { | ||
type args struct { | ||
providerID string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantInstanceID string | ||
wantRegion string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "it parses region & instanceID correctly from providerID", | ||
args: args{ | ||
providerID: "openstack://us-east-1/testInstanceID", | ||
}, | ||
wantInstanceID: "testInstanceID", | ||
wantRegion: "us-east-1", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "it parses instanceID if providerID has empty protocol & no region", | ||
args: args{ | ||
providerID: "/testInstanceID", | ||
}, | ||
wantInstanceID: "testInstanceID", | ||
wantRegion: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "it returns error in case of invalid providerID format with no region", | ||
args: args{ | ||
providerID: "openstack://us-east-1-testInstanceID", | ||
}, | ||
wantInstanceID: "", | ||
wantRegion: "", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "it parses correct instanceID in case the region name is the empty string", | ||
args: args{ | ||
providerID: "openstack:///testInstanceID", | ||
}, | ||
wantInstanceID: "testInstanceID", | ||
wantRegion: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "it appends openstack:// in case of missing protocol in providerID", | ||
args: args{ | ||
providerID: "us-east-1/testInstanceID", | ||
}, | ||
wantInstanceID: "testInstanceID", | ||
wantRegion: "us-east-1", | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotInstanceID, gotRegion, err := instanceIDFromProviderID(tt.args.providerID) | ||
assert.Equal(t, tt.wantInstanceID, gotInstanceID) | ||
assert.Equal(t, tt.wantRegion, gotRegion) | ||
if tt.wantErr == true { | ||
assert.ErrorContains(t, err, "didn't match expected format") | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_instanceNodeUnmanaged(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
providerID string | ||
wantResult bool | ||
}{ | ||
{ | ||
name: "openstack node, providerID not set yet", | ||
providerID: "", | ||
wantResult: false, | ||
}, | ||
{ | ||
name: "openstack node in case of invalid providerID format with no region", | ||
providerID: "openstack://us-east-1-testInstanceID", | ||
wantResult: false, | ||
}, | ||
{ | ||
name: "openstack node, it parses instanceID has empty protocol & no region", | ||
providerID: "/testInstanceID", | ||
wantResult: false, | ||
}, | ||
{ | ||
name: "openstack node, it parses correct instanceID in case the region name is the empty string", | ||
providerID: "openstack:///testInstanceID", | ||
wantResult: false, | ||
}, | ||
{ | ||
name: "openstack node, it parses correct instanceID with region name", | ||
providerID: "openstack://region/testInstanceID", | ||
wantResult: false, | ||
}, | ||
{ | ||
name: "openstack node in case of missing protocol in providerID", | ||
providerID: "us-east-1/testInstanceID", | ||
wantResult: false, | ||
}, | ||
{ | ||
name: "non openstack node, providerID has non openstack protocol", | ||
providerID: "provider:///us-east-1-testInstanceID", | ||
wantResult: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
res := isNodeUnmanaged(tt.providerID) | ||
assert.Equal(t, tt.wantResult, res) | ||
}) | ||
} | ||
} |