bevel-operator-fabric
bevel-operator-fabric copied to clipboard
Temporary Files Accumulation in HLF Operator Pod
What happened?
Issue Description
The HLF operator pod is continuously creating temporary files in the /tmp directory without cleanup, leading to potential disk space issues. The temporary files are CA certificate-related files that are being created during CA client operations.
$ ls | wc -l
72655
# After a few moments...
$ ls | wc -l
74761
$ ls | head
ca-cert1000328084
ca-cert1000572008
ca-cert1000588891
...
Root Cause
The GetClient function in the certs package creates temporary directories and files for CA operations but does not implement cleanup mechanisms:
func GetClient(ca FabricCAParams) (*lib.Client, error) {
caHomeDir, err := ioutil.TempDir("", "fabric-ca-client") // Not cleaned up
caCertFile, err := ioutil.TempFile("", "ca-cert") // Not cleaned up
...
}
This function is frequently called by operations like EnrollUser, ReenrollUser, GetCAInfo, etc., causing the accumulation of temporary files.
Solution (AI gen..)
- Add proper cleanup using
defer:
func GetClient(ca FabricCAParams) (*lib.Client, error) {
caHomeDir, err := ioutil.TempDir("", "fabric-ca-client")
if err != nil {
return nil, fmt.Errorf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(caHomeDir)
caCertFile, err := ioutil.TempFile("", "ca-cert")
if err != nil {
return nil, fmt.Errorf("failed to create temp file: %v", err)
}
defer os.Remove(caCertFile.Name())
defer caCertFile.Close()
...
}
- Add a helper function for structured cleanup:
func WithClient(ca FabricCAParams, fn func(*lib.Client) error) error {
client, err := GetClient(ca)
if err != nil {
return err
}
return fn(client)
}
Implementation Details
-
Added
deferstatements for cleanup of:- Temporary directories
- CA certificate files
- File handles
-
Improved error handling with descriptive messages
-
Added helper function
WithClientto handle cleanup in a structured way -
Modified existing functions to use the new cleanup pattern
Impact
- Prevents accumulation of temporary files in the pod
- Reduces disk space usage
- Proper resource cleanup after operations
- Maintains existing functionality while adding proper resource management
What did you expect to happen?
.
How can we reproduce it (as minimally and precisely as possible)?
.
Anything else we need to know?
No response
Kubernetes version
OS linux (amd64) OS Image Fedora CoreOS 39.20240731.base.0 Kernel version 6.5.12-300.fc39.x86_64 Container runtime containerd://1.7.10 Kubelet version v1.27.11