You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by GitBox <gi...@apache.org> on 2018/09/19 15:19:54 UTC

[GitHub] nicolaferraro closed pull request #100: Add delete integration option

nicolaferraro closed pull request #100: Add delete integration option
URL: https://github.com/apache/camel-k/pull/100
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pkg/client/cmd/delete.go b/pkg/client/cmd/delete.go
new file mode 100644
index 0000000..78acfc8
--- /dev/null
+++ b/pkg/client/cmd/delete.go
@@ -0,0 +1,114 @@
+/*
+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 (
+	"errors"
+	"fmt"
+	"os"
+	"strconv"
+
+	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+	"github.com/operator-framework/operator-sdk/pkg/sdk"
+	"github.com/spf13/cobra"
+	k8errors "k8s.io/apimachinery/pkg/api/errors"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+)
+
+// NewCmdDelete --
+func newCmdDelete(rootCmdOptions *RootCmdOptions) *cobra.Command {
+	options := deleteCmdOptions{
+		RootCmdOptions: rootCmdOptions,
+	}
+	cmd := cobra.Command{
+		Use:   "delete [integration1] [integration2] ...",
+		Short: "Delete integrations deployed on Kubernetes",
+		RunE:  options.run,
+	}
+	cmd.Flags().BoolVar(&options.deleteAll, "all", false, "Delete all integrations")
+	cmd.ParseFlags(os.Args)
+
+	return &cmd
+}
+
+type deleteCmdOptions struct {
+	*RootCmdOptions
+	deleteAll bool
+}
+
+func (o *deleteCmdOptions) run(cmd *cobra.Command, args []string) error {
+	namespace := o.Namespace
+
+	integrationList := v1alpha1.IntegrationList{
+		TypeMeta: metav1.TypeMeta{
+			APIVersion: v1alpha1.SchemeGroupVersion.String(),
+			Kind:       v1alpha1.IntegrationKind,
+		},
+	}
+
+	if len(args) != 0 && !o.deleteAll {
+		i := 0
+		for i < len(args) {
+			integration := v1alpha1.Integration{
+				TypeMeta: metav1.TypeMeta{
+					Kind:       v1alpha1.IntegrationKind,
+					APIVersion: v1alpha1.SchemeGroupVersion.String(),
+				},
+				ObjectMeta: metav1.ObjectMeta{
+					Namespace: namespace,
+					Name:      args[i],
+				},
+			}
+
+			err := sdk.Delete(&integration)
+			if err != nil {
+				if k8errors.IsNotFound(err) {
+					fmt.Println("Integration " + integration.GetName() + " not found. Skipped.")
+				} else {
+					return err
+				}
+			} else {
+				fmt.Println("Integration " + integration.GetName() + " deleted")
+			}
+			i++
+		}
+	} else if o.deleteAll {
+		//Looks like Operator SDK doesn't support deletion of all objects with one command
+		err := sdk.List(namespace, &integrationList)
+		if err != nil {
+			return err
+		}
+		for _, integration := range integrationList.Items {
+			err := sdk.Delete(&integration)
+			if err != nil {
+				return err
+			}
+		}
+		if len(integrationList.Items) == 0 {
+			fmt.Println("Nothing to delete")
+		} else {
+			fmt.Println(strconv.Itoa(len(integrationList.Items)) + " integration(s) deleted")
+		}
+	} else {
+		err := errors.New("The integration name(s) or --all option must be specified")
+		return err
+
+	}
+
+	return nil
+}
diff --git a/pkg/client/cmd/root.go b/pkg/client/cmd/root.go
index 7fbeeef..4c3e574 100644
--- a/pkg/client/cmd/root.go
+++ b/pkg/client/cmd/root.go
@@ -46,9 +46,9 @@ func NewKamelCommand(ctx context.Context) (*cobra.Command, error) {
 		Context: ctx,
 	}
 	var cmd = cobra.Command{
-		Use:                    "kamel",
-		Short:                  "Kamel is a awesome client tool for running Apache Camel integrations natively on Kubernetes",
-		Long:                   kamelCommandLongDescription,
+		Use:   "kamel",
+		Short: "Kamel is a awesome client tool for running Apache Camel integrations natively on Kubernetes",
+		Long:  kamelCommandLongDescription,
 		BashCompletionFunction: bashCompletionFunction,
 	}
 
@@ -76,6 +76,7 @@ func NewKamelCommand(ctx context.Context) (*cobra.Command, error) {
 	cmd.AddCommand(newCmdVersion())
 	cmd.AddCommand(newCmdRun(&options))
 	cmd.AddCommand(newCmdGet(&options))
+	cmd.AddCommand(newCmdDelete(&options))
 	cmd.AddCommand(newCmdInstall(&options))
 	cmd.AddCommand(newCmdContext(&options))
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services