github.com/argoproj/argo-events@v1.9.1/sensors/artifacts/store.go (about)

     1  /*
     2  Copyright 2018 BlackRock, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package artifacts
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	"github.com/ghodss/yaml"
    24  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    25  
    26  	"github.com/argoproj/argo-events/common"
    27  	"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
    28  )
    29  
    30  // ArtifactReader enables reading artifacts from an external store
    31  type ArtifactReader interface {
    32  	Read() ([]byte, error)
    33  }
    34  
    35  // FetchArtifact from the location, decode it using explicit types, and unstructure it
    36  func FetchArtifact(reader ArtifactReader) (*unstructured.Unstructured, error) {
    37  	var obj []byte
    38  
    39  	if err := common.DoWithRetry(&common.DefaultBackoff, func() error {
    40  		var e error
    41  		obj, e = reader.Read()
    42  		return e
    43  	}); err != nil {
    44  		return nil, fmt.Errorf("failed to fetch artifact, %w", err)
    45  	}
    46  	return decodeAndUnstructure(obj)
    47  }
    48  
    49  type Credentials struct {
    50  	accessKey string
    51  	secretKey string
    52  }
    53  
    54  // GetCredentials for this minio
    55  func GetCredentials(art *v1alpha1.ArtifactLocation) (*Credentials, error) {
    56  	if art.S3 != nil {
    57  		accessKey, err := common.GetSecretFromVolume(art.S3.AccessKey)
    58  		if err != nil {
    59  			return nil, fmt.Errorf("failed to retrieve accessKey, %w", err)
    60  		}
    61  		secretKey, err := common.GetSecretFromVolume(art.S3.SecretKey)
    62  		if err != nil {
    63  			return nil, fmt.Errorf("failed to retrieve secretKey, %w", err)
    64  		}
    65  		return &Credentials{
    66  			accessKey: strings.TrimSpace(accessKey),
    67  			secretKey: strings.TrimSpace(secretKey),
    68  		}, nil
    69  	}
    70  
    71  	return nil, nil
    72  }
    73  
    74  // GetArtifactReader returns the ArtifactReader for this location
    75  func GetArtifactReader(loc *v1alpha1.ArtifactLocation, creds *Credentials) (ArtifactReader, error) {
    76  	if loc.S3 != nil {
    77  		return NewS3Reader(loc.S3, creds)
    78  	}
    79  	if loc.Inline != nil {
    80  		return NewInlineReader(loc.Inline)
    81  	}
    82  	if loc.File != nil {
    83  		return NewFileReader(loc.File)
    84  	}
    85  	if loc.URL != nil {
    86  		return NewURLReader(loc.URL)
    87  	}
    88  	if loc.Git != nil {
    89  		return NewGitReader(loc.Git)
    90  	}
    91  	if loc.Configmap != nil {
    92  		return NewConfigMapReader(loc.Configmap)
    93  	}
    94  	if loc.Resource != nil {
    95  		return NewResourceReader(loc.Resource)
    96  	}
    97  	return nil, fmt.Errorf("unknown artifact location: %v", *loc)
    98  }
    99  
   100  func decodeAndUnstructure(b []byte) (*unstructured.Unstructured, error) {
   101  	var result map[string]interface{}
   102  	if err := yaml.Unmarshal(b, &result); err != nil {
   103  		return nil, err
   104  	}
   105  	return &unstructured.Unstructured{Object: result}, nil
   106  }