8000 [Bugfix] Fix Image Discovery by ajanikow · Pull Request #1600 · arangodb/kube-arangodb · GitHub
[go: up one dir, main page]

Skip to content

[Bugfix] Fix Image Discovery #1600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A)
- (Feature) Extract Scheduler API
- (Bugfix) Fix Image Discovery

## [1.2.38](https://github.com/arangodb/kube-arangodb/tree/1.2.38) (2024-02-22)
- (Feature) Extract GRPC Server
Expand Down
2 changes: 2 additions & 0 deletions chart/kube-arangodb/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: MY_CONTAINER_NAME
value: "operator"
- name: MY_POD_IP
valueFrom:
fieldRef:
Expand Down
7 changes: 2 additions & 5 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"k8s.io/client-go/tools/record"

"github.com/arangodb/kube-arangodb/pkg/api"
shared "github.com/arangodb/kube-arangodb/pkg/apis/shared"
"github.com/arangodb/kube-arangodb/pkg/crd"
agencyConfig "github.com/arangodb/kube-arangodb/pkg/deployment/agency/config"
"github.com/arangodb/kube-arangodb/pkg/deployment/features"
Expand Down Expand Up @@ -594,13 +595,9 @@ func getMyPodInfo(kubecli kubernetes.Interface, namespace, name string) (string,
return errors.WithStack(err)
}
sa = pod.Spec.ServiceAccountName
if image, err = k8sutil.GetArangoDBImageIDFromPod(pod); err != nil {
if image, err = k8sutil.GetArangoDBImageIDFromPod(pod, shared.ServerContainerName, shared.OperatorContainerName, constants.MyContainerNameEnv.GetOrDefault(shared.OperatorContainerName)); err != nil {
return errors.Wrap(err, "failed to get image ID from pod")
}
if image == "" {
// Fallback in case we don't know the id.
image = pod.Spec.Containers[0].Image
}
return nil
}
if err := retry.Retry(op, time.Minute*5); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/shared/constants.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,6 +45,7 @@ const (
// Pod constants
ServerContainerName = "server"
ExporterContainerName = "exporter"
OperatorContainerName = "operator"
ArangodVolumeName = "arangod-data"
TlsKeyfileVolumeName = "tls-keyfile"
ClientAuthCAVolumeName = "client-auth-ca"
Expand Down
8 changes: 2 additions & 6 deletions pkg/deployment/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,8 @@ func (ib *imagesBuilder) fetchArangoDBImageIDAndVersion(ctx context.Context, cac
return true, nil
}

imageID, err := k8sutil.GetArangoDBImageIDFromPod(pod)
if err != nil {
log.Err(err).Warn("failed to get image ID from pod")
return true, nil
}
if imageID == "" {
imageID, ok := k8sutil.GetArangoDBImageIDFromContainerStatuses(pod.Status.ContainerStatuses, shared.ServerContainerName)
if !ok {
// Fall back to specified image
imageID = image
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/util/constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,8 @@

package constants

import "github.com/arangodb/kube-arangodb/pkg/util"

const (
EnvOperatorNodeName = "MY_NODE_NAME"
EnvOperatorNodeNameArango = "NODE_NAME"
Expand Down Expand Up @@ -73,6 +75,10 @@ const (
LabelRoleLeader = "leader"
)

const (
MyContainerNameEnv util.EnvironmentVariable = "MY_CONTAINER_NAME"
)

func ManagedFinalizers() []string {
return []string{
FinalizerDeplRemoveChildFinalizers,
Expand Down
11 changes: 11 additions & 0 deletions pkg/util/k8sutil/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ func GetContainerIDByName(containers []core.Container, name string) int {
return -1
}

// GetContainerStatusIDByName returns the container id in the given list with the given name.
// // Returns -1 if not found.
func GetContainerStatusIDByName(containers []core.ContainerStatus, name string) int {
for id, c := range containers {
if c.Name == name {
return id
}
}
return -1
}

// GetContainerByName returns the container in the given pod with the given name.
// Returns false if not found.
func GetContainerByName(p *core.Pod, name string) (core.Container, bool) {
Expand Down
48 changes: 39 additions & 9 deletions pkg/util/k8sutil/images.go
F438
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
core "k8s.io/api/core/v1"

schedulerContainerResourcesApi "github.com/arangodb/kube-arangodb/pkg/apis/scheduler/v1alpha1/container/resources"
shared "github.com/arangodb/kube-arangodb/pkg/apis/shared"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/container"
)

const (
Expand All @@ -44,23 +44,53 @@ func ConvertImageID2Image(imageID string) string {
}

// GetArangoDBImageIDFromPod returns the ArangoDB specific image from a pod
func GetArangoDBImageIDFromPod(pod *core.Pod) (string, error) {
func GetArangoDBImageIDFromPod(pod *core.Pod, names ...string) (string, error) {
if pod == nil {
return "", errors.New("failed to get container statuses from nil pod")
}

if len(pod.Status.ContainerStatuses) == 0 {
return "", errors.New("empty list of ContainerStatuses")
// First try to find container by name
if image, ok := GetArangoDBImageIDFromContainerStatuses(pod.Status.ContainerStatuses, names...); ok {
return image, nil
}
if image, ok := GetArangoDBImageIDFromContainers(pod.Spec.Containers, names...); ok {
return image, nil
}

if cs := pod.Status.ContainerStatuses; len(cs) > 0 {
if image := cs[0].ImageID; image != "" {
return ConvertImageID2Image(image), nil
}
}
if cs := pod.Spec.Containers; len(cs) > 0 {
if image := cs[0].Image; image != "" {
return image, nil
}
}

return "", errors.Errorf("Unable to find image from pod")
}

// GetArangoDBImageIDFromContainerStatuses returns the ArangoDB specific image from a container statuses
func GetArangoDBImageIDFromContainerStatuses(containers []core.ContainerStatus, names ...string) (string, bool) {
for _, name := range names {
if id := container.GetContainerStatusIDByName(containers, name); id != -1 {
return ConvertImageID2Image(containers[id].ImageID), true
}
}

return "", false
}

for _, cs := range pod.Status.ContainerStatuses {
if cs.Name == shared.ServerContainerName {
return ConvertImageID2Image(cs.ImageID), nil
// GetArangoDBImageIDFromContainers returns the ArangoDB specific image from a container specs
func GetArangoDBImageIDFromContainers(containers []core.Container, names ...string) (string, bool) {
for _, name := range names {
if id := container.GetContainerIDByName(containers, name); id != -1 {
return containers[id].Image, true
}
}

// If Server container is not found use first container
return ConvertImageID2Image(pod.Status.ContainerStatuses[0].ImageID), nil
return "", false
}

// GetImageDetails Returns latest defined Image details
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/k8sutil/images_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestGetArangoDBImageIDFromPod(t *testing.T) {
args: args{
pod: &core.Pod{},
},
wantErr: errors.New("empty list of ContainerStatuses"),
wantErr: errors.New("Unable to find image from pod"),
},
"image ID from the only container": {
args: args{
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestGetArangoDBImageIDFromPod(t *testing.T) {

for testName, testCase := range tests {
t.Run(testName, func(t *testing.T) {
got, err := GetArangoDBImageIDFromPod(testCase.args.pod)
got, err := GetArangoDBImageIDFromPod(testCase.args.pod, shared.ServerContainerName)
if testCase.wantErr != nil {
require.EqualError(t, err, testCase.wantErr.Error())
return
Expand Down
0