github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+incompatible/pkg/tiller/environment/environment.go (about)

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