Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding default StorageClass annotation printout for resource_printer and describer and some refactoring #34638

Merged
merged 1 commit into from Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions pkg/api/helpers.go
Expand Up @@ -236,6 +236,20 @@ var standardFinalizers = sets.NewString(
FinalizerOrphan,
)

// HasAnnotation returns a bool if passed in annotation exists
func HasAnnotation(obj ObjectMeta, ann string) bool {
_, found := obj.Annotations[ann]
return found
}

// SetMetaDataAnnotation sets the annotation and value
func SetMetaDataAnnotation(obj *ObjectMeta, ann string, value string) {
if obj.Annotations == nil {
obj.Annotations = make(map[string]string)
}
obj.Annotations[ann] = value
}

func IsStandardFinalizerName(str string) bool {
return standardFinalizers.Has(str)
}
Expand Down
136 changes: 136 additions & 0 deletions pkg/apis/storage/util/helpers.go
@@ -0,0 +1,136 @@
/*
Copyright 2016 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"k8s.io/kubernetes/pkg/api"
)

// IsDefaultStorageClassAnnotation represents a StorageClass annotation that
// marks a class as the default StorageClass
//TODO: Update IsDefaultStorageClassannotation and remove Beta when no longer used
const IsDefaultStorageClassAnnotation = "storageclass.beta.kubernetes.io/is-default-class"
const BetaIsDefaultStorageClassAnnotation = "storageclass.beta.kubernetes.io/is-default-class"

// AlphaStorageClassAnnotation represents the previous alpha storage class
// annotation. it's no longer used and held here for posterity.
const AlphaStorageClassAnnotation = "volume.alpha.kubernetes.io/storage-class"

// BetaStorageClassAnnotation represents the beta/previous StorageClass annotation.
// It's currently still used and will be held for backwards compatibility
const BetaStorageClassAnnotation = "volume.beta.kubernetes.io/storage-class"

// StorageClassAnnotation represents the storage class associated with a resource.
// It currently matches the Beta value and can change when official is set.
// - in PersistentVolumeClaim it represents required class to match.
// Only PersistentVolumes with the same class (i.e. annotation with the same
// value) can be bound to the claim. In case no such volume exists, the
// controller will provision a new one using StorageClass instance with
// the same name as the annotation value.
// - in PersistentVolume it represents storage class to which the persistent
// volume belongs.
//TODO: Update this to final annotation value as it matches BetaStorageClassAnnotation for now
const StorageClassAnnotation = "volume.beta.kubernetes.io/storage-class"

// GetVolumeStorageClass returns value of StorageClassAnnotation or empty string in case
// the annotation does not exist.
// TODO: change to PersistentVolume.Spec.Class value when this attribute is
// introduced.
func GetVolumeStorageClass(volume *api.PersistentVolume) string {
if class, found := volume.Annotations[StorageClassAnnotation]; found {
return class
}

// 'nil' is interpreted as "", i.e. the volume does not belong to any class.
return ""
}

// GetClaimStorageClass returns name of class that is requested by given claim.
// Request for `nil` class is interpreted as request for class "",
// i.e. for a classless PV.
// TODO: change to PersistentVolumeClaim.Spec.Class value when this
// attribute is introduced.
func GetClaimStorageClass(claim *api.PersistentVolumeClaim) string {
if class, found := claim.Annotations[StorageClassAnnotation]; found {
return class
}

return ""
}

// GetStorageClassAnnotation returns the StorageClass value
// if the annotation is set, empty string if not
// TODO: remove Alpha and Beta when no longer used or needed
func GetStorageClassAnnotation(obj api.ObjectMeta) string {
if class, ok := obj.Annotations[StorageClassAnnotation]; ok {
return class
}
if class, ok := obj.Annotations[BetaStorageClassAnnotation]; ok {
return class
}
if class, ok := obj.Annotations[AlphaStorageClassAnnotation]; ok {
return class
}

return ""
}

// HasStorageClassAnnotation returns a boolean
// if the annotation is set
// TODO: remove Alpha and Beta when no longer used or needed
func HasStorageClassAnnotation(obj api.ObjectMeta) bool {
if _, found := obj.Annotations[StorageClassAnnotation]; found {
return found
}
if _, found := obj.Annotations[BetaStorageClassAnnotation]; found {
return found
}
if _, found := obj.Annotations[AlphaStorageClassAnnotation]; found {
return found
}

return false

}

// IsDefaultAnnotationText returns a pretty Yes/No String if
// the annotation is set
// TODO: remove Beta when no longer needed
func IsDefaultAnnotationText(obj api.ObjectMeta) string {
if obj.Annotations[IsDefaultStorageClassAnnotation] == "true" {
return "Yes"
}
if obj.Annotations[BetaIsDefaultStorageClassAnnotation] == "true" {
return "Yes"
}

return "No"
}

// IsDefaultAnnotation returns a boolean if
// the annotation is set
// TODO: remove Beta when no longer needed
func IsDefaultAnnotation(obj api.ObjectMeta) bool {
if obj.Annotations[IsDefaultStorageClassAnnotation] == "true" {
return true
}
if obj.Annotations[BetaIsDefaultStorageClassAnnotation] == "true" {
return true
}

return false
}
21 changes: 11 additions & 10 deletions pkg/controller/volume/persistentvolume/binder_test.go
Expand Up @@ -21,6 +21,7 @@ import (

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/storage"
storageutil "k8s.io/kubernetes/pkg/apis/storage/util"
)

// Test single call to syncClaim and syncVolume methods.
Expand Down Expand Up @@ -430,26 +431,26 @@ func TestSync(t *testing.T) {
"13-1 - binding to class",
[]*api.PersistentVolume{
newVolume("volume13-1-1", "1Gi", "", "", api.VolumePending, api.PersistentVolumeReclaimRetain),
newVolume("volume13-1-2", "10Gi", "", "", api.VolumePending, api.PersistentVolumeReclaimRetain, annClass),
newVolume("volume13-1-2", "10Gi", "", "", api.VolumePending, api.PersistentVolumeReclaimRetain, storageutil.StorageClassAnnotation),
},
[]*api.PersistentVolume{
newVolume("volume13-1-1", "1Gi", "", "", api.VolumePending, api.PersistentVolumeReclaimRetain),
newVolume("volume13-1-2", "10Gi", "uid13-1", "claim13-1", api.VolumeBound, api.PersistentVolumeReclaimRetain, annBoundByController, annClass),
newVolume("volume13-1-2", "10Gi", "uid13-1", "claim13-1", api.VolumeBound, api.PersistentVolumeReclaimRetain, annBoundByController, storageutil.StorageClassAnnotation),
},
newClaimArray("claim13-1", "uid13-1", "1Gi", "", api.ClaimPending, annClass),
withExpectedCapacity("10Gi", newClaimArray("claim13-1", "uid13-1", "1Gi", "volume13-1-2", api.ClaimBound, annBoundByController, annClass, annBindCompleted)),
newClaimArray("claim13-1", "uid13-1", "1Gi", "", api.ClaimPending, storageutil.StorageClassAnnotation),
withExpectedCapacity("10Gi", newClaimArray("claim13-1", "uid13-1", "1Gi", "volume13-1-2", api.ClaimBound, annBoundByController, storageutil.StorageClassAnnotation, annBindCompleted)),
noevents, noerrors, testSyncClaim,
},
{
// syncVolume binds a claim without a class even if there is a
// smaller PV with a class available
"13-2 - binding without a class",
[]*api.PersistentVolume{
newVolume("volume13-2-1", "1Gi", "", "", api.VolumePending, api.PersistentVolumeReclaimRetain, annClass),
newVolume("volume13-2-1", "1Gi", "", "", api.VolumePending, api.PersistentVolumeReclaimRetain, storageutil.StorageClassAnnotation),
newVolume("volume13-2-2", "10Gi", "", "", api.VolumePending, api.PersistentVolumeReclaimRetain),
},
[]*api.PersistentVolume{
newVolume("volume13-2-1", "1Gi", "", "", api.VolumePending, api.PersistentVolumeReclaimRetain, annClass),
newVolume("volume13-2-1", "1Gi", "", "", api.VolumePending, api.PersistentVolumeReclaimRetain, storageutil.StorageClassAnnotation),
newVolume("volume13-2-2", "10Gi", "uid13-2", "claim13-2", api.VolumeBound, api.PersistentVolumeReclaimRetain, annBoundByController),
},
newClaimArray("claim13-2", "uid13-2", "1Gi", "", api.ClaimPending),
Expand All @@ -462,14 +463,14 @@ func TestSync(t *testing.T) {
"13-3 - binding to specific a class",
volumeWithClass("silver", []*api.PersistentVolume{
newVolume("volume13-3-1", "1Gi", "", "", api.VolumePending, api.PersistentVolumeReclaimRetain),
newVolume("volume13-3-2", "10Gi", "", "", api.VolumePending, api.PersistentVolumeReclaimRetain, annClass),
newVolume("volume13-3-2", "10Gi", "", "", api.VolumePending, api.PersistentVolumeReclaimRetain, storageutil.StorageClassAnnotation),
}),
volumeWithClass("silver", []*api.PersistentVolume{
newVolume("volume13-3-1", "1Gi", "", "", api.VolumePending, api.PersistentVolumeReclaimRetain),
newVolume("volume13-3-2", "10Gi", "uid13-3", "claim13-3", api.VolumeBound, api.PersistentVolumeReclaimRetain, annBoundByController, annClass),
newVolume("volume13-3-2", "10Gi", "uid13-3", "claim13-3", api.VolumeBound, api.PersistentVolumeReclaimRetain, annBoundByController, storageutil.StorageClassAnnotation),
}),
newClaimArray("claim13-3", "uid13-3", "1Gi", "", api.ClaimPending, annClass),
withExpectedCapacity("10Gi", newClaimArray("claim13-3", "uid13-3", "1Gi", "volume13-3-2", api.ClaimBound, annBoundByController, annBindCompleted, annClass)),
newClaimArray("claim13-3", "uid13-3", "1Gi", "", api.ClaimPending, storageutil.StorageClassAnnotation),
withExpectedCapacity("10Gi", newClaimArray("claim13-3", "uid13-3", "1Gi", "volume13-3-2", api.ClaimBound, annBoundByController, annBindCompleted, storageutil.StorageClassAnnotation)),
noevents, noerrors, testSyncClaim,
},
{
Expand Down
17 changes: 9 additions & 8 deletions pkg/controller/volume/persistentvolume/framework_test.go
Expand Up @@ -34,6 +34,7 @@ import (
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apis/storage"
storageutil "k8s.io/kubernetes/pkg/apis/storage/util"
"k8s.io/kubernetes/pkg/client/cache"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
Expand Down Expand Up @@ -650,7 +651,7 @@ func newVolume(name, capacity, boundToClaimUID, boundToClaimName string, phase a
switch a {
case annDynamicallyProvisioned:
volume.Annotations[a] = mockPluginName
case annClass:
case storageutil.StorageClassAnnotation:
volume.Annotations[a] = "gold"
default:
volume.Annotations[a] = "yes"
Expand Down Expand Up @@ -699,13 +700,13 @@ func withMessage(message string, volumes []*api.PersistentVolume) []*api.Persist
return volumes
}

// volumeWithClass saves given class into annClass annotation.
// volumeWithClass saves given class into storage.StorageClassAnnotation annotation.
// Meant to be used to compose claims specified inline in a test.
func volumeWithClass(className string, volumes []*api.PersistentVolume) []*api.PersistentVolume {
if volumes[0].Annotations == nil {
volumes[0].Annotations = map[string]string{annClass: className}
volumes[0].Annotations = map[string]string{storageutil.StorageClassAnnotation: className}
} else {
volumes[0].Annotations[annClass] = className
volumes[0].Annotations[storageutil.StorageClassAnnotation] = className
}
return volumes
}
Expand Down Expand Up @@ -747,7 +748,7 @@ func newClaim(name, claimUID, capacity, boundToVolume string, phase api.Persiste
claim.Annotations = make(map[string]string)
for _, a := range annotations {
switch a {
case annClass:
case storageutil.StorageClassAnnotation:
claim.Annotations[a] = "gold"
default:
claim.Annotations[a] = "yes"
Expand All @@ -774,13 +775,13 @@ func newClaimArray(name, claimUID, capacity, boundToVolume string, phase api.Per
}
}

// claimWithClass saves given class into annClass annotation.
// claimWithClass saves given class into storage.StorageClassAnnotation annotation.
// Meant to be used to compose claims specified inline in a test.
func claimWithClass(className string, claims []*api.PersistentVolumeClaim) []*api.PersistentVolumeClaim {
if claims[0].Annotations == nil {
claims[0].Annotations = map[string]string{annClass: className}
claims[0].Annotations = map[string]string{storageutil.StorageClassAnnotation: className}
} else {
claims[0].Annotations[annClass] = className
claims[0].Annotations[storageutil.StorageClassAnnotation] = className
}
return claims
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/controller/volume/persistentvolume/index.go
Expand Up @@ -22,6 +22,7 @@ import (

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
storageutil "k8s.io/kubernetes/pkg/apis/storage/util"
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/labels"
)
Expand Down Expand Up @@ -92,7 +93,7 @@ func (pvIndex *persistentVolumeOrderedIndex) findByClaim(claim *api.PersistentVo
var smallestVolumeSize int64
requestedQty := claim.Spec.Resources.Requests[api.ResourceName(api.ResourceStorage)]
requestedSize := requestedQty.Value()
requestedClass := getClaimClass(claim)
requestedClass := storageutil.GetClaimStorageClass(claim)

var selector labels.Selector
if claim.Spec.Selector != nil {
Expand Down Expand Up @@ -133,7 +134,7 @@ func (pvIndex *persistentVolumeOrderedIndex) findByClaim(claim *api.PersistentVo
// with existing PVs, findByClaim must find only PVs that are
// pre-bound to the claim (by dynamic provisioning). TODO: remove in
// 1.5
if hasAnnotation(claim.ObjectMeta, annAlphaClass) {
if api.HasAnnotation(claim.ObjectMeta, storageutil.AlphaStorageClassAnnotation) {
continue
}

Expand All @@ -146,7 +147,7 @@ func (pvIndex *persistentVolumeOrderedIndex) findByClaim(claim *api.PersistentVo
} else if selector != nil && !selector.Matches(labels.Set(volume.Labels)) {
continue
}
if getVolumeClass(volume) != requestedClass {
if storageutil.GetVolumeStorageClass(volume) != requestedClass {
continue
}

Expand Down
11 changes: 6 additions & 5 deletions pkg/controller/volume/persistentvolume/index_test.go
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/api/unversioned"
storageutil "k8s.io/kubernetes/pkg/apis/storage/util"
)

func TestMatchVolume(t *testing.T) {
Expand Down Expand Up @@ -177,7 +178,7 @@ func TestMatchVolume(t *testing.T) {
Name: "claim01",
Namespace: "myns",
Annotations: map[string]string{
annClass: "silver",
storageutil.StorageClassAnnotation: "silver",
},
},
Spec: api.PersistentVolumeClaimSpec{
Expand All @@ -202,7 +203,7 @@ func TestMatchVolume(t *testing.T) {
Name: "claim01",
Namespace: "myns",
Annotations: map[string]string{
annClass: "silver",
storageutil.StorageClassAnnotation: "silver",
},
},
Spec: api.PersistentVolumeClaimSpec{
Expand Down Expand Up @@ -626,7 +627,7 @@ func createTestVolumes() []*api.PersistentVolume {
"should-exist": "true",
},
Annotations: map[string]string{
annClass: "silver",
storageutil.StorageClassAnnotation: "silver",
},
},
Spec: api.PersistentVolumeSpec{
Expand All @@ -646,7 +647,7 @@ func createTestVolumes() []*api.PersistentVolume {
UID: "gce-pd-silver2",
Name: "gce0024",
Annotations: map[string]string{
annClass: "silver",
storageutil.StorageClassAnnotation: "silver",
},
},
Spec: api.PersistentVolumeSpec{
Expand All @@ -666,7 +667,7 @@ func createTestVolumes() []*api.PersistentVolume {
UID: "gce-pd-gold",
Name: "gce0025",
Annotations: map[string]string{
annClass: "gold",
storageutil.StorageClassAnnotation: "gold",
},
},
Spec: api.PersistentVolumeSpec{
Expand Down