mirror of https://github.com/k3s-io/k3s
Merge pull request #74882 from feiskyer/convert-rg-to-lower
Convert resource group name in Azure provider ID to lower casespull/564/head
commit
e330c0120e
|
@ -423,6 +423,9 @@ func parseConfig(configReader io.Reader) (*Config, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The resource group name may be in different cases from different Azure APIs, hence it is converted to lower here.
|
||||||
|
// See more context at https://github.com/kubernetes/kubernetes/issues/71994.
|
||||||
|
config.ResourceGroup = strings.ToLower(config.ResourceGroup)
|
||||||
return &config, nil
|
return &config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -578,7 +581,7 @@ func (az *Cloud) updateNodeCaches(prevNode, newNode *v1.Node) {
|
||||||
// Add to nodeResourceGroups cache.
|
// Add to nodeResourceGroups cache.
|
||||||
newRG, ok := newNode.ObjectMeta.Labels[externalResourceGroupLabel]
|
newRG, ok := newNode.ObjectMeta.Labels[externalResourceGroupLabel]
|
||||||
if ok && len(newRG) > 0 {
|
if ok && len(newRG) > 0 {
|
||||||
az.nodeResourceGroups[newNode.ObjectMeta.Name] = newRG
|
az.nodeResourceGroups[newNode.ObjectMeta.Name] = strings.ToLower(newRG)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add to unmanagedNodes cache.
|
// Add to unmanagedNodes cache.
|
||||||
|
@ -677,7 +680,7 @@ func (az *Cloud) GetUnmanagedNodes() (sets.String, error) {
|
||||||
// ShouldNodeExcludedFromLoadBalancer returns true if node is unmanaged or in external resource group.
|
// ShouldNodeExcludedFromLoadBalancer returns true if node is unmanaged or in external resource group.
|
||||||
func (az *Cloud) ShouldNodeExcludedFromLoadBalancer(node *v1.Node) bool {
|
func (az *Cloud) ShouldNodeExcludedFromLoadBalancer(node *v1.Node) bool {
|
||||||
labels := node.ObjectMeta.Labels
|
labels := node.ObjectMeta.Labels
|
||||||
if rg, ok := labels[externalResourceGroupLabel]; ok && rg != az.ResourceGroup {
|
if rg, ok := labels[externalResourceGroupLabel]; ok && !strings.EqualFold(rg, az.ResourceGroup) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -246,7 +246,7 @@ func (az *Cloud) InstanceID(ctx context.Context, name types.NodeName) (string, e
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get resource group name.
|
// Get resource group name.
|
||||||
resourceGroup := metadata.Compute.ResourceGroup
|
resourceGroup := strings.ToLower(metadata.Compute.ResourceGroup)
|
||||||
|
|
||||||
// Compose instanceID based on nodeName for standard instance.
|
// Compose instanceID based on nodeName for standard instance.
|
||||||
if az.VMType == vmTypeStandard {
|
if az.VMType == vmTypeStandard {
|
||||||
|
|
|
@ -17,6 +17,7 @@ limitations under the License.
|
||||||
package azure
|
package azure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
@ -45,7 +46,7 @@ type metricContext struct {
|
||||||
func newMetricContext(prefix, request, resourceGroup, subscriptionID string) *metricContext {
|
func newMetricContext(prefix, request, resourceGroup, subscriptionID string) *metricContext {
|
||||||
return &metricContext{
|
return &metricContext{
|
||||||
start: time.Now(),
|
start: time.Now(),
|
||||||
attributes: []string{prefix + "_" + request, resourceGroup, subscriptionID},
|
attributes: []string{prefix + "_" + request, strings.ToLower(resourceGroup), subscriptionID},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -70,7 +70,7 @@ func (az *Cloud) getStandardMachineID(resourceGroup, machineName string) string
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
machineIDTemplate,
|
machineIDTemplate,
|
||||||
az.SubscriptionID,
|
az.SubscriptionID,
|
||||||
resourceGroup,
|
strings.ToLower(resourceGroup),
|
||||||
machineName)
|
machineName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -339,7 +339,14 @@ func (as *availabilitySet) GetInstanceIDByNodeName(name string) (string, error)
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return *machine.ID, nil
|
|
||||||
|
resourceID := *machine.ID
|
||||||
|
convertedResourceID, err := convertResourceGroupNameToLower(resourceID)
|
||||||
|
if err != nil {
|
||||||
|
klog.Errorf("convertResourceGroupNameToLower failed with error: %v", err)
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return convertedResourceID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPowerStatusByNodeName returns the power state of the specified node.
|
// GetPowerStatusByNodeName returns the power state of the specified node.
|
||||||
|
|
|
@ -99,7 +99,7 @@ func TestParseConfig(t *testing.T) {
|
||||||
MaximumLoadBalancerRuleCount: 1,
|
MaximumLoadBalancerRuleCount: 1,
|
||||||
PrimaryAvailabilitySetName: "primaryAvailabilitySetName",
|
PrimaryAvailabilitySetName: "primaryAvailabilitySetName",
|
||||||
PrimaryScaleSetName: "primaryScaleSetName",
|
PrimaryScaleSetName: "primaryScaleSetName",
|
||||||
ResourceGroup: "resourceGroup",
|
ResourceGroup: "resourcegroup",
|
||||||
RouteTableName: "routeTableName",
|
RouteTableName: "routeTableName",
|
||||||
SecurityGroupName: "securityGroupName",
|
SecurityGroupName: "securityGroupName",
|
||||||
SubnetName: "subnetName",
|
SubnetName: "subnetName",
|
||||||
|
|
|
@ -185,7 +185,13 @@ func (ss *scaleSet) GetInstanceIDByNodeName(name string) (string, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return *vm.ID, nil
|
resourceID := *vm.ID
|
||||||
|
convertedResourceID, err := convertResourceGroupNameToLower(resourceID)
|
||||||
|
if err != nil {
|
||||||
|
klog.Errorf("convertResourceGroupNameToLower failed with error: %v", err)
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return convertedResourceID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNodeNameByProviderID gets the node name by provider ID.
|
// GetNodeNameByProviderID gets the node name by provider ID.
|
||||||
|
@ -980,7 +986,7 @@ func (az *Cloud) getVmssMachineID(resourceGroup, scaleSetName, instanceID string
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
vmssMachineIDTemplate,
|
vmssMachineIDTemplate,
|
||||||
az.SubscriptionID,
|
az.SubscriptionID,
|
||||||
resourceGroup,
|
strings.ToLower(resourceGroup),
|
||||||
scaleSetName,
|
scaleSetName,
|
||||||
instanceID)
|
instanceID)
|
||||||
}
|
}
|
||||||
|
|
|
@ -152,7 +152,7 @@ func (ss *scaleSet) newAvailabilitySetNodesCache() (*timedCache, error) {
|
||||||
|
|
||||||
func buildVmssCacheKey(resourceGroup, name string) string {
|
func buildVmssCacheKey(resourceGroup, name string) string {
|
||||||
// key is composed of <resourceGroup>#<vmName>
|
// key is composed of <resourceGroup>#<vmName>
|
||||||
return fmt.Sprintf("%s%s%s", resourceGroup, vmssCacheSeparator, name)
|
return fmt.Sprintf("%s%s%s", strings.ToLower(resourceGroup), vmssCacheSeparator, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractVmssCacheKey(key string) (string, string, error) {
|
func extractVmssCacheKey(key string) (string, string, error) {
|
||||||
|
|
|
@ -37,7 +37,8 @@ var (
|
||||||
nsgCacheTTL = 2 * time.Minute
|
nsgCacheTTL = 2 * time.Minute
|
||||||
rtCacheTTL = 2 * time.Minute
|
rtCacheTTL = 2 * time.Minute
|
||||||
|
|
||||||
azureNodeProviderIDRE = regexp.MustCompile(`^azure:///subscriptions/(?:.*)/resourceGroups/(?:.*)/providers/Microsoft.Compute/(?:.*)`)
|
azureNodeProviderIDRE = regexp.MustCompile(`^azure:///subscriptions/(?:.*)/resourceGroups/(?:.*)/providers/Microsoft.Compute/(?:.*)`)
|
||||||
|
azureResourceGroupNameRE = regexp.MustCompile(`.*/subscriptions/(?:.*)/resourceGroups/(.+)/providers/(?:.*)`)
|
||||||
)
|
)
|
||||||
|
|
||||||
// checkExistsFromError inspects an error and returns a true if err is nil,
|
// checkExistsFromError inspects an error and returns a true if err is nil,
|
||||||
|
@ -303,3 +304,14 @@ func (az *Cloud) IsNodeUnmanaged(nodeName string) (bool, error) {
|
||||||
func (az *Cloud) IsNodeUnmanagedByProviderID(providerID string) bool {
|
func (az *Cloud) IsNodeUnmanagedByProviderID(providerID string) bool {
|
||||||
return !azureNodeProviderIDRE.Match([]byte(providerID))
|
return !azureNodeProviderIDRE.Match([]byte(providerID))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// convertResourceGroupNameToLower converts the resource group name in the resource ID to be lowered.
|
||||||
|
func convertResourceGroupNameToLower(resourceID string) (string, error) {
|
||||||
|
matches := azureResourceGroupNameRE.FindStringSubmatch(resourceID)
|
||||||
|
if len(matches) != 2 {
|
||||||
|
return "", fmt.Errorf("%q isn't in Azure resource ID format %q", resourceID, azureResourceGroupNameRE.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
resourceGroup := matches[1]
|
||||||
|
return strings.Replace(resourceID, resourceGroup, strings.ToLower(resourceGroup), 1), nil
|
||||||
|
}
|
||||||
|
|
|
@ -142,3 +142,59 @@ func TestIsNodeUnmanagedByProviderID(t *testing.T) {
|
||||||
assert.Equal(t, test.expected, isUnmanagedNode, test.providerID)
|
assert.Equal(t, test.expected, isUnmanagedNode, test.providerID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConvertResourceGroupNameToLower(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
desc string
|
||||||
|
resourceID string
|
||||||
|
expected string
|
||||||
|
expectError bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "empty string should report error",
|
||||||
|
resourceID: "",
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "resourceID not in Azure format should report error",
|
||||||
|
resourceID: "invalid-id",
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "providerID not in Azure format should report error",
|
||||||
|
resourceID: "azure://invalid-id",
|
||||||
|
expectError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "resource group name in VM providerID should be converted",
|
||||||
|
resourceID: CloudProviderName + ":///subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroupName/providers/Microsoft.Compute/virtualMachines/k8s-agent-AAAAAAAA-0",
|
||||||
|
expected: CloudProviderName + ":///subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroupname/providers/Microsoft.Compute/virtualMachines/k8s-agent-AAAAAAAA-0",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "resource group name in VM resourceID should be converted",
|
||||||
|
resourceID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroupName/providers/Microsoft.Compute/virtualMachines/k8s-agent-AAAAAAAA-0",
|
||||||
|
expected: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroupname/providers/Microsoft.Compute/virtualMachines/k8s-agent-AAAAAAAA-0",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "resource group name in VMSS providerID should be converted",
|
||||||
|
resourceID: CloudProviderName + ":///subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSetName/virtualMachines/156",
|
||||||
|
expected: CloudProviderName + ":///subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroupname/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSetName/virtualMachines/156",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "resource group name in VMSS resourceID should be converted",
|
||||||
|
resourceID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroupName/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSetName/virtualMachines/156",
|
||||||
|
expected: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroupname/providers/Microsoft.Compute/virtualMachineScaleSets/myScaleSetName/virtualMachines/156",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
real, err := convertResourceGroupNameToLower(test.resourceID)
|
||||||
|
if test.expectError {
|
||||||
|
assert.NotNil(t, err, test.desc)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Nil(t, err, test.desc)
|
||||||
|
assert.Equal(t, test.expected, real, test.desc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue