This is an automated email from the ASF dual-hosted git repository.

astefanutti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 4697722476e3c2366578dc0b7eaae8ea4cc24487
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Thu Jun 3 10:01:23 2021 +0200

    feat(cmd/run): file size limitation
    
    Won't accept any resource/config file larger than 1MB, due to the 
limitation on K8S CustomResources
    
    Ref #2003
---
 pkg/cmd/run_help.go     |  8 ++++++++
 pkg/cmd/util_content.go | 14 ++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/pkg/cmd/run_help.go b/pkg/cmd/run_help.go
index 12264c2..c7f80e7 100644
--- a/pkg/cmd/run_help.go
+++ b/pkg/cmd/run_help.go
@@ -132,6 +132,14 @@ func applyOption(config *RunConfigOption, integrationSpec 
*v1.IntegrationSpec,
                }
                
integrationSpec.AddConfigurationAsResource(string(config.ConfigType), 
config.Value, string(resourceType), config.DestinationPath())
        case ConfigOptionTypeFile:
+               // Don't allow a file size longer than 1 MiB
+               fileSize, err := fileSize(config.Value)
+               printSize := fmt.Sprintf("%.2f", float64(fileSize)/Megabyte)
+               if err != nil {
+                       return err
+               } else if fileSize > Megabyte {
+                       return fmt.Errorf("you cannot provide a file larger 
than 1 MB (it was %s MB), check configmap option or --volume instead", 
printSize)
+               }
                // Don't allow a binary non compressed resource
                rawData, contentType, err := loadRawContent(config.Value)
                if err != nil {
diff --git a/pkg/cmd/util_content.go b/pkg/cmd/util_content.go
index 62cda96..5d35127 100644
--- a/pkg/cmd/util_content.go
+++ b/pkg/cmd/util_content.go
@@ -22,10 +22,24 @@ import (
        "io/ioutil"
        "net/http"
        "net/url"
+       "os"
        "regexp"
        "strings"
 )
 
+const (
+       Megabyte = 1 << 20
+       Kilobyte = 1 << 10
+)
+
+func fileSize(source string) (int64, error) {
+       fi, err := os.Stat(source)
+       if err != nil {
+               return -1, err
+       }
+       return fi.Size(), nil
+}
+
 func loadRawContent(source string) ([]byte, string, error) {
        var content []byte
        var err error

Reply via email to