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 08:51:26 UTC

[camel-k] 10/18: chore(cmd): rebuild replaces Bindings

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

commit cb2758bcc5d08c0bb1eb3dda1930c60fab51a749
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Mon Apr 3 09:58:14 2023 +0200

    chore(cmd): rebuild replaces Bindings
---
 pkg/cmd/rebuild.go      | 91 +++++++++++++++++++++++++++++++++++++++++++++++--
 pkg/cmd/rebuild_test.go | 62 +++++++++++++++++++++++++++++++++
 2 files changed, 151 insertions(+), 2 deletions(-)

diff --git a/pkg/cmd/rebuild.go b/pkg/cmd/rebuild.go
index 8d30feb8d..f4505bf39 100644
--- a/pkg/cmd/rebuild.go
+++ b/pkg/cmd/rebuild.go
@@ -22,11 +22,14 @@ import (
 
 	"github.com/pkg/errors"
 	"github.com/spf13/cobra"
+	"k8s.io/apimachinery/pkg/labels"
+	"k8s.io/apimachinery/pkg/selection"
 
 	k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
 
 	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/util/kubernetes"
 )
 
 func newCmdRebuild(rootCmdOptions *RootCmdOptions) (*cobra.Command, *rebuildCmdOptions) {
@@ -68,11 +71,80 @@ func (o *rebuildCmdOptions) validate(args []string) error {
 }
 
 func (o *rebuildCmdOptions) run(cmd *cobra.Command, args []string) error {
+	errKlbs := o.rebuildBindingType(cmd, args)
+	errIts := o.rebuildIntegrationType(cmd, args)
+
+	if errIts != nil && errKlbs != nil {
+		return errors.Wrap(errIts, errKlbs.Error())
+	}
+
+	return nil
+}
+
+func (o *rebuildCmdOptions) rebuildBindingType(cmd *cobra.Command, args []string) error {
 	c, err := o.GetCmdClient()
 	if err != nil {
 		return err
 	}
+	var bindings []v1.Binding
+	if o.RebuildAll {
+		if bindings, err = o.listAllBindings(c); err != nil {
+			return err
+		}
+	} else if len(args) > 0 {
+		if bindings, err = o.getBindings(c, args); err != nil {
+			return err
+		}
+	}
+
+	if err = o.rebuildBindings(c, bindings); err != nil {
+		return err
+	}
+
+	fmt.Fprintln(cmd.OutOrStdout(), len(bindings), "bindings have been rebuilt")
+	return nil
+}
+
+func (o *rebuildCmdOptions) listAllBindings(c client.Client) ([]v1.Binding, error) {
+	list := v1.NewBindingList()
+	if err := c.List(o.Context, &list, k8sclient.InNamespace(o.Namespace)); err != nil {
+		return nil, errors.Wrap(err, fmt.Sprintf("could not retrieve bindings from namespace %s", o.Namespace))
+	}
+	return list.Items, nil
+}
 
+func (o *rebuildCmdOptions) getBindings(c client.Client, names []string) ([]v1.Binding, error) {
+	klbs := make([]v1.Binding, 0, len(names))
+	for _, n := range names {
+		klb := v1.NewBinding(o.Namespace, n)
+		key := k8sclient.ObjectKey{
+			Name:      n,
+			Namespace: o.Namespace,
+		}
+		if err := c.Get(o.Context, key, &klb); err != nil {
+			return nil, errors.Wrap(err, fmt.Sprintf("could not find binding %s in namespace %s", klb.Name, o.Namespace))
+		}
+		klbs = append(klbs, klb)
+	}
+	return klbs, nil
+}
+
+func (o *rebuildCmdOptions) rebuildBindings(c k8sclient.StatusClient, bindings []v1.Binding) error {
+	for _, i := range bindings {
+		klb := i
+		klb.Status = v1.BindingStatus{}
+		if err := c.Status().Update(o.Context, &klb); err != nil {
+			return errors.Wrap(err, fmt.Sprintf("could not rebuild binding %s in namespace %s", klb.Name, o.Namespace))
+		}
+	}
+	return nil
+}
+
+func (o *rebuildCmdOptions) rebuildIntegrationType(cmd *cobra.Command, args []string) error {
+	c, err := o.GetCmdClient()
+	if err != nil {
+		return err
+	}
 	var integrations []v1.Integration
 	if o.RebuildAll {
 		if integrations, err = o.listAllIntegrations(c); err != nil {
@@ -94,7 +166,19 @@ func (o *rebuildCmdOptions) run(cmd *cobra.Command, args []string) error {
 
 func (o *rebuildCmdOptions) listAllIntegrations(c client.Client) ([]v1.Integration, error) {
 	list := v1.NewIntegrationList()
-	if err := c.List(o.Context, &list, k8sclient.InNamespace(o.Namespace)); err != nil {
+	// Integrations controlled by Bindings are not included
+	excludeItsFromKlbs, err := labels.NewRequirement(kubernetes.CamelCreatorLabelKind, selection.NotEquals, []string{
+		"Binding",
+	})
+	if err != nil {
+		return list.Items, err
+	}
+	if err := c.List(o.Context, &list,
+		k8sclient.InNamespace(o.Namespace),
+		k8sclient.MatchingLabelsSelector{
+			Selector: labels.NewSelector().Add(*excludeItsFromKlbs),
+		},
+	); err != nil {
 		return nil, errors.Wrap(err, fmt.Sprintf("could not retrieve integrations from namespace %s", o.Namespace))
 	}
 	return list.Items, nil
@@ -111,7 +195,10 @@ func (o *rebuildCmdOptions) getIntegrations(c client.Client, names []string) ([]
 		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)
+		// Integrations controlled by Bindings are not included
+		if it.Labels[kubernetes.CamelCreatorLabelKind] != "Binding" {
+			ints = append(ints, it)
+		}
 	}
 	return ints, nil
 }
diff --git a/pkg/cmd/rebuild_test.go b/pkg/cmd/rebuild_test.go
index 021c1cf6b..4b7ac33c5 100644
--- a/pkg/cmd/rebuild_test.go
+++ b/pkg/cmd/rebuild_test.go
@@ -20,6 +20,7 @@ package cmd
 import (
 	"testing"
 
+	"github.com/apache/camel-k/v2/pkg/util/kubernetes"
 	"github.com/apache/camel-k/v2/pkg/util/test"
 	"github.com/spf13/cobra"
 	"github.com/stretchr/testify/assert"
@@ -64,3 +65,64 @@ func TestRebuildAllFlag(t *testing.T) {
 	assert.Nil(t, err)
 	assert.Equal(t, true, rebuildCmdOptions.RebuildAll)
 }
+
+func TestRebuildAllBindingsAndIntegrations(t *testing.T) {
+	defaultIntegration := nominalIntegration("my-it-test")
+	defaultKB := nominalBinding("my-kb-test-rebuild")
+	itGeneratedByKlb := nominalIntegration("my-kb-test-rebuild")
+	itGeneratedByKlb.Labels = map[string]string{
+		kubernetes.CamelCreatorLabelKind: "Binding",
+	}
+
+	_, rebuildCmd, _ := initializeRebuildOptions(t, &defaultIntegration, &defaultKB, &itGeneratedByKlb)
+	output, err := test.ExecuteCommand(rebuildCmd, cmdRebuild, "--all")
+	assert.Nil(t, err)
+	assert.Contains(t, output, "1 bindings have been rebuilt")
+	assert.Contains(t, output, "1 integrations have been rebuilt")
+}
+
+func TestRebuildNone(t *testing.T) {
+	defaultIntegration := nominalIntegration("my-it-test")
+	defaultKB := nominalBinding("my-kb-test-rebuild")
+	itGeneratedByKlb := nominalIntegration("my-kb-test-rebuild")
+	itGeneratedByKlb.Labels = map[string]string{
+		kubernetes.CamelCreatorLabelKind: "Binding",
+	}
+
+	_, rebuildCmd, _ := initializeRebuildOptions(t, &defaultIntegration, &defaultKB, &itGeneratedByKlb)
+	output, err := test.ExecuteCommand(rebuildCmd, cmdRebuild, "my-missing")
+	assert.NotNil(t, err)
+	assert.NotContains(t, output, "have been rebuilt")
+	assert.Contains(t, output, "could not find binding my-missing in namespace default")
+	assert.Contains(t, output, "could not find integration my-missing in namespace default")
+}
+
+func TestRebuildBindingOnly(t *testing.T) {
+	defaultIntegration := nominalIntegration("my-it-test")
+	defaultKB := nominalBinding("my-kb-test-rebuild")
+	itGeneratedByKlb := nominalIntegration("my-kb-test-rebuild")
+	itGeneratedByKlb.Labels = map[string]string{
+		kubernetes.CamelCreatorLabelKind: "Binding",
+	}
+
+	_, rebuildCmd, _ := initializeRebuildOptions(t, &defaultIntegration, &defaultKB, &itGeneratedByKlb)
+	output, err := test.ExecuteCommand(rebuildCmd, cmdRebuild, "my-kb-test-rebuild")
+	assert.Nil(t, err)
+	assert.Contains(t, output, "1 bindings have been rebuilt")
+	assert.NotContains(t, output, "1 integrations have been rebuilt")
+}
+
+func TestRebuildIntegrationOnly(t *testing.T) {
+	defaultIntegration := nominalIntegration("my-it-test")
+	defaultKB := nominalBinding("my-kb-test-rebuild")
+	itGeneratedByKlb := nominalIntegration("my-kb-test-rebuild")
+	itGeneratedByKlb.Labels = map[string]string{
+		kubernetes.CamelCreatorLabelKind: "Binding",
+	}
+
+	_, rebuildCmd, _ := initializeRebuildOptions(t, &defaultIntegration, &defaultKB, &itGeneratedByKlb)
+	output, err := test.ExecuteCommand(rebuildCmd, cmdRebuild, "my-it-test")
+	assert.Nil(t, err)
+	assert.NotContains(t, output, "1 bindings have been rebuilt")
+	assert.Contains(t, output, "1 integrations have been rebuilt")
+}