Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Zimin <alexandr.zimin@flant.com>
  • Loading branch information
AleksZimin committed Aug 18, 2024
1 parent aafe528 commit f300255
Show file tree
Hide file tree
Showing 5 changed files with 406 additions and 18 deletions.
1 change: 1 addition & 0 deletions images/sds-local-volume-scheduler-extender/src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ require (
golang.org/x/time v0.6.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,46 @@ func (s *scheduler) filter(w http.ResponseWriter, r *http.Request) {
}
s.log.Trace(fmt.Sprintf("[filter] input data: %+v", inputData))

if inputData.NodeNames == nil || len(*inputData.NodeNames) == 0 {
s.log.Error(errors.New("no node names in the request"), "[filter] unable to get node names from the request")
http.Error(w, "bad request", http.StatusBadRequest)
return
}
nodeNames := inputData.NodeNames
s.log.Trace(fmt.Sprintf("[filter] node names from the request: %v", nodeNames))

if inputData.Pod == nil {
s.log.Error(errors.New("no pod in the request"), "[filter] unable to get a Pod from the request")
http.Error(w, "bad request", http.StatusBadRequest)
return
}
pod := inputData.Pod
s.log.Debug(fmt.Sprintf("[filter] starts the filtering for Pod %s/%s", pod.Namespace, pod.Name))

s.log.Trace(fmt.Sprintf("[filter] Pod from the request: %+v", pod))

s.log.Debug(fmt.Sprintf("[filter] Find out if the Pod %s/%s should be processed", pod.Namespace, pod.Name))
shouldProcess, err := ShouldProcessPod(s.ctx, s.client, s.log, pod, consts.SdsLocalVolumeProvisioner)
if err != nil {
s.log.Error(err, "[filter] unable to check if the Pod should be processed")
http.Error(w, "bad request", http.StatusBadRequest)
return
}
if !shouldProcess {
s.log.Debug(fmt.Sprintf("[filter] Pod %s/%s should not be processed. Return the same nodes", pod.Namespace, pod.Name))
filteredNodes := &ExtenderFilterResult{
NodeNames: inputData.NodeNames,
}
s.log.Trace(fmt.Sprintf("[filter] filtered nodes: %+v", filteredNodes))

w.Header().Set("content-type", "application/json")
err = json.NewEncoder(w).Encode(filteredNodes)
if err != nil {
s.log.Error(err, "[filter] unable to encode a response")
http.Error(w, "internal error", http.StatusInternalServerError)
return
}
return
}
s.log.Debug(fmt.Sprintf("[filter] Pod %s/%s should be processed", pod.Namespace, pod.Name))

if inputData.NodeNames == nil || len(*inputData.NodeNames) == 0 {
s.log.Error(errors.New("no node names in the request"), "[filter] unable to get node names from the request")
http.Error(w, "bad request", http.StatusBadRequest)
return
}
nodeNames := inputData.NodeNames
for _, nodeName := range *nodeNames {
s.log.Debug(fmt.Sprintf("[filter] Pod %s/%s has node %s from the request", pod.Namespace, pod.Name, nodeName))
}
Expand Down
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
}
Loading

0 comments on commit f300255

Please sign in to comment.