-
Notifications
You must be signed in to change notification settings - Fork 53
/
image.go
56 lines (47 loc) · 1.34 KB
/
image.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
package resources
import (
"context"
"fmt"
"os"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func FindManagerImage(ctx context.Context, c client.Client) (string, corev1.PullPolicy, error) {
var pullPolicy corev1.PullPolicy
if value, ok := os.LookupEnv("OPNI_DEBUG_MANAGER_IMAGE"); ok {
return value, corev1.PullAlways, nil
}
podName, ok := os.LookupEnv("POD_NAME")
if !ok {
return "", "", fmt.Errorf("POD_NAME not set")
}
podNamespace, ok := os.LookupEnv("POD_NAMESPACE")
if !ok {
return "", "", fmt.Errorf("POD_NAMESPACE not set")
}
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Namespace: podNamespace,
},
}
if err := c.Get(ctx, client.ObjectKeyFromObject(pod), pod); err != nil {
return "", "", err
}
var imageID string
for i, containerStatus := range pod.Status.ContainerStatuses {
if containerStatus.Name == "manager" {
imageID = containerStatus.ImageID
pullPolicy = pod.Spec.Containers[i].ImagePullPolicy
}
}
if imageID == "" {
return "", "", fmt.Errorf("manager container not found")
}
// depending on the container runtime, the prefix docker-pullable:// may
// be prepended to the image ID.
imageID = strings.TrimPrefix(imageID, "docker-pullable://")
return imageID, pullPolicy, nil
}