List/Delete HTTP triggers by function (#1327)
Previously, a user has to delete HTTP triggers point to the same function one by one. This PR adds new flag --function to list & delete commands. So that user can delete or list all triggers with the same function. However, this feature is not working for triggers with canary deployment setup.
This commit is contained in:
@@ -23,6 +23,7 @@ import (
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/hashicorp/go-multierror"
|
||||
"github.com/satori/go.uuid"
|
||||
"github.com/urfave/cli"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -289,25 +290,56 @@ func htUpdate(c *cli.Context) error {
|
||||
|
||||
func htDelete(c *cli.Context) error {
|
||||
client := util.GetApiClient(c.GlobalString("server"))
|
||||
|
||||
htName := c.String("name")
|
||||
if len(htName) == 0 {
|
||||
log.Fatal("Need name of trigger to delete, use --name")
|
||||
fnName := c.String("function")
|
||||
if len(htName) == 0 && len(fnName) == 0 {
|
||||
log.Fatal("Need --name or --function")
|
||||
} else if len(htName) > 0 && len(fnName) > 0 {
|
||||
log.Fatal("Need either of --name or --function and not both arguments")
|
||||
}
|
||||
|
||||
triggerNamespace := c.String("triggerNamespace")
|
||||
|
||||
err := client.HTTPTriggerDelete(&metav1.ObjectMeta{
|
||||
Name: htName,
|
||||
Namespace: triggerNamespace,
|
||||
})
|
||||
util.CheckErr(err, "delete trigger")
|
||||
triggers, err := client.HTTPTriggerList(triggerNamespace)
|
||||
util.CheckErr(err, "get HTTP trigger list")
|
||||
|
||||
var triggersToDelete []string
|
||||
|
||||
if len(fnName) > 0 {
|
||||
for _, trigger := range triggers {
|
||||
// TODO: delete canary http triggers as well.
|
||||
if trigger.Spec.FunctionReference.Name == fnName {
|
||||
triggersToDelete = append(triggersToDelete, trigger.Metadata.Name)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
triggersToDelete = []string{htName}
|
||||
}
|
||||
|
||||
errs := &multierror.Error{}
|
||||
|
||||
for _, name := range triggersToDelete {
|
||||
err := client.HTTPTriggerDelete(&metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: triggerNamespace,
|
||||
})
|
||||
if err != nil {
|
||||
errs = multierror.Append(errs, err)
|
||||
} else {
|
||||
fmt.Printf("trigger '%v' deleted\n", name)
|
||||
}
|
||||
}
|
||||
|
||||
util.CheckErr(errs.ErrorOrNil(), "delete trigger(s)")
|
||||
|
||||
fmt.Printf("trigger '%v' deleted\n", htName)
|
||||
return nil
|
||||
}
|
||||
|
||||
func htList(c *cli.Context) error {
|
||||
client := util.GetApiClient(c.GlobalString("server"))
|
||||
triggerNamespace := c.String("triggerNamespace")
|
||||
fnName := c.String("function")
|
||||
|
||||
hts, err := client.HTTPTriggerList(triggerNamespace)
|
||||
util.CheckErr(err, "list HTTP triggers")
|
||||
@@ -315,9 +347,13 @@ func htList(c *cli.Context) error {
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
|
||||
|
||||
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\n", "NAME", "METHOD", "HOST", "URL", "INGRESS", "FUNCTION_NAME")
|
||||
|
||||
for _, ht := range hts {
|
||||
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\n",
|
||||
ht.Metadata.Name, ht.Spec.Method, ht.Spec.Host, ht.Spec.RelativeURL, ht.Spec.CreateIngress, ht.Spec.FunctionReference.Name)
|
||||
// TODO: list canary http triggers as well.
|
||||
if len(fnName) == 0 || (len(fnName) > 0 && fnName == ht.Spec.FunctionReference.Name) {
|
||||
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\n",
|
||||
ht.Metadata.Name, ht.Spec.Method, ht.Spec.Host, ht.Spec.RelativeURL, ht.Spec.CreateIngress, ht.Spec.FunctionReference.Name)
|
||||
}
|
||||
}
|
||||
w.Flush()
|
||||
|
||||
|
||||
@@ -148,14 +148,15 @@ func NewCliApp() *cli.App {
|
||||
htIngressRuleFlag := cli.StringFlag{Name: "ingressrule", Usage: "Host for Ingress rule: --ingressrule host=path (the format of host/path depends on what ingress controller you used)"}
|
||||
htIngressAnnotationFlag := cli.StringSliceFlag{Name: "ingressannotation", Usage: "Annotation for Ingress: --ingressannotation key=value (the format of annotation depends on what ingress controller you used)"}
|
||||
htIngressTLSFlag := cli.StringFlag{Name: "ingresstls", Usage: "Name of the Secret contains TLS key and crt for Ingress (the usability of TLS features depends on what ingress controller you used)"}
|
||||
htFnNameFlag := cli.StringSliceFlag{Name: "function", Usage: "Name(s) of the function for this trigger. If 2 functions are supplied with this flag, traffic gets routed to them based on weights supplied with --weight flag."}
|
||||
htFnNameFlag := cli.StringSliceFlag{Name: "function", Usage: "Name(s) of the function for this trigger. (If 2 functions are supplied with this flag, traffic gets routed to them based on weights supplied with --weight flag.)"}
|
||||
htFnWeightFlag := cli.IntSliceFlag{Name: "weight", Usage: "Weight for each function supplied with --function flag, in the same order. Used for canary deployment"}
|
||||
htFnFilterFlag := cli.StringFlag{Name: "function", Usage: "Name of the function for trigger(s)"}
|
||||
htSubcommands := []cli.Command{
|
||||
{Name: "create", Aliases: []string{"add"}, Usage: "Create HTTP trigger", Flags: []cli.Flag{htNameFlag, htMethodFlag, htUrlFlag, htFnNameFlag, htIngressRuleFlag, htIngressAnnotationFlag, htIngressTLSFlag, htIngressFlag, fnNamespaceFlag, specSaveFlag, htFnWeightFlag, htHostFlag}, Action: htCreate},
|
||||
{Name: "get", Usage: "Get HTTP trigger", Flags: []cli.Flag{htNameFlag}, Action: htGet},
|
||||
{Name: "update", Usage: "Update HTTP trigger", Flags: []cli.Flag{htNameFlag, triggerNamespaceFlag, htFnNameFlag, htIngressRuleFlag, htIngressAnnotationFlag, htIngressTLSFlag, htIngressFlag, htFnWeightFlag, htHostFlag}, Action: htUpdate},
|
||||
{Name: "delete", Usage: "Delete HTTP trigger", Flags: []cli.Flag{htNameFlag, triggerNamespaceFlag}, Action: htDelete},
|
||||
{Name: "list", Usage: "List HTTP triggers", Flags: []cli.Flag{triggerNamespaceFlag}, Action: htList},
|
||||
{Name: "delete", Usage: "Delete HTTP trigger", Flags: []cli.Flag{htNameFlag, triggerNamespaceFlag, htFnFilterFlag}, Action: htDelete},
|
||||
{Name: "list", Usage: "List HTTP triggers", Flags: []cli.Flag{triggerNamespaceFlag, htFnFilterFlag}, Action: htList},
|
||||
}
|
||||
|
||||
// timetriggers
|
||||
|
||||
Reference in New Issue
Block a user