github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/getter/ocigetter.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package getter
    17  
    18  import (
    19  	"bytes"
    20  	"fmt"
    21  	"strings"
    22  
    23  	"github.com/stefanmcshane/helm/pkg/registry"
    24  )
    25  
    26  // OCIGetter is the default HTTP(/S) backend handler
    27  type OCIGetter struct {
    28  	opts options
    29  }
    30  
    31  // Get performs a Get from repo.Getter and returns the body.
    32  func (g *OCIGetter) Get(href string, options ...Option) (*bytes.Buffer, error) {
    33  	for _, opt := range options {
    34  		opt(&g.opts)
    35  	}
    36  	return g.get(href)
    37  }
    38  
    39  func (g *OCIGetter) get(href string) (*bytes.Buffer, error) {
    40  	client := g.opts.registryClient
    41  
    42  	ref := strings.TrimPrefix(href, fmt.Sprintf("%s://", registry.OCIScheme))
    43  
    44  	var pullOpts []registry.PullOption
    45  	requestingProv := strings.HasSuffix(ref, ".prov")
    46  	if requestingProv {
    47  		ref = strings.TrimSuffix(ref, ".prov")
    48  		pullOpts = append(pullOpts,
    49  			registry.PullOptWithChart(false),
    50  			registry.PullOptWithProv(true))
    51  	}
    52  
    53  	result, err := client.Pull(ref, pullOpts...)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	if requestingProv {
    59  		return bytes.NewBuffer(result.Prov.Data), nil
    60  	}
    61  	return bytes.NewBuffer(result.Chart.Data), nil
    62  }
    63  
    64  // NewOCIGetter constructs a valid http/https client as a Getter
    65  func NewOCIGetter(ops ...Option) (Getter, error) {
    66  	registryClient, err := registry.NewClient(
    67  		registry.ClientOptEnableCache(true),
    68  	)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	client := OCIGetter{
    74  		opts: options{
    75  			registryClient: registryClient,
    76  		},
    77  	}
    78  
    79  	for _, opt := range ops {
    80  		opt(&client.opts)
    81  	}
    82  
    83  	return &client, nil
    84  }