@@ -17,9 +17,12 @@ limitations under the License.
17
17
package deploy
18
18
19
19
import (
20
+ "bufio"
20
21
"bytes"
21
22
"context"
23
+ "fmt"
22
24
"io"
25
+ "os"
23
26
"strings"
24
27
25
28
"github.com/pkg/errors"
@@ -73,65 +76,29 @@ func (k *KubectlDeployer) Labels() map[string]string {
73
76
// Deploy templates the provided manifests with a simple `find and replace` and
74
77
// runs `kubectl apply` on those manifests
75
78
func (k * KubectlDeployer ) Deploy (ctx context.Context , out io.Writer , builds []build.Artifact , labellers []Labeller ) * Result {
76
- if err := k .kubectl .CheckVersion (ctx ); err != nil {
77
- color .Default .Fprintln (out , "kubectl client version:" , k .kubectl .Version (ctx ))
78
- color .Default .Fprintln (out , err )
79
- }
79
+ event .DeployInProgress ()
80
+ manifests , err := k .renderManifests (ctx , out , builds )
80
81
81
- manifests , err := k .readManifests (ctx )
82
82
if err != nil {
83
83
event .DeployFailed (err )
84
- return NewDeployErrorResult (errors . Wrap ( err , "reading manifests" ) )
84
+ return NewDeployErrorResult (err )
85
85
}
86
86
87
- for _ , m := range k .RemoteManifests {
88
- manifest , err := k .readRemoteManifest (ctx , m )
89
- if err != nil {
90
- return NewDeployErrorResult (errors .Wrap (err , "get remote manifests" ))
91
- }
92
-
93
- manifests = append (manifests , manifest )
94
- }
95
-
96
- if len (k .originalImages ) == 0 {
97
- k .originalImages , err = manifests .GetImages ()
98
- if err != nil {
99
- return NewDeployErrorResult (errors .Wrap (err , "get images from manifests" ))
100
- }
101
- }
102
-
103
- logrus .Debugln ("manifests" , manifests .String ())
104
-
105
87
if len (manifests ) == 0 {
88
+ event .DeployComplete ()
106
89
return NewDeploySuccessResult (nil )
107
90
}
108
91
109
- event .DeployInProgress ()
110
-
111
- namespaces , err := manifests .CollectNamespaces ()
112
- if err != nil {
113
- event .DeployInfoEvent (errors .Wrap (err , "could not fetch deployed resource namespace. " +
114
- "This might cause port-forward and deploy health-check to fail." ))
115
- }
116
-
117
- manifests , err = manifests .ReplaceImages (builds , k .defaultRepo )
118
- if err != nil {
119
- event .DeployFailed (err )
120
- return NewDeployErrorResult (errors .Wrap (err , "replacing images in manifests" ))
121
- }
122
-
123
92
manifests , err = manifests .SetLabels (merge (labellers ... ))
124
93
if err != nil {
125
94
event .DeployFailed (err )
126
95
return NewDeployErrorResult (errors .Wrap (err , "setting labels in manifests" ))
127
96
}
128
97
129
- for _ , transform := range manifestTransforms {
130
- manifests , err = transform (manifests , builds , k .insecureRegistries )
131
- if err != nil {
132
- event .DeployFailed (err )
133
- return NewDeployErrorResult (errors .Wrap (err , "unable to transform manifests" ))
134
- }
98
+ namespaces , err := manifests .CollectNamespaces ()
99
+ if err != nil {
100
+ event .DeployInfoEvent (errors .Wrap (err , "could not fetch deployed resource namespace. " +
101
+ "This might cause port-forward and deploy health-check to fail." ))
135
102
}
136
103
137
104
if err := k .kubectl .Apply (ctx , textio .NewPrefixWriter (out , " - " ), manifests ); err != nil {
@@ -252,6 +219,72 @@ func (k *KubectlDeployer) readRemoteManifest(ctx context.Context, name string) (
252
219
return manifest .Bytes (), nil
253
220
}
254
221
255
- func (k * KubectlDeployer ) Render (context.Context , io.Writer , []build.Artifact , string ) error {
256
- return errors .New ("not yet implemented" )
222
+ func (k * KubectlDeployer ) Render (ctx context.Context , out io.Writer , builds []build.Artifact , filepath string ) error {
223
+ manifests , err := k .renderManifests (ctx , out , builds )
224
+
225
+ if err != nil {
226
+ return err
227
+ }
228
+
229
+ manifestOut := out
230
+ if filepath != "" {
231
+ f , err := os .Open (filepath )
232
+ if err != nil {
233
+ return errors .Wrap (err , "opening file for writing manifests" )
234
+ }
235
+ manifestOut = bufio .NewWriter (f )
236
+ }
237
+
238
+ for _ , m := range manifests {
239
+ if _ , err := fmt .Fprintln (manifestOut , string (m )); err != nil {
240
+ return errors .Wrap (err , "writing manifests" )
241
+ }
242
+ }
243
+ return nil
244
+ }
245
+
246
+ func (k * KubectlDeployer ) renderManifests (ctx context.Context , out io.Writer , builds []build.Artifact ) (deploy.ManifestList , error ) {
247
+ if err := k .kubectl .CheckVersion (ctx ); err != nil {
248
+ color .Default .Fprintln (out , "kubectl client version:" , k .kubectl .Version (ctx ))
249
+ color .Default .Fprintln (out , err )
250
+ }
251
+
252
+ manifests , err := k .readManifests (ctx )
253
+ if err != nil {
254
+ return nil , errors .Wrap (err , "reading manifests" )
255
+ }
256
+
257
+ for _ , m := range k .RemoteManifests {
258
+ manifest , err := k .readRemoteManifest (ctx , m )
259
+ if err != nil {
260
+ return nil , errors .Wrap (err , "get remote manifests" )
261
+ }
262
+
263
+ manifests = append (manifests , manifest )
264
+ }
265
+
266
+ if len (k .originalImages ) == 0 {
267
+ k .originalImages , err = manifests .GetImages ()
268
+ if err != nil {
269
+ return nil , errors .Wrap (err , "get images from manifests" )
270
+ }
271
+ }
272
+
273
+ if len (manifests ) == 0 {
274
+ return nil , nil
275
+ }
276
+
277
+ manifests , err = manifests .ReplaceImages (builds , k .defaultRepo )
278
+ if err != nil {
279
+ return nil , errors .Wrap (err , "replacing images in manifests" )
280
+ }
281
+
282
+ for _ , transform := range manifestTransforms {
283
+ manifests , err = transform (manifests , builds , k .insecureRegistries )
284
+ if err != nil {
285
+ return nil , errors .Wrap (err , "unable to transform manifests" )
286
+ }
287
+ }
288
+
289
+ return manifests , nil
257
290
}
0 commit comments