github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/flagutil/storage.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     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 flagutil
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  
    24  	"sigs.k8s.io/prow/pkg/io"
    25  )
    26  
    27  type StorageClientOptions struct {
    28  	// GCSCredentialsFile is used for reading/writing to GCS block storage.
    29  	// It's optional, if you want to write to local paths or GCS credentials auto-discovery is used.
    30  	// If set, this file is used to read/write to gs:// paths
    31  	// If not, credential auto-discovery is used
    32  	GCSCredentialsFile string `json:"gcs_credentials_file,omitempty"`
    33  	// S3CredentialsFile is used for reading/writing to s3 block storage.
    34  	// It's optional, if you want to write to local paths or S3 credentials auto-discovery is used.
    35  	// If set, this file is used to read/write to s3:// paths
    36  	// If not, go cloud credential auto-discovery is used
    37  	// For more details see the prow/io/providers pkg.
    38  	S3CredentialsFile string `json:"s3_credentials_file,omitempty"`
    39  }
    40  
    41  // AddFlags injects status client options into the given FlagSet.
    42  func (o *StorageClientOptions) AddFlags(fs *flag.FlagSet) {
    43  	fs.StringVar(&o.GCSCredentialsFile, "gcs-credentials-file", "", "File where GCS credentials are stored")
    44  	fs.StringVar(&o.S3CredentialsFile, "s3-credentials-file", "", "File where s3 credentials are stored. For the exact format see https://github.com/kubernetes-sigs/prow/blob/main/pkg/io/providers/providers.go")
    45  }
    46  
    47  func (o *StorageClientOptions) HasGCSCredentials() bool {
    48  	return o.GCSCredentialsFile != ""
    49  }
    50  
    51  func (o *StorageClientOptions) HasS3Credentials() bool {
    52  	return o.S3CredentialsFile != ""
    53  }
    54  
    55  // Validate validates options.
    56  func (o *StorageClientOptions) Validate(dryRun bool) error {
    57  	return nil
    58  }
    59  
    60  // StorageClient returns a Storage client.
    61  func (o *StorageClientOptions) StorageClient(ctx context.Context) (io.Opener, error) {
    62  	opener, err := io.NewOpener(ctx, o.GCSCredentialsFile, o.S3CredentialsFile)
    63  	if err != nil {
    64  		message := ""
    65  		if o.GCSCredentialsFile != "" {
    66  			message = fmt.Sprintf(" gcs-credentials-file: %s", o.GCSCredentialsFile)
    67  		}
    68  		if o.S3CredentialsFile != "" {
    69  			message = fmt.Sprintf("%s s3-credentials-file: %s", message, o.S3CredentialsFile)
    70  		}
    71  		return opener, fmt.Errorf("error creating opener%s: %w", message, err)
    72  	}
    73  	return opener, nil
    74  }