-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add sub command kamel kamelet for get and delete #2312
- Loading branch information
Showing
7 changed files
with
333 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
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 ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newCmdKamelet(rootCmdOptions *RootCmdOptions) *cobra.Command { | ||
cmd := cobra.Command{ | ||
Use: "kamelet", | ||
Short: "Configure a Kamelet", | ||
Long: `Configure a Kamelet.`, | ||
} | ||
|
||
cmd.AddCommand(cmdOnly(newKameletGetCmd(rootCmdOptions))) | ||
cmd.AddCommand(cmdOnly(newKameletDeleteCmd(rootCmdOptions))) | ||
|
||
return &cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
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" | ||
|
||
"github.com/spf13/cobra" | ||
k8errors "k8s.io/apimachinery/pkg/api/errors" | ||
|
||
k8sclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1" | ||
) | ||
|
||
func newKameletDeleteCmd(rootCmdOptions *RootCmdOptions) (*cobra.Command, *kameletDeleteCommandOptions) { | ||
options := kameletDeleteCommandOptions{ | ||
RootCmdOptions: rootCmdOptions, | ||
} | ||
|
||
cmd := cobra.Command{ | ||
Use: "delete <name>", | ||
Short: "Delete a Kamelet", | ||
Long: `Delete a Kamelet.`, | ||
PreRunE: decode(&options), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if err := options.validate(args); err != nil { | ||
return err | ||
} | ||
if err := options.run(args); err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().Bool("all", false, "Delete all Kamelets") | ||
|
||
return &cmd, &options | ||
} | ||
|
||
type kameletDeleteCommandOptions struct { | ||
*RootCmdOptions | ||
All bool `mapstructure:"all"` | ||
} | ||
|
||
func (command *kameletDeleteCommandOptions) validate(args []string) error { | ||
if command.All && len(args) > 0 { | ||
return errors.New("invalid combination: both all flag and named kamelets are set") | ||
} | ||
if !command.All && len(args) == 0 { | ||
return errors.New("invalid combination: neither all flag nor named kamelets are set") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (command *kameletDeleteCommandOptions) run(args []string) error { | ||
names := args | ||
|
||
c, err := command.GetCmdClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if command.All { | ||
klList := v1alpha1.NewKameletList() | ||
if err := c.List(command.Context, &klList, k8sclient.InNamespace(command.Namespace)); err != nil { | ||
return err | ||
} | ||
names = make([]string, 0, len(klList.Items)) | ||
for _, kl := range klList.Items { | ||
// only include non-bundled, non-readonly kamelets | ||
if kl.Labels[v1alpha1.KameletBundledLabel] != "true" && kl.Labels[v1alpha1.KameletReadOnlyLabel] != "true" { | ||
names = append(names, kl.Name) | ||
} | ||
} | ||
} | ||
|
||
for _, name := range names { | ||
if err := command.delete(name); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (command *kameletDeleteCommandOptions) delete(name string) error { | ||
c, err := command.GetCmdClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
kl := v1alpha1.NewKamelet(command.Namespace, name) | ||
key := k8sclient.ObjectKey{ | ||
Namespace: command.Namespace, | ||
Name: name, | ||
} | ||
err = c.Get(command.Context, key, &kl) | ||
if err != nil { | ||
if k8errors.IsNotFound(err) { | ||
return fmt.Errorf("no kamelet found with name \"%s\"", name) | ||
} else { | ||
return err | ||
} | ||
} | ||
|
||
// check that it is not a bundled nor read-only one which is supposed to belong to platform | ||
// thus not managed by the end user | ||
if kl.Labels[v1alpha1.KameletBundledLabel] == "true" || kl.Labels[v1alpha1.KameletReadOnlyLabel] == "true" { | ||
// skip platform Kamelets while deleting all Kamelets | ||
if command.All { | ||
return nil | ||
} | ||
return fmt.Errorf("kamelet \"%s\" is not editable", name) | ||
} | ||
|
||
err = c.Delete(command.Context, &kl) | ||
if err != nil { | ||
if k8errors.IsNotFound(err) { | ||
return fmt.Errorf("no kamelet found with name \"%s\"", name) | ||
} else { | ||
return fmt.Errorf("error deleting kamelet \"%s\", %s", name, err) | ||
} | ||
} | ||
fmt.Printf("kamelet \"%s\" has been deleted\n", name) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
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" | ||
"strings" | ||
"text/tabwriter" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
k8sclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1" | ||
) | ||
|
||
func newKameletGetCmd(rootCmdOptions *RootCmdOptions) (*cobra.Command, *kameletGetCommandOptions) { | ||
options := kameletGetCommandOptions{ | ||
RootCmdOptions: rootCmdOptions, | ||
} | ||
|
||
cmd := cobra.Command{ | ||
Use: "get", | ||
Short: "Get defined Kamelet", | ||
Long: `Get defined Kamelet.`, | ||
PreRunE: decode(&options), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if err := options.validate(cmd, args); err != nil { | ||
return err | ||
} | ||
if err := options.run(cmd); err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().Bool("sink", false, "Show only sink Kamelets") | ||
cmd.Flags().Bool("source", false, "Show only source Kamelets") | ||
cmd.Flags().Bool("action", false, "Show only action Kamelets") | ||
cmd.Flags().String("group", "", "Filters Kamelets by group") | ||
cmd.Flags().Bool("bundled", true, "Includes bundled Kamelets") | ||
cmd.Flags().Bool("read-only", true, "Includes read-only Kamelets") | ||
|
||
return &cmd, &options | ||
} | ||
|
||
type kameletGetCommandOptions struct { | ||
*RootCmdOptions | ||
Sink bool `mapstructure:"sink"` | ||
Source bool `mapstructure:"source"` | ||
Action bool `mapstructure:"action"` | ||
Group string `mapstructure:"group"` | ||
Bundled bool `mapstructure:"bundled"` | ||
ReadOnly bool `mapstructure:"read-only"` | ||
} | ||
|
||
func (command *kameletGetCommandOptions) validate(cmd *cobra.Command, args []string) error { | ||
count := 0 | ||
for _, b := range []bool{command.Sink, command.Source, command.Action} { | ||
if b { | ||
count++ | ||
} | ||
} | ||
|
||
if count > 1 { | ||
return errors.New("invalid combination: flags --sink, --source, and --action are mutually exclusive") | ||
} | ||
return nil | ||
} | ||
|
||
func (command *kameletGetCommandOptions) run(cmd *cobra.Command) error { | ||
c, err := command.GetCmdClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
klList := v1alpha1.NewKameletList() | ||
if err := c.List(command.Context, &klList, k8sclient.InNamespace(command.Namespace)); err != nil { | ||
return err | ||
} | ||
|
||
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 8, 1, '\t', 0) | ||
fmt.Fprintln(w, "NAME\tPHASE\tTYPE\tGROUP\tBUNDLED\tREAD ONLY\tTITLE") | ||
for _, kl := range klList.Items { | ||
klType := kl.Labels[v1alpha1.KameletTypeLabel] | ||
group := kl.Annotations[v1alpha1.KameletGroupLabel] | ||
bundled := kl.Labels[v1alpha1.KameletBundledLabel] | ||
readOnly := kl.Labels[v1alpha1.KameletReadOnlyLabel] | ||
|
||
if command.Sink && klType != v1alpha1.KameletTypeSink { | ||
continue | ||
} | ||
if command.Source && klType != v1alpha1.KameletTypeSource { | ||
continue | ||
} | ||
if command.Action && klType != v1alpha1.KameletTypeAction { | ||
continue | ||
} | ||
if command.Group != "" && !strings.EqualFold(command.Group, group) { | ||
continue | ||
} | ||
if !command.Bundled && bundled == "true" { | ||
continue | ||
} | ||
if !command.ReadOnly && readOnly == "true" { | ||
continue | ||
} | ||
|
||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", | ||
kl.Name, | ||
string(kl.Status.Phase), | ||
klType, | ||
group, | ||
bundled, | ||
readOnly, | ||
kl.Spec.Definition.Title) | ||
} | ||
w.Flush() | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters