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