github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/cmd/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  
    28  	"k8s.io/helm/pkg/chartutil"
    29  	"k8s.io/helm/pkg/engine"
    30  	"k8s.io/helm/pkg/kube"
    31  	"k8s.io/helm/pkg/proto/hapi/chart"
    32  	"k8s.io/helm/pkg/storage"
    33  	"k8s.io/helm/pkg/storage/driver"
    34  	"k8s.io/kubernetes/pkg/client/unversioned"
    35  	"k8s.io/kubernetes/pkg/client/unversioned/testclient"
    36  )
    37  
    38  // TillerNamespace is the namespace tiller is running in.
    39  const TillerNamespace = "kube-system"
    40  
    41  // GoTplEngine is the name of the Go template engine, as registered in the EngineYard.
    42  const GoTplEngine = "gotpl"
    43  
    44  // DefaultEngine points to the engine that the EngineYard should treat as the
    45  // default. A chart that does not specify an engine may be run through the
    46  // default engine.
    47  var DefaultEngine = GoTplEngine
    48  
    49  // EngineYard maps engine names to engine implementations.
    50  type EngineYard map[string]Engine
    51  
    52  // Get retrieves a template engine by name.
    53  //
    54  // If no matching template engine is found, the second return value will
    55  // be false.
    56  func (y EngineYard) Get(k string) (Engine, bool) {
    57  	e, ok := y[k]
    58  	return e, ok
    59  }
    60  
    61  // Default returns the default template engine.
    62  //
    63  // The default is specified by DefaultEngine.
    64  //
    65  // If the default template engine cannot be found, this panics.
    66  func (y EngineYard) Default() Engine {
    67  	d, ok := y[DefaultEngine]
    68  	if !ok {
    69  		// This is a developer error!
    70  		panic("Default template engine does not exist")
    71  	}
    72  	return d
    73  }
    74  
    75  // Engine represents a template engine that can render templates.
    76  //
    77  // For some engines, "rendering" includes both compiling and executing. (Other
    78  // engines do not distinguish between phases.)
    79  //
    80  // The engine returns a map where the key is the named output entity (usually
    81  // a file name) and the value is the rendered content of the template.
    82  //
    83  // An Engine must be capable of executing multiple concurrent requests, but
    84  // without tainting one request's environment with data from another request.
    85  type Engine interface {
    86  	// Render renders a chart.
    87  	//
    88  	// It receives a chart, a config, and a map of overrides to the config.
    89  	// Overrides are assumed to be passed from the system, not the user.
    90  	Render(*chart.Chart, chartutil.Values) (map[string]string, error)
    91  }
    92  
    93  // KubeClient represents a client capable of communicating with the Kubernetes API.
    94  //
    95  // A KubeClient must be concurrency safe.
    96  type KubeClient interface {
    97  	// Create creates one or more resources.
    98  	//
    99  	// namespace must contain a valid existing namespace.
   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) 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) 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) error
   137  
   138  	// APIClient gets a raw API client for Kubernetes.
   139  	APIClient() (unversioned.Interface, error)
   140  }
   141  
   142  // PrintingKubeClient implements KubeClient, but simply prints the reader to
   143  // the given output.
   144  type PrintingKubeClient struct {
   145  	Out io.Writer
   146  }
   147  
   148  // APIClient always returns an error.
   149  //
   150  // The printing client does not have access to a Kubernetes client at all. So it
   151  // will always return an error if the client is accessed.
   152  func (p *PrintingKubeClient) APIClient() (unversioned.Interface, error) {
   153  	return testclient.NewSimpleFake(), nil
   154  }
   155  
   156  // Create prints the values of what would be created with a real KubeClient.
   157  func (p *PrintingKubeClient) Create(ns string, r io.Reader) error {
   158  	_, err := io.Copy(p.Out, r)
   159  	return err
   160  }
   161  
   162  // Get prints the values of what would be created with a real KubeClient.
   163  func (p *PrintingKubeClient) Get(ns string, r io.Reader) (string, error) {
   164  	_, err := io.Copy(p.Out, r)
   165  	return "", err
   166  }
   167  
   168  // Delete implements KubeClient delete.
   169  //
   170  // It only prints out the content to be deleted.
   171  func (p *PrintingKubeClient) Delete(ns string, r io.Reader) error {
   172  	_, err := io.Copy(p.Out, r)
   173  	return err
   174  }
   175  
   176  // WatchUntilReady implements KubeClient WatchUntilReady.
   177  func (p *PrintingKubeClient) WatchUntilReady(ns string, r io.Reader) error {
   178  	_, err := io.Copy(p.Out, r)
   179  	return err
   180  }
   181  
   182  // Update implements KubeClient Update.
   183  func (p *PrintingKubeClient) Update(ns string, currentReader, modifiedReader io.Reader) error {
   184  	_, err := io.Copy(p.Out, modifiedReader)
   185  	return err
   186  }
   187  
   188  // Environment provides the context for executing a client request.
   189  //
   190  // All services in a context are concurrency safe.
   191  type Environment struct {
   192  	// EngineYard provides access to the known template engines.
   193  	EngineYard EngineYard
   194  	// Releases stores records of releases.
   195  	Releases *storage.Storage
   196  	// KubeClient is a Kubernetes API client.
   197  	KubeClient KubeClient
   198  }
   199  
   200  // New returns an environment initialized with the defaults.
   201  func New() *Environment {
   202  	e := engine.New()
   203  	var ey EngineYard = map[string]Engine{
   204  		// Currently, the only template engine we support is the GoTpl one. But
   205  		// we can easily add some here.
   206  		GoTplEngine: e,
   207  	}
   208  
   209  	return &Environment{
   210  		EngineYard: ey,
   211  		Releases:   storage.Init(driver.NewMemory()),
   212  		KubeClient: kube.New(nil),
   213  	}
   214  }