Add controller API client interface (#1467)

This PR adds an interface for controller API client, it allows us to
implement mock API client for unit testing with ease and we are
able to generate spec file without accessing the real Fission server.
This commit is contained in:
Ta-Ching Chen
2019-12-16 23:14:14 +08:00
committed by GitHub
parent 01bdc5bf89
commit bfbb80efcf
71 changed files with 1021 additions and 1025 deletions
+11 -18
View File
@@ -27,9 +27,9 @@ import (
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"
ferror "github.com/fission/fission/pkg/error"
"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/httptrigger"
_package "github.com/fission/fission/pkg/fission-cli/cmd/package"
"github.com/fission/fission/pkg/fission-cli/cmd/spec"
@@ -44,20 +44,13 @@ const (
)
type CreateSubCommand struct {
client *client.Client
cmd.CommandActioner
function *fv1.Function
specFile string
}
func Create(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := CreateSubCommand{
client: c,
}
return opts.do(input)
return (&CreateSubCommand{}).do(input)
}
func (opts *CreateSubCommand) do(input cli.Input) error {
@@ -83,7 +76,7 @@ func (opts *CreateSubCommand) complete(input cli.Input) error {
if !toSpec {
// check for unique function names within a namespace
fn, err := opts.client.FunctionGet(&metav1.ObjectMeta{
fn, err := opts.Client().V1().Function().Get(&metav1.ObjectMeta{
Name: input.String(flagkey.FnName),
Namespace: input.String(flagkey.NamespaceFunction),
})
@@ -139,7 +132,7 @@ func (opts *CreateSubCommand) complete(input cli.Input) error {
pkgMetadata = &pkg.Metadata
} else {
// use existing package
pkg, err = opts.client.PackageGet(&metav1.ObjectMeta{
pkg, err = opts.Client().V1().Package().Get(&metav1.ObjectMeta{
Namespace: fnNamespace,
Name: pkgName,
})
@@ -181,7 +174,7 @@ func (opts *CreateSubCommand) complete(input cli.Input) error {
fnName, envName))
}
} else {
_, err := opts.client.EnvironmentGet(&metav1.ObjectMeta{
_, err := opts.Client().V1().Environment().Get(&metav1.ObjectMeta{
Namespace: envNamespace,
Name: envName,
})
@@ -213,7 +206,7 @@ func (opts *CreateSubCommand) complete(input cli.Input) error {
pkgName := fmt.Sprintf("%v-%v", fnName, uuid.NewV4().String())
// create new package in the same namespace as the function.
pkgMetadata, err = _package.CreatePackage(input, opts.client, pkgName, fnNamespace, envName, envNamespace,
pkgMetadata, err = _package.CreatePackage(input, opts.Client(), pkgName, fnNamespace, envName, envNamespace,
srcArchiveFiles, deployArchiveFiles, buildcmd, specDir, opts.specFile, noZip)
if err != nil {
return errors.Wrap(err, "error creating package")
@@ -227,7 +220,7 @@ func (opts *CreateSubCommand) complete(input cli.Input) error {
// check the referenced secret is in the same ns as the function, if not give a warning.
if !toSpec { // TODO: workaround in order not to block users from creating function spec, remove it.
for _, secretName := range secretNames {
_, err := opts.client.SecretGet(&metav1.ObjectMeta{
_, err := opts.Client().V1().Misc().SecretGet(&metav1.ObjectMeta{
Namespace: fnNamespace,
Name: secretName,
})
@@ -253,7 +246,7 @@ func (opts *CreateSubCommand) complete(input cli.Input) error {
// check the referenced cfgmap is in the same ns as the function, if not give a warning.
if !toSpec {
for _, cfgMapName := range cfgMapNames {
_, err := opts.client.ConfigMapGet(&metav1.ObjectMeta{
_, err := opts.Client().V1().Misc().ConfigMapGet(&metav1.ObjectMeta{
Namespace: fnNamespace,
Name: cfgMapName,
})
@@ -316,7 +309,7 @@ func (opts *CreateSubCommand) run(input cli.Input) error {
return nil
}
_, err := opts.client.FunctionCreate(opts.function)
_, err := opts.Client().V1().Function().Create(opts.function)
if err != nil {
return errors.Wrap(err, "error creating function")
}
@@ -352,7 +345,7 @@ func (opts *CreateSubCommand) run(input cli.Input) error {
},
},
}
_, err = opts.client.HTTPTriggerCreate(ht)
_, err = opts.Client().V1().HTTPTrigger().Create(ht)
if err != nil {
return errors.Wrap(err, "error creating HTTP trigger")
}
+4 -12
View File
@@ -22,25 +22,17 @@ import (
"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"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
type DeleteSubCommand struct {
client *client.Client
cmd.CommandActioner
}
func Delete(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := DeleteSubCommand{
client: c,
}
return opts.do(input)
return (&DeleteSubCommand{}).do(input)
}
func (opts *DeleteSubCommand) do(input cli.Input) error {
@@ -49,7 +41,7 @@ func (opts *DeleteSubCommand) do(input cli.Input) error {
Namespace: input.String(flagkey.NamespaceFunction),
}
err := opts.client.FunctionDelete(m)
err := opts.Client().V1().Function().Delete(m)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("delete function '%v'", m.Name))
}
+5 -13
View File
@@ -22,29 +22,21 @@ import (
"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"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
type GetSubCommand struct {
client *client.Client
cmd.CommandActioner
}
func Get(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := GetSubCommand{
client: c,
}
return opts.do(input)
return (&GetSubCommand{}).do(input)
}
func (opts *GetSubCommand) do(input cli.Input) error {
fn, err := opts.client.FunctionGet(&metav1.ObjectMeta{
fn, err := opts.Client().V1().Function().Get(&metav1.ObjectMeta{
Name: input.String(flagkey.FnName),
Namespace: input.String(flagkey.NamespaceFunction),
})
@@ -52,7 +44,7 @@ func (opts *GetSubCommand) do(input cli.Input) error {
return errors.Wrap(err, "error getting function")
}
pkg, err := opts.client.PackageGet(&metav1.ObjectMeta{
pkg, err := opts.Client().V1().Package().Get(&metav1.ObjectMeta{
Name: fn.Spec.Package.PackageRef.Name,
Namespace: fn.Spec.Package.PackageRef.Namespace,
})
+4 -12
View File
@@ -24,29 +24,21 @@ import (
"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"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
type GetMetaSubCommand struct {
client *client.Client
cmd.CommandActioner
}
func GetMeta(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := GetMetaSubCommand{
client: c,
}
return opts.do(input)
return (&GetMetaSubCommand{}).do(input)
}
func (opts *GetMetaSubCommand) do(input cli.Input) error {
fn, err := opts.client.FunctionGet(&metav1.ObjectMeta{
fn, err := opts.Client().V1().Function().Get(&metav1.ObjectMeta{
Name: input.String(flagkey.FnName),
Namespace: input.String(flagkey.NamespaceFunction),
})
+4 -12
View File
@@ -24,31 +24,23 @@ 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"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
type ListSubCommand struct {
client *client.Client
cmd.CommandActioner
}
func List(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := ListSubCommand{
client: c,
}
return opts.do(input)
return (&ListSubCommand{}).do(input)
}
func (opts *ListSubCommand) do(input cli.Input) error {
ns := input.String(flagkey.NamespaceFunction)
fns, err := opts.client.FunctionList(ns)
fns, err := opts.Client().V1().Function().List(ns)
if err != nil {
return errors.Wrap(err, "error listing functions")
}
+4 -11
View File
@@ -24,26 +24,19 @@ import (
"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"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/logdb"
"github.com/fission/fission/pkg/fission-cli/util"
)
type LogSubCommand struct {
client *client.Client
cmd.CommandActioner
}
func Log(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := LogSubCommand{
client: c,
}
return opts.do(input)
return (&LogSubCommand{}).do(input)
}
func (opts *LogSubCommand) do(input cli.Input) error {
@@ -57,7 +50,7 @@ func (opts *LogSubCommand) do(input cli.Input) error {
recordLimit = 1000
}
f, err := opts.client.FunctionGet(&metav1.ObjectMeta{
f, err := opts.Client().V1().Function().Get(&metav1.ObjectMeta{
Name: input.String(flagkey.FnName),
Namespace: input.String(flagkey.NamespaceFunction),
})
+6 -12
View File
@@ -30,6 +30,7 @@ import (
"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/httptrigger"
"github.com/fission/fission/pkg/fission-cli/console"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
@@ -37,18 +38,11 @@ import (
)
type TestSubCommand struct {
client *client.Client
cmd.CommandActioner
}
func Test(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := TestSubCommand{
client: c,
}
return opts.do(input)
return (&TestSubCommand{}).do(input)
}
func (opts *TestSubCommand) do(input cli.Input) error {
@@ -124,7 +118,7 @@ func (opts *TestSubCommand) do(input cli.Input) error {
}
console.Errorf("Error calling function %s: %d; Please try again or fix the error: %s\n", m.Name, resp.StatusCode, string(body))
log, err := printPodLogs(opts.client, m)
log, err := printPodLogs(opts.Client(), m)
if err != nil {
console.Errorf("Error getting function logs from controller: %v. Try to get logs from log database.", err)
err = Log(input)
@@ -163,8 +157,8 @@ func doHTTPRequest(ctx context.Context, url string, headers []string, method, bo
return resp, nil
}
func printPodLogs(client *client.Client, fnMeta *metav1.ObjectMeta) (string, error) {
reader, statusCode, err := client.FunctionPodLogs(fnMeta)
func printPodLogs(client client.Interface, fnMeta *metav1.ObjectMeta) (string, error) {
reader, statusCode, err := client.V1().Misc().PodLogs(fnMeta)
if err != nil {
return "", errors.Wrap(err, "error executing get logs request")
}
+11 -18
View File
@@ -24,8 +24,8 @@ import (
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"
_package "github.com/fission/fission/pkg/fission-cli/cmd/package"
"github.com/fission/fission/pkg/fission-cli/console"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
@@ -33,19 +33,12 @@ import (
)
type UpdateSubCommand struct {
client *client.Client
cmd.CommandActioner
function *fv1.Function
}
func Update(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := UpdateSubCommand{
client: c,
}
return opts.do(input)
return (&UpdateSubCommand{}).do(input)
}
func (opts *UpdateSubCommand) do(input cli.Input) error {
@@ -60,7 +53,7 @@ func (opts *UpdateSubCommand) complete(input cli.Input) error {
fnName := input.String(flagkey.FnName)
fnNamespace := input.String(flagkey.NamespaceFunction)
function, err := opts.client.FunctionGet(&metav1.ObjectMeta{
function, err := opts.Client().V1().Function().Get(&metav1.ObjectMeta{
Name: input.String(flagkey.FnName),
Namespace: input.String(flagkey.NamespaceFunction),
})
@@ -94,7 +87,7 @@ func (opts *UpdateSubCommand) complete(input cli.Input) error {
// check that the referenced secret is in the same ns as the function, if not give a warning.
for _, secretName := range secretNames {
_, err := opts.client.SecretGet(&metav1.ObjectMeta{
_, err := opts.Client().V1().Misc().SecretGet(&metav1.ObjectMeta{
Namespace: fnNamespace,
Name: secretName,
})
@@ -118,7 +111,7 @@ func (opts *UpdateSubCommand) complete(input cli.Input) error {
// check that the referenced cfgmap is in the same ns as the function, if not give a warning.
for _, cfgMapName := range cfgMapNames {
_, err := opts.client.ConfigMapGet(&metav1.ObjectMeta{
_, err := opts.Client().V1().Misc().ConfigMapGet(&metav1.ObjectMeta{
Namespace: fnNamespace,
Name: cfgMapName,
})
@@ -174,7 +167,7 @@ func (opts *UpdateSubCommand) complete(input cli.Input) error {
function.Spec.Resources = *resReqs
pkg, err := opts.client.PackageGet(&metav1.ObjectMeta{
pkg, err := opts.Client().V1().Package().Get(&metav1.ObjectMeta{
Namespace: fnNamespace,
Name: pkgName,
})
@@ -184,7 +177,7 @@ func (opts *UpdateSubCommand) complete(input cli.Input) error {
forceUpdate := input.Bool(flagkey.PkgForce)
fnList, err := _package.GetFunctionsByPackage(opts.client, pkg.Metadata.Name, pkg.Metadata.Namespace)
fnList, err := _package.GetFunctionsByPackage(opts.Client(), pkg.Metadata.Name, pkg.Metadata.Namespace)
if err != nil {
return errors.Wrap(err, "error getting function list")
}
@@ -193,7 +186,7 @@ func (opts *UpdateSubCommand) complete(input cli.Input) error {
return errors.Errorf("Package is used by multiple functions, use --%v to force update", flagkey.PkgForce)
}
newPkgMeta, err := _package.UpdatePackage(input, opts.client, pkg)
newPkgMeta, err := _package.UpdatePackage(input, opts.Client(), pkg)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("error updating package '%v'", pkgName))
}
@@ -210,7 +203,7 @@ func (opts *UpdateSubCommand) complete(input cli.Input) error {
fns = append(fns, fn)
}
}
err = _package.UpdateFunctionPackageResourceVersion(opts.client, newPkgMeta, fns...)
err = _package.UpdateFunctionPackageResourceVersion(opts.Client(), newPkgMeta, fns...)
if err != nil {
return errors.Wrap(err, "error updating function package reference resource version")
}
@@ -238,7 +231,7 @@ func (opts *UpdateSubCommand) complete(input cli.Input) error {
}
func (opts *UpdateSubCommand) run(input cli.Input) error {
_, err := opts.client.FunctionUpdate(opts.function)
_, err := opts.Client().V1().Function().Update(opts.function)
if err != nil {
return errors.Wrap(err, "error updating function")
}