Skip to content
This repository was archived by the owner on Jun 4, 2025. It is now read-only.

Commit ba92899

Browse files
authored
Merge pull request #25 from jetstack/fix_secrets_order
Fix secrets order
2 parents b27570d + b4642f8 commit ba92899

File tree

3 files changed

+65
-41
lines changed

3 files changed

+65
-41
lines changed

internal/command/operator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ Note: If --auto-registry-credentials and --registry-credentials-path are unset,
290290

291291
cdv, err := venafi.ParseCertDiscoveryVenafiConfig(certDiscoveryVenafiConnection, vcs, certDiscoveryVenafi)
292292
if err != nil {
293-
return fmt.Errorf("error parsing cert-discovery-venafi config")
293+
return fmt.Errorf("error parsing cert-discovery-venafi config: %w", err)
294294
}
295295
options.CertDiscoveryVenafi = cdv
296296

internal/operator/operator.go

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,14 @@ type ApplyOperatorYAMLOptions struct {
6060
// Secure operator which is then applied via the Applier implementation. It can be customised via the provided
6161
// ApplyOperatorYAMLOptions type.
6262
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-
}
7563

7664
buf := bytes.NewBuffer([]byte{})
77-
if _, err = io.Copy(buf, file); err != nil {
78-
return err
79-
}
8065

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
8269
// pulled from a public registry or that the image pull secrets are already
83-
// in place
70+
// in place.
8471
if options.RegistryCredentials != "" {
8572
secret, err := ImagePullSecret(options.RegistryCredentials)
8673
if err != nil {
@@ -93,11 +80,27 @@ func ApplyOperatorYAML(ctx context.Context, applier Applier, options ApplyOperat
9380
}
9481
secretReader := bytes.NewBuffer(secretData)
9582

96-
buf.WriteString("---\n")
97-
9883
if _, err = io.Copy(buf, secretReader); err != nil {
9984
return err
10085
}
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
101104
}
102105

103106
tpl, err := template.New("install").Parse(buf.String())
@@ -321,11 +324,13 @@ func ApplyInstallationYAML(ctx context.Context, applier Applier, options ApplyIn
321324
registryCredentials = string(registryCredentialsBytes)
322325
}
323326

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)
327333
}
328-
manifestTemplates.secrets = append(manifestTemplates.secrets, secret)
329334

330335
if err := generateVenafiIssuerManifests(manifestTemplates, options); err != nil {
331336
return fmt.Errorf("error building manifests for Venafi issuers: %w", err)
@@ -377,16 +382,11 @@ type manifests struct {
377382
}
378383

379384
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{})
386386

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.
388389
for _, secret := range mf.secrets {
389-
buf.WriteString("---\n")
390390
secretJson, err := yaml.Marshal(secret)
391391
if err != nil {
392392
return nil, fmt.Errorf("failed to marshal Secret data: %w", err)
@@ -395,6 +395,20 @@ func marshalManifests(mf *manifests) (io.Reader, error) {
395395
if _, err = io.Copy(buf, secretReader); err != nil {
396396
return nil, fmt.Errorf("error writing secret data to buffer: %w", err)
397397
}
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)
398412
}
399413

400414
return buf, nil

internal/operator/operator_test.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"context"
66
"encoding/json"
77
"io"
8+
"strings"
89
"testing"
910

1011
"github.com/cert-manager/cert-manager/pkg/apis/certmanager"
11-
certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
12+
cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
1213
operatorv1alpha1 "github.com/jetstack/js-operator/pkg/apis/operator/v1alpha1"
1314
"github.com/stretchr/testify/assert"
1415
corev1 "k8s.io/api/core/v1"
@@ -145,8 +146,12 @@ func TestApplyInstallationYAML(t *testing.T) {
145146
err := operator.ApplyInstallationYAML(ctx, applier, options)
146147
assert.NoError(t, err)
147148

149+
var secret corev1.Secret
148150
var actual operatorv1alpha1.Installation
149-
assert.NoError(t, yaml.Unmarshal(applier.data.Bytes(), &actual))
151+
s := strings.Split(string(applier.data.Bytes()), "---")
152+
assert.Len(t, s, 2)
153+
assert.NoError(t, yaml.Unmarshal([]byte(s[0]), &secret))
154+
assert.NoError(t, yaml.Unmarshal([]byte(s[1]), &actual))
150155

151156
assert.NotNil(t, actual.Spec.VenafiOauthHelper)
152157
assert.Contains(t, actual.Spec.VenafiOauthHelper.ImagePullSecrets, "jse-gcr-creds")
@@ -180,8 +185,12 @@ func TestApplyInstallationYAML(t *testing.T) {
180185
err := operator.ApplyInstallationYAML(ctx, applier, options)
181186
assert.NoError(t, err)
182187

188+
var secret corev1.Secret
183189
var installation operatorv1alpha1.Installation
184-
assert.NoError(t, yaml.Unmarshal(applier.data.Bytes(), &installation))
190+
s := strings.Split(string(applier.data.Bytes()), "---")
191+
assert.Len(t, s, 2)
192+
assert.NoError(t, yaml.Unmarshal([]byte(s[0]), &secret))
193+
assert.NoError(t, yaml.Unmarshal([]byte(s[1]), &installation))
185194

186195
assert.NotNil(t, installation.Spec.CertDiscoveryVenafi)
187196
assert.Equal(t, installation.Spec.CertDiscoveryVenafi.TPP.URL, "foo")
@@ -203,12 +212,13 @@ func TestApplyInstallationYAML(t *testing.T) {
203212

204213
err := operator.ApplyInstallationYAML(ctx, applier, options)
205214
assert.NoError(t, err)
215+
var installation operatorv1alpha1.Installation
216+
s := strings.Split(string(applier.data.Bytes()), "---")
217+
assert.Len(t, s, 3)
218+
assert.NoError(t, yaml.Unmarshal([]byte(s[2]), &installation))
206219

207-
var actual operatorv1alpha1.Installation
208-
assert.NoError(t, yaml.Unmarshal(applier.data.Bytes(), &actual))
209-
210-
assert.NotNil(t, actual.Spec.CertDiscoveryVenafi)
211-
assert.Contains(t, actual.Spec.CertDiscoveryVenafi.ImagePullSecrets, "jse-gcr-creds")
220+
assert.NotNil(t, installation.Spec.CertDiscoveryVenafi)
221+
assert.Contains(t, installation.Spec.CertDiscoveryVenafi.ImagePullSecrets, "jse-gcr-creds")
212222
})
213223

214224
t.Run("It should have a blank Istio CSR block when no issuer is provided", func(t *testing.T) {
@@ -244,7 +254,7 @@ func TestApplyInstallationYAML(t *testing.T) {
244254
issuer := actual.Spec.IstioCSR.IssuerRef
245255

246256
assert.EqualValues(t, certmanager.GroupName, issuer.Group)
247-
assert.EqualValues(t, certmanagerv1.IssuerKind, issuer.Kind)
257+
assert.EqualValues(t, cmapi.IssuerKind, issuer.Kind)
248258
assert.EqualValues(t, options.IstioCSRIssuer, issuer.Name)
249259
}
250260
})

0 commit comments

Comments
 (0)