gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/cache/s3/adapter.go (about)

     1  package s3
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"time"
     7  
     8  	"github.com/sirupsen/logrus"
     9  
    10  	"gitlab.com/gitlab-org/gitlab-runner/cache"
    11  	"gitlab.com/gitlab-org/gitlab-runner/common"
    12  )
    13  
    14  type s3Adapter struct {
    15  	timeout    time.Duration
    16  	config     *common.CacheS3Config
    17  	objectName string
    18  	client     minioClient
    19  }
    20  
    21  func (a *s3Adapter) GetDownloadURL() *url.URL {
    22  	URL, err := a.client.PresignedGetObject(a.config.BucketName, a.objectName, a.timeout, nil)
    23  	if err != nil {
    24  		logrus.WithError(err).Error("error while generating S3 pre-signed URL")
    25  
    26  		return nil
    27  	}
    28  
    29  	return URL
    30  }
    31  
    32  func (a *s3Adapter) GetUploadURL() *url.URL {
    33  	URL, err := a.client.PresignedPutObject(a.config.BucketName, a.objectName, a.timeout)
    34  	if err != nil {
    35  		logrus.WithError(err).Error("error while generating S3 pre-signed URL")
    36  
    37  		return nil
    38  	}
    39  
    40  	return URL
    41  }
    42  
    43  func New(config *common.CacheConfig, timeout time.Duration, objectName string) (cache.Adapter, error) {
    44  	c := deprecatedConfigHandler(config)
    45  
    46  	s3 := c.S3
    47  	if s3 == nil {
    48  		return nil, fmt.Errorf("missing S3 configuration")
    49  	}
    50  
    51  	client, err := newMinioClient(s3)
    52  	if err != nil {
    53  		return nil, fmt.Errorf("error while creating S3 cache storage client: %v", err)
    54  	}
    55  
    56  	a := &s3Adapter{
    57  		config:     s3,
    58  		timeout:    timeout,
    59  		objectName: objectName,
    60  		client:     client,
    61  	}
    62  
    63  	return a, nil
    64  }
    65  
    66  // TODO: Remove in 12.0
    67  var deprecatedConfigHandler = func(config *common.CacheConfig) *common.CacheConfig {
    68  	if config.S3 != nil {
    69  		return config
    70  	}
    71  
    72  	logrus.Warningln("Runner uses S3 caching with deprecated configuration format. Support for deprecated format will be removed in GitLab Runner 12.0")
    73  
    74  	config.S3 = &common.CacheS3Config{
    75  		ServerAddress:  config.GetServerAddress(),
    76  		AccessKey:      config.GetAccessKey(),
    77  		SecretKey:      config.GetSecretKey(),
    78  		BucketName:     config.GetBucketName(),
    79  		BucketLocation: config.GetBucketLocation(),
    80  		Insecure:       config.GetInsecure(),
    81  	}
    82  
    83  	return config
    84  }
    85  
    86  func init() {
    87  	err := cache.Factories().Register("s3", New)
    88  	if err != nil {
    89  		panic(err)
    90  	}
    91  }