Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
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
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
Loading