10000 [Bugfix] Wait for ImageStatus · arangodb/kube-arangodb@ec2c4c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit ec2c4c6

Browse files
committed
[Bugfix] Wait for ImageStatus
1 parent b085209 commit ec2c4c6

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- (Feature) Extract Scheduler API
55
- (Bugfix) Fix Image Discovery
66
- (Bugfix) Fix Resources Copy mechanism to prevent invalid pod creation
7+
- (Bugfix) Wait for ImageStatus in ImageDiscover
78

89
## [1.2.38](https://github.com/arangodb/kube-arangodb/tree/1.2.38) (2024-02-22)
910
- (Feature) Extract GRPC Server

cmd/cmd.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ var (
161161
backupArangoD time.Duration
162162
backupUploadArangoD time.Duration
163163
}
164+
operatorImageDiscovery struct {
165+
timeout time.Duration
166+
defaultStatusDiscovery bool
167+
}
164168
operatorReconciliationRetry struct {
165169
delay time.Duration
166170
count int
@@ -241,6 +245,8 @@ func init() {
241245
f.IntVar(&operatorBackup.concurrentUploads, "backup-concurrent-uploads", globals.DefaultBackupConcurrentUploads, "Number of concurrent uploads per deployment")
242246
f.Uint64Var(&memoryLimit.hardLimit, "memory-limit", 0, "Define memory limit for hard shutdown and the dump of goroutines. Used for 10000 testing")
243247
f.StringArrayVar(&metricsOptions.excludedMetricPrefixes, "metrics.excluded-prefixes", nil, "List of the excluded metrics prefixes")
248+
f.BoolVar(&operatorImageDiscovery.defaultStatusDiscovery, "image.discovery.status", true, "Discover Operator Image from Pod Status by default")
249+
f.DurationVar(&operatorImageDiscovery.timeout, "image.discovery.timeout", time.Minute, "Timeout for image discovery process")
244250
if err := features.Init(&cmdMain); err != nil {
245251
panic(err.Error())
246252
}
@@ -584,6 +590,20 @@ func newOperatorConfigAndDeps(id, namespace, name string) (operator.Config, oper
584590
// getMyPodInfo looks up the image & service account of the pod with given name in given namespace
585591
// Returns image, serviceAccount, error.
586592
func getMyPodInfo(kubecli kubernetes.Interface, namespace, name string) (string, string, error) {
593+
if image, sa, ok := getMyPodInfoWrap(kubecli, namespace, name, getMyImageInfoFunc(operatorImageDiscovery.defaultStatusDiscovery)); ok {
594+
return image, sa, nil
595+
}
596+
597+
logger.Warn("Unable to discover image, fallback to second method")
598+
599+
if image, sa, ok := getMyPodInfoWrap(kubecli, namespace, name, getMyImageInfoFunc(!operatorImageDiscovery.defaultStatusDiscovery)); ok {
600+
return image, sa, nil
601+
}
602+
603+
return "", "", errors.Errorf("Unable to discover image")
604+
}
605+
606+
func getMyPodInfoWrap(kubecli kubernetes.Interface, namespace, name string, imageFunc func(in *core.Pod) (string, bool)) (string, string, bool) {
587607
var image, sa string
588608
op := func() error {
589609
pod, err := kubecli.CoreV1().Pods(namespace).Get(context.Background(), name, meta.GetOptions{})
@@ -595,15 +615,30 @@ func getMyPodInfo(kubecli kubernetes.Interface, namespace, name string) (string,
595615
return errors.WithStack(err)
596616
}
597617
sa = pod.Spec.ServiceAccountName
598-
if image, err = k8sutil.GetArangoDBImageIDFromPod(pod, shared.ServerContainerName, shared.OperatorContainerName, constants.MyContainerNameEnv.GetOrDefault(shared.OperatorContainerName)); err != nil {
618+
if i, ok := imageFunc(pod); !ok {
599619
return errors.Wrap(err, "failed to get image ID from pod")
620+
} else {
621+
image = i
600622
}
601623
return nil
602624
}
603-
if err := retry.Retry(op, time.Minute*5); err != nil {
604-
return "", "", errors.WithStack(err)
625+
if err := retry.Retry(op, operatorImageDiscovery.timeout/2); err == nil {
626+
return image, sa, true
627+
}
628+
return "", "", false
629+
}
630+
631+
func getMyImageInfoFunc(status bool) func(pod *core.Pod) (string, bool) {
632+
return func(pod *core.Pod) (string, bool) {
633+
if image, ok := constants.MyImageEnv.Lookup(); ok {
634+
return image, true
635+
}
636+
637+
if status {
638+
return k8sutil.GetArangoDBImageIDFromContainerStatuses(pod.Status.ContainerStatuses, shared.ServerContainerName, shared.OperatorContainerName, constants.MyContainerNameEnv.GetOrDefault(shared.OperatorContainerName))
639+
}
640+
return k8sutil.GetArangoDBImageIDFromContainers(pod.Spec.Containers, shared.ServerContainerName, shared.OperatorContainerName, constants.MyContainerNameEnv.GetOrDefault(shared.OperatorContainerName))
605641
}
606-
return image, sa, nil
607642
}
608643

609644
func createRecorder(kubecli kubernetes.Interface, name, namespace string) record.EventRecorder {

pkg/util/constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const (
7777

7878
const (
7979
MyContainerNameEnv util.EnvironmentVariable = "MY_CONTAINER_NAME"
80+
MyImageEnv util.EnvironmentVariable = "MY_IMAGE"
8081
)
8182

8283
func ManagedFinalizers() []string {

pkg/util/k8sutil/images.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@ func GetArangoDBImageIDFromPod(pod *core.Pod, names ...string) (string, error) {
5353
if image, ok := GetArangoDBImageIDFromContainerStatuses(pod.Status.ContainerStatuses, names...); ok {
5454
return image, nil
5555
}
56+
5657
if image, ok := GetArangoDBImageIDFromContainers(pod.Spec.Containers, names...); ok {
5758
return image, nil
5859
}
5960

6061
if cs := pod.Status.ContainerStatuses; len(cs) > 0 {
6162
if image := cs[0].ImageID; image != "" {
62-
return ConvertImageID2Image(image), nil
63+
if disc := ConvertImageID2Image(image); disc != "" {
64+
return disc, nil
65+
}
6366
}
6467
}
6568
if cs := pod.Spec.Containers; len(cs) > 0 {
@@ -75,7 +78,11 @@ func GetArangoDBImageIDFromPod(pod *core.Pod, names ...string) (string, error) {
7578
func GetArangoDBImageIDFromContainerStatuses(containers []core.ContainerStatus, names ...string) (string, bool) {
7679
for _, name := range names {
7780
if id := container.GetContainerStatusIDByName(containers, name); id != -1 {
78-
return ConvertImageID2Image(containers[id].ImageID), true
81+
if image := containers[id].ImageID; image != "" {
82+
if disc := ConvertImageID2Image(image); disc != "" {
83+
return disc, true
84+
}
85+
}
7986
}
8087
}
8188

@@ -86,7 +93,9 @@ func GetArangoDBImageIDFromContainerStatuses(containers []core.ContainerStatus,
8693
func GetArangoDBImageIDFromContainers(containers []core.Container, names ...string) (string, bool) {
8794
for _, name := range names {
8895
if id := container.GetContainerIDByName(containers, name); id != -1 {
89-
return containers[id].Image, true
96+
if image := containers[id].Image; image != "" {
97+
return image, true
98+
}
9099
}
91100
}
92101

0 commit comments

Comments
 (0)
0