mirror of
https://github.com/hanzo-ml/kuberay.git
synced 2026-10-30 19:48:47 +00:00
168 lines
4.6 KiB
Go
168 lines
4.6 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"unicode"
|
|
|
|
rayiov1alpha1 "github.com/ray-project/kuberay/ray-operator/api/raycluster/v1alpha1"
|
|
"github.com/sirupsen/logrus"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
)
|
|
|
|
// IsCreated returns true if pod has been created and is maintained by the API server
|
|
func IsCreated(pod *corev1.Pod) bool {
|
|
return pod.Status.Phase != ""
|
|
}
|
|
|
|
// CheckName makes sure the name does not start with a numeric value and the total length is < 63 char
|
|
func CheckName(s string) string {
|
|
maxLenght := 50 // 63 - (max(8,6) + 5 ) // 6 to 8 char are consumed at the end with "-head-" or -worker- + 5 generated.
|
|
|
|
if len(s) > maxLenght {
|
|
//shorten the name
|
|
offset := int(math.Abs(float64(maxLenght) - float64(len(s))))
|
|
fmt.Printf("pod name is too long: len = %v, we will shorten it by offset = %v\n", len(s), offset)
|
|
s = s[offset:]
|
|
}
|
|
|
|
// cannot start with a numeric value
|
|
if unicode.IsDigit(rune(s[0])) {
|
|
s = "r" + s[1:]
|
|
}
|
|
|
|
// cannot start with a punctuation
|
|
if unicode.IsPunct(rune(s[0])) {
|
|
fmt.Println(s)
|
|
s = "r" + s[1:]
|
|
}
|
|
|
|
return s
|
|
}
|
|
|
|
// CheckLabel makes sure the label value does not start with a punctuation and the total length is < 63 char
|
|
func CheckLabel(s string) string {
|
|
maxLenght := 63
|
|
|
|
if len(s) > maxLenght {
|
|
//shorten the name
|
|
offset := int(math.Abs(float64(maxLenght) - float64(len(s))))
|
|
fmt.Printf("label value is too long: len = %v, we will shorten it by offset = %v\n", len(s), offset)
|
|
s = s[offset:]
|
|
}
|
|
|
|
// cannot start with a punctuation
|
|
if unicode.IsPunct(rune(s[0])) {
|
|
fmt.Println(s)
|
|
s = "r" + s[1:]
|
|
}
|
|
|
|
return s
|
|
}
|
|
|
|
// Before Get substring before a string.
|
|
func Before(value string, a string) string {
|
|
pos := strings.Index(value, a)
|
|
if pos == -1 {
|
|
return ""
|
|
}
|
|
return value[0:pos]
|
|
}
|
|
|
|
// FormatInt returns the string representation of i in the given base,
|
|
// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
|
|
// for digit values >= 10.
|
|
func FormatInt32(n int32) string {
|
|
return strconv.FormatInt(int64(n), 10)
|
|
}
|
|
|
|
// GetNamespace return namespace
|
|
func GetNamespace(metaData metav1.ObjectMeta) string {
|
|
if metaData.Namespace == "" {
|
|
return "default"
|
|
}
|
|
return metaData.Namespace
|
|
}
|
|
|
|
// GenerateServiceName generates a ray head service name from cluster name
|
|
func GenerateServiceName(clusterName string) string {
|
|
return fmt.Sprintf("%s-%s-%s", clusterName, rayiov1alpha1.HeadNode, "svc")
|
|
}
|
|
|
|
// GenerateIdentifier generates identifier of same group pods
|
|
func GenerateIdentifier(clusterName string, nodeType rayiov1alpha1.RayNodeType) string {
|
|
return fmt.Sprintf("%s-%s", clusterName, nodeType)
|
|
}
|
|
|
|
// TODO: find target container through name instead of using index 0.
|
|
// FindRayContainerIndex finds the ray head/worker container's index in the pod
|
|
func FindRayContainerIndex(spec corev1.PodSpec) (index int) {
|
|
// We only support one container at this moment. We definitely need a better way to filter out sidecar containers.
|
|
if len(spec.Containers) > 1 {
|
|
logrus.Warnf("Pod has multiple containers, we choose index=0 as Ray container")
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// CalculateDesiredReplicas calculate desired worker replicas at the cluster level
|
|
func CalculateDesiredReplicas(cluster *rayiov1alpha1.RayCluster) int32 {
|
|
count := int32(0)
|
|
for _, nodeGroup := range cluster.Spec.WorkerGroupSpecs {
|
|
count += *nodeGroup.Replicas
|
|
}
|
|
|
|
return count
|
|
}
|
|
|
|
// CalculateDesiredReplicas calculate desired worker replicas at the cluster level
|
|
func CalculateMinReplicas(cluster *rayiov1alpha1.RayCluster) int32 {
|
|
count := int32(0)
|
|
for _, nodeGroup := range cluster.Spec.WorkerGroupSpecs {
|
|
count += *nodeGroup.MinReplicas
|
|
}
|
|
|
|
return count
|
|
}
|
|
|
|
// CalculateDesiredReplicas calculate desired worker replicas at the cluster level
|
|
func CalculateMaxReplicas(cluster *rayiov1alpha1.RayCluster) int32 {
|
|
count := int32(0)
|
|
for _, nodeGroup := range cluster.Spec.WorkerGroupSpecs {
|
|
count += *nodeGroup.MaxReplicas
|
|
}
|
|
|
|
return count
|
|
}
|
|
|
|
// CalculateDesiredReplicas calculate desired worker replicas at the cluster level
|
|
func CalculateAvailableReplicas(pods corev1.PodList) int32 {
|
|
count := int32(0)
|
|
for _, pod := range pods.Items {
|
|
if pod.Status.Phase == corev1.PodPending || pod.Status.Phase == corev1.PodRunning {
|
|
count++
|
|
}
|
|
}
|
|
|
|
return count
|
|
}
|
|
|
|
func Contains(s []string, searchTerm string) bool {
|
|
i := sort.SearchStrings(s, searchTerm)
|
|
return i < len(s) && s[i] == searchTerm
|
|
}
|
|
|
|
func FilterContainerByName(containers []corev1.Container, name string) (corev1.Container, error) {
|
|
for _, container := range containers {
|
|
if strings.Compare(container.Name, name) == 0 {
|
|
return container, nil
|
|
}
|
|
}
|
|
|
|
return corev1.Container{}, fmt.Errorf("can not find container %s", name)
|
|
}
|