Merge pull request #58871 from edisonxiang/supportGetLabelsForVolume
Automatic merge from submit-queue (batch tested with PRs 59012, 58871). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Support GetLabelsForVolume In OpenStack **What this PR does / why we need it**: Since PersistentVolumeLabelController will invoke ```GetLabelsForVolume``` interface in Cloud-Controller-Manager, OpenStack Provider should support it. https://github.com/kubernetes/kubernetes/blob/master/pkg/cloudprovider/cloud.go#L213 **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes #58870 **Special notes for your reviewer**: **Release note**: ```release-note Support GetLabelsForVolume in OpenStack Provider ```
This commit is contained in:
		@@ -24,6 +24,7 @@ go_library(
 | 
			
		||||
        "//pkg/apis/core/v1/helper:go_default_library",
 | 
			
		||||
        "//pkg/cloudprovider:go_default_library",
 | 
			
		||||
        "//pkg/controller:go_default_library",
 | 
			
		||||
        "//pkg/kubelet/apis:go_default_library",
 | 
			
		||||
        "//pkg/util/mount:go_default_library",
 | 
			
		||||
        "//pkg/volume:go_default_library",
 | 
			
		||||
        "//pkg/volume/util:go_default_library",
 | 
			
		||||
 
 | 
			
		||||
@@ -25,8 +25,11 @@ import (
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"k8s.io/api/core/v1"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/api/resource"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/types"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/cloudprovider"
 | 
			
		||||
	kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis"
 | 
			
		||||
	k8s_volume "k8s.io/kubernetes/pkg/volume"
 | 
			
		||||
	volumeutil "k8s.io/kubernetes/pkg/volume/util"
 | 
			
		||||
 | 
			
		||||
@@ -71,6 +74,8 @@ type Volume struct {
 | 
			
		||||
	AttachedServerId string
 | 
			
		||||
	// Device file path
 | 
			
		||||
	AttachedDevice string
 | 
			
		||||
	// AvailabilityZone is which availability zone the volume is in
 | 
			
		||||
	AvailabilityZone string
 | 
			
		||||
	// Unique identifier for the volume.
 | 
			
		||||
	ID string
 | 
			
		||||
	// Human-readable display name for the volume.
 | 
			
		||||
@@ -89,6 +94,9 @@ type VolumeCreateOpts struct {
 | 
			
		||||
	Metadata     map[string]string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// implements PVLabeler.
 | 
			
		||||
var _ cloudprovider.PVLabeler = (*OpenStack)(nil)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	VolumeAvailableStatus = "available"
 | 
			
		||||
	VolumeInUseStatus     = "in-use"
 | 
			
		||||
@@ -171,10 +179,11 @@ func (volumes *VolumesV1) getVolume(volumeID string) (Volume, error) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	volume := Volume{
 | 
			
		||||
		ID:     volumeV1.ID,
 | 
			
		||||
		Name:   volumeV1.Name,
 | 
			
		||||
		Status: volumeV1.Status,
 | 
			
		||||
		Size:   volumeV1.Size,
 | 
			
		||||
		AvailabilityZone: volumeV1.AvailabilityZone,
 | 
			
		||||
		ID:               volumeV1.ID,
 | 
			
		||||
		Name:             volumeV1.Name,
 | 
			
		||||
		Status:           volumeV1.Status,
 | 
			
		||||
		Size:             volumeV1.Size,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(volumeV1.Attachments) > 0 && volumeV1.Attachments[0]["server_id"] != nil {
 | 
			
		||||
@@ -195,10 +204,11 @@ func (volumes *VolumesV2) getVolume(volumeID string) (Volume, error) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	volume := Volume{
 | 
			
		||||
		ID:     volumeV2.ID,
 | 
			
		||||
		Name:   volumeV2.Name,
 | 
			
		||||
		Status: volumeV2.Status,
 | 
			
		||||
		Size:   volumeV2.Size,
 | 
			
		||||
		AvailabilityZone: volumeV2.AvailabilityZone,
 | 
			
		||||
		ID:               volumeV2.ID,
 | 
			
		||||
		Name:             volumeV2.Name,
 | 
			
		||||
		Status:           volumeV2.Status,
 | 
			
		||||
		Size:             volumeV2.Size,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(volumeV2.Attachments) > 0 {
 | 
			
		||||
@@ -219,10 +229,10 @@ func (volumes *VolumesV3) getVolume(volumeID string) (Volume, error) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	volume := Volume{
 | 
			
		||||
		ID:     volumeV3.ID,
 | 
			
		||||
		Name:   volumeV3.Name,
 | 
			
		||||
		Status: volumeV3.Status,
 | 
			
		||||
		Size:   volumeV3.Size,
 | 
			
		||||
		AvailabilityZone: volumeV3.AvailabilityZone,
 | 
			
		||||
		ID:               volumeV3.ID,
 | 
			
		||||
		Name:             volumeV3.Name,
 | 
			
		||||
		Status:           volumeV3.Status,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(volumeV3.Attachments) > 0 {
 | 
			
		||||
@@ -680,6 +690,28 @@ func (os *OpenStack) ShouldTrustDevicePath() bool {
 | 
			
		||||
	return os.bsOpts.TrustDevicePath
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetLabelsForVolume implements PVLabeler.GetLabelsForVolume
 | 
			
		||||
func (os *OpenStack) GetLabelsForVolume(pv *v1.PersistentVolume) (map[string]string, error) {
 | 
			
		||||
	// Ignore any volumes that are being provisioned
 | 
			
		||||
	if pv.Spec.Cinder.VolumeID == k8s_volume.ProvisionedVolumeName {
 | 
			
		||||
		return nil, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Get Volume
 | 
			
		||||
	volume, err := os.getVolume(pv.Spec.Cinder.VolumeID)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Contruct Volume Labels
 | 
			
		||||
	labels := make(map[string]string)
 | 
			
		||||
	labels[kubeletapis.LabelZoneFailureDomain] = volume.AvailabilityZone
 | 
			
		||||
	labels[kubeletapis.LabelZoneRegion] = os.region
 | 
			
		||||
	glog.V(4).Infof("The Volume %s has labels %v", pv.Spec.Cinder.VolumeID, labels)
 | 
			
		||||
 | 
			
		||||
	return labels, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// recordOpenstackOperationMetric records openstack operation metrics
 | 
			
		||||
func recordOpenstackOperationMetric(operation string, timeTaken float64, err error) {
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user