diff --git a/chart/Chart.yaml b/chart/Chart.yaml index 5a6d373..1382627 100644 --- a/chart/Chart.yaml +++ b/chart/Chart.yaml @@ -10,9 +10,9 @@ description: | type: application -version: 0.2.29 +version: 0.2.30 -appVersion: "0.2.29" +appVersion: "0.2.30" home: https://github.com/lukaszraczylo/kubernetes-images-sync-operator diff --git a/chart/values.yaml b/chart/values.yaml index c913aae..0f130fb 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -12,7 +12,7 @@ sa: - ALL image: repository: ghcr.io/lukaszraczylo/kubernetes-images-sync-operator - tag: 0.2.29 + tag: 0.2.30 resources: limits: cpu: 500m diff --git a/cmd/main.go b/cmd/main.go index 612db35..01ea269 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "context" "crypto/tls" "flag" "os" @@ -25,7 +26,9 @@ import ( // to ensure that exec-entrypoint and run can make use of them. _ "k8s.io/client-go/plugin/pkg/client/auth" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" utilruntime "k8s.io/apimachinery/pkg/util/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" ctrl "sigs.k8s.io/controller-runtime" @@ -150,10 +153,29 @@ func main() { os.Exit(1) } - if err = (&raczylocomcontroller.ClusterImageExportReconciler{ + // Get controller pod annotations + ctx := context.Background() + podName := os.Getenv("POD_NAME") + podNamespace := os.Getenv("POD_NAMESPACE") + var podAnnotations map[string]string + if podName != "" && podNamespace != "" { + pod := &corev1.Pod{} + if err := mgr.GetAPIReader().Get(ctx, types.NamespacedName{ + Name: podName, + Namespace: podNamespace, + }, pod); err == nil { + podAnnotations = pod.Annotations + } else { + setupLog.Error(err, "unable to get controller pod annotations") + } + } + + exportController := &raczylocomcontroller.ClusterImageExportReconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), - }).SetupWithManager(mgr); err != nil { + } + exportController.InjectPodAnnotations(podAnnotations) + if err = exportController.SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "ClusterImageExport") os.Exit(1) } diff --git a/internal/controller/raczylo.com/clusterimageexport_controller.go b/internal/controller/raczylo.com/clusterimageexport_controller.go index d5e1ba5..51ec044 100644 --- a/internal/controller/raczylo.com/clusterimageexport_controller.go +++ b/internal/controller/raczylo.com/clusterimageexport_controller.go @@ -26,6 +26,11 @@ import ( type ClusterImageExportReconciler struct { client.Client Scheme *runtime.Scheme + podAnnotations map[string]string +} + +func (r *ClusterImageExportReconciler) InjectPodAnnotations(annotations map[string]string) { + r.podAnnotations = annotations } // +kubebuilder:rbac:groups=raczylo.com,resources=*,verbs=get;list;watch;create;update;patch;delete @@ -320,12 +325,21 @@ func (r *ClusterImageExportReconciler) runCleanupJob(ctx context.Context, cluste backoffLimit := int32(2) // 3 total attempts (initial + 2 retries) ttlSecondsAfterFinished := int32(30) // Delete job 30 seconds after completion + // Merge controller pod annotations with job annotations + mergedAnnotations := make(map[string]string) + for k, v := range r.podAnnotations { + mergedAnnotations[k] = v + } + for k, v := range clusterImageExport.Spec.JobAnnotations { + mergedAnnotations[k] = v + } + jobParams := shared.JobParams{ Name: normalisedImageName, Namespace: clusterImageExport.Namespace, Image: shared.BACKUP_JOB_IMAGE, Commands: defaultCommands, - Annotations: clusterImageExport.Spec.JobAnnotations, + Annotations: mergedAnnotations, ServiceAccount: "", ImagePullSecrets: clusterImageExport.Spec.ImagePullSecrets, BackoffLimit: &backoffLimit,