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
@@ -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