@@ -17,8 +17,10 @@ limitations under the License.
17
17
package deploy
18
18
19
19
import (
20
+ "bytes"
20
21
"context"
21
22
"io"
23
+ "strings"
22
24
23
25
"github.com/pkg/errors"
24
26
"github.com/sirupsen/logrus"
@@ -38,6 +40,7 @@ import (
38
40
type KubectlDeployer struct {
39
41
* latest.KubectlDeploy
40
42
43
+ originalImages []build.Artifact
41
44
workingDir string
42
45
kubectl deploy.CLI
43
46
defaultRepo string
@@ -80,6 +83,24 @@ func (k *KubectlDeployer) Deploy(ctx context.Context, out io.Writer, builds []bu
80
83
return errors .Wrap (err , "reading manifests" )
81
84
}
82
85
86
+ for _ , m := range k .RemoteManifests {
87
+ manifest , err := k .readRemoteManifest (ctx , m )
88
+ if err != nil {
89
+ return errors .Wrap (err , "get remote manifests" )
90
+ }
91
+
92
+ manifests = append (manifests , manifest )
93
+ }
94
+
95
+ if len (k .originalImages ) == 0 {
96
+ k .originalImages , err = manifests .GetImages ()
97
+ if err != nil {
98
+ return errors .Wrap (err , "get images from manifests" )
99
+ }
100
+ }
101
+
102
+ logrus .Debugln ("manifests" , manifests .String ())
103
+
83
104
if len (manifests ) == 0 {
84
105
return nil
85
106
}
@@ -122,6 +143,22 @@ func (k *KubectlDeployer) Cleanup(ctx context.Context, out io.Writer) error {
122
143
return errors .Wrap (err , "reading manifests" )
123
144
}
124
145
146
+ // pull remote manifests
147
+ var rm deploy.ManifestList
148
+ for _ , m := range k .RemoteManifests {
149
+ manifest , err := k .readRemoteManifest (ctx , m )
150
+ if err != nil {
151
+ return errors .Wrap (err , "get remote manifests" )
152
+ }
153
+ rm = append (rm , manifest )
154
+ }
155
+ upd , err := rm .ReplaceImages (k .originalImages , k .defaultRepo )
156
+ if err != nil {
157
+ return errors .Wrap (err , "replacing with originals" )
158
+ }
159
+ if err := k .kubectl .Apply (ctx , out , upd ); err != nil {
160
+ return errors .Wrap (err , "apply original" )
161
+ }
125
162
if err := k .kubectl .Delete (ctx , out , manifests ); err != nil {
126
163
return errors .Wrap (err , "delete" )
127
164
}
@@ -182,3 +219,23 @@ func (k *KubectlDeployer) readManifests(ctx context.Context) (deploy.ManifestLis
182
219
183
220
return k .kubectl .ReadManifests (ctx , manifests )
184
221
}
222
+
223
+ // readRemoteManifests will try to read manifests from the given kubernetes
224
+ // context in the specified namespace and for the specified type
225
+ func (k * KubectlDeployer ) readRemoteManifest (ctx context.Context , name string ) ([]byte , error ) {
226
+ var args []string
227
+ ns := ""
228
+ if parts := strings .Split (name , ":" ); len (parts ) > 1 {
229
+ ns = parts [0 ]
230
+ name = parts [1 ]
231
+ }
232
+ args = append (args , name , "-o" , "yaml" )
233
+
234
+ var manifest bytes.Buffer
235
+ err := k .kubectl .RunInNamespace (ctx , nil , & manifest , "get" , ns , args ... )
236
+ if err != nil {
237
+ return nil , errors .Wrap (err , "getting manifest" )
238
+ }
239
+
240
+ return manifest .Bytes (), nil
241
+ }
0 commit comments