Use Kubernetes Client instead of Controller APIs from CLI (#2605)
Use the Kubernetes and Fission Client from CLI instead of Controller API. This removes port-forwarding for the controller across Fission CLI mostly. * Use configurable client in CLI * Move resource namespace under cmd client * use server to get fission version * get archive with URL Signed-off-by: Sanket Sudake <sanketsudake@gmail.com> Co-authored-by: Sanket Sudake <sanketsudake@gmail.com>
This commit is contained in:
co-authored by
Sanket Sudake
parent
261bf24974
commit
b71a36dc1c
@@ -28,7 +28,6 @@ import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
fv1 "github.com/fission/fission/pkg/apis/core/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"
|
||||
@@ -65,7 +64,7 @@ func (opts *CreateSubCommand) run(input cli.Input) error {
|
||||
|
||||
envName := input.String(flagkey.PkgEnvironment)
|
||||
|
||||
userProvidedNS, pkgNamespace, err := util.GetResourceNamespace(input, flagkey.NamespacePackage)
|
||||
userProvidedNS, pkgNamespace, err := opts.GetResourceNamespace(input, flagkey.NamespacePackage)
|
||||
if err != nil {
|
||||
return fv1.AggregateValidationErrors("Environment", err)
|
||||
}
|
||||
@@ -126,7 +125,7 @@ func (opts *CreateSubCommand) run(input cli.Input) error {
|
||||
}
|
||||
|
||||
// TODO: get all necessary value from CLI input directly
|
||||
func CreatePackage(input cli.Input, client client.Interface, pkgName string, pkgNamespace string, envName string,
|
||||
func CreatePackage(input cli.Input, client cmd.Client, pkgName string, pkgNamespace string, envName string,
|
||||
srcArchiveFiles []string, deployArchiveFiles []string, buildcmd string, specDir string, specFile string, noZip bool, userProvidedNS string) (*metav1.ObjectMeta, error) {
|
||||
|
||||
insecure := input.Bool(flagkey.PkgInsecure)
|
||||
@@ -224,11 +223,12 @@ func CreatePackage(input cli.Input, client client.Interface, pkgName string, pkg
|
||||
return &pkg.ObjectMeta, nil
|
||||
} else {
|
||||
pkg.ObjectMeta.Namespace = pkgNamespace
|
||||
pkgMetadata, err := client.V1().Package().Create(pkg)
|
||||
|
||||
pkgMetadata, err := client.FissionClientSet.CoreV1().Packages(pkgNamespace).Create(input.Context(), pkg, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error creating package")
|
||||
}
|
||||
fmt.Printf("Package '%v' created\n", pkgMetadata.GetName())
|
||||
return pkgMetadata, nil
|
||||
return &pkgMetadata.ObjectMeta, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,17 +17,17 @@ limitations under the License.
|
||||
package _package
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
kerrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
fv1 "github.com/fission/fission/pkg/apis/core/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 {
|
||||
@@ -52,7 +52,7 @@ func (opts *DeleteSubCommand) do(input cli.Input) error {
|
||||
|
||||
func (opts *DeleteSubCommand) complete(input cli.Input) (err error) {
|
||||
opts.name = input.String(flagkey.PkgName)
|
||||
_, opts.namespace, err = util.GetResourceNamespace(input, flagkey.NamespacePackage)
|
||||
_, opts.namespace, err = opts.GetResourceNamespace(input, flagkey.NamespacePackage)
|
||||
if err != nil {
|
||||
return fv1.AggregateValidationErrors("Environment", err)
|
||||
}
|
||||
@@ -69,18 +69,15 @@ func (opts *DeleteSubCommand) complete(input cli.Input) (err error) {
|
||||
|
||||
func (opts *DeleteSubCommand) run(input cli.Input) error {
|
||||
if len(opts.name) != 0 {
|
||||
_, err := opts.Client().V1().Package().Get(&metav1.ObjectMeta{
|
||||
Namespace: opts.namespace,
|
||||
Name: opts.name,
|
||||
})
|
||||
_, err := opts.Client().FissionClientSet.CoreV1().Packages(opts.namespace).Get(input.Context(), opts.name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
if input.Bool(flagkey.IgnoreNotFound) && util.IsNotFound(err) {
|
||||
if input.Bool(flagkey.IgnoreNotFound) && kerrors.IsNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
return errors.Wrap(err, "find package")
|
||||
}
|
||||
|
||||
fnList, err := GetFunctionsByPackage(opts.Client(), opts.name, opts.namespace)
|
||||
fnList, err := GetFunctionsByPackage(input.Context(), opts.Client(), opts.name, opts.namespace)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -88,7 +85,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(input.Context(), opts.Client(), opts.name, opts.namespace)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -97,7 +94,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(input.Context(), opts.Client(), opts.namespace)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "deleting orphan packages")
|
||||
}
|
||||
@@ -107,20 +104,20 @@ func (opts *DeleteSubCommand) run(input cli.Input) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func deleteOrphanPkgs(client client.Interface, pkgNamespace string) error {
|
||||
pkgList, err := client.V1().Package().List(pkgNamespace)
|
||||
func deleteOrphanPkgs(ctx context.Context, client cmd.Client, pkgNamespace string) error {
|
||||
pkgList, err := client.FissionClientSet.CoreV1().Packages(pkgNamespace).List(ctx, metav1.ListOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// range through all packages and find out the ones not referenced by any function
|
||||
for _, pkg := range pkgList {
|
||||
fnList, err := GetFunctionsByPackage(client, pkg.ObjectMeta.Name, pkgNamespace)
|
||||
for _, pkg := range pkgList.Items {
|
||||
fnList, err := GetFunctionsByPackage(ctx, client, pkg.ObjectMeta.Name, pkgNamespace)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, fmt.Sprintf("get functions sharing package %s", pkg.ObjectMeta.Name))
|
||||
}
|
||||
if len(fnList) == 0 {
|
||||
err = deletePackage(client, pkg.ObjectMeta.Name, pkgNamespace)
|
||||
err = deletePackage(ctx, client, pkg.ObjectMeta.Name, pkgNamespace)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -129,9 +126,6 @@ func deleteOrphanPkgs(client client.Interface, pkgNamespace string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func deletePackage(client client.Interface, pkgName string, pkgNamespace string) error {
|
||||
return client.V1().Package().Delete(&metav1.ObjectMeta{
|
||||
Namespace: pkgNamespace,
|
||||
Name: pkgName,
|
||||
})
|
||||
func deletePackage(ctx context.Context, client cmd.Client, pkgName string, pkgNamespace string) error {
|
||||
return client.FissionClientSet.CoreV1().Packages(pkgNamespace).Delete(ctx, pkgName, metav1.DeleteOptions{})
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
fv1 "github.com/fission/fission/pkg/apis/core/v1"
|
||||
@@ -28,7 +29,6 @@ import (
|
||||
"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 (
|
||||
@@ -61,7 +61,7 @@ func (opts *GetSubCommand) do(input cli.Input) error {
|
||||
|
||||
func (opts *GetSubCommand) complete(input cli.Input) (err error) {
|
||||
opts.name = input.String(flagkey.PkgName)
|
||||
_, opts.namespace, err = util.GetResourceNamespace(input, flagkey.NamespacePackage)
|
||||
_, opts.namespace, err = opts.GetResourceNamespace(input, flagkey.NamespacePackage)
|
||||
if err != nil {
|
||||
return fv1.AggregateValidationErrors("Environment", err)
|
||||
}
|
||||
@@ -70,10 +70,8 @@ func (opts *GetSubCommand) complete(input cli.Input) (err error) {
|
||||
}
|
||||
|
||||
func (opts *GetSubCommand) run(input cli.Input) error {
|
||||
pkg, err := opts.Client().V1().Package().Get(&metav1.ObjectMeta{
|
||||
Namespace: opts.namespace,
|
||||
Name: opts.name,
|
||||
})
|
||||
|
||||
pkg, err := opts.Client().FissionClientSet.CoreV1().Packages(opts.namespace).Get(input.Context(), opts.name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -87,10 +85,12 @@ 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.DownloadStrorageURL(input.Context(), opts.Client(), archive.URL)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.Wrapf(err, "error downloading from storage service url: %s", archive.URL)
|
||||
}
|
||||
|
||||
defer readCloser.Close()
|
||||
reader = readCloser
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ import (
|
||||
"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 {
|
||||
@@ -51,7 +50,7 @@ func (opts *InfoSubCommand) do(input cli.Input) error {
|
||||
func (opts *InfoSubCommand) complete(input cli.Input) (err error) {
|
||||
opts.name = input.String(flagkey.PkgName)
|
||||
|
||||
_, opts.namespace, err = util.GetResourceNamespace(input, flagkey.NamespacePackage)
|
||||
_, opts.namespace, err = opts.GetResourceNamespace(input, flagkey.NamespacePackage)
|
||||
if err != nil {
|
||||
return fv1.AggregateValidationErrors("Environment", err)
|
||||
}
|
||||
@@ -59,10 +58,11 @@ func (opts *InfoSubCommand) complete(input cli.Input) (err error) {
|
||||
}
|
||||
|
||||
func (opts *InfoSubCommand) run(input cli.Input) error {
|
||||
pkg, err := opts.Client().V1().Package().Get(&metav1.ObjectMeta{
|
||||
Namespace: opts.namespace,
|
||||
Name: opts.name,
|
||||
})
|
||||
pkg, err := opts.Client().FissionClientSet.CoreV1().Packages(opts.namespace).Get(input.Context(), opts.name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "error finding package %s", opts.name)
|
||||
}
|
||||
|
||||
@@ -24,12 +24,12 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
fv1 "github.com/fission/fission/pkg/apis/core/v1"
|
||||
"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 {
|
||||
@@ -55,7 +55,7 @@ func (opts *ListSubCommand) complete(input cli.Input) (err error) {
|
||||
// option for the user to list all orphan packages (not referenced by any function)
|
||||
opts.listOrphans = input.Bool(flagkey.PkgOrphan)
|
||||
opts.status = input.String(flagkey.PkgStatus)
|
||||
_, opts.pkgNamespace, err = util.GetResourceNamespace(input, flagkey.NamespacePackage)
|
||||
_, opts.pkgNamespace, err = opts.GetResourceNamespace(input, flagkey.NamespacePackage)
|
||||
if err != nil {
|
||||
return fv1.AggregateValidationErrors("Environment", err)
|
||||
}
|
||||
@@ -64,29 +64,28 @@ func (opts *ListSubCommand) complete(input cli.Input) (err error) {
|
||||
|
||||
func (opts *ListSubCommand) run(input cli.Input) (err error) {
|
||||
|
||||
var pkgList []fv1.Package
|
||||
if input.Bool(flagkey.AllNamespaces) {
|
||||
pkgList, err = opts.Client().V1().Package().List("")
|
||||
} else {
|
||||
pkgList, err = opts.Client().V1().Package().List(opts.pkgNamespace)
|
||||
opts.pkgNamespace = v1.NamespaceAll
|
||||
}
|
||||
pkgList, err := opts.Client().FissionClientSet.CoreV1().Packages(opts.pkgNamespace).List(input.Context(), v1.ListOptions{})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// sort the package list by lastUpdatedTimestamp
|
||||
sort.Slice(pkgList, func(i, j int) bool {
|
||||
return pkgList[i].Status.LastUpdateTimestamp.After(pkgList[j].Status.LastUpdateTimestamp.Time)
|
||||
sort.Slice(pkgList.Items, func(i, j int) bool {
|
||||
return pkgList.Items[i].Status.LastUpdateTimestamp.After(pkgList.Items[j].Status.LastUpdateTimestamp.Time)
|
||||
})
|
||||
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
|
||||
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\n", "NAME", "BUILD_STATUS", "ENV", "LASTUPDATEDAT", "NAMESPACE")
|
||||
|
||||
for _, pkg := range pkgList {
|
||||
for _, pkg := range pkgList.Items {
|
||||
show := true
|
||||
// TODO improve list speed when --orphan
|
||||
if opts.listOrphans {
|
||||
fnList, err := GetFunctionsByPackage(opts.Client(), pkg.ObjectMeta.Name, pkg.ObjectMeta.Namespace)
|
||||
fnList, err := GetFunctionsByPackage(input.Context(), opts.Client(), pkg.ObjectMeta.Name, pkg.ObjectMeta.Namespace)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, fmt.Sprintf("get functions sharing package %s", pkg.ObjectMeta.Name))
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package _package
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -27,10 +28,11 @@ import (
|
||||
"github.com/hashicorp/go-multierror"
|
||||
"github.com/pkg/errors"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
fv1 "github.com/fission/fission/pkg/apis/core/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"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd/spec"
|
||||
spectypes "github.com/fission/fission/pkg/fission-cli/cmd/spec/types"
|
||||
@@ -44,7 +46,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.Interface, input cli.Input, includeFiles []string, noZip bool, insecure bool, checksum string, specDir string, specFile string) (*fv1.Archive, error) {
|
||||
func CreateArchive(client cmd.Client, input cli.Input, includeFiles []string, noZip bool, insecure bool, checksum string, specDir string, specFile string) (*fv1.Archive, error) {
|
||||
// get root dir
|
||||
var rootDir string
|
||||
var err error
|
||||
@@ -251,13 +253,13 @@ func archiveName(givenNameHint string, includedFiles []string) string {
|
||||
return fmt.Sprintf("%v-%v", util.KubifyName(includedFiles[0]), uniuri.NewLen(4))
|
||||
}
|
||||
|
||||
func GetFunctionsByPackage(client client.Interface, pkgName, pkgNamespace string) ([]fv1.Function, error) {
|
||||
fnList, err := client.V1().Function().List(pkgNamespace)
|
||||
func GetFunctionsByPackage(ctx context.Context, client cmd.Client, pkgName, pkgNamespace string) ([]fv1.Function, error) {
|
||||
fnList, err := client.FissionClientSet.CoreV1().Functions(pkgNamespace).List(ctx, v1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fns := []fv1.Function{}
|
||||
for _, fn := range fnList {
|
||||
for _, fn := range fnList.Items {
|
||||
if fn.Spec.Package.PackageRef.Name == pkgName {
|
||||
fns = append(fns, fn)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ import (
|
||||
"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 {
|
||||
@@ -49,7 +48,7 @@ func (opts *RebuildSubCommand) do(input cli.Input) error {
|
||||
|
||||
func (opts *RebuildSubCommand) complete(input cli.Input) (err error) {
|
||||
opts.name = input.String(flagkey.PkgName)
|
||||
_, opts.namespace, err = util.GetResourceNamespace(input, flagkey.NamespacePackage)
|
||||
_, opts.namespace, err = opts.GetResourceNamespace(input, flagkey.NamespacePackage)
|
||||
if err != nil {
|
||||
return fv1.AggregateValidationErrors("Environment", err)
|
||||
}
|
||||
@@ -57,10 +56,10 @@ func (opts *RebuildSubCommand) complete(input cli.Input) (err error) {
|
||||
}
|
||||
|
||||
func (opts *RebuildSubCommand) run(input cli.Input) error {
|
||||
pkg, err := opts.Client().V1().Package().Get(&metav1.ObjectMeta{
|
||||
Name: opts.name,
|
||||
Namespace: opts.namespace,
|
||||
})
|
||||
pkg, err := opts.Client().FissionClientSet.CoreV1().Packages(opts.namespace).Get(input.Context(), opts.name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "find package")
|
||||
}
|
||||
@@ -70,7 +69,7 @@ func (opts *RebuildSubCommand) run(input cli.Input) error {
|
||||
pkg.ObjectMeta.Name, fv1.BuildStatusFailed))
|
||||
}
|
||||
|
||||
_, err = updatePackageStatus(opts.Client(), pkg, fv1.BuildStatusPending)
|
||||
_, err = updatePackageStatus(input.Context(), opts.Client(), pkg, fv1.BuildStatusPending)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "update package status")
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package _package
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -25,11 +26,9 @@ import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
fv1 "github.com/fission/fission/pkg/apis/core/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 {
|
||||
@@ -53,7 +52,7 @@ func (opts *UpdateSubCommand) do(input cli.Input) error {
|
||||
|
||||
func (opts *UpdateSubCommand) complete(input cli.Input) (err error) {
|
||||
opts.pkgName = input.String(flagkey.PkgName)
|
||||
_, opts.pkgNamespace, err = util.GetResourceNamespace(input, flagkey.NamespacePackage)
|
||||
_, opts.pkgNamespace, err = opts.GetResourceNamespace(input, flagkey.NamespacePackage)
|
||||
if err != nil {
|
||||
return fv1.AggregateValidationErrors("Environment", err)
|
||||
}
|
||||
@@ -62,17 +61,17 @@ func (opts *UpdateSubCommand) complete(input cli.Input) (err error) {
|
||||
}
|
||||
|
||||
func (opts *UpdateSubCommand) run(input cli.Input) error {
|
||||
pkg, err := opts.Client().V1().Package().Get(&metav1.ObjectMeta{
|
||||
Namespace: opts.pkgNamespace,
|
||||
Name: opts.pkgName,
|
||||
})
|
||||
pkg, err := opts.Client().FissionClientSet.CoreV1().Packages(opts.pkgNamespace).Get(input.Context(), opts.pkgName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "get package")
|
||||
}
|
||||
|
||||
forceUpdate := input.Bool(flagkey.PkgForce)
|
||||
|
||||
fnList, err := GetFunctionsByPackage(opts.Client(), pkg.ObjectMeta.Name, pkg.ObjectMeta.Namespace)
|
||||
fnList, err := GetFunctionsByPackage(input.Context(), opts.Client(), pkg.ObjectMeta.Name, pkg.ObjectMeta.Namespace)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error getting function list")
|
||||
}
|
||||
@@ -87,7 +86,7 @@ func (opts *UpdateSubCommand) run(input cli.Input) error {
|
||||
}
|
||||
|
||||
if pkg.ObjectMeta.ResourceVersion != newPkgMeta.ResourceVersion {
|
||||
err = UpdateFunctionPackageResourceVersion(opts.Client(), newPkgMeta, fnList...)
|
||||
err = UpdateFunctionPackageResourceVersion(input.Context(), opts.Client(), newPkgMeta, fnList...)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error updating function package reference resource version")
|
||||
}
|
||||
@@ -96,7 +95,7 @@ func (opts *UpdateSubCommand) run(input cli.Input) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdatePackage(input cli.Input, client client.Interface, pkg *fv1.Package) (*metav1.ObjectMeta, error) {
|
||||
func UpdatePackage(input cli.Input, client cmd.Client, pkg *fv1.Package) (*metav1.ObjectMeta, error) {
|
||||
envName := input.String(flagkey.PkgEnvironment)
|
||||
srcArchiveFiles := input.StringSlice(flagkey.PkgSrcArchive)
|
||||
deployArchiveFiles := input.StringSlice(flagkey.PkgDeployArchive)
|
||||
@@ -175,23 +174,23 @@ func UpdatePackage(input cli.Input, client client.Interface, pkg *fv1.Package) (
|
||||
}
|
||||
}
|
||||
|
||||
newPkgMeta, err := client.V1().Package().Update(pkg)
|
||||
newPkgMeta, err := client.FissionClientSet.CoreV1().Packages(pkg.ObjectMeta.Namespace).Update(input.Context(), pkg, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "update package")
|
||||
}
|
||||
|
||||
fmt.Printf("Package '%v' updated\n", newPkgMeta.GetName())
|
||||
|
||||
return newPkgMeta, err
|
||||
return &newPkgMeta.ObjectMeta, err
|
||||
}
|
||||
|
||||
func UpdateFunctionPackageResourceVersion(client client.Interface, pkgMeta *metav1.ObjectMeta, fnList ...fv1.Function) error {
|
||||
func UpdateFunctionPackageResourceVersion(ctx context.Context, client cmd.Client, 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.V1().Function().Update(&fn)
|
||||
_, err := client.FissionClientSet.CoreV1().Functions(fn.ObjectMeta.Namespace).Update(ctx, &fn, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
errs = multierror.Append(errs, errors.Wrapf(err, "error updating package resource version of function '%v'", fn.ObjectMeta.Name))
|
||||
}
|
||||
@@ -200,15 +199,15 @@ func UpdateFunctionPackageResourceVersion(client client.Interface, pkgMeta *meta
|
||||
return errs.ErrorOrNil()
|
||||
}
|
||||
|
||||
func updatePackageStatus(client client.Interface, pkg *fv1.Package, status fv1.BuildStatus) (*metav1.ObjectMeta, error) {
|
||||
func updatePackageStatus(ctx context.Context, client cmd.Client, 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: metav1.Time{Time: time.Now().UTC()},
|
||||
}
|
||||
pkg, err := client.V1().Package().Update(pkg)
|
||||
return pkg, err
|
||||
pkg, err := client.FissionClientSet.CoreV1().Packages(pkg.Namespace).Update(ctx, pkg, metav1.UpdateOptions{})
|
||||
return &pkg.ObjectMeta, err
|
||||
}
|
||||
return nil, errors.New("unknown package status")
|
||||
}
|
||||
|
||||
@@ -31,11 +31,13 @@ import (
|
||||
|
||||
fv1 "github.com/fission/fission/pkg/apis/core/v1"
|
||||
"github.com/fission/fission/pkg/controller/client"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||
"github.com/fission/fission/pkg/fission-cli/util"
|
||||
storageSvcClient "github.com/fission/fission/pkg/storagesvc/client"
|
||||
"github.com/fission/fission/pkg/utils"
|
||||
)
|
||||
|
||||
func UploadArchiveFile(ctx context.Context, client client.Interface, fileName string) (*fv1.Archive, error) {
|
||||
func UploadArchiveFile(ctx context.Context, client cmd.Client, fileName string) (*fv1.Archive, error) {
|
||||
var archive fv1.Archive
|
||||
|
||||
size, err := utils.FileSize(fileName)
|
||||
@@ -50,26 +52,24 @@ func UploadArchiveFile(ctx context.Context, client client.Interface, fileName st
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
u := strings.TrimSuffix(client.ServerURL(), "/") + "/proxy/storage"
|
||||
ssClient := storageSvcClient.MakeClient(u)
|
||||
|
||||
storagesvcURL, err := util.GetStorageURL(ctx, client)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error getting fission storage service URL")
|
||||
}
|
||||
|
||||
storageClient := storageSvcClient.MakeClient(storagesvcURL.String())
|
||||
// TODO add a progress bar
|
||||
id, err := ssClient.Upload(ctx, fileName, nil)
|
||||
id, err := storageClient.Upload(ctx, fileName, nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error uploading file %v", fileName)
|
||||
return nil, errors.Wrapf(err, "error uploading to fission storage service")
|
||||
}
|
||||
|
||||
storageSvc, err := client.V1().Misc().GetSvcURL("application=fission-storage")
|
||||
storageSvcURL := "http://" + storageSvc
|
||||
archiveURL, err := getArchiveURL(ctx, client, id, storagesvcURL)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error getting fission storage service name")
|
||||
return nil, errors.Wrapf(err, "could not get URL of archive")
|
||||
}
|
||||
|
||||
// We make a new client with actual URL of Storage service so that the URL is not
|
||||
// pointing to 127.0.0.1 i.e. proxy. DON'T reuse previous ssClient
|
||||
pkgClient := storageSvcClient.MakeClient(storageSvcURL)
|
||||
archiveURL := pkgClient.GetUrl(id)
|
||||
|
||||
archive.Type = fv1.ArchiveTypeUrl
|
||||
archive.URL = archiveURL
|
||||
|
||||
@@ -84,6 +84,42 @@ func UploadArchiveFile(ctx context.Context, client client.Interface, fileName st
|
||||
return &archive, nil
|
||||
}
|
||||
|
||||
func getArchiveURL(ctx context.Context, client cmd.Client, archiveID string, serverURL *url.URL) (archiveURL string, err error) {
|
||||
relativeURL, _ := url.Parse(util.FISSION_STORAGE_URI)
|
||||
|
||||
queryString := relativeURL.Query()
|
||||
queryString.Set("id", archiveID)
|
||||
relativeURL.RawQuery = queryString.Encode()
|
||||
|
||||
storageAccessURL := serverURL.ResolveReference(relativeURL)
|
||||
|
||||
resp, err := http.Head(storageAccessURL.String())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("error getting URL. Exited with Status: %s", resp.Status)
|
||||
}
|
||||
|
||||
storageType := resp.Header.Get("X-FISSION-STORAGETYPE")
|
||||
|
||||
if storageType == "local" {
|
||||
storageSvc, err := util.GetSvcName(ctx, client.KubernetesClient, "fission-storage")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
storagesvcURL := "http://" + storageSvc
|
||||
client := storageSvcClient.MakeClient(storagesvcURL)
|
||||
return client.GetUrl(archiveID), nil
|
||||
} else if storageType == "s3" {
|
||||
storageBucket := resp.Header.Get("X-FISSION-BUCKET")
|
||||
s3url := fmt.Sprintf("https://%s.s3.amazonaws.com/%s", storageBucket, archiveID)
|
||||
return s3url, nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
func GetContents(filePath string) ([]byte, error) {
|
||||
code, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
@@ -133,6 +169,42 @@ func DownloadURL(fileUrl string) (io.ReadCloser, error) {
|
||||
return resp.Body, nil
|
||||
}
|
||||
|
||||
func DownloadStrorageURL(ctx context.Context, client cmd.Client, fileUrl string) (io.ReadCloser, error) {
|
||||
var resp *http.Response
|
||||
storageSvc, err := util.GetSvcName(ctx, client.KubernetesClient, "fission-storage")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if strings.HasPrefix(fileUrl, "http://"+storageSvc+"/v1/archive?id=") {
|
||||
url, err := url.Parse(fileUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id := url.Query().Get("id")
|
||||
storageAccessURL, err := util.GetStorageURL(ctx, client)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client := storageSvcClient.MakeClient(storageAccessURL.String())
|
||||
resp, err = client.GetFile(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
resp, err = http.Get(fileUrl)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("%v - HTTP response returned non 200 status", resp.StatusCode)
|
||||
}
|
||||
return resp.Body, nil
|
||||
}
|
||||
|
||||
func WriteArchiveToFile(fileName string, reader io.Reader) error {
|
||||
tmpDir, err := utils.GetTempDir()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user