This is an automated email from the ASF dual-hosted git repository. cdeppisch pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit fc225f18898eae766a7ebe131b71eaa58d1c18e6 Author: Christoph Deppisch <cdeppi...@redhat.com> AuthorDate: Mon Mar 4 21:56:05 2024 +0100 chore(e2e): Fix concurrent access to viper flags --- pkg/cmd/install.go | 5 +++++ pkg/cmd/root.go | 8 ++++++++ pkg/cmd/run.go | 5 +++++ pkg/cmd/uninstall.go | 5 +++++ pkg/cmd/util.go | 16 ++++------------ 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/pkg/cmd/install.go b/pkg/cmd/install.go index 8567165b6..4e54b0046 100644 --- a/pkg/cmd/install.go +++ b/pkg/cmd/install.go @@ -676,6 +676,11 @@ func (o *installCmdOptions) postRun(cmd *cobra.Command, _ []string) error { func (o *installCmdOptions) decode(cmd *cobra.Command, _ []string) error { path := pathToRoot(cmd) + + // Requires synchronization as viper bind flag is not able to handle concurrency + m.Lock() + defer m.Unlock() + if err := decodeKey(o, path); err != nil { return err } diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index a43e5a349..70c18209e 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -23,6 +23,7 @@ import ( "fmt" "os" "strings" + "sync" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -39,6 +40,9 @@ const kamelCommandLongDescription = `Apache Camel K is a lightweight integration superpowers. ` +// Mutex to synchronize flag operations as viper library is not able to handle concurrency. +var m = sync.Mutex{} + // RootCmdOptions --. // nolint: containedctx type RootCmdOptions struct { @@ -93,6 +97,10 @@ func kamelPreAddCommandInit(options *RootCmdOptions) *cobra.Command { } func kamelPostAddCommandInit(cmd *cobra.Command) error { + // Requires synchronization as viper bind flag is not able to handle concurrency + m.Lock() + defer m.Unlock() + if err := bindPFlagsHierarchy(cmd); err != nil { return err } diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 12ce4da6b..4c33c4154 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -187,6 +187,11 @@ func (o *runCmdOptions) decode(cmd *cobra.Command, args []string) error { // load from kamel.run (1) pathToRoot := pathToRoot(cmd) + + // Requires synchronization as viper bind flag is not able to handle concurrency + m.Lock() + defer m.Unlock() + if err := decodeKey(o, pathToRoot); err != nil { return err } diff --git a/pkg/cmd/uninstall.go b/pkg/cmd/uninstall.go index 461435c0b..489da6c1f 100644 --- a/pkg/cmd/uninstall.go +++ b/pkg/cmd/uninstall.go @@ -107,6 +107,11 @@ var defaultListOptions = metav1.ListOptions{ func (o *uninstallCmdOptions) decode(cmd *cobra.Command, _ []string) error { path := pathToRoot(cmd) + + // Requires synchronization as viper bind flag is not able to handle concurrency + m.Lock() + defer m.Unlock() + if err := decodeKey(o, path); err != nil { return err } diff --git a/pkg/cmd/util.go b/pkg/cmd/util.go index 4365addee..aec422477 100644 --- a/pkg/cmd/util.go +++ b/pkg/cmd/util.go @@ -25,7 +25,6 @@ import ( "log" "reflect" "strings" - "sync" "github.com/mitchellh/mapstructure" @@ -45,9 +44,6 @@ const ( offlineCommandLabel = "camel.apache.org/cmd.offline" ) -// Mutex to synchronize flag operations as viper library is not able to handle concurrency. -var m = sync.Mutex{} - // DeleteIntegration --. func DeleteIntegration(ctx context.Context, c client.Client, name string, namespace string) error { integration := v1.Integration{ @@ -78,10 +74,6 @@ func bindPFlagsHierarchy(cmd *cobra.Command) error { } func bindPFlags(cmd *cobra.Command) error { - // Requires synchronization as viper bind flag is not able to handle concurrency - m.Lock() - defer m.Unlock() - prefix := pathToRoot(cmd) pl := p.NewClient() @@ -122,10 +114,6 @@ func pathToRoot(cmd *cobra.Command) string { } func decodeKey(target interface{}, key string) error { - // Requires synchronization as viper all settings is not able to handle concurrency - m.Lock() - defer m.Unlock() - nodes := strings.Split(key, ".") settings := viper.AllSettings() @@ -168,6 +156,10 @@ func decodeKey(target interface{}, key string) error { func decode(target interface{}) func(*cobra.Command, []string) error { return func(cmd *cobra.Command, args []string) error { + // Requires synchronization as viper bind flag is not able to handle concurrency + m.Lock() + defer m.Unlock() + path := pathToRoot(cmd) if err := decodeKey(target, path); err != nil { return err