Skip to content

Commit 685ced1

Browse files
committed
Handle invalid cert-manager ValidityDuration
This commit will handle a scenario of invalid cert-manager ValidityDuration user input and throw an error. In case, ValidityDuration is not defined by user it will default to 90days for cert-manager Signed-off-by: ArkaSaha30 <arkasaha30@gmail.com>
1 parent e40989f commit 685ced1

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

internal/controller/utils.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -566,11 +566,19 @@ func getPeerCertName(etcdClusterName string) string {
566566
return peerCertName
567567
}
568568

569-
func createCMCertificateConfig(ec *ecv1alpha1.EtcdCluster) *certInterface.Config {
569+
func createCMCertificateConfig(ec *ecv1alpha1.EtcdCluster) (*certInterface.Config, error) {
570570
cmConfig := ec.Spec.TLS.ProviderCfg.CertManagerCfg
571-
duration, err := time.ParseDuration(cmConfig.ValidityDuration)
572-
if err != nil {
573-
log.Printf("Failed to parse ValidityDuration: %s", err)
571+
572+
// Set default duration to 90 days for cert-manager if not provided
573+
var duration time.Duration
574+
if cmConfig.ValidityDuration == "" {
575+
duration = certInterface.DefaultCertManagerValidity
576+
} else {
577+
var err error
578+
duration, err = time.ParseDuration(cmConfig.ValidityDuration)
579+
if err != nil {
580+
return nil, fmt.Errorf("failed to parse ValidityDuration: %w", err)
581+
}
574582
}
575583

576584
var getAltNames certInterface.AltNames
@@ -596,7 +604,7 @@ func createCMCertificateConfig(ec *ecv1alpha1.EtcdCluster) *certInterface.Config
596604
"issuerKind": cmConfig.IssuerKind,
597605
},
598606
}
599-
return config
607+
return config, nil
600608
}
601609

602610
func createAutoCertificateConfig(ec *ecv1alpha1.EtcdCluster) *certInterface.Config {
@@ -653,7 +661,10 @@ func createCertificate(ec *ecv1alpha1.EtcdCluster, ctx context.Context, c client
653661
}
654662
return nil
655663
case ec.Spec.TLS.ProviderCfg.CertManagerCfg != nil:
656-
cmConfig := createCMCertificateConfig(ec)
664+
cmConfig, err := createCMCertificateConfig(ec)
665+
if err != nil {
666+
return fmt.Errorf("error creating cert-manager certificate config: %w", err)
667+
}
657668
createCertErr := cert.EnsureCertificateSecret(ctx, certName, ec.Namespace, cmConfig)
658669
if createCertErr != nil {
659670
log.Printf("Error creating certificate: %s", createCertErr)

pkg/certificate/interfaces/interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ const (
4141
// with a delay of RetryInterval between consecutive retries
4242
MaxRetries = 36
4343
RetryInterval = 5 * time.Second
44+
45+
// DefaultCertManagerValidity is the default validity duration for cert-manager certificates (90 days)
46+
DefaultCertManagerValidity = 90 * 24 * time.Hour
4447
)
4548

4649
// AltNames contains the domain names and IP addresses that will be added

0 commit comments

Comments
 (0)