Refactor record command (#1378)

This commit is contained in:
Ta-Ching Chen
2019-11-05 10:30:16 +08:00
committed by GitHub
parent c33f1e112e
commit b0d27ee5d2
16 changed files with 605 additions and 344 deletions
-1
View File
@@ -64,7 +64,6 @@ func (opts *CreateSubCommand) do(flags cli.Input) error {
return opts.run(flags)
}
// complete creates a environment objects and populates it with default value and CLI inputs.
func (opts *CreateSubCommand) complete(flags cli.Input) error {
fnNamespace := flags.String("fnNamespace")
envNamespace := flags.String("envNamespace")
+1 -2
View File
@@ -18,10 +18,10 @@ package httptrigger
import (
"fmt"
"github.com/pkg/errors"
"net/http"
"strings"
"github.com/pkg/errors"
"github.com/satori/go.uuid"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -55,7 +55,6 @@ func (opts *CreateSubCommand) do(flags cli.Input) error {
return opts.run(flags)
}
// complete creates a environment objects and populates it with default value and CLI inputs.
func (opts *CreateSubCommand) complete(flags cli.Input) error {
functionList := flags.StringSlice("function")
functionWeightsList := flags.IntSlice("weight")
@@ -50,7 +50,6 @@ func (opts *DeleteSubCommand) do(flags cli.Input) error {
return opts.run(flags)
}
// complete creates a environment objects and populates it with default value and CLI inputs.
func (opts *DeleteSubCommand) complete(flags cli.Input) error {
opts.triggerName = flags.String("name")
opts.functionName = flags.String("function")
-1
View File
@@ -52,7 +52,6 @@ func (opts *GetSubCommand) do(flags cli.Input) error {
return opts.run(flags)
}
// complete creates a environment objects and populates it with default value and CLI inputs.
func (opts *GetSubCommand) complete(flags cli.Input) error {
opts.trigger = flags.String("name")
opts.namespace = flags.String("fnNamespace")
-1
View File
@@ -46,7 +46,6 @@ func (opts *ListSubCommand) do(flags cli.Input) error {
return opts.run(flags)
}
// complete creates a environment objects and populates it with default value and CLI inputs.
func (opts *ListSubCommand) complete(flags cli.Input) error {
opts.triggerNamespace = flags.String("triggerNamespace")
opts.filterFunctionName = flags.String("function")
@@ -50,7 +50,6 @@ func (opts *UpdateSubCommand) do(flags cli.Input) error {
return opts.run(flags)
}
// complete creates a environment objects and populates it with default value and CLI inputs.
func (opts *UpdateSubCommand) complete(flags cli.Input) error {
htName := flags.String("name")
if len(htName) == 0 {
+120
View File
@@ -0,0 +1,120 @@
/*
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 recorder
import (
"fmt"
"strings"
"github.com/pkg/errors"
"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
recorder *fv1.Recorder
}
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 {
recName := flags.String("name")
if len(recName) == 0 {
recName = uuid.NewV4().String()
}
fnName := flags.String("function")
triggersOriginal := flags.StringSlice("trigger")
// Function XOR triggers can be given
if len(fnName) == 0 && len(triggersOriginal) == 0 {
return errors.New("Need to specify at least one function or one trigger, use --function, --trigger")
}
if len(fnName) != 0 && len(triggersOriginal) != 0 {
return errors.New("Can specify either one function or one or more triggers, but not both")
}
// TODO: Validate here or elsewhere that all triggers belong to the same namespace
var triggers []string
if len(triggersOriginal) != 0 {
ts := strings.Split(triggersOriginal[0], ",")
for _, name := range ts {
if len(name) > 0 {
triggers = append(triggers, name)
}
}
}
// TODO: Define appropriate set of policies and defaults
//retPolicy := flags.String("retention")
//evictPolicy := flags.String("eviction")
opts.recorder = &fv1.Recorder{
Metadata: metav1.ObjectMeta{
Name: recName,
Namespace: "default",
},
Spec: fv1.RecorderSpec{
Name: recName,
Function: fnName,
Triggers: triggers,
RetentionPolicy: "Permanent", // TODO: Implement customizable policies for expiration of records
EvictionPolicy: "None",
Enabled: true,
},
}
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("recorder-%v.yaml", opts.recorder.Metadata.Name)
err := spec.SpecSave(*opts.recorder, specFile)
if err != nil {
return errors.Wrap(err, "error creating recorder spec")
}
return nil
}
_, err := opts.client.RecorderCreate(opts.recorder)
if err != nil {
return errors.Wrap(err, "error creating recorder")
}
fmt.Printf("recorder '%s' created\n", opts.recorder.Metadata.Name)
return nil
}
+66
View File
@@ -0,0 +1,66 @@
/*
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 recorder
import (
"fmt"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/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 DeleteSubCommand struct {
client *client.Client
metadata *metav1.ObjectMeta
}
func Delete(flags cli.Input) error {
opts := DeleteSubCommand{
client: cmd.GetServer(flags),
}
return opts.do(flags)
}
func (opts *DeleteSubCommand) do(flags cli.Input) error {
err := opts.complete(flags)
if err != nil {
return err
}
return opts.run(flags)
}
func (opts *DeleteSubCommand) complete(flags cli.Input) error {
m, err := cmd.GetMetadata("name", "recorderns", flags)
if err != nil {
return err
}
opts.metadata = m
return nil
}
func (opts *DeleteSubCommand) run(flags cli.Input) error {
err := opts.client.RecorderDelete(opts.metadata)
if err != nil {
return errors.Wrap(err, "error deleting recorder")
}
fmt.Printf("recorder '%v' deleted\n", opts.metadata.Name)
return nil
}
+78
View File
@@ -0,0 +1,78 @@
/*
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 recorder
import (
"fmt"
"os"
"text/tabwriter"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/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 GetSubCommand struct {
client *client.Client
name string
}
func Get(flags cli.Input) error {
opts := GetSubCommand{
client: cmd.GetServer(flags),
}
return opts.do(flags)
}
func (opts *GetSubCommand) do(flags cli.Input) error {
err := opts.complete(flags)
if err != nil {
return err
}
return opts.run(flags)
}
func (opts *GetSubCommand) complete(flags cli.Input) error {
opts.name = flags.String("name")
if len(opts.name) <= 0 {
return errors.New("need a recorder name, use --name")
}
return nil
}
func (opts *GetSubCommand) run(flags cli.Input) error {
recorder, err := opts.client.RecorderGet(&metav1.ObjectMeta{
Name: opts.name,
Namespace: "default",
})
if err != nil {
return errors.Wrap(err, "error getting recorder")
}
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", "ENABLED", "FUNCTION", "TRIGGERS", "RETENTION_POLICY", "EVICTION_POLICY")
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\n",
recorder.Metadata.Name, recorder.Spec.Enabled, recorder.Spec.Function, recorder.Spec.Triggers, recorder.Spec.RetentionPolicy, recorder.Spec.EvictionPolicy)
w.Flush()
return nil
}
+62
View File
@@ -0,0 +1,62 @@
/*
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 recorder
import (
"fmt"
"os"
"text/tabwriter"
"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 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 {
return opts.run(flags)
}
func (opts *ListSubCommand) run(flags cli.Input) error {
recorders, err := opts.client.RecorderList("default")
if err != nil {
return errors.Wrap(err, "error listing recorders")
}
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", "ENABLED", "FUNCTIONS", "TRIGGERS", "RETENTION_POLICY", "EVICTION_POLICY")
for _, r := range recorders {
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\n",
r.Metadata.Name, r.Spec.Enabled, r.Spec.Function, r.Spec.Triggers, r.Spec.RetentionPolicy, r.Spec.EvictionPolicy)
}
w.Flush()
return nil
}
+141
View File
@@ -0,0 +1,141 @@
/*
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 recorder
import (
"fmt"
"strings"
"github.com/pkg/errors"
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"
)
type UpdateSubCommand struct {
client *client.Client
recorder *fv1.Recorder
}
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 {
recName := flags.String("name")
enable := flags.Bool("enable")
disable := flags.Bool("disable")
//retPolicy := flags.String("retention")
//evictPolicy := flags.String("eviction")
triggers := flags.StringSlice("trigger")
function := flags.String("function")
if enable && disable {
return errors.New("Cannot enable and disable a recorder simultaneously.")
}
// Prevent enable or disable while trying to update other fields. These flags must be standalone.
if enable || disable {
if len(triggers) > 0 || len(function) > 0 {
return errors.New("Enabling or disabling a recorder with other (non-name) flags set is not supported.")
}
} else if len(triggers) == 0 && len(function) == 0 {
return errors.New("Need to specify either a function or trigger(s) for this recorder")
}
if len(recName) == 0 {
return errors.New("Need name of recorder, use --name")
}
recorder, err := opts.client.RecorderGet(&metav1.ObjectMeta{
Name: recName,
Namespace: "default",
})
if err != nil {
return errors.Wrap(err, "error getting recorder")
}
updated := false
// TODO: Additional validation on type of supported retention policy, eviction policy
//if len(retPolicy) > 0 {
// recorder.Spec.RetentionPolicy = retPolicy
// updated = true
//}
//if len(evictPolicy) > 0 {
// recorder.Spec.EvictionPolicy = evictPolicy
// updated = true
//}
if enable {
recorder.Spec.Enabled = true
updated = true
}
if disable {
recorder.Spec.Enabled = false
updated = true
}
if len(triggers) > 0 {
var newTriggers []string
triggs := strings.Split(triggers[0], ",")
for _, name := range triggs {
if len(name) > 0 {
newTriggers = append(newTriggers, name)
}
}
recorder.Spec.Triggers = newTriggers
updated = true
}
if len(function) > 0 {
recorder.Spec.Function = function
updated = true
}
if !updated {
return errors.New("Nothing to update. Use --function, --triggers, --enable or --disable")
}
opts.recorder = recorder
return nil
}
func (opts *UpdateSubCommand) run(flags cli.Input) error {
_, err := opts.client.RecorderUpdate(opts.recorder)
if err != nil {
return errors.Wrap(err, "error updating recorder")
}
fmt.Printf("recorder '%v' updated\n", opts.recorder.Metadata.Name)
return nil
}
+172
View File
@@ -0,0 +1,172 @@
/*
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 records
import (
"fmt"
"os"
"text/tabwriter"
"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"
"github.com/fission/fission/pkg/fission-cli/util"
redisCache "github.com/fission/fission/pkg/redis/build/gen"
)
type ViewSubCommand struct {
client *client.Client
}
func View(flags cli.Input) error {
opts := ViewSubCommand{
client: cmd.GetServer(flags),
}
return opts.do(flags)
}
func (opts *ViewSubCommand) do(flags cli.Input) error {
return opts.run(flags)
}
func (opts *ViewSubCommand) run(flags cli.Input) error {
var verbosity int
if flags.Bool("v") && flags.Bool("vv") {
return errors.New("conflicting verbosity levels, use either --v or --vv")
}
if flags.Bool("v") {
verbosity = 1
}
if flags.Bool("vv") {
verbosity = 2
}
function := flags.String("function")
trigger := flags.String("trigger")
from := flags.String("from")
to := flags.String("to")
//Refuse multiple filters for now
if multipleFiltersSpecified(function, trigger, from+to) {
return errors.New("maximum of one filter is currently supported, either --function, --trigger, or --from,--to")
}
if len(function) != 0 {
return recordsByFunction(function, verbosity, flags)
}
if len(trigger) != 0 {
return recordsByTrigger(trigger, verbosity, flags)
}
if len(from) != 0 && len(to) != 0 {
return recordsByTime(from, to, verbosity, flags)
}
err := recordsAll(verbosity, flags)
if err != nil {
return errors.Wrap(err, "error viewing records")
}
return nil
}
func recordsAll(verbosity int, flags cli.Input) error {
fc := util.GetApiClient(flags.GlobalString("server"))
records, err := fc.RecordsAll()
if err != nil {
return errors.Wrap(err, "error viewing records")
}
showRecords(records, verbosity)
return nil
}
func recordsByTrigger(trigger string, verbosity int, flags cli.Input) error {
fc := util.GetApiClient(flags.GlobalString("server"))
records, err := fc.RecordsByTrigger(trigger)
if err != nil {
return errors.Wrap(err, "error viewing records")
}
showRecords(records, verbosity)
return nil
}
// TODO: More accurate function name (function filter)
func recordsByFunction(function string, verbosity int, flags cli.Input) error {
fc := util.GetApiClient(flags.GlobalString("server"))
records, err := fc.RecordsByFunction(function)
if err != nil {
return errors.Wrap(err, "error viewing records")
}
showRecords(records, verbosity)
return nil
}
func recordsByTime(from string, to string, verbosity int, flags cli.Input) error {
fc := util.GetApiClient(flags.GlobalString("server"))
records, err := fc.RecordsByTime(from, to)
if err != nil {
return errors.Wrap(err, "error viewing records")
}
showRecords(records, verbosity)
return nil
}
func showRecords(records []*redisCache.RecordedEntry, verbosity int) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
if verbosity == 1 {
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\n",
"REQUID", "REQUEST METHOD", "FUNCTION", "RESPONSE STATUS", "TRIGGER")
for _, record := range records {
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\n",
record.ReqUID, record.Req.Method, record.Req.Header["X-Fission-Function-Name"], record.Resp.Status, record.Trigger)
}
} else if verbosity == 2 {
for _, record := range records {
fmt.Println(record)
}
} else {
fmt.Fprintf(w, "%v\n",
"REQUID")
for _, record := range records {
fmt.Fprintf(w, "%v\n",
record.ReqUID)
}
}
w.Flush()
}
func multipleFiltersSpecified(entries ...string) bool {
var specified int
for _, entry := range entries {
if len(entry) > 0 {
specified += 1
}
}
return specified > 1
}
+67
View File
@@ -0,0 +1,67 @@
/*
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 replay
import (
"fmt"
"os"
"text/tabwriter"
"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 ReplaySubCommand struct {
client *client.Client
}
func Replay(flags cli.Input) error {
opts := ReplaySubCommand{
client: cmd.GetServer(flags),
}
return opts.do(flags)
}
func (opts *ReplaySubCommand) do(flags cli.Input) error {
return opts.run(flags)
}
func (opts *ReplaySubCommand) run(flags cli.Input) error {
reqUID := flags.String("reqUID")
if len(reqUID) == 0 {
return errors.New("Need a reqUID, use --reqUID flag to specify")
}
responses, err := opts.client.ReplayByReqUID(reqUID)
if err != nil {
return errors.Wrap(err, "error replaying records")
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
for _, resp := range responses {
fmt.Fprintf(w, "%v",
resp,
)
}
w.Flush()
return nil
}