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 c2302fa9b25a7a22e247037161786bc4da76ef3d Author: Christoph Deppisch <[email protected]> AuthorDate: Fri Jun 3 09:56:46 2022 +0200 Fix #2177: Align namespace operator lease with operator id - Use operator id when creating the operator lease so only operator instances with the same id elect a leader --- pkg/cmd/operator.go | 18 +++++++++++++++--- pkg/platform/operator.go | 10 +++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/operator.go b/pkg/cmd/operator.go index b1b8c59dd..b2570a356 100644 --- a/pkg/cmd/operator.go +++ b/pkg/cmd/operator.go @@ -18,10 +18,12 @@ limitations under the License. package cmd import ( + "fmt" + "github.com/apache/camel-k/pkg/platform" + "github.com/apache/camel-k/pkg/util/defaults" "github.com/spf13/cobra" "github.com/apache/camel-k/pkg/cmd/operator" - "github.com/apache/camel-k/pkg/platform" ) const operatorCommand = "operator" @@ -41,7 +43,7 @@ func newCmdOperator() (*cobra.Command, *operatorCmdOptions) { cmd.Flags().Int32("health-port", 8081, "The port of the health endpoint") cmd.Flags().Int32("monitoring-port", 8080, "The port of the metrics endpoint") cmd.Flags().Bool("leader-election", true, "Use leader election") - cmd.Flags().String("leader-election-id", platform.OperatorLockName, "Use the given ID as the leader election Lease name") + cmd.Flags().String("leader-election-id", "", "Use the given ID as the leader election Lease name") return &cmd, &options } @@ -54,5 +56,15 @@ type operatorCmdOptions struct { } func (o *operatorCmdOptions) run(_ *cobra.Command, _ []string) { - operator.Run(o.HealthPort, o.MonitoringPort, o.LeaderElection, o.LeaderElectionID) + + leaderElectionId := o.LeaderElectionID + if leaderElectionId == "" { + if defaults.OperatorID() != "" { + leaderElectionId = fmt.Sprintf("%s-lock", defaults.OperatorID()) + } else { + leaderElectionId = platform.OperatorLockName + } + } + + operator.Run(o.HealthPort, o.MonitoringPort, o.LeaderElection, leaderElectionId) } diff --git a/pkg/platform/operator.go b/pkg/platform/operator.go index c9fe67c77..602a99105 100644 --- a/pkg/platform/operator.go +++ b/pkg/platform/operator.go @@ -19,6 +19,7 @@ package platform import ( "context" + "fmt" "os" "strings" @@ -80,8 +81,15 @@ func IsNamespaceLocked(ctx context.Context, c ctrl.Reader, namespace string) (bo return false, nil } + var operatorLockName string + if defaults.OperatorID() != "" { + operatorLockName = fmt.Sprintf("%s-lock", defaults.OperatorID()) + } else { + operatorLockName = OperatorLockName + } + lease := coordination.Lease{} - if err := c.Get(ctx, ctrl.ObjectKey{Namespace: namespace, Name: OperatorLockName}, &lease); err != nil && k8serrors.IsNotFound(err) { + if err := c.Get(ctx, ctrl.ObjectKey{Namespace: namespace, Name: operatorLockName}, &lease); err != nil && k8serrors.IsNotFound(err) { return false, nil } else if err != nil { return true, err
