Refactor time trigger command (#1376)
This commit is contained in:
@@ -40,6 +40,7 @@ import (
|
|||||||
plugincmd "github.com/fission/fission/pkg/fission-cli/cmd/plugin"
|
plugincmd "github.com/fission/fission/pkg/fission-cli/cmd/plugin"
|
||||||
"github.com/fission/fission/pkg/fission-cli/cmd/spec"
|
"github.com/fission/fission/pkg/fission-cli/cmd/spec"
|
||||||
"github.com/fission/fission/pkg/fission-cli/cmd/support"
|
"github.com/fission/fission/pkg/fission-cli/cmd/support"
|
||||||
|
"github.com/fission/fission/pkg/fission-cli/cmd/timetrigger"
|
||||||
"github.com/fission/fission/pkg/fission-cli/cmd/version"
|
"github.com/fission/fission/pkg/fission-cli/cmd/version"
|
||||||
"github.com/fission/fission/pkg/fission-cli/log"
|
"github.com/fission/fission/pkg/fission-cli/log"
|
||||||
"github.com/fission/fission/pkg/info"
|
"github.com/fission/fission/pkg/info"
|
||||||
@@ -172,12 +173,11 @@ func NewCliApp() *cli.App {
|
|||||||
ttFnNameFlag := cli.StringFlag{Name: "function", Usage: "Function name"}
|
ttFnNameFlag := cli.StringFlag{Name: "function", Usage: "Function name"}
|
||||||
ttRoundFlag := cli.IntFlag{Name: "round", Value: 1, Usage: "Get next N rounds of invocation time"}
|
ttRoundFlag := cli.IntFlag{Name: "round", Value: 1, Usage: "Get next N rounds of invocation time"}
|
||||||
ttSubcommands := []cli.Command{
|
ttSubcommands := []cli.Command{
|
||||||
{Name: "create", Aliases: []string{"add"}, Usage: "Create time trigger", Flags: []cli.Flag{ttNameFlag, ttFnNameFlag, fnNamespaceFlag, ttCronFlag, specSaveFlag}, Action: ttCreate},
|
{Name: "create", Aliases: []string{"add"}, Usage: "Create time trigger", Flags: []cli.Flag{ttNameFlag, ttFnNameFlag, fnNamespaceFlag, ttCronFlag, specSaveFlag}, Action: urfavecli.Wrapper(timetrigger.Create)},
|
||||||
{Name: "get", Usage: "Get time trigger", Flags: []cli.Flag{triggerNamespaceFlag}, Action: ttGet},
|
{Name: "update", Usage: "Update time trigger", Flags: []cli.Flag{ttNameFlag, triggerNamespaceFlag, ttCronFlag, ttFnNameFlag}, Action: urfavecli.Wrapper(timetrigger.Update)},
|
||||||
{Name: "update", Usage: "Update time trigger", Flags: []cli.Flag{ttNameFlag, triggerNamespaceFlag, ttCronFlag, ttFnNameFlag}, Action: ttUpdate},
|
{Name: "delete", Usage: "Delete time trigger", Flags: []cli.Flag{ttNameFlag, triggerNamespaceFlag}, Action: urfavecli.Wrapper(timetrigger.Delete)},
|
||||||
{Name: "delete", Usage: "Delete time trigger", Flags: []cli.Flag{ttNameFlag, triggerNamespaceFlag}, Action: ttDelete},
|
{Name: "list", Usage: "List time triggers", Flags: []cli.Flag{triggerNamespaceFlag}, Action: urfavecli.Wrapper(timetrigger.List)},
|
||||||
{Name: "list", Usage: "List time triggers", Flags: []cli.Flag{triggerNamespaceFlag}, Action: ttList},
|
{Name: "showschedule", Aliases: []string{"show"}, Usage: "Show schedule for cron spec", Flags: []cli.Flag{ttCronFlag, ttRoundFlag}, Action: urfavecli.Wrapper(timetrigger.Show)},
|
||||||
{Name: "showschedule", Aliases: []string{"show"}, Usage: "Show schedule for cron spec", Flags: []cli.Flag{ttCronFlag, ttRoundFlag}, Action: ttTest},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Message queue trigger
|
// Message queue trigger
|
||||||
|
|||||||
@@ -0,0 +1,143 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2019 The Fission Authors.
|
||||||
|
|
||||||
|
Licensed 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 timetrigger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"github.com/robfig/cron"
|
||||||
|
uuid "github.com/satori/go.uuid"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
|
||||||
|
fv1 "github.com/fission/fission/pkg/apis/fission.io/v1"
|
||||||
|
"github.com/fission/fission/pkg/controller/client"
|
||||||
|
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||||
|
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||||
|
"github.com/fission/fission/pkg/fission-cli/cmd/spec"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CreateSubCommand struct {
|
||||||
|
client *client.Client
|
||||||
|
trigger *fv1.TimeTrigger
|
||||||
|
}
|
||||||
|
|
||||||
|
func Create(flags cli.Input) error {
|
||||||
|
opts := CreateSubCommand{
|
||||||
|
client: cmd.GetServer(flags),
|
||||||
|
}
|
||||||
|
return opts.do(flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (opts *CreateSubCommand) do(flags cli.Input) error {
|
||||||
|
err := opts.complete(flags)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return opts.run(flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (opts *CreateSubCommand) complete(flags cli.Input) error {
|
||||||
|
name := flags.String("name")
|
||||||
|
if len(name) == 0 {
|
||||||
|
name = uuid.NewV4().String()
|
||||||
|
}
|
||||||
|
|
||||||
|
fnName := flags.String("function")
|
||||||
|
if len(fnName) == 0 {
|
||||||
|
return errors.New("Need a function name to create a trigger, use --function")
|
||||||
|
}
|
||||||
|
|
||||||
|
fnNamespace := flags.String("fnNamespace")
|
||||||
|
|
||||||
|
cronSpec := flags.String("cron")
|
||||||
|
if len(cronSpec) == 0 {
|
||||||
|
return errors.New("Need a cron spec like '0 30 * * * *', '@every 1h30m', or '@hourly'; use --cron")
|
||||||
|
}
|
||||||
|
|
||||||
|
opts.trigger = &fv1.TimeTrigger{
|
||||||
|
Metadata: metav1.ObjectMeta{
|
||||||
|
Name: name,
|
||||||
|
Namespace: fnNamespace,
|
||||||
|
},
|
||||||
|
Spec: fv1.TimeTriggerSpec{
|
||||||
|
Cron: cronSpec,
|
||||||
|
FunctionReference: fv1.FunctionReference{
|
||||||
|
Type: fv1.FunctionReferenceTypeFunctionName,
|
||||||
|
Name: fnName,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (opts *CreateSubCommand) run(flags cli.Input) error {
|
||||||
|
// if we're writing a spec, don't call the API
|
||||||
|
if flags.Bool("spec") {
|
||||||
|
specFile := fmt.Sprintf("timetrigger-%v.yaml", opts.trigger.Metadata.Name)
|
||||||
|
err := spec.SpecSave(*opts.trigger, specFile)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "error creating time trigger spec")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := opts.client.TimeTriggerCreate(opts.trigger)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "error creating Time trigger")
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("trigger '%v' created\n", opts.trigger.Metadata.Name)
|
||||||
|
|
||||||
|
t, err := getAPITimeInfo(opts.client)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = getCronNextNActivationTime(opts.trigger.Spec.Cron, t, 1)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "error passing cron spec examination")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAPITimeInfo(client *client.Client) (time.Time, error) {
|
||||||
|
serverInfo, err := client.ServerInfo()
|
||||||
|
if err != nil {
|
||||||
|
return time.Time{}, errors.Errorf("Error syncing server time information: %v", err)
|
||||||
|
}
|
||||||
|
return serverInfo.ServerTime.CurrentTime, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCronNextNActivationTime(cronSpec string, serverTime time.Time, round int) error {
|
||||||
|
sched, err := cron.Parse(cronSpec)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Current Server Time: \t%v\n", serverTime.Format(time.RFC3339))
|
||||||
|
|
||||||
|
for i := 0; i < round; i++ {
|
||||||
|
serverTime = sched.Next(serverTime)
|
||||||
|
fmt.Printf("Next %v invocation: \t%v\n", i+1, serverTime.Format(time.RFC3339))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2019 The Fission Authors.
|
||||||
|
|
||||||
|
Licensed 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 timetrigger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
"github.com/fission/fission/pkg/controller/client"
|
||||||
|
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||||
|
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DeleteSubCommand struct {
|
||||||
|
client *client.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func Delete(flags cli.Input) error {
|
||||||
|
opts := DeleteSubCommand{
|
||||||
|
client: cmd.GetServer(flags),
|
||||||
|
}
|
||||||
|
return opts.do(flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (opts *DeleteSubCommand) do(flags cli.Input) error {
|
||||||
|
m, err := cmd.GetMetadata("name", "triggerns", flags)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = opts.client.TimeTriggerDelete(m)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "error deleting trigger")
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("trigger '%v' deleted\n", m.Name)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2019 The Fission Authors.
|
||||||
|
|
||||||
|
Licensed 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 timetrigger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"text/tabwriter"
|
||||||
|
|
||||||
|
"github.com/fission/fission/pkg/controller/client"
|
||||||
|
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||||
|
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ListSubCommand struct {
|
||||||
|
client *client.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func List(flags cli.Input) error {
|
||||||
|
opts := ListSubCommand{
|
||||||
|
client: cmd.GetServer(flags),
|
||||||
|
}
|
||||||
|
return opts.do(flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (opts *ListSubCommand) do(flags cli.Input) error {
|
||||||
|
ttNs := flags.String("triggerns")
|
||||||
|
tts, err := opts.client.TimeTriggerList(ttNs)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "list Time triggers")
|
||||||
|
}
|
||||||
|
|
||||||
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
|
||||||
|
|
||||||
|
fmt.Fprintf(w, "%v\t%v\t%v\n", "NAME", "CRON", "FUNCTION_NAME")
|
||||||
|
for _, tt := range tts {
|
||||||
|
fmt.Fprintf(w, "%v\t%v\t%v\n",
|
||||||
|
tt.Metadata.Name, tt.Spec.Cron, tt.Spec.FunctionReference.Name)
|
||||||
|
}
|
||||||
|
w.Flush()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2019 The Fission Authors.
|
||||||
|
|
||||||
|
Licensed 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 timetrigger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
"github.com/fission/fission/pkg/controller/client"
|
||||||
|
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||||
|
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ShowSubCommand struct {
|
||||||
|
client *client.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func Show(flags cli.Input) error {
|
||||||
|
opts := ShowSubCommand{
|
||||||
|
client: cmd.GetServer(flags),
|
||||||
|
}
|
||||||
|
return opts.do(flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (opts *ShowSubCommand) do(flags cli.Input) error {
|
||||||
|
return opts.run(flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (opts *ShowSubCommand) run(flags cli.Input) error {
|
||||||
|
round := flags.Int("round")
|
||||||
|
cronSpec := flags.String("cron")
|
||||||
|
if len(cronSpec) == 0 {
|
||||||
|
return errors.New("need a cron spec like '0 30 * * * *', '@every 1h30m', or '@hourly'; use --cron")
|
||||||
|
}
|
||||||
|
|
||||||
|
t, err := getAPITimeInfo(opts.client)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = getCronNextNActivationTime(cronSpec, t, round)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "error passing cron spec examination")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2019 The Fission Authors.
|
||||||
|
|
||||||
|
Licensed 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 timetrigger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
fv1 "github.com/fission/fission/pkg/apis/fission.io/v1"
|
||||||
|
"github.com/fission/fission/pkg/controller/client"
|
||||||
|
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||||
|
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UpdateSubCommand struct {
|
||||||
|
client *client.Client
|
||||||
|
trigger *fv1.TimeTrigger
|
||||||
|
}
|
||||||
|
|
||||||
|
func Update(flags cli.Input) error {
|
||||||
|
opts := UpdateSubCommand{
|
||||||
|
client: cmd.GetServer(flags),
|
||||||
|
}
|
||||||
|
return opts.do(flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (opts *UpdateSubCommand) do(flags cli.Input) error {
|
||||||
|
err := opts.complete(flags)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return opts.run(flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (opts *UpdateSubCommand) complete(flags cli.Input) error {
|
||||||
|
m, err := cmd.GetMetadata("name", "triggerns", flags)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
tt, err := opts.client.TimeTriggerGet(m)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "error getting time trigger")
|
||||||
|
}
|
||||||
|
|
||||||
|
updated := false
|
||||||
|
newCron := flags.String("cron")
|
||||||
|
if len(newCron) != 0 {
|
||||||
|
tt.Spec.Cron = newCron
|
||||||
|
updated = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO : During update, function has to be in the same ns as the trigger object
|
||||||
|
// but since we are not checking this for other triggers too, not sure if we need a check here.
|
||||||
|
|
||||||
|
fnName := flags.String("function")
|
||||||
|
if len(fnName) > 0 {
|
||||||
|
tt.Spec.FunctionReference.Name = fnName
|
||||||
|
updated = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if !updated {
|
||||||
|
return errors.New("nothing to update. Use --cron or --function")
|
||||||
|
}
|
||||||
|
|
||||||
|
opts.trigger = tt
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (opts *UpdateSubCommand) run(flags cli.Input) error {
|
||||||
|
_, err := opts.client.TimeTriggerUpdate(opts.trigger)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "error updating Time trigger")
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Time trigger '%v' updated\n", opts.trigger.Metadata.Name)
|
||||||
|
|
||||||
|
t, err := getAPITimeInfo(opts.client)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = getCronNextNActivationTime(opts.trigger.Spec.Cron, t, 1)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "error passing cron spec examination")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,212 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2017 The Fission Authors.
|
|
||||||
|
|
||||||
Licensed 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 fission_cli
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"text/tabwriter"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/robfig/cron"
|
|
||||||
"github.com/satori/go.uuid"
|
|
||||||
"github.com/urfave/cli"
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
||||||
|
|
||||||
fv1 "github.com/fission/fission/pkg/apis/fission.io/v1"
|
|
||||||
"github.com/fission/fission/pkg/controller/client"
|
|
||||||
"github.com/fission/fission/pkg/fission-cli/cmd/spec"
|
|
||||||
"github.com/fission/fission/pkg/fission-cli/log"
|
|
||||||
"github.com/fission/fission/pkg/fission-cli/util"
|
|
||||||
)
|
|
||||||
|
|
||||||
func getAPITimeInfo(client *client.Client) time.Time {
|
|
||||||
serverInfo, err := client.ServerInfo()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(fmt.Sprintf("Error syncing server time information: %v", err))
|
|
||||||
}
|
|
||||||
return serverInfo.ServerTime.CurrentTime
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCronNextNActivationTime(cronSpec string, serverTime time.Time, round int) error {
|
|
||||||
sched, err := cron.Parse(cronSpec)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("Current Server Time: \t%v\n", serverTime.Format(time.RFC3339))
|
|
||||||
|
|
||||||
for i := 0; i < round; i++ {
|
|
||||||
serverTime = sched.Next(serverTime)
|
|
||||||
fmt.Printf("Next %v invocation: \t%v\n", i+1, serverTime.Format(time.RFC3339))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ttCreate(c *cli.Context) error {
|
|
||||||
client := util.GetApiClient(c.GlobalString("server"))
|
|
||||||
|
|
||||||
name := c.String("name")
|
|
||||||
if len(name) == 0 {
|
|
||||||
name = uuid.NewV4().String()
|
|
||||||
}
|
|
||||||
fnName := c.String("function")
|
|
||||||
if len(fnName) == 0 {
|
|
||||||
log.Fatal("Need a function name to create a trigger, use --function")
|
|
||||||
}
|
|
||||||
|
|
||||||
fnNamespace := c.String("fnNamespace")
|
|
||||||
|
|
||||||
cronSpec := c.String("cron")
|
|
||||||
if len(cronSpec) == 0 {
|
|
||||||
log.Fatal("Need a cron spec like '0 30 * * * *', '@every 1h30m', or '@hourly'; use --cron")
|
|
||||||
}
|
|
||||||
|
|
||||||
tt := &fv1.TimeTrigger{
|
|
||||||
Metadata: metav1.ObjectMeta{
|
|
||||||
Name: name,
|
|
||||||
Namespace: fnNamespace,
|
|
||||||
},
|
|
||||||
Spec: fv1.TimeTriggerSpec{
|
|
||||||
Cron: cronSpec,
|
|
||||||
FunctionReference: fv1.FunctionReference{
|
|
||||||
Type: fv1.FunctionReferenceTypeFunctionName,
|
|
||||||
Name: fnName,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// if we're writing a spec, don't call the API
|
|
||||||
if c.Bool("spec") {
|
|
||||||
specFile := fmt.Sprintf("timetrigger-%v.yaml", name)
|
|
||||||
err := spec.SpecSave(*tt, specFile)
|
|
||||||
util.CheckErr(err, "create time trigger spec")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := client.TimeTriggerCreate(tt)
|
|
||||||
util.CheckErr(err, "create Time trigger")
|
|
||||||
|
|
||||||
fmt.Printf("trigger '%v' created\n", name)
|
|
||||||
|
|
||||||
err = getCronNextNActivationTime(cronSpec, getAPITimeInfo(client), 1)
|
|
||||||
util.CheckErr(err, "pass cron spec examination")
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func ttGet(c *cli.Context) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ttUpdate(c *cli.Context) error {
|
|
||||||
client := util.GetApiClient(c.GlobalString("server"))
|
|
||||||
ttName := c.String("name")
|
|
||||||
if len(ttName) == 0 {
|
|
||||||
log.Fatal("Need name of trigger, use --name")
|
|
||||||
}
|
|
||||||
ttNs := c.String("triggerns")
|
|
||||||
|
|
||||||
tt, err := client.TimeTriggerGet(&metav1.ObjectMeta{
|
|
||||||
Name: ttName,
|
|
||||||
Namespace: ttNs,
|
|
||||||
})
|
|
||||||
util.CheckErr(err, "get time trigger")
|
|
||||||
|
|
||||||
updated := false
|
|
||||||
newCron := c.String("cron")
|
|
||||||
if len(newCron) != 0 {
|
|
||||||
tt.Spec.Cron = newCron
|
|
||||||
updated = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO : During update, function has to be in the same ns as the trigger object
|
|
||||||
// but since we are not checking this for other triggers too, not sure if we need a check here.
|
|
||||||
|
|
||||||
fnName := c.String("function")
|
|
||||||
if len(fnName) > 0 {
|
|
||||||
tt.Spec.FunctionReference.Name = fnName
|
|
||||||
updated = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if !updated {
|
|
||||||
log.Fatal("Nothing to update. Use --cron or --function.")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = client.TimeTriggerUpdate(tt)
|
|
||||||
util.CheckErr(err, "update Time trigger")
|
|
||||||
|
|
||||||
fmt.Printf("trigger '%v' updated\n", ttName)
|
|
||||||
|
|
||||||
err = getCronNextNActivationTime(newCron, getAPITimeInfo(client), 1)
|
|
||||||
util.CheckErr(err, "pass cron spec examination")
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ttDelete(c *cli.Context) error {
|
|
||||||
client := util.GetApiClient(c.GlobalString("server"))
|
|
||||||
ttName := c.String("name")
|
|
||||||
if len(ttName) == 0 {
|
|
||||||
log.Fatal("Need name of trigger to delete, use --name")
|
|
||||||
}
|
|
||||||
ttNs := c.String("triggerns")
|
|
||||||
|
|
||||||
err := client.TimeTriggerDelete(&metav1.ObjectMeta{
|
|
||||||
Name: ttName,
|
|
||||||
Namespace: ttNs,
|
|
||||||
})
|
|
||||||
util.CheckErr(err, "delete trigger")
|
|
||||||
|
|
||||||
fmt.Printf("trigger '%v' deleted\n", ttName)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ttList(c *cli.Context) error {
|
|
||||||
client := util.GetApiClient(c.GlobalString("server"))
|
|
||||||
ttNs := c.String("triggerns")
|
|
||||||
|
|
||||||
tts, err := client.TimeTriggerList(ttNs)
|
|
||||||
util.CheckErr(err, "list Time triggers")
|
|
||||||
|
|
||||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
|
|
||||||
|
|
||||||
fmt.Fprintf(w, "%v\t%v\t%v\n", "NAME", "CRON", "FUNCTION_NAME")
|
|
||||||
for _, tt := range tts {
|
|
||||||
fmt.Fprintf(w, "%v\t%v\t%v\n",
|
|
||||||
tt.Metadata.Name, tt.Spec.Cron, tt.Spec.FunctionReference.Name)
|
|
||||||
}
|
|
||||||
w.Flush()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ttTest(c *cli.Context) error {
|
|
||||||
client := util.GetApiClient(c.GlobalString("server"))
|
|
||||||
|
|
||||||
round := c.Int("round")
|
|
||||||
cronSpec := c.String("cron")
|
|
||||||
if len(cronSpec) == 0 {
|
|
||||||
log.Fatal("Need a cron spec like '0 30 * * * *', '@every 1h30m', or '@hourly'; use --cron")
|
|
||||||
}
|
|
||||||
|
|
||||||
err := getCronNextNActivationTime(cronSpec, getAPITimeInfo(client), round)
|
|
||||||
util.CheckErr(err, "pass cron spec examination")
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user