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

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

commit 2a538884e9873aa2d3b93c7f64bbb219f9f21dcf
Author: Tadayoshi Sato <[email protected]>
AuthorDate: Mon Jul 25 15:56:03 2022 +0900

    fix(cli): fix kamel promote resources validation
---
 pkg/cmd/promote.go | 83 +++++++++++++++++++++++++++++++++---------------------
 1 file changed, 51 insertions(+), 32 deletions(-)

diff --git a/pkg/cmd/promote.go b/pkg/cmd/promote.go
index 4eb216c89..bf81e3e81 100644
--- a/pkg/cmd/promote.go
+++ b/pkg/cmd/promote.go
@@ -20,6 +20,7 @@ package cmd
 import (
        "context"
        "fmt"
+       "reflect"
        "regexp"
        "strings"
 
@@ -194,43 +195,61 @@ func (o *promoteCmdOptions) validateDestResources(c 
client.Client, it *v1.Integr
        for t, v := range mount {
                switch t {
                case "configs":
-                       if list, ok := v.([]string); ok {
-                               for _, cn := range list {
-                                       if conf, parseErr := 
resource.ParseConfig(cn); parseErr == nil {
-                                               if conf.StorageType() == 
resource.StorageTypeConfigmap {
-                                                       configmaps = 
append(configmaps, conf.Name())
-                                               } else if conf.StorageType() == 
resource.StorageTypeSecret {
-                                                       secrets = 
append(secrets, conf.Name())
-                                               }
-                                       } else {
-                                               return parseErr
+                       list, ok := v.([]interface{})
+                       if !ok {
+                               return fmt.Errorf("invalid %s type: %s, value: 
%s", t, reflect.TypeOf(v), v)
+                       }
+                       for _, cn := range list {
+                               s, ok := cn.(string)
+                               if !ok {
+                                       return fmt.Errorf("invalid %s type: %s, 
value: %s", t, reflect.TypeOf(cn), cn)
+                               }
+                               if conf, parseErr := resource.ParseConfig(s); 
parseErr == nil {
+                                       if conf.StorageType() == 
resource.StorageTypeConfigmap {
+                                               configmaps = append(configmaps, 
conf.Name())
+                                       } else if conf.StorageType() == 
resource.StorageTypeSecret {
+                                               secrets = append(secrets, 
conf.Name())
                                        }
+                               } else {
+                                       return parseErr
                                }
                        }
                case "resources":
-                       if list, ok := v.([]string); ok {
-                               for _, cn := range list {
-                                       if conf, parseErr := 
resource.ParseResource(cn); parseErr == nil {
-                                               if conf.StorageType() == 
resource.StorageTypeConfigmap {
-                                                       configmaps = 
append(configmaps, conf.Name())
-                                               } else if conf.StorageType() == 
resource.StorageTypeSecret {
-                                                       secrets = 
append(secrets, conf.Name())
-                                               }
-                                       } else {
-                                               return parseErr
+                       list, ok := v.([]interface{})
+                       if !ok {
+                               return fmt.Errorf("invalid %s type: %s, value: 
%s", t, reflect.TypeOf(v), v)
+                       }
+                       for _, cn := range list {
+                               s, ok := cn.(string)
+                               if !ok {
+                                       return fmt.Errorf("invalid %s type: %s, 
value: %s", t, reflect.TypeOf(cn), cn)
+                               }
+                               if conf, parseErr := resource.ParseResource(s); 
parseErr == nil {
+                                       if conf.StorageType() == 
resource.StorageTypeConfigmap {
+                                               configmaps = append(configmaps, 
conf.Name())
+                                       } else if conf.StorageType() == 
resource.StorageTypeSecret {
+                                               secrets = append(secrets, 
conf.Name())
                                        }
+                               } else {
+                                       return parseErr
                                }
                        }
                case "volumes":
-                       if list, ok := v.([]string); ok {
-                               for _, cn := range list {
-                                       if conf, parseErr := 
resource.ParseVolume(cn); parseErr == nil {
-                                               if conf.StorageType() == 
resource.StorageTypePVC {
-                                                       pvcs = append(pvcs, 
conf.Name())
-                                               }
-                                       } else {
-                                               return parseErr
+                       list, ok := v.([]interface{})
+                       if !ok {
+                               return fmt.Errorf("invalid %s type: %s, value: 
%s", t, reflect.TypeOf(v), v)
+                       }
+                       for _, cn := range list {
+                               s, ok := cn.(string)
+                               if !ok {
+                                       return fmt.Errorf("invalid %s type: %s, 
value: %s", t, reflect.TypeOf(cn), cn)
+                               }
+                               if conf, parseErr := resource.ParseVolume(s); 
parseErr == nil {
+                                       if conf.StorageType() == 
resource.StorageTypePVC {
+                                               pvcs = append(pvcs, conf.Name())
                                        }
+                               } else {
+                                       return parseErr
                                }
                        }
                }
@@ -270,25 +289,25 @@ func (o *promoteCmdOptions) validateDestResources(c 
client.Client, it *v1.Integr
        for _, name := range configmaps {
                if !existsCm(o.Context, c, name, o.To) {
                        anyError = true
-                       errorTrace += fmt.Sprintf("Configmap %s is missing from 
%s namespace\n", name, o.To)
+                       errorTrace += fmt.Sprintf("\n\tConfigmap %s is missing 
from %s namespace", name, o.To)
                }
        }
        for _, name := range secrets {
                if !existsSecret(o.Context, c, name, o.To) {
                        anyError = true
-                       errorTrace += fmt.Sprintf("Secret %s is missing from %s 
namespace\n", name, o.To)
+                       errorTrace += fmt.Sprintf("\n\tSecret %s is missing 
from %s namespace", name, o.To)
                }
        }
        for _, name := range pvcs {
                if !existsPv(o.Context, c, name, o.To) {
                        anyError = true
-                       errorTrace += fmt.Sprintf("PersistentVolume %s is 
missing from %s namespace\n", name, o.To)
+                       errorTrace += fmt.Sprintf("\n\tPersistentVolume %s is 
missing from %s namespace", name, o.To)
                }
        }
        for _, name := range kamelets {
                if !existsKamelet(o.Context, c, name, o.To) {
                        anyError = true
-                       errorTrace += fmt.Sprintf("Kamelet %s is missing from 
%s namespace\n", name, o.To)
+                       errorTrace += fmt.Sprintf("\n\tKamelet %s is missing 
from %s namespace", name, o.To)
                }
        }
 

Reply via email to