Added code to get the log object from GCS.

Added switch statement for GCS versus S3.
This commit is contained in:
Aleksandr Melnikov
2020-07-08 13:17:07 -07:00
parent 8d662d6ce0
commit 66431a21eb

View File

@@ -2,6 +2,7 @@ package v1
import ( import (
"bufio" "bufio"
"cloud.google.com/go/storage"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@@ -10,6 +11,7 @@ import (
"github.com/onepanelio/core/pkg/util/pagination" "github.com/onepanelio/core/pkg/util/pagination"
"github.com/onepanelio/core/pkg/util/ptr" "github.com/onepanelio/core/pkg/util/ptr"
uid2 "github.com/onepanelio/core/pkg/util/uid" uid2 "github.com/onepanelio/core/pkg/util/uid"
"golang.org/x/net/context"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io" "io"
"io/ioutil" "io/ioutil"
@@ -792,6 +794,7 @@ func (c *Client) GetWorkflowExecutionLogs(namespace, uid, podName, containerName
var ( var (
stream io.ReadCloser stream io.ReadCloser
s3Client *s3.Client s3Client *s3.Client
gcsClient *storage.Client
config *NamespaceConfig config *NamespaceConfig
endOffset int endOffset int
) )
@@ -809,37 +812,59 @@ func (c *Client) GetWorkflowExecutionLogs(namespace, uid, podName, containerName
return nil, util.NewUserError(codes.NotFound, "Can't get configuration.") return nil, util.NewUserError(codes.NotFound, "Can't get configuration.")
} }
s3Client, err = c.GetS3Client(namespace, config.ArtifactRepository.S3) switch {
if err != nil { case config.ArtifactRepository.S3 != nil:
log.WithFields(log.Fields{ {
"Namespace": namespace, s3Client, err = c.GetS3Client(namespace, config.ArtifactRepository.S3)
"UID": uid, if err != nil {
"PodName": podName, log.WithFields(log.Fields{
"ContainerName": containerName, "Namespace": namespace,
"Error": err.Error(), "UID": uid,
}).Error("Can't connect to S3 storage.") "PodName": podName,
return nil, util.NewUserError(codes.NotFound, "Can't connect to S3 storage.") "ContainerName": containerName,
} "Error": err.Error(),
}).Error("Can't connect to S3 storage.")
return nil, util.NewUserError(codes.NotFound, "Can't connect to S3 storage.")
}
opts := s3.GetObjectOptions{} opts := s3.GetObjectOptions{}
endOffset, err = strconv.Atoi(readEndOffset) endOffset, err = strconv.Atoi(readEndOffset)
if err != nil { if err != nil {
return nil, util.NewUserError(codes.InvalidArgument, "Invalid range.") return nil, util.NewUserError(codes.InvalidArgument, "Invalid range.")
} }
err = opts.SetRange(0, int64(endOffset)) err = opts.SetRange(0, int64(endOffset))
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"Namespace": namespace, "Namespace": namespace,
"UID": uid, "UID": uid,
"PodName": podName, "PodName": podName,
"ContainerName": containerName, "ContainerName": containerName,
"Error": err.Error(), "Error": err.Error(),
}).Error("Can't set range.") }).Error("Can't set range.")
return nil, util.NewUserError(codes.NotFound, "Can't connect to S3 storage.") return nil, util.NewUserError(codes.NotFound, "Can't connect to S3 storage.")
} }
key := config.ArtifactRepository.S3.FormatKey(namespace, uid, podName) + "/" + containerName + ".log" key := config.ArtifactRepository.S3.FormatKey(namespace, uid, podName) + "/" + containerName + ".log"
stream, err = s3Client.GetObject(config.ArtifactRepository.S3.Bucket, key, opts) stream, err = s3Client.GetObject(config.ArtifactRepository.S3.Bucket, key, opts)
}
case config.ArtifactRepository.GCS != nil:
{
ctx := context.Background()
gcsClient, err = c.GetGCSClient(namespace, config.ArtifactRepository.GCS)
if err != nil {
log.WithFields(log.Fields{
"Namespace": namespace,
"UID": uid,
"PodName": podName,
"ContainerName": containerName,
"Error": err.Error(),
}).Error("Can't connect to GCS storage.")
return nil, util.NewUserError(codes.NotFound, "Can't connect to GCS storage.")
}
key := config.ArtifactRepository.GCS.FormatKey(namespace, uid, podName) + "/" + containerName + ".log"
stream, err = gcsClient.Bucket(config.ArtifactRepository.GCS.Bucket).Object(key).NewReader(ctx)
}
}
} else { } else {
stream, err = c.CoreV1().Pods(namespace).GetLogs(podName, &corev1.PodLogOptions{ stream, err = c.CoreV1().Pods(namespace).GetLogs(podName, &corev1.PodLogOptions{
Container: containerName, Container: containerName,