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

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

commit aee4b4690d1e05d345ddd7832c6b2062a5a6524b
Author: nferraro <ni.ferr...@gmail.com>
AuthorDate: Thu Jun 27 16:23:51 2019 +0200

    fix #692: add rebuild command and other options
---
 e2e/test_support.go |  40 +++++++++++++++-----
 e2e/upgrade_test.go |   9 +++--
 pkg/cmd/rebuild.go  | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 pkg/cmd/reset.go    |  26 +++++++++----
 pkg/cmd/root.go     |   1 +
 5 files changed, 162 insertions(+), 20 deletions(-)

diff --git a/e2e/test_support.go b/e2e/test_support.go
index 4f5ada9..8112dd1 100644
--- a/e2e/test_support.go
+++ b/e2e/test_support.go
@@ -193,15 +193,6 @@ func setIntegrationVersion(ns string, name string, version 
string) error {
        return testClient.Status().Update(testContext, it)
 }
 
-func setIntegrationPhase(ns string, name string, phase 
v1alpha1.IntegrationPhase) error {
-       it := integration(ns, name)()
-       if it == nil {
-               return fmt.Errorf("no integration named %s found", name)
-       }
-       it.Status.Phase = phase
-       return testClient.Status().Update(testContext, it)
-}
-
 func kits(ns string) func() []v1alpha1.IntegrationKit {
        return func() []v1alpha1.IntegrationKit {
                lst := v1alpha1.NewIntegrationKitList()
@@ -250,6 +241,16 @@ func operatorImage(ns string) func() string {
        }
 }
 
+func operatorPodPhase(ns string) func() v1.PodPhase {
+       return func() v1.PodPhase {
+               pod := operatorPod(ns)()
+               if pod == nil {
+                       return ""
+               }
+               return pod.Status.Phase
+       }
+}
+
 func configmap(ns string, name string) func() *v1.ConfigMap {
        return func() *v1.ConfigMap {
                cm := v1.ConfigMap{
@@ -408,6 +409,24 @@ func scaleOperator(ns string, replicas int32) error {
        Namespace testing functions
 */
 
+func numPods(ns string) func() int {
+       return func() int {
+               lst := v1.PodList{
+                       TypeMeta: metav1.TypeMeta{
+                               Kind:       "Pod",
+                               APIVersion: v1.SchemeGroupVersion.String(),
+                       },
+               }
+               opts := k8sclient.ListOptions{
+                       Namespace: ns,
+               }
+               if err := testClient.List(testContext, &opts, &lst); err != nil 
{
+                       panic(err)
+               }
+               return len(lst.Items)
+       }
+}
+
 func withNewTestNamespace(doRun func(string)) {
        ns := newTestNamespace()
        defer deleteTestNamespace(ns)
@@ -438,6 +457,9 @@ func deleteTestNamespace(ns metav1.Object) {
                        log.Error(err, "cannot delete test namespace", "name", 
ns.GetName())
                }
        }
+
+       // Wait for all pods to be deleted
+       gomega.Eventually(numPods(ns.GetName()), 30 * 
time.Second).Should(gomega.Equal(0))
 }
 
 func newTestNamespace() metav1.Object {
diff --git a/e2e/upgrade_test.go b/e2e/upgrade_test.go
index 1469e98..771a8ff 100644
--- a/e2e/upgrade_test.go
+++ b/e2e/upgrade_test.go
@@ -25,7 +25,6 @@ import (
        "testing"
        "time"
 
-       "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
        "github.com/apache/camel-k/pkg/util/defaults"
        . "github.com/onsi/gomega"
        v1 "k8s.io/api/core/v1"
@@ -79,9 +78,13 @@ func TestIntegrationUpgrade(t *testing.T) {
                // Scale the operator up
                Expect(scaleOperator(ns, 1)).Should(BeNil())
                Eventually(operatorPod(ns)).ShouldNot(BeNil())
+               Eventually(operatorPodPhase(ns)).Should(Equal(v1.PodRunning))
 
-               // Clear the integration phase
-               Expect(setIntegrationPhase(ns, "js", 
v1alpha1.IntegrationPhaseNone)).Should(BeNil())
+               // No auto-update expected
+               Consistently(integrationVersion(ns, "js"), 
3*time.Second).Should(Equal("an.older.one"))
+
+               // Clear the integration status
+               Expect(kamel("rebuild", "js", "-n", 
ns).Execute()).Should(BeNil())
 
                // Check the integration version change
                Eventually(integrationVersion(ns, 
"js")).Should(Equal(defaults.Version))
diff --git a/pkg/cmd/rebuild.go b/pkg/cmd/rebuild.go
new file mode 100644
index 0000000..f062492
--- /dev/null
+++ b/pkg/cmd/rebuild.go
@@ -0,0 +1,106 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package cmd
+
+import (
+       "fmt"
+
+       "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+       "github.com/apache/camel-k/pkg/client"
+       "github.com/pkg/errors"
+       "github.com/spf13/cobra"
+       k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
+)
+
+func newCmdRebuild(rootCmdOptions *RootCmdOptions) *cobra.Command {
+       options := rebuildCmdOptions{
+               RootCmdOptions: rootCmdOptions,
+       }
+       cmd := cobra.Command{
+               Use:   "rebuild [integration]",
+               Short: "Clear the state of integrations to rebuild them",
+               Long:  `Clear the state of one or more integrations causing a 
rebuild.`,
+               RunE:  options.rebuild,
+       }
+
+       return &cmd
+}
+
+type rebuildCmdOptions struct {
+       *RootCmdOptions
+}
+
+func (o *rebuildCmdOptions) rebuild(_ *cobra.Command, args []string) error {
+       c, err := o.GetCmdClient()
+       if err != nil {
+               return err
+       }
+
+       var integrations []v1alpha1.Integration
+       if len(args) == 0 {
+               if integrations, err = o.listAllIntegrations(c); err != nil {
+                       return err
+               }
+       } else {
+               if integrations, err = o.getIntegrations(c, args); err != nil {
+                       return err
+               }
+       }
+
+       if err = o.rebuildIntegrations(c, integrations); err != nil {
+               return err
+       }
+
+       fmt.Printf("%d integrations have been rebuilt\n", len(integrations))
+       return nil
+}
+
+func (o *rebuildCmdOptions) listAllIntegrations(c client.Client) 
([]v1alpha1.Integration, error) {
+       list := v1alpha1.NewIntegrationList()
+       if err := c.List(o.Context, &k8sclient.ListOptions{Namespace: 
o.Namespace}, &list); err != nil {
+               return nil, errors.Wrap(err, fmt.Sprintf("could not retrieve 
integrations from namespace %s", o.Namespace))
+       }
+       return list.Items, nil
+}
+
+func (o *rebuildCmdOptions) getIntegrations(c client.Client, names []string) 
([]v1alpha1.Integration, error) {
+       ints := make([]v1alpha1.Integration, 0, len(names))
+       for _, n := range names {
+               it := v1alpha1.NewIntegration(o.Namespace, n)
+               key := k8sclient.ObjectKey{
+                       Name:      n,
+                       Namespace: o.Namespace,
+               }
+               if err := c.Get(o.Context, key, &it); err != nil {
+                       return nil, errors.Wrap(err, fmt.Sprintf("could not 
find integration %s in namespace %s", it.Name, o.Namespace))
+               }
+               ints = append(ints, it)
+       }
+       return ints, nil
+}
+
+func (o *rebuildCmdOptions) rebuildIntegrations(c client.Client, integrations 
[]v1alpha1.Integration) error {
+       for _, i := range integrations {
+               it := i
+               it.Status = v1alpha1.IntegrationStatus{}
+               if err := c.Status().Update(o.Context, &it); err != nil {
+                       return errors.Wrap(err, fmt.Sprintf("could not rebuild 
integration %s in namespace %s", it.Name, o.Namespace))
+               }
+       }
+       return nil
+}
diff --git a/pkg/cmd/reset.go b/pkg/cmd/reset.go
index ad372ff..4aa00ee 100644
--- a/pkg/cmd/reset.go
+++ b/pkg/cmd/reset.go
@@ -38,11 +38,16 @@ func newCmdReset(rootCmdOptions *RootCmdOptions) 
*cobra.Command {
                Run:   options.reset,
        }
 
+       cmd.Flags().BoolVar(&options.SkipKits, "skip-kits", false, "Do not 
delete the integration kits")
+       cmd.Flags().BoolVar(&options.SkipIntegrations, "skip-integrations", 
false, "Do not delete the integrations")
+
        return &cmd
 }
 
 type resetCmdOptions struct {
        *RootCmdOptions
+       SkipKits         bool
+       SkipIntegrations bool
 }
 
 func (o *resetCmdOptions) reset(_ *cobra.Command, _ []string) {
@@ -51,18 +56,23 @@ func (o *resetCmdOptions) reset(_ *cobra.Command, _ 
[]string) {
                fmt.Print(err)
                return
        }
+
        var n int
-       if n, err = o.deleteAllIntegrations(c); err != nil {
-               fmt.Print(err)
-               return
+       if !o.SkipIntegrations {
+               if n, err = o.deleteAllIntegrations(c); err != nil {
+                       fmt.Print(err)
+                       return
+               }
+               fmt.Printf("%d integrations deleted from namespace %s\n", n, 
o.Namespace)
        }
-       fmt.Printf("%d integrations deleted from namespace %s\n", n, 
o.Namespace)
 
-       if n, err = o.deleteAllIntegrationKits(c); err != nil {
-               fmt.Print(err)
-               return
+       if !o.SkipKits {
+               if n, err = o.deleteAllIntegrationKits(c); err != nil {
+                       fmt.Print(err)
+                       return
+               }
+               fmt.Printf("%d integration kits deleted from namespace %s\n", 
n, o.Namespace)
        }
-       fmt.Printf("%d integration kits deleted from namespace %s\n", n, 
o.Namespace)
 
        if err = o.resetIntegrationPlatform(c); err != nil {
                fmt.Print(err)
diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go
index a5fe022..f197fc9 100644
--- a/pkg/cmd/root.go
+++ b/pkg/cmd/root.go
@@ -65,6 +65,7 @@ func NewKamelCommand(ctx context.Context) (*cobra.Command, 
error) {
        cmd.AddCommand(newCmdKit(&options))
        cmd.AddCommand(newCmdReset(&options))
        cmd.AddCommand(newCmdDescribe(&options))
+       cmd.AddCommand(newCmdRebuild(&options))
 
        return &cmd, nil
 }

Reply via email to