-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Aleksandr Zimin <alexandr.zimin@flant.com>
- Loading branch information
1 parent
aafe528
commit f300255
Showing
5 changed files
with
406 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
images/sds-local-volume-scheduler-extender/src/pkg/scheduler/func.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
Copyright 2024 Flant JSC | ||
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 scheduler | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
storagev1 "k8s.io/api/storage/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"sds-local-volume-scheduler-extender/pkg/logger" | ||
) | ||
|
||
const ( | ||
annotationBetaStorageProvisioner = "volume.beta.kubernetes.io/storage-provisioner" | ||
annotationStorageProvisioner = "volume.kubernetes.io/storage-provisioner" | ||
) | ||
|
||
func ShouldProcessPod(ctx context.Context, cl client.Client, log logger.Logger, pod *corev1.Pod, targetProvisioner string) (bool, error) { | ||
log.Trace(fmt.Sprintf("[ShouldProcessPod] targetProvisioner=%s, pod: %+v", targetProvisioner, pod)) | ||
var discoveredProvisioner string | ||
|
||
for _, volume := range pod.Spec.Volumes { | ||
if volume.PersistentVolumeClaim != nil { | ||
log.Trace(fmt.Sprintf("[ShouldProcessPod] process volume: %+v that has pvc: %+v", volume, volume.PersistentVolumeClaim)) | ||
pvcName := volume.PersistentVolumeClaim.ClaimName | ||
pvc := &corev1.PersistentVolumeClaim{} | ||
err := cl.Get(ctx, client.ObjectKey{Namespace: pod.Namespace, Name: pvcName}, pvc) | ||
if err != nil { | ||
return false, fmt.Errorf("[ShouldProcessPod] error getting PVC %s: %v", pvcName, err) | ||
} | ||
|
||
log.Trace(fmt.Sprintf("[ShouldProcessPod] get pvc: %+v", pvc)) | ||
log.Trace(fmt.Sprintf("[ShouldProcessPod] check provisioner in pvc annotations: %+v", pvc.Annotations)) | ||
provisioner, ok := pvc.Annotations[annotationStorageProvisioner] | ||
if !ok { | ||
provisioner, ok = pvc.Annotations[annotationBetaStorageProvisioner] | ||
} | ||
|
||
if ok { | ||
discoveredProvisioner = provisioner | ||
log.Trace(fmt.Sprintf("[ShouldProcessPod] discovered provisioner in pvc annotations: %s", discoveredProvisioner)) | ||
} else if pvc.Spec.StorageClassName != nil && *pvc.Spec.StorageClassName != "" { | ||
log.Trace(fmt.Sprintf("[ShouldProcessPod] can't find provisioner in pvc annotations, check in storageClass with name: %s", *pvc.Spec.StorageClassName)) | ||
storageClass := &storagev1.StorageClass{} | ||
err := cl.Get(ctx, client.ObjectKey{Name: *pvc.Spec.StorageClassName}, storageClass) | ||
if err != nil { | ||
return false, fmt.Errorf("[ShouldProcessPod] error getting StorageClass %s: %v", *pvc.Spec.StorageClassName, err) | ||
} | ||
|
||
discoveredProvisioner = storageClass.Provisioner | ||
log.Trace(fmt.Sprintf("[ShouldProcessPod] discover provisioner %s in storageClass: %+v", discoveredProvisioner, storageClass)) | ||
} else if pvc.Spec.VolumeName != "" { | ||
log.Trace(fmt.Sprintf("[ShouldProcessPod] can't find provisioner in pvc annotations and StorageClass, check in PV with name: %s", pvc.Spec.VolumeName)) | ||
pv := &corev1.PersistentVolume{} | ||
err := cl.Get(ctx, client.ObjectKey{Name: pvc.Spec.VolumeName}, pv) | ||
if err != nil { | ||
return false, fmt.Errorf("[ShouldProcessPod] error getting PV %s: %v", pvc.Spec.VolumeName, err) | ||
} | ||
|
||
if pv.Spec.CSI != nil { | ||
discoveredProvisioner = pv.Spec.CSI.Driver | ||
} | ||
|
||
log.Trace(fmt.Sprintf("[ShouldProcessPod] discover provisioner %s in PV: %+v", discoveredProvisioner, pv)) | ||
} | ||
|
||
log.Trace(fmt.Sprintf("[ShouldProcessPod] discovered provisioner: %s", discoveredProvisioner)) | ||
if discoveredProvisioner == targetProvisioner { | ||
log.Trace(fmt.Sprintf("[ShouldProcessPod] provisioner matches targetProvisioner %s. Pod: %s/%s", pod.Namespace, pod.Name, targetProvisioner)) | ||
return true, nil | ||
} | ||
log.Trace(fmt.Sprintf("[ShouldProcessPod] provisioner %s doesn't match targetProvisioner %s. Skip volume %s.", discoveredProvisioner, targetProvisioner, volume.Name)) | ||
} | ||
} | ||
log.Trace(fmt.Sprintf("[ShouldProcessPod] can't find targetProvisioner %s in pod volumes. Skip pod: %s/%s", targetProvisioner, pod.Namespace, pod.Name)) | ||
return false, nil | ||
} |
Oops, something went wrong.