Skip to content
Permalink
Browse files
fix(helm): fix werf panic and helm plugins with error codes
* Exit with correct process exit code when plugin fails.
* FIx panic when werf called without arguments.

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Jun 15, 2022
1 parent a473d03 commit a39a1a0b87c1fd96c8620aab5e0f34f12ab52996
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 30 deletions.
@@ -3,7 +3,6 @@ package helm
import (
"fmt"
"os"
"reflect"
"time"

"github.com/spf13/cobra"
@@ -108,11 +107,13 @@ func NewCmd() *cobra.Command {
helm_v3.LoadPlugins(cmd, os.Stdout)
} else {
func() {
saveArgs := os.Args
os.Args = os.Args[1:]
defer func() {
os.Args = saveArgs
}()
if len(os.Args) > 1 {
saveArgs := os.Args
os.Args = os.Args[1:]
defer func() {
os.Args = saveArgs
}()
}

helm_v3.LoadPlugins(cmd, os.Stdout)
}()
@@ -144,8 +145,8 @@ func NewCmd() *cobra.Command {
return err
}

// FIXME: setup namespace env var for helm diff plugin
os.Setenv("WERF_HELM3_MODE", "1")
os.Setenv("HELM_NAMESPACE", namespace)

ctx := common.GetContext()

@@ -182,22 +183,7 @@ func NewCmd() *cobra.Command {
})

if oldRunE != nil {
if err := oldRunE(cmd, args); err != nil {
if helm_v3.IsPluginError(err) {
return err
} else if errValue := reflect.ValueOf(err); errValue.Kind() == reflect.Struct {
if !errValue.IsZero() {
codeValue := errValue.FieldByName("code")
if codeValue.IsValid() && !codeValue.IsZero() {
os.Exit(int(codeValue.Int()))
}
}
}

return err
}

return nil
return oldRunE(cmd, args)
} else if oldRun != nil {
oldRun(cmd, args)
return nil
@@ -208,8 +194,10 @@ func NewCmd() *cobra.Command {
}
}

cmd.PersistentFlags().ParseErrorsWhitelist.UnknownFlags = true
cmd.PersistentFlags().Parse(os.Args[1:])
if len(os.Args) > 1 {
cmd.PersistentFlags().ParseErrorsWhitelist.UnknownFlags = true
cmd.PersistentFlags().Parse(os.Args[1:])
}

return cmd
}
@@ -6,6 +6,8 @@ import (
"os"
"path/filepath"

helm_v3 "helm.sh/helm/v3/cmd/helm"

Check failure on line 9 in cmd/werf/main.go

GitHub Actions / Lint

cmd/werf/main.go#L9

Expected '"', Found 'h' at cmd/werf/main.go[line 9,col 2] (gci)

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

@@ -70,6 +72,9 @@ func main() {
rootCmd := constructRootCmd()

if err := rootCmd.Execute(); err != nil {
if helm_v3.IsPluginError(err) {
common.TerminateWithError(err.Error(), helm_v3.PluginErrorCode(err))
}
common.TerminateWithError(err.Error(), 1)
}
}
2 go.mod
@@ -307,6 +307,6 @@ replace k8s.io/helm => github.com/werf/helm v0.0.0-20210202111118-81e74d46da0f

replace github.com/deislabs/oras => github.com/werf/third-party-oras v0.9.1-0.20210927171747-6d045506f4c8

replace helm.sh/helm/v3 => github.com/werf/3p-helm/v3 v3.0.0-20220608085145-0943d4045660
replace helm.sh/helm/v3 => github.com/werf/3p-helm/v3 v3.0.0-20220615081302-d5ffa8d30462

replace github.com/go-git/go-git/v5 => github.com/ZauberNerd/go-git/v5 v5.4.3-0.20220315170230-29ec1bc1e5db
6 go.sum
@@ -2036,14 +2036,12 @@ github.com/vmware/govmomi v0.20.3/go.mod h1:URlwyTFZX72RmxtxuaFL2Uj3fD1JTvZdx59b
github.com/weppos/publicsuffix-go v0.4.0/go.mod h1:z3LCPQ38eedDQSwmsSRW4Y7t2L8Ln16JPQ02lHAdn5k=
github.com/weppos/publicsuffix-go v0.5.0 h1:rutRtjBJViU/YjcI5d80t4JAVvDltS6bciJg2K1HrLU=
github.com/weppos/publicsuffix-go v0.5.0/go.mod h1:z3LCPQ38eedDQSwmsSRW4Y7t2L8Ln16JPQ02lHAdn5k=
github.com/werf/3p-helm/v3 v3.0.0-20220608085145-0943d4045660 h1:y38t8rviJW3+dk0n6QG7Jv1WbKZvCGJdBVjrOJaxEEk=
github.com/werf/3p-helm/v3 v3.0.0-20220608085145-0943d4045660/go.mod h1:NxtE2KObf2PrzDl6SIamPFPKyAqWi10iWuvKlQn/Yao=
github.com/werf/3p-helm/v3 v3.0.0-20220615081302-d5ffa8d30462 h1:u0njIasP6iKAiFKX7KJwz9/ufgYY+ph04wIWTHvqEPQ=
github.com/werf/3p-helm/v3 v3.0.0-20220615081302-d5ffa8d30462/go.mod h1:NxtE2KObf2PrzDl6SIamPFPKyAqWi10iWuvKlQn/Yao=
github.com/werf/copy-recurse v0.2.4 h1:kEyGUKhgS8WdEOjInNQKgk4lqPWzP2AgR27F3dcGsVc=
github.com/werf/copy-recurse v0.2.4/go.mod h1:KVHSQ90p19xflWW0B7BJhLBwmSbEtuxIaBnjlUYRPhk=
github.com/werf/helm v0.0.0-20210202111118-81e74d46da0f h1:81YscYTF9mmTf0ULOsCmm42YWQp+qWDzWi1HjWniZrg=
github.com/werf/helm v0.0.0-20210202111118-81e74d46da0f/go.mod h1:OMONwLWU9zEENgaVjWEX+M+xik2QakejzKHG1+6mnUo=
github.com/werf/kubedog v0.6.5-0.20220524172024-3d978f3318f0 h1:PxvSQzWNc0gM4QsMBEUIg/A2SHia6zLMBbxi36Q5tvw=
github.com/werf/kubedog v0.6.5-0.20220524172024-3d978f3318f0/go.mod h1:543dUdVbkeJeH/PaB2fiVNR++rGnWmZMmKkD9nAfjI8=
github.com/werf/kubedog v0.6.5-0.20220608151530-2555924b0ce9 h1:EAGbbyVsin6oYLd7TerlIgr/0x/Qj6v43Nc7zzG3FlE=
github.com/werf/kubedog v0.6.5-0.20220608151530-2555924b0ce9/go.mod h1:543dUdVbkeJeH/PaB2fiVNR++rGnWmZMmKkD9nAfjI8=
github.com/werf/lockgate v0.0.0-20200729113342-ec2c142f71ea h1:R5tJUhL5a3YfHTrHWyuAdJW3h//fmONrpHJjjAZ79e4=

0 comments on commit a39a1a0

Please sign in to comment.