48 lines
1.5 KiB
Go
48 lines
1.5 KiB
Go
package util
|
|
|
|
import (
|
|
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
|
|
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
|
|
)
|
|
|
|
// GetBindingClusterNames will get clusterName list from bind clusters field
|
|
func GetBindingClusterNames(binding *workv1alpha1.ResourceBinding) []string {
|
|
var clusterNames []string
|
|
for _, targetCluster := range binding.Spec.Clusters {
|
|
clusterNames = append(clusterNames, targetCluster.Name)
|
|
}
|
|
return clusterNames
|
|
}
|
|
|
|
// IsBindingReplicasChanged will check if the sum of replicas is different from the replicas of object
|
|
func IsBindingReplicasChanged(bindingSpec *workv1alpha1.ResourceBindingSpec, strategy *policyv1alpha1.ReplicaSchedulingStrategy) bool {
|
|
if strategy == nil {
|
|
return false
|
|
}
|
|
if strategy.ReplicaSchedulingType == policyv1alpha1.ReplicaSchedulingTypeDuplicated {
|
|
for _, targetCluster := range bindingSpec.Clusters {
|
|
if targetCluster.Replicas != bindingSpec.Resource.Replicas {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
if strategy.ReplicaSchedulingType == policyv1alpha1.ReplicaSchedulingTypeDivided {
|
|
replicasSum := int32(0)
|
|
for _, targetCluster := range bindingSpec.Clusters {
|
|
replicasSum += targetCluster.Replicas
|
|
}
|
|
return replicasSum != bindingSpec.Resource.Replicas
|
|
}
|
|
return false
|
|
}
|
|
|
|
// GetSumOfReplicas will get the sum of replicas in target clusters
|
|
func GetSumOfReplicas(clusters []workv1alpha1.TargetCluster) int32 {
|
|
replicasSum := int32(0)
|
|
for i := range clusters {
|
|
replicasSum += clusters[i].Replicas
|
|
}
|
|
return replicasSum
|
|
}
|