Refactor record command (#1378)
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user