You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pc...@apache.org on 2023/04/26 16:24:11 UTC

[camel-k] branch main updated: fix(cmd): uninstall to wait the operator pod

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 861f9831f fix(cmd): uninstall to wait the operator pod
861f9831f is described below

commit 861f9831f4b4c3a8563a4821254b5f35102f876a
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Fri Apr 14 17:15:33 2023 +0200

    fix(cmd): uninstall to wait the operator pod
    
    Closes #4246
---
 pkg/cmd/uninstall.go    | 16 ++++++++++++++++
 pkg/util/watch/watch.go | 26 ++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/pkg/cmd/uninstall.go b/pkg/cmd/uninstall.go
index 06b97db15..6b9731bd2 100644
--- a/pkg/cmd/uninstall.go
+++ b/pkg/cmd/uninstall.go
@@ -20,6 +20,7 @@ package cmd
 import (
 	"context"
 	"fmt"
+	"time"
 
 	"github.com/pkg/errors"
 	"github.com/spf13/cobra"
@@ -35,7 +36,9 @@ import (
 
 	v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
 	"github.com/apache/camel-k/v2/pkg/client"
+	"github.com/apache/camel-k/v2/pkg/platform"
 	"github.com/apache/camel-k/v2/pkg/util/olm"
+	"github.com/apache/camel-k/v2/pkg/util/watch"
 )
 
 func newCmdUninstall(rootCmdOptions *RootCmdOptions) (*cobra.Command, *uninstallCmdOptions) {
@@ -156,6 +159,19 @@ func (o *uninstallCmdOptions) uninstall(cmd *cobra.Command, _ []string) error {
 				return err
 			}
 			fmt.Fprintf(cmd.OutOrStdout(), "Camel K Operator removed from namespace %s\n", o.Namespace)
+
+			// Let's wait the Pod has completed all the tasks before removing roles, as it may cause
+			// problems during the shutdown
+
+			pod := platform.GetOperatorPod(o.Context, c, o.Namespace)
+			if pod != nil {
+				tctx, cancel := context.WithTimeout(o.Context, 15*time.Second)
+				defer cancel()
+				err := watch.WaitPodToTerminate(tctx, c, pod)
+				if err != nil {
+					return errors.Wrap(err, "error while waiting the operator pod to terminate gracefully")
+				}
+			}
 		}
 
 		if err = o.uninstallNamespaceRoles(o.Context, cmd, c); err != nil {
diff --git a/pkg/util/watch/watch.go b/pkg/util/watch/watch.go
index e975ce8c8..32ee72788 100644
--- a/pkg/util/watch/watch.go
+++ b/pkg/util/watch/watch.go
@@ -23,6 +23,7 @@ import (
 
 	corev1 "k8s.io/api/core/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"k8s.io/apimachinery/pkg/watch"
 
 	v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
 	"github.com/apache/camel-k/v2/pkg/client"
@@ -212,6 +213,31 @@ func HandleIntegrationPlatformEvents(ctx context.Context, c client.Client, p *v1
 	}
 }
 
+// WaitPodToTerminate will wait for a given pod to teminate.
+func WaitPodToTerminate(ctx context.Context, c client.Client, pod *corev1.Pod) error {
+	opts := metav1.ListOptions{
+		TypeMeta:      metav1.TypeMeta{},
+		FieldSelector: fmt.Sprintf("metadata.name=%s", pod.Name),
+	}
+	watcher, err := c.CoreV1().Pods(pod.Namespace).Watch(ctx, opts)
+	if err != nil {
+		return err
+	}
+
+	defer watcher.Stop()
+
+	for {
+		select {
+		case event := <-watcher.ResultChan():
+			if event.Type == watch.Deleted {
+				return nil
+			}
+		case <-ctx.Done():
+			return nil
+		}
+	}
+}
+
 func isAllowed(lastEvent, event *corev1.Event, baseTime int64) bool {
 	if lastEvent == nil {
 		return true