@@ -19,9 +19,11 @@ package controller
1919import (
2020 "errors"
2121 "fmt"
22+ "strings"
2223
2324 "github.com/distribution/reference"
2425 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26+ "k8s.io/apimachinery/pkg/runtime"
2527 "k8s.io/client-go/kubernetes/scheme"
2628
2729 appsv1 "k8s.io/api/apps/v1"
@@ -30,10 +32,6 @@ import (
3032 configclient "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
3133)
3234
33- const (
34- daemonSetKind = "DaemonSet"
35- )
36-
3735func imageOverrides (component string , overrides configclient.Client ) func (objs []unstructured.Unstructured ) ([]unstructured.Unstructured , error ) {
3836 imageOverridesWrapper := func (objs []unstructured.Unstructured ) ([]unstructured.Unstructured , error ) {
3937 if overrides == nil {
@@ -86,41 +84,43 @@ func fixImages(objs []unstructured.Unstructured, alterImageFunc func(image strin
8684 return objs , nil
8785}
8886
89- func fixDeploymentImages (o * unstructured.Unstructured , alterImageFunc func (image string ) (string , error )) error {
90- if o .GetKind () != deploymentKind {
87+ // fixWorkloadImages is a generic helper that converts an unstructured object into a typed
88+ // workload, applies image fixups to its PodSpec, and converts it back. This eliminates
89+ // duplication between Deployment and DaemonSet image fixing.
90+ func fixWorkloadImages [T runtime.Object ](
91+ o * unstructured.Unstructured ,
92+ kind string ,
93+ target T ,
94+ getPodSpec func (T ) * corev1.PodSpec ,
95+ alterImageFunc func (image string ) (string , error ),
96+ ) error {
97+ if o .GetKind () != kind {
9198 return nil
9299 }
93100
94- // Convert Unstructured into a typed object
95- d := & appsv1.Deployment {}
96- if err := scheme .Scheme .Convert (o , d , nil ); err != nil {
101+ if err := scheme .Scheme .Convert (o , target , nil ); err != nil {
97102 return err
98103 }
99104
100- if err := fixPodSpecImages (& d . Spec . Template . Spec , alterImageFunc ); err != nil {
101- return fmt .Errorf ("%w: failed to fix containers in deployment %s" , err , d . Name )
105+ if err := fixPodSpecImages (getPodSpec ( target ) , alterImageFunc ); err != nil {
106+ return fmt .Errorf ("%w: failed to fix containers in %s %s" , err , strings . ToLower ( kind ), o . GetName () )
102107 }
103108
104- // Convert typed object back to Unstructured
105- return scheme .Scheme .Convert (d , o , nil )
109+ return scheme .Scheme .Convert (target , o , nil )
106110}
107111
108- func fixDaemonSetImages (o * unstructured.Unstructured , alterImageFunc func (image string ) (string , error )) error {
109- if o .GetKind () != daemonSetKind {
110- return nil
111- }
112-
113- // Convert Unstructured into a typed object
114- d := & appsv1.DaemonSet {}
115- if err := scheme .Scheme .Convert (o , d , nil ); err != nil {
116- return err
117- }
112+ func fixDeploymentImages (o * unstructured.Unstructured , alterImageFunc func (image string ) (string , error )) error {
113+ return fixWorkloadImages (o , deploymentKind , & appsv1.Deployment {},
114+ func (d * appsv1.Deployment ) * corev1.PodSpec { return & d .Spec .Template .Spec },
115+ alterImageFunc ,
116+ )
117+ }
118118
119- if err := fixPodSpecImages ( & d . Spec . Template . Spec , alterImageFunc ); err != nil {
120- return fmt . Errorf ( "%w: failed to fix containers in deamonSet %s" , err , d . Name )
121- }
122- // Convert typed object back to Unstructured
123- return scheme . Scheme . Convert ( d , o , nil )
119+ func fixDaemonSetImages ( o * unstructured. Unstructured , alterImageFunc func ( image string ) ( string , error )) error {
120+ return fixWorkloadImages ( o , daemonSetKind , & appsv1. DaemonSet {},
121+ func ( d * appsv1. DaemonSet ) * corev1. PodSpec { return & d . Spec . Template . Spec },
122+ alterImageFunc ,
123+ )
124124}
125125
126126func fixPodSpecImages (podSpec * corev1.PodSpec , alterImageFunc func (image string ) (string , error )) error {
0 commit comments