@@ -60,27 +60,14 @@ type ApplyOperatorYAMLOptions struct {
60
60
// Secure operator which is then applied via the Applier implementation. It can be customised via the provided
61
61
// ApplyOperatorYAMLOptions type.
62
62
func ApplyOperatorYAML (ctx context.Context , applier Applier , options ApplyOperatorYAMLOptions ) error {
63
- var file io.Reader
64
- var err error
65
-
66
- if options .Version == "" {
67
- file , err = latestManifest ()
68
- } else {
69
- file , err = manifestVersion (options .Version )
70
- }
71
-
72
- if err != nil {
73
- return err
74
- }
75
63
76
64
buf := bytes .NewBuffer ([]byte {})
77
- if _ , err = io .Copy (buf , file ); err != nil {
78
- return err
79
- }
80
65
81
- // if there is no registry credentials, we assume that the images can be
66
+ // Write any secrets to the buffer first, so they get applied to cluster
67
+ // before any Deployments that use them.
68
+ // If there is no registry credentials, we assume that the images can be
82
69
// pulled from a public registry or that the image pull secrets are already
83
- // in place
70
+ // in place.
84
71
if options .RegistryCredentials != "" {
85
72
secret , err := ImagePullSecret (options .RegistryCredentials )
86
73
if err != nil {
@@ -93,11 +80,27 @@ func ApplyOperatorYAML(ctx context.Context, applier Applier, options ApplyOperat
93
80
}
94
81
secretReader := bytes .NewBuffer (secretData )
95
82
96
- buf .WriteString ("---\n " )
97
-
98
83
if _ , err = io .Copy (buf , secretReader ); err != nil {
99
84
return err
100
85
}
86
+ buf .WriteString ("---\n " )
87
+ }
88
+
89
+ var file io.Reader
90
+ var err error
91
+
92
+ if options .Version == "" {
93
+ file , err = latestManifest ()
94
+ } else {
95
+ file , err = manifestVersion (options .Version )
96
+ }
97
+
98
+ if err != nil {
99
+ return err
100
+ }
101
+
102
+ if _ , err = io .Copy (buf , file ); err != nil {
103
+ return err
101
104
}
102
105
103
106
tpl , err := template .New ("install" ).Parse (buf .String ())
@@ -321,11 +324,13 @@ func ApplyInstallationYAML(ctx context.Context, applier Applier, options ApplyIn
321
324
registryCredentials = string (registryCredentialsBytes )
322
325
}
323
326
324
- secret , err := ImagePullSecret (registryCredentials )
325
- if err != nil {
326
- return fmt .Errorf ("failed to parse image pull secret: %w" , err )
327
+ if registryCredentials != "" {
328
+ secret , err := ImagePullSecret (registryCredentials )
329
+ if err != nil {
330
+ return fmt .Errorf ("failed to parse image pull secret: %w" , err )
331
+ }
332
+ manifestTemplates .secrets = append (manifestTemplates .secrets , secret )
327
333
}
328
- manifestTemplates .secrets = append (manifestTemplates .secrets , secret )
329
334
330
335
if err := generateVenafiIssuerManifests (manifestTemplates , options ); err != nil {
331
336
return fmt .Errorf ("error building manifests for Venafi issuers: %w" , err )
@@ -377,16 +382,11 @@ type manifests struct {
377
382
}
378
383
379
384
func marshalManifests (mf * manifests ) (io.Reader , error ) {
380
- // Add Installation to the buffer
381
- data , err := yaml .Marshal (mf .installation )
382
- if err != nil {
383
- return nil , fmt .Errorf ("error marshalling Installation resource: %w" , err )
384
- }
385
- buf := bytes .NewBuffer (data )
385
+ buf := bytes .NewBuffer ([]byte {})
386
386
387
- // Add all Secrets to the buffer
387
+ // Add all Secrets to the buffer first to ensure that they get applied
388
+ // to the cluster before any Deployments that might want to use them.
388
389
for _ , secret := range mf .secrets {
389
- buf .WriteString ("---\n " )
390
390
secretJson , err := yaml .Marshal (secret )
391
391
if err != nil {
392
392
return nil , fmt .Errorf ("failed to marshal Secret data: %w" , err )
@@ -395,6 +395,20 @@ func marshalManifests(mf *manifests) (io.Reader, error) {
395
395
if _ , err = io .Copy (buf , secretReader ); err != nil {
396
396
return nil , fmt .Errorf ("error writing secret data to buffer: %w" , err )
397
397
}
398
+ buf .WriteString ("---\n " )
399
+ }
400
+ if mf .installation .Spec .CertManager == nil {
401
+ panic ("cert manager is nil" )
402
+ }
403
+ installationData , err := yaml .Marshal (mf .installation )
404
+ if err != nil {
405
+ return nil , fmt .Errorf ("error marshalling Installation resource: %w" , err )
406
+ }
407
+
408
+ installationBuffer := bytes .NewReader (installationData )
409
+
410
+ if _ , err = io .Copy (buf , installationBuffer ); err != nil {
411
+ return nil , fmt .Errorf ("Error writing installation data to buffer: %w" , err )
398
412
}
399
413
400
414
return buf , nil
0 commit comments