Skip to content

Commit

Permalink
Refactor error return
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinliu24 committed Jun 11, 2024
1 parent ca222ec commit efc7905
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions cmd/certificate-generator/app/certificate-generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,23 @@ func Run(ctx context.Context, opts *options.Options) error {
clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
klog.Errorf("Error building Kubernetes clientset: %v", err.Error())
return err
}

if opts.InitOnly {
klog.Infof("Init only detected. Doing cert initialization for webhook [%v/%v]", opts.WebhookNamespace, opts.WebhookServerName)
err := doCertRotation(clientset, opts)
if err != nil {
klog.Errorf("Error rotating certificate for webhook [%v/%v]: %v", opts.WebhookNamespace, opts.WebhookServerName, err)
os.Exit(1)
return err
}
} else {
if opts.LeaderElection.LeaderElect {
klog.Info("Doing leader election")
id, err := os.Hostname()
if err != nil {
klog.Errorf("Failed to get hostname: %v", err)
return err
}

rl, err := resourcelock.New(opts.LeaderElection.ResourceLock,
Expand All @@ -102,6 +104,7 @@ func Run(ctx context.Context, opts *options.Options) error {
})
if err != nil {
klog.Errorf("Error creating resource lock: %v", err)
return err
}

leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
Expand All @@ -115,6 +118,7 @@ func Run(ctx context.Context, opts *options.Options) error {
err = rotateCertificate(clientset, opts)
if err != nil {
klog.Errorf("Failed to rotate certificates: %v", err)
os.Exit(1)
}
},
OnStoppedLeading: func() {
Expand All @@ -127,6 +131,7 @@ func Run(ctx context.Context, opts *options.Options) error {
err := rotateCertificate(clientset, opts)
if err != nil {
klog.Errorf("Failed to rotate certificates: %v", err)
return err
}
}
}
Expand Down Expand Up @@ -255,7 +260,7 @@ func generateServerCert(caCert []byte, caKey *ecdsa.PrivateKey, opts *options.Op
certPem := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: serverCert})
marshalledKey, err := x509.MarshalECPrivateKey(serverKey)
if err != nil {
klog.Errorf("Error marshalling webhook server certificate key: %v", err)
return nil, nil, fmt.Errorf("error marshalling webhook server certificate key: %v", err)
}

keyPem := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: marshalledKey})
Expand Down

0 comments on commit efc7905

Please sign in to comment.