From 52b1e0aa6ba6e1631a33c7d7072fc168f05ffa42 Mon Sep 17 00:00:00 2001 From: deviantony Date: Wed, 20 Sep 2023 07:28:42 +0000 Subject: [PATCH] feat: support empty configmaps and secrets --- internal/adapter/persistentvolumeclaim.go | 4 -- .../adapter/store/filesystem/configmap.go | 43 ++++++++++++------- internal/adapter/store/filesystem/files.go | 23 ---------- internal/adapter/store/filesystem/secret.go | 43 ++++++++++++------- pkg/filesystem/file.go | 4 +- 5 files changed, 58 insertions(+), 59 deletions(-) delete mode 100644 internal/adapter/store/filesystem/files.go diff --git a/internal/adapter/persistentvolumeclaim.go b/internal/adapter/persistentvolumeclaim.go index 64aa3a0..0fb0bb1 100644 --- a/internal/adapter/persistentvolumeclaim.go +++ b/internal/adapter/persistentvolumeclaim.go @@ -91,10 +91,6 @@ func (adapter *KubeDockerAdapter) CreatePersistentVolumeClaim(ctx context.Contex k2dtypes.LastAppliedConfigLabelKey: persistentVolumeClaim.ObjectMeta.Annotations["kubectl.kubernetes.io/last-applied-configuration"], }, }, - // TODO: optimize this by only storing the necessary data in the metadata - Data: map[string]string{ - "persistentVolumeClaim": persistentVolumeClaim.Name, - }, } err := adapter.CreateSystemConfigMap(pvcConfigMap) if err != nil { diff --git a/internal/adapter/store/filesystem/configmap.go b/internal/adapter/store/filesystem/configmap.go index f6ee67e..b4478f1 100644 --- a/internal/adapter/store/filesystem/configmap.go +++ b/internal/adapter/store/filesystem/configmap.go @@ -35,23 +35,29 @@ func (s *FileSystemStore) DeleteConfigMap(configMapName, namespace string) error s.mutex.Lock() defer s.mutex.Unlock() - files, err := os.ReadDir(s.configMapPath) + metadataFileName := buildConfigMapMetadataFileName(configMapName, namespace) + metadataFilePath := path.Join(s.configMapPath, metadataFileName) + + metadataFileExists, err := filesystem.FileExists(metadataFilePath) if err != nil { - return fmt.Errorf("unable to read configmap directory: %w", err) + return fmt.Errorf("unable to check if configmap metadata file %s exists: %w", metadataFileName, err) } - filePrefix := buildConfigMapFilePrefix(configMapName, namespace) - configMapFileFound := containsFileWithPrefix(files, filePrefix) - if !configMapFileFound { + if !metadataFileExists { return errors.ErrResourceNotFound } - metadataFileName := buildConfigMapMetadataFileName(configMapName, namespace) - err = os.Remove(path.Join(s.configMapPath, metadataFileName)) + err = os.Remove(metadataFilePath) if err != nil { return fmt.Errorf("unable to remove configmap metadata file %s: %w", metadataFileName, err) } + files, err := os.ReadDir(s.configMapPath) + if err != nil { + return fmt.Errorf("unable to read configmap directory: %w", err) + } + + filePrefix := buildConfigMapFilePrefix(configMapName, namespace) for _, file := range files { if strings.HasPrefix(file.Name(), filePrefix) { err := os.Remove(path.Join(s.configMapPath, file.Name())) @@ -102,19 +108,19 @@ func (s *FileSystemStore) GetConfigMap(configMapName, namespace string) (*core.C s.mutex.RLock() defer s.mutex.RUnlock() - files, err := os.ReadDir(s.configMapPath) + metadataFileName := buildConfigMapMetadataFileName(configMapName, namespace) + metadataFilePath := path.Join(s.configMapPath, metadataFileName) + + metadataFileExists, err := filesystem.FileExists(metadataFilePath) if err != nil { - return nil, fmt.Errorf("unable to read configmap directory: %w", err) + return nil, fmt.Errorf("unable to check if configmap metadata file %s exists: %w", metadataFileName, err) } - filePrefix := buildConfigMapFilePrefix(configMapName, namespace) - configMapFileFound := containsFileWithPrefix(files, filePrefix) - if !configMapFileFound { + if !metadataFileExists { return nil, errors.ErrResourceNotFound } - metadataFileName := buildConfigMapMetadataFileName(configMapName, namespace) - metadata, err := filesystem.LoadMetadataFromDisk(s.configMapPath, metadataFileName) + metadata, err := filesystem.LoadMetadataFromDisk(metadataFilePath) if err != nil { return nil, fmt.Errorf("unable to load configmap metadata from disk: %w", err) } @@ -124,6 +130,12 @@ func (s *FileSystemStore) GetConfigMap(configMapName, namespace string) (*core.C return nil, fmt.Errorf("unable to build configmap from metadata: %w", err) } + files, err := os.ReadDir(s.configMapPath) + if err != nil { + return nil, fmt.Errorf("unable to read configmap directory: %w", err) + } + + filePrefix := buildConfigMapFilePrefix(configMapName, namespace) for _, file := range files { if strings.HasPrefix(file.Name(), filePrefix) { err := s.updateConfigMapDataFromFile(&configMap, file.Name()) @@ -278,7 +290,8 @@ func (s *FileSystemStore) loadMetadataAndInitConfigMaps(metadataFiles []string, configMaps := map[string]core.ConfigMap{} for _, metadataFile := range metadataFiles { - metadata, err := filesystem.LoadMetadataFromDisk(s.configMapPath, metadataFile) + metadataFilePath := path.Join(s.configMapPath, metadataFile) + metadata, err := filesystem.LoadMetadataFromDisk(metadataFilePath) if err != nil { return configMaps, fmt.Errorf("unable to load configmap metadata from disk: %w", err) } diff --git a/internal/adapter/store/filesystem/files.go b/internal/adapter/store/filesystem/files.go deleted file mode 100644 index c0a820d..0000000 --- a/internal/adapter/store/filesystem/files.go +++ /dev/null @@ -1,23 +0,0 @@ -package filesystem - -import ( - "os" - "strings" -) - -// containsFileWithPrefix checks if there is at least one file in the given list that has a filename starting with the specified prefix. -// -// Parameters: -// - files: A slice of os.DirEntry objects representing the list of files to be checked. -// - filePrefix: The prefix to be used for matching the filenames. -// -// Returns: -// - bool: Returns true if at least one file with the specified prefix is found; otherwise, returns false. -func containsFileWithPrefix(files []os.DirEntry, filePrefix string) bool { - for _, file := range files { - if strings.HasPrefix(file.Name(), filePrefix) { - return true - } - } - return false -} diff --git a/internal/adapter/store/filesystem/secret.go b/internal/adapter/store/filesystem/secret.go index af58067..a7f5a62 100644 --- a/internal/adapter/store/filesystem/secret.go +++ b/internal/adapter/store/filesystem/secret.go @@ -35,23 +35,29 @@ func (s *FileSystemStore) DeleteSecret(secretName, namespace string) error { s.mutex.Lock() defer s.mutex.Unlock() - files, err := os.ReadDir(s.secretPath) + metadataFileName := buildSecretMetadataFileName(secretName, namespace) + metadataFilePath := path.Join(s.secretPath, metadataFileName) + + metadataFileExists, err := filesystem.FileExists(metadataFilePath) if err != nil { - return fmt.Errorf("unable to read secret directory: %w", err) + return fmt.Errorf("unable to check if secret metadata file %s exists: %w", metadataFileName, err) } - filePrefix := buildSecretFilePrefix(secretName, namespace) - secretFileFound := containsFileWithPrefix(files, filePrefix) - if !secretFileFound { + if !metadataFileExists { return errors.ErrResourceNotFound } - metadataFileName := buildSecretMetadataFileName(secretName, namespace) - err = os.Remove(path.Join(s.secretPath, metadataFileName)) + err = os.Remove(metadataFilePath) if err != nil { return fmt.Errorf("unable to remove secret metadata file %s: %w", metadataFileName, err) } + files, err := os.ReadDir(s.secretPath) + if err != nil { + return fmt.Errorf("unable to read secret directory: %w", err) + } + + filePrefix := buildSecretFilePrefix(secretName, namespace) for _, file := range files { if strings.HasPrefix(file.Name(), filePrefix) { err := os.Remove(path.Join(s.secretPath, file.Name())) @@ -102,19 +108,19 @@ func (s *FileSystemStore) GetSecret(secretName, namespace string) (*core.Secret, s.mutex.RLock() defer s.mutex.RUnlock() - files, err := os.ReadDir(s.secretPath) + metadataFileName := buildSecretMetadataFileName(secretName, namespace) + metadataFilePath := path.Join(s.secretPath, metadataFileName) + + metadataFileExists, err := filesystem.FileExists(metadataFilePath) if err != nil { - return nil, fmt.Errorf("unable to read secret directory: %w", err) + return nil, fmt.Errorf("unable to check if secret metadata file %s exists: %w", metadataFileName, err) } - filePrefix := buildSecretFilePrefix(secretName, namespace) - secretFileFound := containsFileWithPrefix(files, filePrefix) - if !secretFileFound { + if !metadataFileExists { return nil, errors.ErrResourceNotFound } - metadataFileName := buildSecretMetadataFileName(secretName, namespace) - metadata, err := filesystem.LoadMetadataFromDisk(s.secretPath, metadataFileName) + metadata, err := filesystem.LoadMetadataFromDisk(metadataFilePath) if err != nil { return nil, fmt.Errorf("unable to load secret metadata from disk: %w", err) } @@ -124,6 +130,12 @@ func (s *FileSystemStore) GetSecret(secretName, namespace string) (*core.Secret, return nil, fmt.Errorf("unable to build secret from metadata: %w", err) } + files, err := os.ReadDir(s.secretPath) + if err != nil { + return nil, fmt.Errorf("unable to read secret directory: %w", err) + } + + filePrefix := buildSecretFilePrefix(secretName, namespace) for _, file := range files { if strings.HasPrefix(file.Name(), filePrefix) { @@ -294,7 +306,8 @@ func (s *FileSystemStore) loadMetadataAndInitSecrets(metadataFiles []string, nam secrets := map[string]core.Secret{} for _, metadataFile := range metadataFiles { - metadata, err := filesystem.LoadMetadataFromDisk(s.secretPath, metadataFile) + metadataFilePath := path.Join(s.secretPath, metadataFile) + metadata, err := filesystem.LoadMetadataFromDisk(metadataFilePath) if err != nil { return secrets, fmt.Errorf("unable to load secret metadata from disk: %w", err) } diff --git a/pkg/filesystem/file.go b/pkg/filesystem/file.go index 6bd50ca..f9bb0e3 100644 --- a/pkg/filesystem/file.go +++ b/pkg/filesystem/file.go @@ -112,8 +112,8 @@ func StoreMetadataOnDisk(storagePath, fileName string, data map[string]string) e // It returns a map where the keys and values are taken from the lines in the file. // The process will skip empty lines. // If an error occurs during this process, it returns the error and a nil map. -func LoadMetadataFromDisk(storagePath, fileName string) (map[string]string, error) { - file, err := os.Open(path.Join(storagePath, fileName)) +func LoadMetadataFromDisk(metadataFilePath string) (map[string]string, error) { + file, err := os.Open(metadataFilePath) if err != nil { return nil, fmt.Errorf("an error occurred while opening the file: %w", err) }