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
+6 -12
View File
@@ -30,6 +30,7 @@ import (
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"
"github.com/fission/fission/pkg/fission-cli/console"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
@@ -37,18 +38,11 @@ import (
)
type CreateSubCommand struct {
client *client.Client
cmd.CommandActioner
}
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 {
@@ -114,14 +108,14 @@ func (opts *CreateSubCommand) run(input cli.Input) error {
specFile = fmt.Sprintf("package-%v.yaml", pkgName)
}
_, err := CreatePackage(input, opts.client, pkgName, pkgNamespace, envName, envNamespace,
_, err := CreatePackage(input, opts.Client(), pkgName, pkgNamespace, envName, envNamespace,
srcArchiveFiles, deployArchiveFiles, buildcmd, specDir, specFile, noZip)
return err
}
// TODO: get all necessary value from CLI input directly
func CreatePackage(input cli.Input, client *client.Client, pkgName string, pkgNamespace string, envName string, envNamespace string,
func CreatePackage(input cli.Input, client client.Interface, pkgName string, pkgNamespace string, envName string, envNamespace string,
srcArchiveFiles []string, deployArchiveFiles []string, buildcmd string, specDir string, specFile string, noZip bool) (*metav1.ObjectMeta, error) {
insecure := input.Bool(flagkey.PkgInsecure)
@@ -201,7 +195,7 @@ func CreatePackage(input cli.Input, client *client.Client, pkgName string, pkgNa
}
return &pkg.Metadata, nil
} else {
pkgMetadata, err := client.PackageCreate(pkg)
pkgMetadata, err := client.V1().Package().Create(pkg)
if err != nil {
return nil, errors.Wrap(err, "error creating package")
}
+11 -18
View File
@@ -24,12 +24,12 @@ 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"
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
name string
namespace string
deleteOrphans bool
@@ -37,14 +37,7 @@ type DeleteSubCommand struct {
}
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 {
@@ -70,7 +63,7 @@ func (opts *DeleteSubCommand) complete(input cli.Input) error {
func (opts *DeleteSubCommand) run(input cli.Input) error {
if len(opts.name) != 0 {
_, err := opts.client.PackageGet(&metav1.ObjectMeta{
_, err := opts.Client().V1().Package().Get(&metav1.ObjectMeta{
Namespace: opts.namespace,
Name: opts.name,
})
@@ -78,7 +71,7 @@ func (opts *DeleteSubCommand) run(input cli.Input) error {
return errors.Wrap(err, "find package")
}
fnList, err := GetFunctionsByPackage(opts.client, opts.name, opts.namespace)
fnList, err := GetFunctionsByPackage(opts.Client(), opts.name, opts.namespace)
if err != nil {
return err
}
@@ -86,7 +79,7 @@ func (opts *DeleteSubCommand) run(input cli.Input) error {
if !opts.force && len(fnList) > 0 {
return errors.New("Package is used by at least one function, use -f to force delete")
}
err = deletePackage(opts.client, opts.name, opts.namespace)
err = deletePackage(opts.Client(), opts.name, opts.namespace)
if err != nil {
return err
}
@@ -95,7 +88,7 @@ func (opts *DeleteSubCommand) run(input cli.Input) error {
// TODO improve list speed when --orphan
if opts.deleteOrphans {
err := deleteOrphanPkgs(opts.client, opts.namespace)
err := deleteOrphanPkgs(opts.Client(), opts.namespace)
if err != nil {
return errors.Wrap(err, "deleting orphan packages")
}
@@ -105,8 +98,8 @@ func (opts *DeleteSubCommand) run(input cli.Input) error {
return nil
}
func deleteOrphanPkgs(client *client.Client, pkgNamespace string) error {
pkgList, err := client.PackageList(pkgNamespace)
func deleteOrphanPkgs(client client.Interface, pkgNamespace string) error {
pkgList, err := client.V1().Package().List(pkgNamespace)
if err != nil {
return err
}
@@ -127,8 +120,8 @@ func deleteOrphanPkgs(client *client.Client, pkgNamespace string) error {
return nil
}
func deletePackage(client *client.Client, pkgName string, pkgNamespace string) error {
return client.PackageDelete(&metav1.ObjectMeta{
func deletePackage(client client.Interface, pkgName string, pkgNamespace string) error {
return client.V1().Package().Delete(&metav1.ObjectMeta{
Namespace: pkgNamespace,
Name: pkgName,
})
+6 -23
View File
@@ -24,11 +24,10 @@ 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"
pkgutil "github.com/fission/fission/pkg/fission-cli/cmd/package/util"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
const (
@@ -37,7 +36,7 @@ const (
)
type GetSubCommand struct {
client *client.Client
cmd.CommandActioner
name string
namespace string
output string
@@ -45,27 +44,11 @@ type GetSubCommand struct {
}
func GetSrc(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := GetSubCommand{
client: c,
archiveType: sourceArchive,
}
return opts.do(input)
return (&GetSubCommand{}).do(input)
}
func GetDeploy(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := GetSubCommand{
client: c,
archiveType: deployArchive,
}
return opts.do(input)
return (&GetSubCommand{}).do(input)
}
func (opts *GetSubCommand) do(input cli.Input) error {
@@ -84,7 +67,7 @@ func (opts *GetSubCommand) complete(input cli.Input) error {
}
func (opts *GetSubCommand) run(input cli.Input) error {
pkg, err := opts.client.PackageGet(&metav1.ObjectMeta{
pkg, err := opts.Client().V1().Package().Get(&metav1.ObjectMeta{
Namespace: opts.namespace,
Name: opts.name,
})
@@ -101,7 +84,7 @@ func (opts *GetSubCommand) run(input cli.Input) error {
if pkg.Spec.Deployment.Type == fv1.ArchiveTypeLiteral {
reader = bytes.NewReader(archive.Literal)
} else if pkg.Spec.Deployment.Type == fv1.ArchiveTypeUrl {
readCloser, err := pkgutil.DownloadStoragesvcURL(opts.client, archive.URL)
readCloser, err := pkgutil.DownloadStoragesvcURL(opts.Client(), archive.URL)
if err != nil {
return err
}
+4 -12
View File
@@ -22,28 +22,20 @@ 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"
pkgutil "github.com/fission/fission/pkg/fission-cli/cmd/package/util"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
type InfoSubCommand struct {
client *client.Client
cmd.CommandActioner
name string
namespace string
}
func Info(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := InfoSubCommand{
client: c,
}
return opts.do(input)
return (&InfoSubCommand{}).do(input)
}
func (opts *InfoSubCommand) do(input cli.Input) error {
@@ -61,7 +53,7 @@ func (opts *InfoSubCommand) complete(input cli.Input) error {
}
func (opts *InfoSubCommand) run(input cli.Input) error {
pkg, err := opts.client.PackageGet(&metav1.ObjectMeta{
pkg, err := opts.Client().V1().Package().Get(&metav1.ObjectMeta{
Namespace: opts.namespace,
Name: opts.name,
})
+5 -13
View File
@@ -25,28 +25,20 @@ 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
listOrphans bool
status string
pkgNamespace string
}
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 {
@@ -66,7 +58,7 @@ func (opts *ListSubCommand) complete(input cli.Input) error {
}
func (opts *ListSubCommand) run(input cli.Input) error {
pkgList, err := opts.client.PackageList(opts.pkgNamespace)
pkgList, err := opts.Client().V1().Package().List(opts.pkgNamespace)
if err != nil {
return err
}
@@ -83,7 +75,7 @@ func (opts *ListSubCommand) run(input cli.Input) error {
show := true
// TODO improve list speed when --orphan
if opts.listOrphans {
fnList, err := GetFunctionsByPackage(opts.client, pkg.Metadata.Name, pkg.Metadata.Namespace)
fnList, err := GetFunctionsByPackage(opts.Client(), pkg.Metadata.Name, pkg.Metadata.Namespace)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("get functions sharing package %s", pkg.Metadata.Name))
}
+3 -3
View File
@@ -45,7 +45,7 @@ import (
// create an archive upload spec in the specs directory; otherwise
// upload the archive using client. noZip avoids zipping the
// includeFiles, but is ignored if there's more than one includeFile.
func CreateArchive(client *client.Client, includeFiles []string, noZip bool, insecure bool, checksum string, specDir string, specFile string) (*fv1.Archive, error) {
func CreateArchive(client client.Interface, includeFiles []string, noZip bool, insecure bool, checksum string, specDir string, specFile string) (*fv1.Archive, error) {
// get root dir
var rootDir string
var err error
@@ -241,8 +241,8 @@ func archiveName(givenNameHint string, includedFiles []string) string {
return fmt.Sprintf("%v-%v", util.KubifyName(includedFiles[0]), uniuri.NewLen(4))
}
func GetFunctionsByPackage(client *client.Client, pkgName, pkgNamespace string) ([]fv1.Function, error) {
fnList, err := client.FunctionList(pkgNamespace)
func GetFunctionsByPackage(client client.Interface, pkgName, pkgNamespace string) ([]fv1.Function, error) {
fnList, err := client.V1().Function().List(pkgNamespace)
if err != nil {
return nil, err
}
+5 -13
View File
@@ -23,27 +23,19 @@ 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"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
type RebuildSubCommand struct {
client *client.Client
cmd.CommandActioner
name string
namespace string
}
func Rebuild(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := RebuildSubCommand{
client: c,
}
return opts.do(input)
return (&RebuildSubCommand{}).do(input)
}
func (opts *RebuildSubCommand) do(input cli.Input) error {
@@ -61,7 +53,7 @@ func (opts *RebuildSubCommand) complete(input cli.Input) error {
}
func (opts *RebuildSubCommand) run(input cli.Input) error {
pkg, err := opts.client.PackageGet(&metav1.ObjectMeta{
pkg, err := opts.Client().V1().Package().Get(&metav1.ObjectMeta{
Name: opts.name,
Namespace: opts.namespace,
})
@@ -74,7 +66,7 @@ func (opts *RebuildSubCommand) run(input cli.Input) error {
pkg.Metadata.Name, fv1.BuildStatusFailed))
}
_, err = updatePackageStatus(opts.client, pkg, fv1.BuildStatusPending)
_, err = updatePackageStatus(opts.Client(), pkg, fv1.BuildStatusPending)
if err != nil {
return errors.Wrap(err, "update package status")
}
+13 -20
View File
@@ -27,26 +27,19 @@ import (
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"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
type UpdateSubCommand struct {
client *client.Client
cmd.CommandActioner
pkgName string
pkgNamespace string
force bool
}
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 {
@@ -65,7 +58,7 @@ func (opts *UpdateSubCommand) complete(input cli.Input) error {
}
func (opts *UpdateSubCommand) run(input cli.Input) error {
pkg, err := opts.client.PackageGet(&metav1.ObjectMeta{
pkg, err := opts.Client().V1().Package().Get(&metav1.ObjectMeta{
Namespace: opts.pkgNamespace,
Name: opts.pkgName,
})
@@ -75,7 +68,7 @@ func (opts *UpdateSubCommand) run(input cli.Input) error {
forceUpdate := input.Bool(flagkey.PkgForce)
fnList, err := GetFunctionsByPackage(opts.client, pkg.Metadata.Name, pkg.Metadata.Namespace)
fnList, err := GetFunctionsByPackage(opts.Client(), pkg.Metadata.Name, pkg.Metadata.Namespace)
if err != nil {
return errors.Wrap(err, "error getting function list")
}
@@ -84,13 +77,13 @@ func (opts *UpdateSubCommand) run(input cli.Input) error {
return errors.Errorf("package is used by multiple functions, use --%v to force update", flagkey.PkgForce)
}
newPkgMeta, err := UpdatePackage(input, opts.client, pkg)
newPkgMeta, err := UpdatePackage(input, opts.Client(), pkg)
if err != nil {
return errors.Wrap(err, "error updating package")
}
if pkg.Metadata.ResourceVersion != newPkgMeta.ResourceVersion {
err = UpdateFunctionPackageResourceVersion(opts.client, newPkgMeta, fnList...)
err = UpdateFunctionPackageResourceVersion(opts.Client(), newPkgMeta, fnList...)
if err != nil {
return errors.Wrap(err, "error updating function package reference resource version")
}
@@ -99,7 +92,7 @@ func (opts *UpdateSubCommand) run(input cli.Input) error {
return nil
}
func UpdatePackage(input cli.Input, client *client.Client, pkg *fv1.Package) (*metav1.ObjectMeta, error) {
func UpdatePackage(input cli.Input, client client.Interface, pkg *fv1.Package) (*metav1.ObjectMeta, error) {
envName := input.String(flagkey.PkgEnvironment)
envNamespace := input.String(flagkey.NamespaceEnvironment)
srcArchiveFiles := input.StringSlice(flagkey.PkgSrcArchive)
@@ -185,7 +178,7 @@ func UpdatePackage(input cli.Input, client *client.Client, pkg *fv1.Package) (*m
}
}
newPkgMeta, err := client.PackageUpdate(pkg)
newPkgMeta, err := client.V1().Package().Update(pkg)
if err != nil {
return nil, errors.Wrap(err, "update package")
}
@@ -195,13 +188,13 @@ func UpdatePackage(input cli.Input, client *client.Client, pkg *fv1.Package) (*m
return newPkgMeta, err
}
func UpdateFunctionPackageResourceVersion(client *client.Client, pkgMeta *metav1.ObjectMeta, fnList ...fv1.Function) error {
func UpdateFunctionPackageResourceVersion(client client.Interface, pkgMeta *metav1.ObjectMeta, fnList ...fv1.Function) error {
errs := &multierror.Error{}
// update resource version of package reference of functions that shared the same package
for _, fn := range fnList {
fn.Spec.Package.PackageRef.ResourceVersion = pkgMeta.ResourceVersion
_, err := client.FunctionUpdate(&fn)
_, err := client.V1().Function().Update(&fn)
if err != nil {
errs = multierror.Append(errs, errors.Wrapf(err, "error updating package resource version of function '%v'", fn.Metadata.Name))
}
@@ -210,14 +203,14 @@ func UpdateFunctionPackageResourceVersion(client *client.Client, pkgMeta *metav1
return errs.ErrorOrNil()
}
func updatePackageStatus(client *client.Client, pkg *fv1.Package, status fv1.BuildStatus) (*metav1.ObjectMeta, error) {
func updatePackageStatus(client client.Interface, pkg *fv1.Package, status fv1.BuildStatus) (*metav1.ObjectMeta, error) {
switch status {
case fv1.BuildStatusNone, fv1.BuildStatusPending, fv1.BuildStatusRunning, fv1.BuildStatusSucceeded, fv1.CanaryConfigStatusAborted:
pkg.Status = fv1.PackageStatus{
BuildStatus: status,
LastUpdateTimestamp: time.Now().UTC(),
}
pkg, err := client.PackageUpdate(pkg)
pkg, err := client.V1().Package().Update(pkg)
return pkg, err
}
return nil, errors.New("unknown package status")
+5 -5
View File
@@ -37,7 +37,7 @@ import (
"github.com/fission/fission/pkg/utils"
)
func UploadArchiveFile(ctx context.Context, client *client.Client, fileName string) (*fv1.Archive, error) {
func UploadArchiveFile(ctx context.Context, client client.Interface, fileName string) (*fv1.Archive, error) {
var archive fv1.Archive
size, err := utils.FileSize(fileName)
@@ -52,7 +52,7 @@ func UploadArchiveFile(ctx context.Context, client *client.Client, fileName stri
return nil, err
}
} else {
u := strings.TrimSuffix(client.Url, "/") + "/proxy/storage"
u := strings.TrimSuffix(client.ServerURL(), "/") + "/proxy/storage"
ssClient := storageSvcClient.MakeClient(u)
// TODO add a progress bar
@@ -61,7 +61,7 @@ func UploadArchiveFile(ctx context.Context, client *client.Client, fileName stri
return nil, errors.Wrapf(err, "error uploading file %v", fileName)
}
storageSvc, err := client.GetSvcURL("application=fission-storage")
storageSvc, err := client.V1().Misc().GetSvcURL("application=fission-storage")
storageSvcURL := "http://" + storageSvc
if err != nil {
return nil, errors.Wrapf(err, "error getting fission storage service name")
@@ -161,14 +161,14 @@ func WriteArchiveToFile(fileName string, reader io.Reader) error {
}
// DownloadStoragesvcURL downloads and return archive content with given storage service url
func DownloadStoragesvcURL(client *client.Client, fileUrl string) (io.ReadCloser, error) {
func DownloadStoragesvcURL(client client.Interface, fileUrl string) (io.ReadCloser, error) {
u, err := url.ParseRequestURI(fileUrl)
if err != nil {
return nil, err
}
// replace in-cluster storage service host with controller server url
fileDownloadUrl := strings.TrimSuffix(client.Url, "/") + "/proxy/storage/" + u.RequestURI()
fileDownloadUrl := strings.TrimSuffix(client.ServerURL(), "/") + "/proxy/storage/" + u.RequestURI()
reader, err := DownloadURL(fileDownloadUrl)
if err != nil {
return nil, errors.Wrapf(err, fmt.Sprintf("error downloading from storage service url: %v", fileUrl))