Skip to content

Commit

Permalink
Merge pull request #63 from portainer/feat62-empty-configmaps-secrets
Browse files Browse the repository at this point in the history
feat: support empty configmaps and secrets
  • Loading branch information
deviantony authored Sep 20, 2023
2 parents 602d9ab + 52b1e0a commit 57e348b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 59 deletions.
4 changes: 0 additions & 4 deletions internal/adapter/persistentvolumeclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 28 additions & 15 deletions internal/adapter/store/filesystem/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down Expand Up @@ -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)
}
Expand All @@ -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())
Expand Down Expand Up @@ -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)
}
Expand Down
23 changes: 0 additions & 23 deletions internal/adapter/store/filesystem/files.go

This file was deleted.

43 changes: 28 additions & 15 deletions internal/adapter/store/filesystem/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down Expand Up @@ -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)
}
Expand All @@ -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) {

Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/filesystem/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 57e348b

Please sign in to comment.