|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sort" |
| 6 | + |
| 7 | + "github.com/Azure/placement-policy-scheduler-plugins/apis/v1alpha1" |
| 8 | + ppclientset "github.com/Azure/placement-policy-scheduler-plugins/pkg/client/clientset/versioned" |
| 9 | + ppinformers "github.com/Azure/placement-policy-scheduler-plugins/pkg/client/informers/externalversions/apis/v1alpha1" |
| 10 | + pplisters "github.com/Azure/placement-policy-scheduler-plugins/pkg/client/listers/apis/v1alpha1" |
| 11 | + "github.com/Azure/placement-policy-scheduler-plugins/pkg/utils" |
| 12 | + |
| 13 | + corev1 "k8s.io/api/core/v1" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/labels" |
| 16 | + "k8s.io/client-go/kubernetes" |
| 17 | + corelisters "k8s.io/client-go/listers/core/v1" |
| 18 | + "k8s.io/kubernetes/pkg/scheduler/framework" |
| 19 | +) |
| 20 | + |
| 21 | +// Manager defines the interfaces for PlacementPolicy management. |
| 22 | +type Manager interface { |
| 23 | + GetPlacementPolicyForPod(context.Context, *corev1.Pod) (*v1alpha1.PlacementPolicy, error) |
| 24 | + GetPodsWithLabels(context.Context, map[string]string) ([]*corev1.Pod, error) |
| 25 | + AnnotatePod(context.Context, *corev1.Pod, *v1alpha1.PlacementPolicy, bool) (*corev1.Pod, error) |
| 26 | + GetPlacementPolicy(context.Context, string, string) (*v1alpha1.PlacementPolicy, error) |
| 27 | +} |
| 28 | + |
| 29 | +type PlacementPolicyManager struct { |
| 30 | + // client is a clientset for the kube API server. |
| 31 | + client kubernetes.Interface |
| 32 | + // client is a placementPolicy client |
| 33 | + ppClient ppclientset.Interface |
| 34 | + // podLister is pod lister |
| 35 | + podLister corelisters.PodLister |
| 36 | + // snapshotSharedLister is pod shared list |
| 37 | + snapshotSharedLister framework.SharedLister |
| 38 | + // ppLister is placementPolicy lister |
| 39 | + ppLister pplisters.PlacementPolicyLister |
| 40 | +} |
| 41 | + |
| 42 | +func NewPlacementPolicyManager( |
| 43 | + client kubernetes.Interface, |
| 44 | + ppClient ppclientset.Interface, |
| 45 | + snapshotSharedLister framework.SharedLister, |
| 46 | + ppInformer ppinformers.PlacementPolicyInformer, |
| 47 | + podLister corelisters.PodLister) *PlacementPolicyManager { |
| 48 | + return &PlacementPolicyManager{ |
| 49 | + client: client, |
| 50 | + ppClient: ppClient, |
| 51 | + snapshotSharedLister: snapshotSharedLister, |
| 52 | + ppLister: ppInformer.Lister(), |
| 53 | + podLister: podLister, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// GetPlacementPolicyForPod returns the placement policy for the given pod |
| 58 | +func (m *PlacementPolicyManager) GetPlacementPolicyForPod(ctx context.Context, pod *corev1.Pod) (*v1alpha1.PlacementPolicy, error) { |
| 59 | + ppList, err := m.ppLister.PlacementPolicies(pod.Namespace).List(labels.Everything()) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + // filter the placement policy list based on the pod's labels |
| 64 | + ppList = m.filterPlacementPolicyList(ppList, pod) |
| 65 | + if len(ppList) == 0 { |
| 66 | + return nil, nil |
| 67 | + } |
| 68 | + if len(ppList) > 1 { |
| 69 | + // if there are multiple placement policies, sort them by weight and return the first one |
| 70 | + sort.Sort(sort.Reverse(ByWeight(ppList))) |
| 71 | + } |
| 72 | + |
| 73 | + return ppList[0], nil |
| 74 | +} |
| 75 | + |
| 76 | +func (m *PlacementPolicyManager) GetPodsWithLabels(ctx context.Context, podLabels map[string]string) ([]*corev1.Pod, error) { |
| 77 | + return m.podLister.List(labels.Set(podLabels).AsSelector()) |
| 78 | +} |
| 79 | + |
| 80 | +// AnnotatePod annotates the pod with the placement policy. |
| 81 | +func (m *PlacementPolicyManager) AnnotatePod(ctx context.Context, pod *corev1.Pod, pp *v1alpha1.PlacementPolicy, preferredNodeWithMatchingLabels bool) (*corev1.Pod, error) { |
| 82 | + annotations := map[string]string{} |
| 83 | + if pod.Annotations != nil { |
| 84 | + annotations = pod.Annotations |
| 85 | + } |
| 86 | + |
| 87 | + preference := "false" |
| 88 | + if preferredNodeWithMatchingLabels { |
| 89 | + preference = "true" |
| 90 | + } |
| 91 | + annotations[v1alpha1.PlacementPolicyAnnotationKey] = pp.Name |
| 92 | + annotations[v1alpha1.PlacementPolicyPreferenceAnnotationKey] = preference |
| 93 | + pod.Annotations = annotations |
| 94 | + return m.client.CoreV1().Pods(pod.Namespace).Update(ctx, pod, metav1.UpdateOptions{}) |
| 95 | +} |
| 96 | + |
| 97 | +func (m *PlacementPolicyManager) GetPlacementPolicy(ctx context.Context, namespace, name string) (*v1alpha1.PlacementPolicy, error) { |
| 98 | + return m.ppLister.PlacementPolicies(namespace).Get(name) |
| 99 | +} |
| 100 | + |
| 101 | +func (m *PlacementPolicyManager) filterPlacementPolicyList(ppList []*v1alpha1.PlacementPolicy, pod *corev1.Pod) []*v1alpha1.PlacementPolicy { |
| 102 | + var filteredPPList []*v1alpha1.PlacementPolicy |
| 103 | + for _, pp := range ppList { |
| 104 | + labels := pp.Spec.PodSelector.MatchLabels |
| 105 | + if utils.HasMatchingLabels(pod.Labels, labels) { |
| 106 | + filteredPPList = append(filteredPPList, pp) |
| 107 | + } |
| 108 | + } |
| 109 | + return filteredPPList |
| 110 | +} |
0 commit comments