Skip to content

Commit

Permalink
feat: workloadrebalancer LabelSelector implement
Browse files Browse the repository at this point in the history
  • Loading branch information
weidalin committed Sep 25, 2024
1 parent d95e7a0 commit a5c935f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 h1:W6cLQc5pnqM
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3/go.mod h1:JWP1Fj0VWGHyw3YUPjXSQnRnrwezrZSrApfX5S0nIag=
sigs.k8s.io/mcs-api v0.1.0 h1:edDbg0oRGfXw8TmZjKYep06LcJLv/qcYLidejnUp0PM=
sigs.k8s.io/mcs-api v0.1.0/go.mod h1:gGiAryeFNB4GBsq2LBmVqSgKoobLxt+p7ii/WG5QYYw=
sigs.k8s.io/metrics-server v0.7.1 h1:LhdCzkaI7VI7/N7pR4hDauTuWyc9Pxr+ihjTDuS9GIo=
sigs.k8s.io/metrics-server v0.7.1 h1:8m2SOf/xrA1RLVl8k6sN56pxKMHIUfP1GPRK2+l3aUA=
sigs.k8s.io/metrics-server v0.7.1/go.mod h1:vt+pIEbw5tpmyRR46WJb3pRm1JEzf/HxRN+VClTKuqI=
sigs.k8s.io/structured-merge-diff/v3 v3.0.0-20200116222232-67a7b8c61874/go.mod h1:PlARxl6Hbt/+BC80dRLi1qAmnMqwqDg62YvvVkZjemw=
sigs.k8s.io/structured-merge-diff/v3 v3.0.0/go.mod h1:PlARxl6Hbt/+BC80dRLi1qAmnMqwqDg62YvvVkZjemw=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ import (
"fmt"
"reflect"
"sort"
"strings"

"time"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -121,7 +126,22 @@ func (c *RebalancerController) syncWorkloadsFromSpecToStatus(rebalancer *appsv1a

specWorkloadSet := sets.New[appsv1alpha1.ObjectReference]()
for _, workload := range rebalancer.Spec.Workloads {
specWorkloadSet.Insert(workload)
if workload.Name != "" {
specWorkloadSet.Insert(workload)
} else {
workloadsSelectByRs := []appsv1alpha1.ObjectReference{}

selectedWorkloads, err := c.getResourcesBySelector(workload)
if err != nil {
klog.Errorf("Failed to get resources by label selector %+v %+v, : %v", workload, workload.LabelSelector, err)
continue
}
klog.Infof("Selected %d workloads by ResourceSelector %+v %+v", len(selectedWorkloads), workload, workload.LabelSelector)
workloadsSelectByRs = append(workloadsSelectByRs, selectedWorkloads...)
for _, workload := range workloadsSelectByRs {
specWorkloadSet.Insert(workload)
}
}
}

for _, item := range rebalancer.Status.ObservedWorkloads {
Expand Down Expand Up @@ -298,3 +318,65 @@ func timeLeft(r *appsv1alpha1.WorkloadRebalancer) time.Duration {
klog.V(4).Infof("Found Rebalancer(%s) finished at: %+v, remainingTTL: %+v", r.Name, r.Status.FinishTime.UTC(), remainingTTL)
return remainingTTL
}

func (c *RebalancerController) getResourcesBySelector(workload appsv1alpha1.ObjectReference) ([]appsv1alpha1.ObjectReference, error) {
gvk := schema.GroupVersionKind{
Group: getGroupFromAPIVersion(workload.APIVersion),
Version: getVersionFromAPIVersion(workload.APIVersion),
Kind: workload.Kind,
}

resource := &unstructured.UnstructuredList{}
resource.SetGroupVersionKind(gvk)

var labelSelector client.MatchingLabels
if workload.LabelSelector != nil {
labelSelector = workload.LabelSelector.MatchLabels
}

listOptions := []client.ListOption{}
if workload.Namespace != "" {
listOptions = append(listOptions, client.InNamespace(workload.Namespace))
}

if labelSelector != nil {
listOptions = append(listOptions, client.MatchingLabels(labelSelector))
}

err := c.Client.List(context.TODO(), resource, listOptions...)
if err != nil {
return nil, fmt.Errorf("failed to list resources: %v", err)
}

return convertToWorkloadReferences(resource.Items), nil
}

func convertToWorkloadReferences(items []unstructured.Unstructured) []appsv1alpha1.ObjectReference {
workloadRefs := make([]appsv1alpha1.ObjectReference, 0, len(items))
for _, item := range items {
ref := appsv1alpha1.ObjectReference{
APIVersion: item.GetAPIVersion(),
Kind: item.GetKind(),
Name: item.GetName(),
Namespace: item.GetNamespace(),
}
workloadRefs = append(workloadRefs, ref)
}
return workloadRefs
}

func getGroupFromAPIVersion(apiVersion string) string {
parts := strings.Split(apiVersion, "/")
if len(parts) == 1 {
return ""
}
return parts[0]
}

func getVersionFromAPIVersion(apiVersion string) string {
parts := strings.Split(apiVersion, "/")
if len(parts) == 1 {
return parts[0]
}
return parts[1]
}
6 changes: 6 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ github.com/gogo/protobuf/protoc-gen-gogo/plugin
github.com/gogo/protobuf/sortkeys
github.com/gogo/protobuf/vanity
github.com/gogo/protobuf/vanity/command
# github.com/golang-jwt/jwt/v5 v5.2.1
## explicit; go 1.18
github.com/golang-jwt/jwt/v5
# github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
## explicit
github.com/golang/groupcache/lru
Expand Down Expand Up @@ -416,6 +419,9 @@ github.com/robfig/cron/v3
# github.com/rogpeppe/go-internal v1.11.0
## explicit; go 1.19
github.com/rogpeppe/go-internal/fmtsort
# github.com/rs/xid v1.3.0
## explicit; go 1.12
github.com/rs/xid
# github.com/rs/zerolog v1.26.1
## explicit; go 1.15
github.com/rs/zerolog
Expand Down

0 comments on commit a5c935f

Please sign in to comment.