-
Notifications
You must be signed in to change notification settings - Fork 980
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented volume topology aware scheduling
- Loading branch information
Showing
3 changed files
with
222 additions
and
6 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
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,95 @@ | ||
package selection | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/aws/karpenter/pkg/apis/provisioning/v1alpha5" | ||
v1 "k8s.io/api/core/v1" | ||
storagev1 "k8s.io/api/storage/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"knative.dev/pkg/ptr" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func NewVolumeTopology(kubeClient client.Client) *VolumeTopology { | ||
return &VolumeTopology{kubeClient: kubeClient} | ||
} | ||
|
||
type VolumeTopology struct { | ||
kubeClient client.Client | ||
} | ||
|
||
func (v *VolumeTopology) GetRequirements(ctx context.Context, pod *v1.Pod) (v1alpha5.Requirements, error) { | ||
var requirements v1alpha5.Requirements | ||
for _, volume := range pod.Spec.Volumes { | ||
req, err := v.getRequirements(ctx, pod, volume) | ||
if err != nil { | ||
return nil, err | ||
} | ||
requirements = append(requirements, req...) | ||
} | ||
return requirements, nil | ||
} | ||
|
||
func (v *VolumeTopology) getRequirements(ctx context.Context, pod *v1.Pod, volume v1.Volume) (v1alpha5.Requirements, error) { | ||
// Get PVC | ||
if volume.PersistentVolumeClaim == nil { | ||
return nil, nil | ||
} | ||
pvc := &v1.PersistentVolumeClaim{} | ||
if err := v.kubeClient.Get(ctx, types.NamespacedName{Name: volume.PersistentVolumeClaim.ClaimName, Namespace: pod.Namespace}, pvc); err != nil { | ||
return nil, fmt.Errorf("getting persistent volume claim %s, %w", volume.PersistentVolumeClaim.ClaimName, err) | ||
} | ||
// Persistent Volume Requirements | ||
if pvc.Spec.VolumeName != "" { | ||
requirements, err := v.getPersistentVolumeRequirements(ctx, pod, pvc) | ||
if err != nil { | ||
return nil, fmt.Errorf("getting existing requirements, %w", err) | ||
} | ||
return requirements, nil | ||
} | ||
// Storage Class Requirements | ||
if ptr.StringValue(pvc.Spec.StorageClassName) != "" { | ||
requirements, err := v.getStorageClassRequirements(ctx, pvc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return requirements, nil | ||
} | ||
return nil, nil | ||
} | ||
|
||
func (v *VolumeTopology) getStorageClassRequirements(ctx context.Context, pvc *v1.PersistentVolumeClaim) (v1alpha5.Requirements, error) { | ||
storageClass := &storagev1.StorageClass{} | ||
if err := v.kubeClient.Get(ctx, types.NamespacedName{Name: ptr.StringValue(pvc.Spec.StorageClassName)}, storageClass); err != nil { | ||
return nil, fmt.Errorf("getting storage class %q, %w", ptr.StringValue(pvc.Spec.StorageClassName), err) | ||
} | ||
var requirements v1alpha5.Requirements | ||
if len(storageClass.AllowedTopologies) > 0 { | ||
// Terms are ORed, only use the first term | ||
for _, requirement := range storageClass.AllowedTopologies[0].MatchLabelExpressions { | ||
requirements = append(requirements, v1.NodeSelectorRequirement{Key: requirement.Key, Operator: v1.NodeSelectorOpIn, Values: requirement.Values}) | ||
} | ||
} | ||
return requirements, nil | ||
} | ||
|
||
func (v *VolumeTopology) getPersistentVolumeRequirements(ctx context.Context, pod *v1.Pod, pvc *v1.PersistentVolumeClaim) (v1alpha5.Requirements, error) { | ||
pv := &v1.PersistentVolume{} | ||
if err := v.kubeClient.Get(ctx, types.NamespacedName{Name: pvc.Spec.VolumeName, Namespace: pod.Namespace}, pv); err != nil { | ||
return nil, fmt.Errorf("getting persistent volume %q, %w", pvc.Spec.VolumeName, err) | ||
} | ||
if pv.Spec.NodeAffinity == nil { | ||
return nil, nil | ||
} | ||
if pv.Spec.NodeAffinity.Required == nil { | ||
return nil, nil | ||
} | ||
var requirements v1alpha5.Requirements | ||
if len(pv.Spec.NodeAffinity.Required.NodeSelectorTerms) > 0 { | ||
// Terms are ORed, only use the first term | ||
requirements = append(requirements, pv.Spec.NodeAffinity.Required.NodeSelectorTerms[0].MatchExpressions...) | ||
} | ||
return requirements, nil | ||
} |