-
Notifications
You must be signed in to change notification settings - Fork 18
/
common.go
99 lines (83 loc) · 2.64 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright 2023 RisingWave Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package utils
import (
"strings"
"github.com/samber/lo"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// GetNamespacedName returns the NamespacedName of the given object.
func GetNamespacedName(obj client.Object) types.NamespacedName {
return types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}
}
// IsDeleted returns true when object's deletion timestamp isn't null.
func IsDeleted(obj client.Object) bool {
return obj != nil && !obj.GetDeletionTimestamp().IsZero()
}
// IsPodRunning returns true when Pod's phase is Running.
func IsPodRunning(pod *corev1.Pod) bool {
return pod != nil && pod.Status.Phase == corev1.PodRunning
}
// GetVersionFromImage return the version from spec.image.
func GetVersionFromImage(image string) string {
if image == "" {
return ""
}
lastRepoIdx := strings.LastIndex(image, "/")
lastTagIdx := strings.LastIndex(image[lastRepoIdx+1:], ":")
if lastTagIdx < 0 {
return "latest"
}
return image[lastRepoIdx+lastTagIdx+2:]
}
// GetContainerFromPod gets a pointer to the container with the same name. Nil is returned when
// the container isn't found.
func GetContainerFromPod(pod *corev1.Pod, name string) *corev1.Container {
if pod == nil {
return nil
}
for _, container := range pod.Spec.Containers {
if container.Name == name {
return &container
}
}
return nil
}
// GetPortFromContainer gets the specified port from the container. If the port's not found,
// it returns a false.
func GetPortFromContainer(container *corev1.Container, name string) (int32, bool) {
if container == nil {
return 0, false
}
for _, containerPort := range container.Ports {
if containerPort.Name == name {
return containerPort.ContainerPort, true
}
}
return 0, false
}
type ptrAsObject[T any] interface {
client.Object
*T
}
// MapObjectsToNames converts a list of k8s objects to their names.
func MapObjectsToNames[T any, TP ptrAsObject[T]](objects []T) []string {
return lo.Map(objects, func(item T, _ int) string {
return TP(&item).GetName()
})
}