github.com/wangchanggan/helm@v0.0.0-20211020154240-11b1b7d5406d/pkg/getter/plugingetter.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  	"os"
    22  	"os/exec"
    23  	"path/filepath"
    24  	"strings"
    25  
    26  	"k8s.io/helm/pkg/helm/environment"
    27  	"k8s.io/helm/pkg/plugin"
    28  )
    29  
    30  // collectPlugins scans for getter plugins.
    31  // This will load plugins according to the environment.
    32  func collectPlugins(settings environment.EnvSettings) (Providers, error) {
    33  	plugins, err := plugin.FindPlugins(settings.PluginDirs())
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	var result Providers
    38  	for _, plugin := range plugins {
    39  		for _, downloader := range plugin.Metadata.Downloaders {
    40  			result = append(result, Provider{
    41  				Schemes: downloader.Protocols,
    42  				New: newPluginGetter(
    43  					downloader.Command,
    44  					settings,
    45  					plugin.Metadata.Name,
    46  					plugin.Dir,
    47  				),
    48  			})
    49  		}
    50  	}
    51  	return result, nil
    52  }
    53  
    54  // pluginGetter is a generic type to invoke custom downloaders,
    55  // implemented in plugins.
    56  type pluginGetter struct {
    57  	command                   string
    58  	certFile, keyFile, cAFile string
    59  	settings                  environment.EnvSettings
    60  	name                      string
    61  	base                      string
    62  }
    63  
    64  // Get runs downloader plugin command
    65  func (p *pluginGetter) Get(href string) (*bytes.Buffer, error) {
    66  	commands := strings.Split(p.command, " ")
    67  	argv := append(commands[1:], p.certFile, p.keyFile, p.cAFile, href)
    68  	prog := exec.Command(filepath.Join(p.base, commands[0]), argv...)
    69  	plugin.SetupPluginEnv(p.settings, p.name, p.base)
    70  	prog.Env = os.Environ()
    71  	buf := bytes.NewBuffer(nil)
    72  	prog.Stdout = buf
    73  	prog.Stderr = os.Stderr
    74  	prog.Stdin = os.Stdin
    75  	if err := prog.Run(); err != nil {
    76  		if eerr, ok := err.(*exec.ExitError); ok {
    77  			os.Stderr.Write(eerr.Stderr)
    78  			return nil, fmt.Errorf("plugin %q exited with error", p.command)
    79  		}
    80  		return nil, err
    81  	}
    82  	return buf, nil
    83  }
    84  
    85  // newPluginGetter constructs a valid plugin getter
    86  func newPluginGetter(command string, settings environment.EnvSettings, name, base string) Constructor {
    87  	return func(URL, CertFile, KeyFile, CAFile string) (Getter, error) {
    88  		result := &pluginGetter{
    89  			command:  command,
    90  			certFile: CertFile,
    91  			keyFile:  KeyFile,
    92  			cAFile:   CAFile,
    93  			settings: settings,
    94  			name:     name,
    95  			base:     base,
    96  		}
    97  		return result, nil
    98  	}
    99  }