github.com/Beeketing/helm@v2.12.1+incompatible/pkg/tiller/environment/environment.go (about)

     1  /*
     2  Copyright The Helm 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 environment describes the operating environment for Tiller.
    18  
    19  Tiller's environment encapsulates all of the service dependencies Tiller has.
    20  These dependencies are expressed as interfaces so that alternate implementations
    21  (mocks, etc.) can be easily generated.
    22  */
    23  package environment
    24  
    25  import (
    26  	"io"
    27  	"time"
    28  
    29  	"k8s.io/api/core/v1"
    30  	"k8s.io/cli-runtime/pkg/genericclioptions/resource"
    31  
    32  	"k8s.io/helm/pkg/chartutil"
    33  	"k8s.io/helm/pkg/engine"
    34  	"k8s.io/helm/pkg/kube"
    35  	"k8s.io/helm/pkg/proto/hapi/chart"
    36  	"k8s.io/helm/pkg/storage"
    37  	"k8s.io/helm/pkg/storage/driver"
    38  )
    39  
    40  // DefaultTillerNamespace is the default namespace for Tiller.
    41  const DefaultTillerNamespace = "kube-system"
    42  
    43  // GoTplEngine is the name of the Go template engine, as registered in the EngineYard.
    44  const GoTplEngine = "gotpl"
    45  
    46  // DefaultEngine points to the engine that the EngineYard should treat as the
    47  // default. A chart that does not specify an engine may be run through the
    48  // default engine.
    49  var DefaultEngine = GoTplEngine
    50  
    51  // EngineYard maps engine names to engine implementations.
    52  type EngineYard map[string]Engine
    53  
    54  // Get retrieves a template engine by name.
    55  //
    56  // If no matching template engine is found, the second return value will
    57  // be false.
    58  func (y EngineYard) Get(k string) (Engine, bool) {
    59  	e, ok := y[k]
    60  	return e, ok
    61  }
    62  
    63  // Default returns the default template engine.
    64  //
    65  // The default is specified by DefaultEngine.
    66  //
    67  // If the default template engine cannot be found, this panics.
    68  func (y EngineYard) Default() Engine {
    69  	d, ok := y[DefaultEngine]
    70  	if !ok {
    71  		// This is a developer error!
    72  		panic("Default template engine does not exist")
    73  	}
    74  	return d
    75  }
    76  
    77  // Engine represents a template engine that can render templates.
    78  //
    79  // For some engines, "rendering" includes both compiling and executing. (Other
    80  // engines do not distinguish between phases.)
    81  //
    82  // The engine returns a map where the key is the named output entity (usually
    83  // a file name) and the value is the rendered content of the template.
    84  //
    85  // An Engine must be capable of executing multiple concurrent requests, but
    86  // without tainting one request's environment with data from another request.
    87  type Engine interface {
    88  	// Render renders a chart.
    89  	//
    90  	// It receives a chart, a config, and a map of overrides to the config.
    91  	// Overrides are assumed to be passed from the system, not the user.
    92  	Render(*chart.Chart, chartutil.Values) (map[string]string, error)
    93  }
    94  
    95  // KubeClient represents a client capable of communicating with the Kubernetes API.
    96  //
    97  // A KubeClient must be concurrency safe.
    98  type KubeClient interface {
    99  	// Create creates one or more resources.
   100  	//
   101  	// reader must contain a YAML stream (one or more YAML documents separated
   102  	// by "\n---\n").
   103  	Create(namespace string, reader io.Reader, timeout int64, shouldWait bool) error
   104  
   105  	// Get gets one or more resources. Returned string hsa the format like kubectl
   106  	// provides with the column headers separating the resource types.
   107  	//
   108  	// namespace must contain a valid existing namespace.
   109  	//
   110  	// reader must contain a YAML stream (one or more YAML documents separated
   111  	// by "\n---\n").
   112  	Get(namespace string, reader io.Reader) (string, error)
   113  
   114  	// Delete destroys one or more resources.
   115  	//
   116  	// namespace must contain a valid existing namespace.
   117  	//
   118  	// reader must contain a YAML stream (one or more YAML documents separated
   119  	// by "\n---\n").
   120  	Delete(namespace string, reader io.Reader) error
   121  
   122  	// Watch the resource in reader until it is "ready".
   123  	//
   124  	// For Jobs, "ready" means the job ran to completion (excited without error).
   125  	// For all other kinds, it means the kind was created or modified without
   126  	// error.
   127  	WatchUntilReady(namespace string, reader io.Reader, timeout int64, shouldWait bool) error
   128  
   129  	// Update updates one or more resources or creates the resource
   130  	// if it doesn't exist.
   131  	//
   132  	// namespace must contain a valid existing namespace.
   133  	//
   134  	// reader must contain a YAML stream (one or more YAML documents separated
   135  	// by "\n---\n").
   136  	Update(namespace string, originalReader, modifiedReader io.Reader, force bool, recreate bool, timeout int64, shouldWait bool) error
   137  
   138  	Build(namespace string, reader io.Reader) (kube.Result, error)
   139  	BuildUnstructured(namespace string, reader io.Reader) (kube.Result, error)
   140  
   141  	// WaitAndGetCompletedPodPhase waits up to a timeout until a pod enters a completed phase
   142  	// and returns said phase (PodSucceeded or PodFailed qualify).
   143  	WaitAndGetCompletedPodPhase(namespace string, reader io.Reader, timeout time.Duration) (v1.PodPhase, error)
   144  }
   145  
   146  // PrintingKubeClient implements KubeClient, but simply prints the reader to
   147  // the given output.
   148  type PrintingKubeClient struct {
   149  	Out io.Writer
   150  }
   151  
   152  // Create prints the values of what would be created with a real KubeClient.
   153  func (p *PrintingKubeClient) Create(ns string, r io.Reader, timeout int64, shouldWait bool) error {
   154  	_, err := io.Copy(p.Out, r)
   155  	return err
   156  }
   157  
   158  // Get prints the values of what would be created with a real KubeClient.
   159  func (p *PrintingKubeClient) Get(ns string, r io.Reader) (string, error) {
   160  	_, err := io.Copy(p.Out, r)
   161  	return "", err
   162  }
   163  
   164  // Delete implements KubeClient delete.
   165  //
   166  // It only prints out the content to be deleted.
   167  func (p *PrintingKubeClient) Delete(ns string, r io.Reader) error {
   168  	_, err := io.Copy(p.Out, r)
   169  	return err
   170  }
   171  
   172  // WatchUntilReady implements KubeClient WatchUntilReady.
   173  func (p *PrintingKubeClient) WatchUntilReady(ns string, r io.Reader, timeout int64, shouldWait bool) error {
   174  	_, err := io.Copy(p.Out, r)
   175  	return err
   176  }
   177  
   178  // Update implements KubeClient Update.
   179  func (p *PrintingKubeClient) Update(ns string, currentReader, modifiedReader io.Reader, force bool, recreate bool, timeout int64, shouldWait bool) error {
   180  	_, err := io.Copy(p.Out, modifiedReader)
   181  	return err
   182  }
   183  
   184  // Build implements KubeClient Build.
   185  func (p *PrintingKubeClient) Build(ns string, reader io.Reader) (kube.Result, error) {
   186  	return []*resource.Info{}, nil
   187  }
   188  
   189  // BuildUnstructured implements KubeClient BuildUnstructured.
   190  func (p *PrintingKubeClient) BuildUnstructured(ns string, reader io.Reader) (kube.Result, error) {
   191  	return []*resource.Info{}, nil
   192  }
   193  
   194  // WaitAndGetCompletedPodPhase implements KubeClient WaitAndGetCompletedPodPhase.
   195  func (p *PrintingKubeClient) WaitAndGetCompletedPodPhase(namespace string, reader io.Reader, timeout time.Duration) (v1.PodPhase, error) {
   196  	_, err := io.Copy(p.Out, reader)
   197  	return v1.PodUnknown, err
   198  }
   199  
   200  // Environment provides the context for executing a client request.
   201  //
   202  // All services in a context are concurrency safe.
   203  type Environment struct {
   204  	// EngineYard provides access to the known template engines.
   205  	EngineYard EngineYard
   206  	// Releases stores records of releases.
   207  	Releases *storage.Storage
   208  	// KubeClient is a Kubernetes API client.
   209  	KubeClient KubeClient
   210  }
   211  
   212  // New returns an environment initialized with the defaults.
   213  func New() *Environment {
   214  	e := engine.New()
   215  	var ey EngineYard = map[string]Engine{
   216  		// Currently, the only template engine we support is the GoTpl one. But
   217  		// we can easily add some here.
   218  		GoTplEngine: e,
   219  	}
   220  
   221  	return &Environment{
   222  		EngineYard: ey,
   223  		Releases:   storage.Init(driver.NewMemory()),
   224  	}
   225  }