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

OCPBUGS-17805: Do not error during capacity check if annotation does not exist on the node #385

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions controllers/persistent-volume-claim/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

const (
capacityAnnotation = "capacity.topolvm.io/00default"
CapacityAnnotation = "capacity.topolvm.io/"
)

// PersistentVolumeClaimReconciler reconciles a PersistentVolumeClaim object
Expand Down Expand Up @@ -67,7 +67,9 @@ func (r *PersistentVolumeClaimReconciler) Reconcile(ctx context.Context, req ctr
return ctrl.Result{}, nil
}

if !strings.HasPrefix(*pvc.Spec.StorageClassName, controllers.StorageClassPrefix) {
// Skip if StorageClassName does not contain the lvms prefix
lvmsPrefix, deviceClass, exists := strings.Cut(*pvc.Spec.StorageClassName, "-")
suleymanakbas91 marked this conversation as resolved.
Show resolved Hide resolved
if !exists || fmt.Sprintf("%s-", lvmsPrefix) != controllers.StorageClassPrefix {
logger.Info("skipping pvc as the storageClassName does not contain desired prefix",
"desired-prefix", controllers.StorageClassPrefix)
return ctrl.Result{}, nil
Expand All @@ -90,11 +92,9 @@ func (r *PersistentVolumeClaimReconciler) Reconcile(ctx context.Context, req ctr
requestedStorage := pvc.Spec.Resources.Requests.Storage()
var nodeMessage []string
for _, node := range nodeList.Items {
capacity, ok := node.Annotations[capacityAnnotation]
capacity, ok := node.Annotations[CapacityAnnotation+deviceClass]
if !ok {
errMessage := fmt.Sprintf("could not find capacity annotation on the node %s", node.Name)
logger.Error(fmt.Errorf(errMessage), errMessage)
return ctrl.Result{}, nil
continue
suleymanakbas91 marked this conversation as resolved.
Show resolved Hide resolved
}

capacityQuantity, err := resource.ParseQuantity(capacity)
Expand Down
106 changes: 97 additions & 9 deletions controllers/persistent-volume-claim/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
persistentvolumeclaim "github.com/openshift/lvm-operator/controllers/persistent-volume-claim"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -107,6 +108,90 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) {
},
expectNoStorageAvailable: true,
},
{
name: "testing PVC requesting more storage than capacity in the node",
req: controllerruntime.Request{NamespacedName: types.NamespacedName{
Namespace: defaultNamespace,
Name: "test-pending-PVC",
}},
objs: []client.Object{
&v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{Namespace: defaultNamespace, Name: "test-pending-PVC"},
Spec: v1.PersistentVolumeClaimSpec{
StorageClassName: pointer.String(controllers.StorageClassPrefix + "bla"),
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceStorage: *resource.NewQuantity(100, resource.DecimalSI),
},
},
},
Status: v1.PersistentVolumeClaimStatus{
Phase: v1.ClaimPending,
},
},
&v1.Node{
ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{persistentvolumeclaim.CapacityAnnotation + "bla": "10"}},
},
},
expectNoStorageAvailable: true,
},
{
name: "testing PVC requesting less storage than capacity in the node",
req: controllerruntime.Request{NamespacedName: types.NamespacedName{
Namespace: defaultNamespace,
Name: "test-pending-PVC",
}},
objs: []client.Object{
&v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{Namespace: defaultNamespace, Name: "test-pending-PVC"},
Spec: v1.PersistentVolumeClaimSpec{
StorageClassName: pointer.String(controllers.StorageClassPrefix + "bla"),
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceStorage: *resource.NewQuantity(10, resource.DecimalSI),
},
},
},
Status: v1.PersistentVolumeClaimStatus{
Phase: v1.ClaimPending,
},
},
&v1.Node{
ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{persistentvolumeclaim.CapacityAnnotation + "bla": "100"}},
},
},
expectNoStorageAvailable: false,
},
{
name: "testing PVC requesting less storage than capacity in one node, having another node without annotation",
req: controllerruntime.Request{NamespacedName: types.NamespacedName{
Namespace: defaultNamespace,
Name: "test-pending-PVC",
}},
objs: []client.Object{
&v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{Namespace: defaultNamespace, Name: "test-pending-PVC"},
Spec: v1.PersistentVolumeClaimSpec{
StorageClassName: pointer.String(controllers.StorageClassPrefix + "bla"),
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceStorage: *resource.NewQuantity(10, resource.DecimalSI),
},
},
},
Status: v1.PersistentVolumeClaimStatus{
Phase: v1.ClaimPending,
},
},
&v1.Node{
ObjectMeta: metav1.ObjectMeta{Name: "annotated", Annotations: map[string]string{persistentvolumeclaim.CapacityAnnotation + "bla": "100"}},
},
&v1.Node{
ObjectMeta: metav1.ObjectMeta{Name: "idle"},
},
},
expectNoStorageAvailable: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -125,18 +210,21 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) {
return
}

if tt.expectNoStorageAvailable {
select {
case event := <-recorder.Events:
if !strings.Contains(event, "NotEnoughCapacity") {
t.Errorf("event was captured but it did not contain the reason NotEnoughCapacity")
return
}
case <-time.After(100 * time.Millisecond):
select {
case event := <-recorder.Events:
if !strings.Contains(event, "NotEnoughCapacity") {
t.Errorf("event was captured but it did not contain the reason NotEnoughCapacity")
return
}
if !tt.expectNoStorageAvailable {
t.Errorf("event was captured but was not expected")
return
}
case <-time.After(100 * time.Millisecond):
if tt.expectNoStorageAvailable {
t.Errorf("wanted event that no storage is available but none was sent")
return
}

}
})
}
Expand Down