Skip to content

Commit

Permalink
Use dryrun options of k8s client-go (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
dskatz authored Jun 28, 2022
1 parent 3f80405 commit 8c9d8d1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (c *GarbageCollectionController) Reconcile(ctx context.Context, request rec
deleteCert := false
deleteOptions := metav1.DeleteOptions{}
if c.dryRun {
deleteOptions.DryRun = []string{"All"}
deleteOptions.DryRun = []string{metav1.DryRunAll}
}

gateway, err := c.istioClient.NetworkingV1beta1().Gateways(gatewayNamespace).Get(ctx, gatewayName, metav1.GetOptions{})
Expand Down
13 changes: 9 additions & 4 deletions internal/controllers/v1beta1/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,12 @@ func (c *GatewayController) CreateCertificate(ctx context.Context, gateway *netw
},
},
}
createOptions := metav1.CreateOptions{FieldManager: FieldManager}
if c.dryRun {
log.Info("[dryrun] create certificate", "cert", cert)
return nil
createOptions.DryRun = []string{metav1.DryRunAll}
}
_, err := c.certClient.CertmanagerV1().Certificates(c.certificateNamespace).Create(ctx, cert, metav1.CreateOptions{FieldManager: FieldManager})
_, err := c.certClient.CertmanagerV1().Certificates(c.certificateNamespace).Create(ctx, cert, createOptions)
return err
}

Expand All @@ -195,11 +196,15 @@ func (c *GatewayController) UpdateCertificate(ctx context.Context, cert *v1certm

if updatedDNSNames || updatedIssuer {
log.V(1).Info("pre-update", "cert", cert)

updateOptions := metav1.UpdateOptions{FieldManager: FieldManager}
if c.dryRun {
fmt.Println("DRY RUN UPDATE ", cert)
log.Info("[dryrun] update certificate", "cert", cert)
return nil
updateOptions.DryRun = []string{metav1.DryRunAll}
}
_, err := c.certClient.CertmanagerV1().Certificates(c.certificateNamespace).Update(ctx, cert, metav1.UpdateOptions{FieldManager: FieldManager})

_, err := c.certClient.CertmanagerV1().Certificates(c.certificateNamespace).Update(ctx, cert, updateOptions)
if err != nil {
log.Error(err, "error on certificate update", "cert", cert)
}
Expand Down
28 changes: 0 additions & 28 deletions internal/controllers/v1beta1/gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,31 +402,3 @@ func TestGatewayReconcile_UpdateCertificateNoOp(t *testing.T) {
assertCertificateUpdated(t, helper)
assert.Equal(t, 0, updated)
}

func TestGatewayReconcile_UpdatesCertificateDryRunWillNotCommit(t *testing.T) {
t.Parallel()
helper := NewTestHelperWithCertificates(WithTestDryRun(), WithAnnotations(map[string]string{
v1beta1labels.IssuerAnnotation: "achange",
}))

updated := 0
helper.CertClient.CertmanagerV1().(*certmanagerv1fake.FakeCertmanagerV1).PrependReactor("update", "certificates", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
updated++
return true, nil, nil
})

assertCertificateUpdated(t, helper)
assert.Equal(t, 0, updated)
}

func TestGatewayReconcile_CreatCertificateWithDryRun(t *testing.T) {
t.Parallel()
helper := NewTestHelperWithGateways(WithTestDryRun())
created := 0
helper.CertClient.CertmanagerV1().(*certmanagerv1fake.FakeCertmanagerV1).PrependReactor("create", "certificates", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
created++
return true, nil, nil
})
assertCreateCertificateCalled(t, helper)
assert.Equal(t, 0, created)
}

0 comments on commit 8c9d8d1

Please sign in to comment.