Skip to content

Commit

Permalink
fix(cmd): uninstall to wait the operator pod
Browse files Browse the repository at this point in the history
Closes #4246
  • Loading branch information
squakez committed Apr 26, 2023
1 parent e7ddc2f commit 861f983
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package cmd
import (
"context"
"fmt"
"time"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions pkg/util/watch/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 861f983

Please sign in to comment.