github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+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/kubernetes/pkg/api" 30 "k8s.io/kubernetes/pkg/kubectl/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 // namespace must contain a valid existing namespace. 102 // 103 // reader must contain a YAML stream (one or more YAML documents separated 104 // by "\n---\n"). 105 Create(namespace string, reader io.Reader, timeout int64, shouldWait bool) error 106 107 // Get gets one or more resources. Returned string hsa the format like kubectl 108 // provides with the column headers separating the resource types. 109 // 110 // namespace must contain a valid existing namespace. 111 // 112 // reader must contain a YAML stream (one or more YAML documents separated 113 // by "\n---\n"). 114 Get(namespace string, reader io.Reader) (string, error) 115 116 // Delete destroys one or more resources. 117 // 118 // namespace must contain a valid existing namespace. 119 // 120 // reader must contain a YAML stream (one or more YAML documents separated 121 // by "\n---\n"). 122 Delete(namespace string, reader io.Reader) error 123 124 // Watch the resource in reader until it is "ready". 125 // 126 // For Jobs, "ready" means the job ran to completion (excited without error). 127 // For all other kinds, it means the kind was created or modified without 128 // error. 129 WatchUntilReady(namespace string, reader io.Reader, timeout int64, shouldWait bool) error 130 131 // Update updates one or more resources or creates the resource 132 // if it doesn't exist. 133 // 134 // namespace must contain a valid existing namespace. 135 // 136 // reader must contain a YAML stream (one or more YAML documents separated 137 // by "\n---\n"). 138 Update(namespace string, originalReader, modifiedReader io.Reader, force bool, recreate bool, timeout int64, shouldWait bool) error 139 140 Build(namespace string, reader io.Reader) (kube.Result, error) 141 BuildUnstructured(namespace string, reader io.Reader) (kube.Result, error) 142 143 // WaitAndGetCompletedPodPhase waits up to a timeout until a pod enters a completed phase 144 // and returns said phase (PodSucceeded or PodFailed qualify). 145 WaitAndGetCompletedPodPhase(namespace string, reader io.Reader, timeout time.Duration) (api.PodPhase, error) 146 } 147 148 // PrintingKubeClient implements KubeClient, but simply prints the reader to 149 // the given output. 150 type PrintingKubeClient struct { 151 Out io.Writer 152 } 153 154 // Create prints the values of what would be created with a real KubeClient. 155 func (p *PrintingKubeClient) Create(ns string, r io.Reader, timeout int64, shouldWait bool) error { 156 _, err := io.Copy(p.Out, r) 157 return err 158 } 159 160 // Get prints the values of what would be created with a real KubeClient. 161 func (p *PrintingKubeClient) Get(ns string, r io.Reader) (string, error) { 162 _, err := io.Copy(p.Out, r) 163 return "", err 164 } 165 166 // Delete implements KubeClient delete. 167 // 168 // It only prints out the content to be deleted. 169 func (p *PrintingKubeClient) Delete(ns string, r io.Reader) error { 170 _, err := io.Copy(p.Out, r) 171 return err 172 } 173 174 // WatchUntilReady implements KubeClient WatchUntilReady. 175 func (p *PrintingKubeClient) WatchUntilReady(ns string, r io.Reader, timeout int64, shouldWait bool) error { 176 _, err := io.Copy(p.Out, r) 177 return err 178 } 179 180 // Update implements KubeClient Update. 181 func (p *PrintingKubeClient) Update(ns string, currentReader, modifiedReader io.Reader, force bool, recreate bool, timeout int64, shouldWait bool) error { 182 _, err := io.Copy(p.Out, modifiedReader) 183 return err 184 } 185 186 // Build implements KubeClient Build. 187 func (p *PrintingKubeClient) Build(ns string, reader io.Reader) (kube.Result, error) { 188 return []*resource.Info{}, nil 189 } 190 191 // BuildUnstructured implements KubeClient BuildUnstructured. 192 func (p *PrintingKubeClient) BuildUnstructured(ns string, reader io.Reader) (kube.Result, error) { 193 return []*resource.Info{}, nil 194 } 195 196 // WaitAndGetCompletedPodPhase implements KubeClient WaitAndGetCompletedPodPhase. 197 func (p *PrintingKubeClient) WaitAndGetCompletedPodPhase(namespace string, reader io.Reader, timeout time.Duration) (api.PodPhase, error) { 198 _, err := io.Copy(p.Out, reader) 199 return api.PodUnknown, err 200 } 201 202 // Environment provides the context for executing a client request. 203 // 204 // All services in a context are concurrency safe. 205 type Environment struct { 206 // EngineYard provides access to the known template engines. 207 EngineYard EngineYard 208 // Releases stores records of releases. 209 Releases *storage.Storage 210 // KubeClient is a Kubernetes API client. 211 KubeClient KubeClient 212 } 213 214 // New returns an environment initialized with the defaults. 215 func New() *Environment { 216 e := engine.New() 217 var ey EngineYard = map[string]Engine{ 218 // Currently, the only template engine we support is the GoTpl one. But 219 // we can easily add some here. 220 GoTplEngine: e, 221 } 222 223 return &Environment{ 224 EngineYard: ey, 225 Releases: storage.Init(driver.NewMemory()), 226 } 227 }