From b4f4b0d126cea3b69feb4d87600551ef921f5d02 Mon Sep 17 00:00:00 2001 From: ultram4rine Date: Fri, 16 Feb 2024 18:39:16 +0400 Subject: [PATCH 1/2] fix: change deployment image reconciling Signed-off-by: ultram4rine --- controllers/k8sgpt_controller.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/controllers/k8sgpt_controller.go b/controllers/k8sgpt_controller.go index 0622f3f5..e300ea1e 100644 --- a/controllers/k8sgpt_controller.go +++ b/controllers/k8sgpt_controller.go @@ -165,18 +165,21 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr if deployment.Status.ReadyReplicas > 0 { - // Check the version of the deployment image matches the version set in the K8sGPT CR + // Check the repo and version of the deployment image matches the repo and version set in the K8sGPT CR imageURI := deployment.Spec.Template.Spec.Containers[0].Image image := strings.Split(imageURI, ":") - imageRepository := image[0] - imageVersion := image[1] + imageRepository := strings.Join(image[0:len(image)-1], ":") + imageVersion := image[len(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) + deployment.Spec.Template.Spec.Containers[0].Image = fmt.Sprintf( + "%s:%s", + k8sgptConfig.Spec.Repository, + k8sgptConfig.Spec.Version, + ) err = r.Update(ctx, &deployment) if err != nil { k8sgptReconcileErrorCount.Inc() From 19dee35e44759dc70c6277752ff5d545d73855fa Mon Sep 17 00:00:00 2001 From: ultram4rine Date: Fri, 19 Apr 2024 13:33:59 +0400 Subject: [PATCH 2/2] improve image reconciling Signed-off-by: ultram4rine --- controllers/k8sgpt_controller.go | 43 +++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/controllers/k8sgpt_controller.go b/controllers/k8sgpt_controller.go index 8dabd492..ae1eef2e 100644 --- a/controllers/k8sgpt_controller.go +++ b/controllers/k8sgpt_controller.go @@ -181,10 +181,7 @@ func (r *K8sGPTReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr // Check the repo and version of the deployment image matches the repo and version set in the K8sGPT CR imageURI := deployment.Spec.Template.Spec.Containers[0].Image - - image := strings.Split(imageURI, ":") - imageRepository := strings.Join(image[0:len(image)-1], ":") - imageVersion := image[len(image)-1] + imageRepository, imageVersion := parseImageURI(imageURI) // if one of repository or tag is changed, we need to update the deployment if imageRepository != k8sgptConfig.Spec.Repository || imageVersion != k8sgptConfig.Spec.Version { @@ -454,3 +451,41 @@ func (r *K8sGPTReconciler) finishReconcile(err error, requeueImmediate bool) (ct fmt.Println("Finished Reconciling k8sGPT") return ctrl.Result{Requeue: true, RequeueAfter: interval}, nil } + +// https://kubernetes.io/docs/concepts/containers/images/#image-names +func parseImageURI(uri string) (string, string) { + // We have possible image variants: + // - pause + // - pause:v1.0.0 + // With registry + // - fictional.registry.example/imagename + // - fictional.registry.example:10443/imagename + // - fictional.registry.example/imagename:v1.0.0 + // - fictional.registry.example:10443/imagename:v1.0.0 + + var ( + repository string + version string + ) + + if strings.Contains(uri, "/") { + parts := strings.SplitN(uri, "/", 2) + registry := parts[0] + name := parts[1] + if strings.Contains(name, ":") { + nameParts := strings.SplitN(name, ":", 2) + repository = registry + "/" + nameParts[0] + version = nameParts[1] + } else { + repository = registry + "/" + name + } + } else if strings.Contains(uri, ":") { + imageParts := strings.SplitN(uri, ":", 2) + repository = imageParts[0] + version = imageParts[1] + } else { + repository = uri + } + + return repository, version +}