Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions pkg/cmd/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package factory
import (
"context"
"fmt"
"math/rand"
"net"
"os"
"strings"
Expand Down Expand Up @@ -41,11 +40,15 @@ type ControllerFlags struct {
EnableLeaderElection bool
// ComponentNamespace is the namespace to run component
ComponentNamespace string

servingOption *genericapiserveroptions.SecureServingOptions
}

// NewControllerFlags returns flags with default values set
func NewControllerFlags() *ControllerFlags {
return &ControllerFlags{}
return &ControllerFlags{
servingOption: genericapiserveroptions.NewSecureServingOptions(),
}
}

// AddFlags register and binds the default flags
Expand All @@ -56,6 +59,8 @@ func (f *ControllerFlags) AddFlags(cmd *cobra.Command) {
flags.StringVar(&f.KubeConfigFile, "kubeconfig", f.KubeConfigFile, "Location of the master configuration file to run from.")
flags.StringVar(&f.ComponentNamespace, "component-namespace", f.ComponentNamespace, "Namespace of the component.")
flags.BoolVar(&f.EnableLeaderElection, "enable-leader-election", f.EnableLeaderElection, "Enables the leader election for the controller")

f.servingOption.AddFlags(flags)
}

// ControllerCommandConfig holds values required to construct a command to run.
Expand Down Expand Up @@ -92,8 +97,7 @@ func (c *ControllerCommandConfig) NewCommand() *cobra.Command {
ctx := context.TODO()
cmd := &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// boiler plate for the "normal" command
rand.Seed(time.Now().UTC().UnixNano())
// boilerplate for the "normal" command
logs.InitLogs()

// handle SIGTERM and SIGINT by cancelling the context.
Expand Down Expand Up @@ -128,7 +132,7 @@ func (c *ControllerCommandConfig) StartController(ctx context.Context) error {
}

var server *genericapiserver.GenericAPIServer
serverConfig, err := toServerConfig()
serverConfig, err := toServerConfig(c.basicFlags.servingOption)
if err != nil {
return err
}
Expand All @@ -141,7 +145,7 @@ func (c *ControllerCommandConfig) StartController(ctx context.Context) error {
}

go func() {
if err := server.PrepareRun().Run(ctx.Done()); err != nil {
if err := server.PrepareRun().RunWithContext(ctx); err != nil {
klog.Fatal(err)
}
klog.Info("server exited")
Expand Down Expand Up @@ -260,26 +264,28 @@ func toLeaderElection(clientConfig *rest.Config, component, namespace string) (l
}, nil
}

func toServerConfig() (*genericapiserver.Config, error) {
func toServerConfig(servingOptions *genericapiserveroptions.SecureServingOptions) (*genericapiserver.Config, error) {
scheme := runtime.NewScheme()
metav1.AddToGroupVersion(scheme, metav1.SchemeGroupVersion)
config := genericapiserver.NewConfig(serializer.NewCodecFactory(scheme))

servingOptions := genericapiserveroptions.NewSecureServingOptions()
servingOptions.BindPort = 8443
temporaryCertDir, err := os.MkdirTemp("", "serving-cert")
if err != nil {
return nil, err
if servingOptions.BindPort == 0 {
servingOptions.BindPort = 8443
}

if servingOptions.ServerCert.CertDirectory == "" {
temporaryCertDir, err := os.MkdirTemp("", "serving-cert")
if err != nil {
return nil, err
}
servingOptions.ServerCert.CertDirectory = temporaryCertDir
}
servingOptions.ServerCert.CertDirectory = temporaryCertDir
servingOptions.ServerCert.PairName = "tls"
if err := servingOptions.MaybeDefaultWithSelfSignedCerts("localhost", nil, []net.IP{net.ParseIP("127.0.0.1")}); err != nil {
return nil, err
}
Comment on lines +276 to 286

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Temporary certificate directory is never cleaned up.

The temp directory created at line 277 is not removed when the server shuts down, leading to accumulation of orphaned directories over time on systems with frequent restarts.

Consider registering cleanup via the context or a shutdown hook:

🧹 Proposed fix to register cleanup
 func toServerConfig(servingOptions *genericapiserveroptions.SecureServingOptions) (*genericapiserver.Config, error) {
 	scheme := runtime.NewScheme()
 	metav1.AddToGroupVersion(scheme, metav1.SchemeGroupVersion)
 	config := genericapiserver.NewConfig(serializer.NewCodecFactory(scheme))

 	if servingOptions.BindPort == 0 {
 		servingOptions.BindPort = 8443
 	}

 	if servingOptions.ServerCert.CertDirectory == "" {
 		temporaryCertDir, err := os.MkdirTemp("", "serving-cert")
 		if err != nil {
 			return nil, err
 		}
+		// Note: Consider adding cleanup logic via config.AddPostStartHook or 
+		// documenting that callers should clean up the cert directory on shutdown
 		servingOptions.ServerCert.CertDirectory = temporaryCertDir
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@pkg/cmd/factory/factory.go` around lines 276 - 286, The temp directory
created when servingOptions.ServerCert.CertDirectory is empty is never removed;
add a cleanup hook to remove servingOptions.ServerCert.CertDirectory on
shutdown: after creating temporaryCertDir and setting
servingOptions.ServerCert.CertDirectory/PairName, register a cleanup callback
tied to the server lifecycle or provided context (e.g., via a defer-style
cleanup list, shutdown handler, or context cancellation) that
os.RemoveAll(servingOptions.ServerCert.CertDirectory) when the server stops or
context is done; ensure the cleanup is registered in the same scope that creates
the dir (where MaybeDefaultWithSelfSignedCerts is called) and handle/remove
errors appropriately.


servingOptionsWithLoopback := servingOptions.WithLoopback()

if err := servingOptionsWithLoopback.ApplyTo(&config.SecureServing, &config.LoopbackClientConfig); err != nil {
if err := servingOptions.ApplyTo(&config.SecureServing); err != nil {
return nil, err
}

Expand Down
Loading