Support GetLabelsForVolume In OpenStack

This commit is contained in:
edisonxiang 2018-01-26 17:19:49 +08:00
parent d352fc6f3f
commit 9326e845e4
2 changed files with 45 additions and 12 deletions

View File

@ -24,6 +24,7 @@ go_library(
"//pkg/apis/core/v1/helper:go_default_library", "//pkg/apis/core/v1/helper:go_default_library",
"//pkg/cloudprovider:go_default_library", "//pkg/cloudprovider:go_default_library",
"//pkg/controller:go_default_library", "//pkg/controller:go_default_library",
"//pkg/kubelet/apis:go_default_library",
"//pkg/util/mount:go_default_library", "//pkg/util/mount:go_default_library",
"//pkg/volume:go_default_library", "//pkg/volume:go_default_library",
"//pkg/volume/util:go_default_library", "//pkg/volume/util:go_default_library",

View File

@ -25,8 +25,11 @@ import (
"strings" "strings"
"time" "time"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
"k8s.io/kubernetes/pkg/cloudprovider"
kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis"
k8s_volume "k8s.io/kubernetes/pkg/volume" k8s_volume "k8s.io/kubernetes/pkg/volume"
volumeutil "k8s.io/kubernetes/pkg/volume/util" volumeutil "k8s.io/kubernetes/pkg/volume/util"
@ -71,6 +74,8 @@ type Volume struct {
AttachedServerId string AttachedServerId string
// Device file path // Device file path
AttachedDevice string AttachedDevice string
// AvailabilityZone is which availability zone the volume is in
AvailabilityZone string
// Unique identifier for the volume. // Unique identifier for the volume.
ID string ID string
// Human-readable display name for the volume. // Human-readable display name for the volume.
@ -89,6 +94,9 @@ type VolumeCreateOpts struct {
Metadata map[string]string Metadata map[string]string
} }
// implements PVLabeler.
var _ cloudprovider.PVLabeler = (*OpenStack)(nil)
const ( const (
VolumeAvailableStatus = "available" VolumeAvailableStatus = "available"
VolumeInUseStatus = "in-use" VolumeInUseStatus = "in-use"
@ -171,6 +179,7 @@ func (volumes *VolumesV1) getVolume(volumeID string) (Volume, error) {
} }
volume := Volume{ volume := Volume{
AvailabilityZone: volumeV1.AvailabilityZone,
ID: volumeV1.ID, ID: volumeV1.ID,
Name: volumeV1.Name, Name: volumeV1.Name,
Status: volumeV1.Status, Status: volumeV1.Status,
@ -195,6 +204,7 @@ func (volumes *VolumesV2) getVolume(volumeID string) (Volume, error) {
} }
volume := Volume{ volume := Volume{
AvailabilityZone: volumeV2.AvailabilityZone,
ID: volumeV2.ID, ID: volumeV2.ID,
Name: volumeV2.Name, Name: volumeV2.Name,
Status: volumeV2.Status, Status: volumeV2.Status,
@ -219,10 +229,10 @@ func (volumes *VolumesV3) getVolume(volumeID string) (Volume, error) {
} }
volume := Volume{ volume := Volume{
AvailabilityZone: volumeV3.AvailabilityZone,
ID: volumeV3.ID, ID: volumeV3.ID,
Name: volumeV3.Name, Name: volumeV3.Name,
Status: volumeV3.Status, Status: volumeV3.Status,
Size: volumeV3.Size,
} }
if len(volumeV3.Attachments) > 0 { if len(volumeV3.Attachments) > 0 {
@ -680,6 +690,28 @@ func (os *OpenStack) ShouldTrustDevicePath() bool {
return os.bsOpts.TrustDevicePath 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 // recordOpenstackOperationMetric records openstack operation metrics
func recordOpenstackOperationMetric(operation string, timeTaken float64, err error) { func recordOpenstackOperationMetric(operation string, timeTaken float64, err error) {
if err != nil { if err != nil {