Skip to content

Commit e6ed7d7

Browse files
Merge pull request #7 from scality/improvement/remove-validate-from-reconcile
chore: Remove validation from reconcile loop
2 parents 1eab025 + 6775a3b commit e6ed7d7

File tree

5 files changed

+14
-32
lines changed

5 files changed

+14
-32
lines changed

api/v1alpha1/managedcrl_types.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type CRLExposeSpec struct {
9595

9696
// Image specifies the container image to use for exposing the CRL.
9797
// +optional
98-
Image *ImageSpec `json:"image"`
98+
Image ImageSpec `json:"image,omitempty"`
9999
// Node Selector to deploy the CRL server
100100
// +optional
101101
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
@@ -128,8 +128,7 @@ type RevocationSpec struct {
128128
RevocationTime *metav1.Time `json:"revocationTime,omitempty"`
129129

130130
// Reason is the reason for revocation (refer to RFC 5280 Section 5.3.1.).
131-
// +optional
132-
ReasonCode *int `json:"reasonCode,omitempty"`
131+
ReasonCode int `json:"reasonCode,omitempty"`
133132
}
134133

135134
// ManagedCRLSpec defines the desired state of ManagedCRL.
@@ -300,15 +299,9 @@ func (rs *RevocationSpec) withDefaults() {
300299
if rs.RevocationTime == nil {
301300
rs.RevocationTime = &metav1.Time{Time: metav1.Now().Time}
302301
}
303-
if rs.ReasonCode == nil {
304-
rs.ReasonCode = ptr.To(0) // Unspecified
305-
}
306302
}
307303

308304
func (ces *CRLExposeSpec) withDefaults() {
309-
if ces.Image == nil {
310-
ces.Image = &ImageSpec{}
311-
}
312305
ces.Image.withDefaults()
313306

314307
if ces.Ingress != nil {
@@ -340,6 +333,7 @@ func (is *IngressSpec) withDefaults() {
340333

341334
// Validate validates the ManagedCRL resource.
342335
func (mcrl *ManagedCRL) Validate() error {
336+
mcrl.WithDefaults()
343337
err := mcrl.Spec.validate()
344338
if err != nil {
345339
return fmt.Errorf("spec validation failed: %w", err)
@@ -439,12 +433,14 @@ func (rs RevocationSpec) ToRevocationListEntry() (x509.RevocationListEntry, erro
439433
return x509.RevocationListEntry{
440434
SerialNumber: serial,
441435
RevocationTime: rs.RevocationTime.Time,
442-
ReasonCode: *rs.ReasonCode,
436+
ReasonCode: rs.ReasonCode,
443437
}, nil
444438
}
445439

446440
// GetRevokedListEntries converts the Revocations in ManagedCRLSpec to a slice of x509.RevocationListEntry.
447441
func (mcrls *ManagedCRLSpec) GetRevokedListEntries() ([]x509.RevocationListEntry, error) {
442+
mcrls.withDefaults()
443+
448444
if mcrls.Revocations == nil {
449445
return []x509.RevocationListEntry{}, nil
450446
}
@@ -462,6 +458,8 @@ func (mcrls *ManagedCRLSpec) GetRevokedListEntries() ([]x509.RevocationListEntry
462458

463459
// GetImage returns the full image string in the format "repository/name:tag".
464460
func (is *ImageSpec) GetImage() string {
461+
is.withDefaults()
462+
465463
image := fmt.Sprintf("%s:%s", *is.Name, *is.Tag)
466464
if is.Repository != nil {
467465
image = fmt.Sprintf("%s/%s", *is.Repository, image)
@@ -471,6 +469,8 @@ func (is *ImageSpec) GetImage() string {
471469

472470
// GetCRLDistributionPoint returns the CRL distribution point URL based on the Ingress configuration.
473471
func (mcrl *ManagedCRL) GetCRLDistributionPoint() []string {
472+
mcrl.WithDefaults()
473+
474474
var urls []string
475475

476476
// Add Ingress URLs if enabled

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/controller/managedcrl_controller.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ func (r *ManagedCRLReconciler) Reconcile(ctx context.Context, req ctrl.Request)
152152
}
153153
// Apply defaults
154154
instance.WithDefaults()
155-
if err := instance.Validate(); err != nil {
156-
return ctrl.Result{}, fmt.Errorf("validation failed: %w", err)
157-
}
158155

159156
needRenewal := false
160157
original := instance.DeepCopy()

internal/webhook/v1alpha1/managedcrl_webhook.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ func (v *ManagedCRLCustomValidator) ValidateDelete(ctx context.Context, obj runt
103103

104104
// validationManagedCRL validates the ManagedCRL fields.
105105
func validationManagedCRL(logger logr.Logger, ctx context.Context, c client.Client, managedcrl *crloperatorv1alpha1.ManagedCRL) error {
106-
managedcrl.WithDefaults()
107106
if err := managedcrl.Validate(); err != nil {
108107
logger.Error(err, "Validation failed")
109108
return err

test/integration/managedcrl_controller_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var (
6969
spec: crloperatorv1alpha1.ManagedCRLSpec{
7070
Expose: &crloperatorv1alpha1.CRLExposeSpec{
7171
Enabled: true,
72-
Image: &crloperatorv1alpha1.ImageSpec{Repository: ptr.To("custom/repo"), Tag: ptr.To("v1.2.3")},
72+
Image: crloperatorv1alpha1.ImageSpec{Repository: ptr.To("custom/repo"), Tag: ptr.To("v1.2.3")},
7373
Internal: ptr.To(false),
7474
},
7575
},
@@ -401,7 +401,7 @@ var _ = Describe("ManagedCRL Controller", func() {
401401
retrieved.Spec.Revocations = []crloperatorv1alpha1.RevocationSpec{
402402
{
403403
SerialNumber: "123456789",
404-
ReasonCode: ptr.To(2),
404+
ReasonCode: 2,
405405
},
406406
}
407407
Expect(k8sClient.Update(ctx, retrieved)).To(Succeed())
@@ -412,7 +412,7 @@ var _ = Describe("ManagedCRL Controller", func() {
412412
retrieved.Spec.Revocations = []crloperatorv1alpha1.RevocationSpec{
413413
{
414414
SerialNumber: "123456789",
415-
ReasonCode: ptr.To(1),
415+
ReasonCode: 1,
416416
},
417417
}
418418
Expect(k8sClient.Update(ctx, retrieved)).To(Succeed())
@@ -510,7 +510,6 @@ func checkSecret(mcrlRef types.NamespacedName) {
510510
}
511511
return false
512512
}, 10*time.Second, time.Second).Should(BeTrue())
513-
retrieved.WithDefaults()
514513

515514
Expect(retrieved.ObjectMeta.Finalizers).To(ContainElement("crl-operator.scality.com/finalizer"))
516515

@@ -576,7 +575,6 @@ func checkExposePod(mcrlRef types.NamespacedName, shouldRestart bool) {
576575
}
577576
return false
578577
}, 10*time.Second, time.Second).Should(BeTrue())
579-
retrieved.WithDefaults()
580578
Expect(retrieved.Status.PodExposed).To(PointTo(BeFalse()))
581579

582580
// Check the deployment
@@ -615,7 +613,6 @@ func checkExposePod(mcrlRef types.NamespacedName, shouldRestart bool) {
615613
}
616614
return false
617615
}, 10*time.Second, time.Second).Should(BeTrue())
618-
retrieved.WithDefaults()
619616

620617
Expect(retrieved.Status.PodExposed).To(PointTo(BeTrue()))
621618

@@ -663,7 +660,6 @@ func checkIngress(mcrlRef types.NamespacedName) {
663660
}
664661
return false
665662
}, 10*time.Second, time.Second).Should(BeTrue())
666-
retrieved.WithDefaults()
667663

668664
Expect(retrieved.Status.IngressExposed).To(PointTo(BeTrue()))
669665

@@ -707,7 +703,6 @@ func checkIssuerConfigured(mcrlRef types.NamespacedName) {
707703
}
708704
return false
709705
}, 10*time.Second, time.Second).Should(BeTrue())
710-
retrieved.WithDefaults()
711706

712707
Expect(retrieved.Status.IssuerConfigured).To(PointTo(BeTrue()))
713708

0 commit comments

Comments
 (0)