@@ -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,68 @@ 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
+ fmt .Fprintln (manifestOut , manifests .String ())
239
+ return nil
240
+ }
241
+
242
+ func (k * KubectlDeployer ) renderManifests (ctx context.Context , out io.Writer , builds []build.Artifact ) (deploy.ManifestList , error ) {
243
+ if err := k .kubectl .CheckVersion (ctx ); err != nil {
244
+ color .Default .Fprintln (out , "kubectl client version:" , k .kubectl .Version (ctx ))
245
+ color .Default .Fprintln (out , err )
246
+ }
247
+
248
+ manifests , err := k .readManifests (ctx )
249
+ if err != nil {
250
+ return nil , errors .Wrap (err , "reading manifests" )
251
+ }
252
+
253
+ for _ , m := range k .RemoteManifests {
254
+ manifest , err := k .readRemoteManifest (ctx , m )
255
+ if err != nil {
256
+ return nil , errors .Wrap (err , "get remote manifests" )
257
+ }
258
+
259
+ manifests = append (manifests , manifest )
260
+ }
261
+
262
+ if len (k .originalImages ) == 0 {
263
+ k .originalImages , err = manifests .GetImages ()
264
+ if err != nil {
265
+ return nil , errors .Wrap (err , "get images from manifests" )
266
+ }
267
+ }
268
+
269
+ if len (manifests ) == 0 {
270
+ return nil , nil
271
+ }
272
+
273
+ manifests , err = manifests .ReplaceImages (builds , k .defaultRepo )
274
+ if err != nil {
275
+ return nil , errors .Wrap (err , "replacing images in manifests" )
276
+ }
277
+
278
+ for _ , transform := range manifestTransforms {
279
+ manifests , err = transform (manifests , builds , k .insecureRegistries )
280
+ if err != nil {
281
+ return nil , errors .Wrap (err , "unable to transform manifests" )
282
+ }
283
+ }
284
+
285
+ return manifests , nil
257
286
}
0 commit comments