k8sgpt-ai/k8sgpt-operator

[Bug]: Wrong deployment image processing

ultram4rine opened this issue · 2 comments

Checklist

  • I've searched for similar issues and couldn't find anything matching
  • I've included steps to reproduce the behavior

Affected Components

  • K8sGPT (CLI)
  • K8sGPT Operator

K8sGPT Version

v0.3.8

Kubernetes Version

v1.28.0

Host OS and its Version

No response

Steps to reproduce

Include port in repository in K8sGPT object like <host>:<port>/<path>/<image>.

Expected behaviour

Operator will create a deployment with normal image.

Actual behaviour

Operator will turn image in <host>:<tag>, thus result into ImagePullBackOff.

Additional Information

I've found the lines were this happens:

// Check the version of the deployment image matches the version set in the K8sGPT CR
imageURI := deployment.Spec.Template.Spec.Containers[0].Image
image := strings.Split(imageURI, ":")
imageRepository := image[0]
imageVersion := image[1]
// if one of repository or tag is changed, we need to update the deployment
if imageRepository != k8sgptConfig.Spec.Repository || imageVersion != k8sgptConfig.Spec.Version {
// Update the deployment image
deployment.Spec.Template.Spec.Containers[0].Image = fmt.Sprintf("%s:%s",
imageRepository, k8sgptConfig.Spec.Version)
err = r.Update(ctx, &deployment)

Quick solution is to parse image like this:

image := strings.Split(imageURI, ":")
if len(image) == 2 {
	imageRepository = image[0]
	imageVersion = image[1]
} else {
	imageRepository = fmt.Sprintf("%s:%s", image[0], image[1])
	imageVersion = image[2]
}

or

image := strings.Split(imageURI, ":")

imageRepository := strings.Join(image[0:len(image)-1], ":")
imageVersion := image[len(image)-1]

Possible better solution is to use maybe net/url or some other lib.

Test: https://go.dev/play/p/nIFGk2QroT5

Btw, what is going on here:

// if one of repository or tag is changed, we need to update the deployment
if imageRepository != k8sgptConfig.Spec.Repository || imageVersion != k8sgptConfig.Spec.Version {
// Update the deployment image
deployment.Spec.Template.Spec.Containers[0].Image = fmt.Sprintf("%s:%s",
imageRepository, k8sgptConfig.Spec.Version)

If image repo is not the same as in K8sGPT CRD, it leaves it as is and updates only version.

Maybe it should update image repo to k8sgptConfig.Spec.Repository?

Btw, what is going on here:

// if one of repository or tag is changed, we need to update the deployment
if imageRepository != k8sgptConfig.Spec.Repository || imageVersion != k8sgptConfig.Spec.Version {
// Update the deployment image
deployment.Spec.Template.Spec.Containers[0].Image = fmt.Sprintf("%s:%s",
imageRepository, k8sgptConfig.Spec.Version)

If image repo is not the same as in K8sGPT CRD, it leaves it as is and updates only version.

Maybe it should update image repo to k8sgptConfig.Spec.Repository?

yes that looks wrong 👍