mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Implement GetLabelsForVolume for AzureDisk
This commit is contained in:
parent
28b6fb5f7d
commit
87c5883337
@ -59,6 +59,9 @@ var (
|
|||||||
defaultExcludeMasterFromStandardLB = true
|
defaultExcludeMasterFromStandardLB = true
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Azure implements PVLabeler.
|
||||||
|
var _ cloudprovider.PVLabeler = (*Cloud)(nil)
|
||||||
|
|
||||||
// Config holds the configuration parsed from the --cloud-config flag
|
// Config holds the configuration parsed from the --cloud-config flag
|
||||||
// All fields are required unless otherwise specified
|
// All fields are required unless otherwise specified
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
@ -17,16 +17,21 @@ limitations under the License.
|
|||||||
package azure
|
package azure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute"
|
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute"
|
||||||
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2017-10-01/storage"
|
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2017-10-01/storage"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
|
|
||||||
|
"k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/resource"
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
kwait "k8s.io/apimachinery/pkg/util/wait"
|
kwait "k8s.io/apimachinery/pkg/util/wait"
|
||||||
|
kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis"
|
||||||
|
"k8s.io/kubernetes/pkg/volume"
|
||||||
"k8s.io/kubernetes/pkg/volume/util"
|
"k8s.io/kubernetes/pkg/volume/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -201,3 +206,58 @@ func getResourceGroupFromDiskURI(diskURI string) (string, error) {
|
|||||||
}
|
}
|
||||||
return fields[4], nil
|
return fields[4], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetLabelsForVolume implements PVLabeler.GetLabelsForVolume
|
||||||
|
func (c *Cloud) GetLabelsForVolume(ctx context.Context, pv *v1.PersistentVolume) (map[string]string, error) {
|
||||||
|
// Ignore if not AzureDisk.
|
||||||
|
if pv.Spec.AzureDisk == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore any volumes that are being provisioned
|
||||||
|
if pv.Spec.AzureDisk.DiskName == volume.ProvisionedVolumeName {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.GetAzureDiskLabels(pv.Spec.AzureDisk.DataDiskURI)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAzureDiskLabels gets availability zone labels for Azuredisk.
|
||||||
|
func (c *Cloud) GetAzureDiskLabels(diskURI string) (map[string]string, error) {
|
||||||
|
// Get disk's resource group.
|
||||||
|
diskName := path.Base(diskURI)
|
||||||
|
resourceGroup, err := getResourceGroupFromDiskURI(diskURI)
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf("Failed to get resource group for AzureDisk %q: %v", diskName, err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get information of the disk.
|
||||||
|
ctx, cancel := getContextWithCancel()
|
||||||
|
defer cancel()
|
||||||
|
disk, err := c.DisksClient.Get(ctx, resourceGroup, diskName)
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf("Failed to get information for AzureDisk %q: %v", diskName, err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check whether availability zone is specified.
|
||||||
|
if disk.Zones == nil || len(*disk.Zones) == 0 {
|
||||||
|
glog.V(4).Infof("Azure disk %q is not zoned", diskName)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
zones := *disk.Zones
|
||||||
|
zoneID, err := strconv.Atoi(zones[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse zone %v for AzureDisk %v: %v", zones, diskName, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
zone := c.makeZone(zoneID)
|
||||||
|
glog.V(4).Infof("Get zone %q for Azure disk %q", zone, diskName)
|
||||||
|
labels := map[string]string{
|
||||||
|
kubeletapis.LabelZoneRegion: c.Location,
|
||||||
|
kubeletapis.LabelZoneFailureDomain: zone,
|
||||||
|
}
|
||||||
|
return labels, nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user