mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
Merge pull request #92356 from andyzhangx/azuredisk-tags
add tags support for azure disk driver
This commit is contained in:
commit
27687161d9
@ -35,6 +35,11 @@ import (
|
|||||||
"k8s.io/legacy-cloud-providers/azure"
|
"k8s.io/legacy-cloud-providers/azure"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
TagsDelimiter = ","
|
||||||
|
TagKeyValueDelimiter = "="
|
||||||
|
)
|
||||||
|
|
||||||
type azureDiskProvisioner struct {
|
type azureDiskProvisioner struct {
|
||||||
plugin *azureDataDiskPlugin
|
plugin *azureDataDiskPlugin
|
||||||
options volume.VolumeOptions
|
options volume.VolumeOptions
|
||||||
@ -118,6 +123,7 @@ func (p *azureDiskProvisioner) Provision(selectedNode *v1.Node, allowedTopologie
|
|||||||
diskIopsReadWrite string
|
diskIopsReadWrite string
|
||||||
diskMbpsReadWrite string
|
diskMbpsReadWrite string
|
||||||
diskEncryptionSetID string
|
diskEncryptionSetID string
|
||||||
|
customTags string
|
||||||
|
|
||||||
maxShares int
|
maxShares int
|
||||||
)
|
)
|
||||||
@ -164,6 +170,8 @@ func (p *azureDiskProvisioner) Provision(selectedNode *v1.Node, allowedTopologie
|
|||||||
diskMbpsReadWrite = v
|
diskMbpsReadWrite = v
|
||||||
case "diskencryptionsetid":
|
case "diskencryptionsetid":
|
||||||
diskEncryptionSetID = v
|
diskEncryptionSetID = v
|
||||||
|
case "tags":
|
||||||
|
customTags = v
|
||||||
case azure.WriteAcceleratorEnabled:
|
case azure.WriteAcceleratorEnabled:
|
||||||
writeAcceleratorEnabled = v
|
writeAcceleratorEnabled = v
|
||||||
case "maxshares":
|
case "maxshares":
|
||||||
@ -261,9 +269,14 @@ func (p *azureDiskProvisioner) Provision(selectedNode *v1.Node, allowedTopologie
|
|||||||
diskURI := ""
|
diskURI := ""
|
||||||
labels := map[string]string{}
|
labels := map[string]string{}
|
||||||
if kind == v1.AzureManagedDisk {
|
if kind == v1.AzureManagedDisk {
|
||||||
tags := make(map[string]string)
|
tags, err := ConvertTagsToMap(customTags)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if p.options.CloudTags != nil {
|
if p.options.CloudTags != nil {
|
||||||
tags = *(p.options.CloudTags)
|
for k, v := range *(p.options.CloudTags) {
|
||||||
|
tags[k] = v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if strings.EqualFold(writeAcceleratorEnabled, "true") {
|
if strings.EqualFold(writeAcceleratorEnabled, "true") {
|
||||||
tags[azure.WriteAcceleratorEnabled] = "true"
|
tags[azure.WriteAcceleratorEnabled] = "true"
|
||||||
@ -386,3 +399,28 @@ func (p *azureDiskProvisioner) Provision(selectedNode *v1.Node, allowedTopologie
|
|||||||
|
|
||||||
return pv, nil
|
return pv, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConvertTagsToMap convert the tags from string to map
|
||||||
|
// the valid tags fomat is "key1=value1,key2=value2", which could be converted to
|
||||||
|
// {"key1": "value1", "key2": "value2"}
|
||||||
|
func ConvertTagsToMap(tags string) (map[string]string, error) {
|
||||||
|
m := make(map[string]string)
|
||||||
|
if tags == "" {
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
s := strings.Split(tags, TagsDelimiter)
|
||||||
|
for _, tag := range s {
|
||||||
|
kv := strings.Split(tag, TagKeyValueDelimiter)
|
||||||
|
if len(kv) != 2 {
|
||||||
|
return nil, fmt.Errorf("Tags '%s' are invalid, the format should like: 'key1=value1,key2=value2'", tags)
|
||||||
|
}
|
||||||
|
key := strings.TrimSpace(kv[0])
|
||||||
|
if key == "" {
|
||||||
|
return nil, fmt.Errorf("Tags '%s' are invalid, the format should like: 'key1=value1,key2=value2'", tags)
|
||||||
|
}
|
||||||
|
value := strings.TrimSpace(kv[1])
|
||||||
|
m[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
@ -20,10 +20,11 @@ package azure_dd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseZoned(t *testing.T) {
|
func TestParseZoned(t *testing.T) {
|
||||||
@ -96,3 +97,69 @@ func TestParseZoned(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConvertTagsToMap(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
desc string
|
||||||
|
tags string
|
||||||
|
expectedOutput map[string]string
|
||||||
|
expectedError bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "should return empty map when tag is empty",
|
||||||
|
tags: "",
|
||||||
|
expectedOutput: map[string]string{},
|
||||||
|
expectedError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "sing valid tag should be converted",
|
||||||
|
tags: "key=value",
|
||||||
|
expectedOutput: map[string]string{
|
||||||
|
"key": "value",
|
||||||
|
},
|
||||||
|
expectedError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "multiple valid tags should be converted",
|
||||||
|
tags: "key1=value1,key2=value2",
|
||||||
|
expectedOutput: map[string]string{
|
||||||
|
"key1": "value1",
|
||||||
|
"key2": "value2",
|
||||||
|
},
|
||||||
|
expectedError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "whitespaces should be trimmed",
|
||||||
|
tags: "key1=value1, key2=value2",
|
||||||
|
expectedOutput: map[string]string{
|
||||||
|
"key1": "value1",
|
||||||
|
"key2": "value2",
|
||||||
|
},
|
||||||
|
expectedError: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "should return error for invalid format",
|
||||||
|
tags: "foo,bar",
|
||||||
|
expectedOutput: nil,
|
||||||
|
expectedError: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "should return error for when key is missed",
|
||||||
|
tags: "key1=value1,=bar",
|
||||||
|
expectedOutput: nil,
|
||||||
|
expectedError: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, c := range testCases {
|
||||||
|
m, err := ConvertTagsToMap(c.tags)
|
||||||
|
if c.expectedError {
|
||||||
|
assert.NotNil(t, err, "TestCase[%d]: %s", i, c.desc)
|
||||||
|
} else {
|
||||||
|
assert.Nil(t, err, "TestCase[%d]: %s", i, c.desc)
|
||||||
|
if !reflect.DeepEqual(m, c.expectedOutput) {
|
||||||
|
t.Errorf("got: %v, expected: %v, desc: %v", m, c.expectedOutput, c.desc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user