Skip to content

Commit

Permalink
feat(storage): throw error when file(s) are to large for a configMap) (
Browse files Browse the repository at this point in the history
…#587)

* feat(storage): throw error when file(s) are to large for a configMap)

Signed-off-by: Smuu <18609909+Smuu@users.noreply.github.com>

* feat: make max size a const

Signed-off-by: Smuu <18609909+Smuu@users.noreply.github.com>

---------

Signed-off-by: Smuu <18609909+Smuu@users.noreply.github.com>
  • Loading branch information
smuu authored Dec 6, 2024
1 parent 31010f1 commit c435ce0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkg/instance/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ var (
ErrApplyingFunctionToSidecar = errors.New("ApplyingFunctionToSidecar", "error applying function to sidecar '%s'")
ErrInitializingSidecar = errors.New("InitializingSidecar", "error initializing sidecar for instance '%s'")
ErrSidecarInstanceIsNil = errors.New("SidecarInstanceIsNil", "sidecar instance is nil for instance '%s'")
ErrFailedToCreatePersistentVolumeClaim = errors.New("FailedToCreatePersistentVolumeClaim", "failed to create persistent volume claim")
ErrFailedToDeletePersistentVolumeClaim = errors.New("FailedToDeletePersistentVolumeClaim", "failed to delete persistent volume claim")
ErrUpgradingImageNotAllowed = errors.New("UpgradingImageNotAllowed", "upgrading image is only allowed in state 'Started'. Current state is '%s'")
ErrAddingHostToProxyNotAllowed = errors.New("AddingHostToProxyNotAllowed", "adding host to proxy is only allowed in state 'Started' and 'Preparing'. Current state is '%s'")
Expand All @@ -224,4 +225,7 @@ var (
ErrCannotCloneInstance = errors.New("CannotCloneInstance", "cannot clone instance '%s' in state '%s'")
ErrGettingIPNotAllowed = errors.New("GettingIPNotAllowed", "getting IP is allowed in state 'Started'. Current state is '%s'")
ErrPodIPNotReady = errors.New("PodIPNotReady", "pod IP is not ready for pod '%s'")
ErrFailedToGetFileSize = errors.New("FailedToGetFileSize", "failed to get file size")
ErrFileTooLargeCommitted = errors.New("FileTooLargeCommitted", "file '%s' is too large (max 1MiB) to add after instance is committed")
ErrTotalFilesSizeTooLarge = errors.New("TotalFilesSizeTooLarge", "total files size is too large (max 1MiB)")
)
31 changes: 30 additions & 1 deletion pkg/instance/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/celestiaorg/knuu/pkg/names"
)

const maxTotalFilesBytes = 1024 * 1024

type storage struct {
instance *Instance
volumes []*k8s.Volume
Expand Down Expand Up @@ -51,6 +53,13 @@ func (s *storage) AddFile(src string, dest string, chown string) error {
s.instance.build.addFileToBuilder(src, dest, chown)
return nil
case StateCommitted, StateStopped:
srcInfo, err := os.Stat(src)
if err != nil {
return ErrFailedToGetFileSize.Wrap(err)
}
if srcInfo.Size() > maxTotalFilesBytes {
return ErrFileTooLargeCommitted.WithParams(src)
}
return s.addFileToInstance(dstPath, dest, chown)
}

Expand Down Expand Up @@ -274,6 +283,23 @@ func (s *storage) addFileToInstance(dstPath, dest, chown string) error {
return ErrSrcDoesNotExistOrIsDirectory.WithParams(dstPath).Wrap(err)
}

size := int64(0)
for _, file := range s.files {
srcInfo, err := os.Stat(file.Source)
if err != nil {
return ErrFailedToGetFileSize.Wrap(err)
}
size += srcInfo.Size()
}
srcInfo, err = os.Stat(dstPath)
if err != nil {
return ErrFailedToGetFileSize.Wrap(err)
}
size += srcInfo.Size()
if size > maxTotalFilesBytes {
return ErrTotalFilesSizeTooLarge.WithParams(dstPath)
}

file := s.instance.K8sClient.NewFile(dstPath, dest)
parts := strings.Split(chown, ":")
if len(parts) != 2 {
Expand Down Expand Up @@ -307,7 +333,10 @@ func (s *storage) deployVolume(ctx context.Context) error {
for _, volume := range s.volumes {
totalSize.Add(volume.Size)
}
s.instance.K8sClient.CreatePersistentVolumeClaim(ctx, s.instance.name, s.instance.execution.Labels(), totalSize)
err := s.instance.K8sClient.CreatePersistentVolumeClaim(ctx, s.instance.name, s.instance.execution.Labels(), totalSize)
if err != nil {
return ErrFailedToCreatePersistentVolumeClaim.Wrap(err)
}
s.instance.Logger.WithFields(logrus.Fields{
"total_size": totalSize.String(),
"instance": s.instance.name,
Expand Down

0 comments on commit c435ce0

Please sign in to comment.