@@ -26,9 +26,10 @@ type Image struct {
2626 Digest string `json:"digest,omitempty" yaml:"digest,omitempty"`
2727}
2828
29- type Result struct {
30- CurrentValue string
31- ProposedValue string
29+ // setImageResult keeps the old value and new value for logging
30+ type setImageResult struct {
31+ currentValue string
32+ proposedValue string
3233}
3334
3435// SetImage supports the set-image workflow, it uses Config to parse functionConfig, Transform to change the image
@@ -43,8 +44,37 @@ type SetImage struct {
4344 resultCount int
4445}
4546
47+ // Run implements the Runner interface that transforms the resource and log the results
48+ func (t SetImage ) Run (ctx * fn.Context , functionConfig * fn.KubeObject , items fn.KubeObjects ) {
49+ err := t .configDefaultData ()
50+ if err != nil {
51+ ctx .ResultErrAndDie (err .Error (), nil )
52+ }
53+ err = t .validateInput ()
54+ if err != nil {
55+ ctx .ResultErrAndDie (err .Error (), nil )
56+ }
57+
58+ for _ , o := range items .Where (t .hasPodContainers ) {
59+ err , result := t .setPodContainers (o )
60+ t .logResult (ctx , err , result , o )
61+ }
62+
63+ for _ , o := range items .Where (t .hasPodSpecContainers ) {
64+ err , result := t .setPodSpecContainers (o )
65+ t .logResult (ctx , err , result , o )
66+ }
67+
68+ if t .AdditionalImageFields != nil {
69+ custom .SetAdditionalFieldSpec (functionConfig .GetMap ("image" ), items , functionConfig .GetSlice ("additionalImageFields" ), ctx )
70+ }
71+
72+ summary := fmt .Sprintf ("summary: updated a total of %v image(s)" , t .resultCount )
73+ ctx .ResultInfo (summary , nil )
74+ }
75+
4676// Config transforms the data from ConfigMap to SetImage struct
47- func (t * SetImage ) ConfigDefaultData () error {
77+ func (t * SetImage ) configDefaultData () error {
4878 for key , val := range t .DataFromDefaultConfig {
4979 switch key {
5080 case "name" :
@@ -78,12 +108,12 @@ func (t *SetImage) validateInput() error {
78108}
79109
80110// updateContainerImages updates the images inside containers, return potential error, and a list of logging results
81- func (t * SetImage ) updateContainerImages (pod * fn.SubObject ) (error , []Result ) {
111+ func (t * SetImage ) updateContainerImages (pod * fn.SubObject ) (error , []setImageResult ) {
82112 var containers fn.SliceSubObjects
83113 containers = append (containers , pod .GetSlice ("iniContainers" )... )
84114 containers = append (containers , pod .GetSlice ("containers" )... )
85115
86- var result []Result
116+ var result []setImageResult
87117 for _ , o := range containers {
88118 oldValue := o .NestedStringOrDie ("image" )
89119 if ! image .IsImageMatched (oldValue , t .Image .Name ) {
@@ -99,15 +129,15 @@ func (t *SetImage) updateContainerImages(pod *fn.SubObject) (error, []Result) {
99129 }
100130 t .resultCount += 1
101131
102- result = append (result , Result {
103- CurrentValue : oldValue ,
104- ProposedValue : newName ,
132+ result = append (result , setImageResult {
133+ currentValue : oldValue ,
134+ proposedValue : newName ,
105135 })
106136 }
107137 return nil , result
108138}
109139
110- func (t * SetImage ) setPodSpecContainers (o * fn.KubeObject ) (error , []Result ) {
140+ func (t * SetImage ) setPodSpecContainers (o * fn.KubeObject ) (error , []setImageResult ) {
111141 spec := o .GetMap ("spec" )
112142 if spec == nil {
113143 return nil , nil
@@ -124,7 +154,7 @@ func (t *SetImage) setPodSpecContainers(o *fn.KubeObject) (error, []Result) {
124154 return nil , result
125155}
126156
127- func (t * SetImage ) setPodContainers (o * fn.KubeObject ) (error , []Result ) {
157+ func (t * SetImage ) setPodContainers (o * fn.KubeObject ) (error , []setImageResult ) {
128158 spec := o .GetMap ("spec" )
129159 if spec == nil {
130160 return nil , nil
@@ -160,41 +190,12 @@ func getNewImageName(oldValue string, newImage Image) string {
160190 return newName
161191}
162192
163- func (t SetImage ) LogResult (ctx * fn.Context , err error , result []Result , o * fn.KubeObject ) {
193+ func (t SetImage ) logResult (ctx * fn.Context , err error , result []setImageResult , o * fn.KubeObject ) {
164194 if err != nil {
165195 ctx .ResultErr (err .Error (), o )
166196 }
167197 for _ , val := range result {
168- msg := fmt .Sprintf ("updated image from %v to %v" , val .CurrentValue , val .ProposedValue )
198+ msg := fmt .Sprintf ("updated image from %v to %v" , val .currentValue , val .proposedValue )
169199 ctx .ResultInfo (msg , o )
170200 }
171201}
172-
173- // Run implements the Runner interface that transforms the resource and log the results
174- func (t SetImage ) Run (ctx * fn.Context , functionConfig * fn.KubeObject , items fn.KubeObjects ) {
175- err := t .ConfigDefaultData ()
176- if err != nil {
177- ctx .ResultErrAndDie (err .Error (), nil )
178- }
179- err = t .validateInput ()
180- if err != nil {
181- ctx .ResultErrAndDie (err .Error (), nil )
182- }
183-
184- for _ , o := range items .Where (t .hasPodContainers ) {
185- err , result := t .setPodContainers (o )
186- t .LogResult (ctx , err , result , o )
187- }
188-
189- for _ , o := range items .Where (t .hasPodSpecContainers ) {
190- err , result := t .setPodSpecContainers (o )
191- t .LogResult (ctx , err , result , o )
192- }
193-
194- if t .AdditionalImageFields != nil {
195- custom .SetAdditionalFieldSpec (functionConfig .GetMap ("image" ), items , functionConfig .GetSlice ("additionalImageFields" ), ctx )
196- }
197-
198- summary := fmt .Sprintf ("summary: updated a total of %v image(s)" , t .resultCount )
199- ctx .ResultInfo (summary , nil )
200- }
0 commit comments