github.com/aaronmell/helm@v3.0.0-beta.2+incompatible/pkg/kube/fake/printer.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 fake 18 19 import ( 20 "io" 21 "strings" 22 "time" 23 24 v1 "k8s.io/api/core/v1" 25 "k8s.io/cli-runtime/pkg/resource" 26 27 "helm.sh/helm/pkg/kube" 28 ) 29 30 // PrintingKubeClient implements KubeClient, but simply prints the reader to 31 // the given output. 32 type PrintingKubeClient struct { 33 Out io.Writer 34 } 35 36 // Create prints the values of what would be created with a real KubeClient. 37 func (p *PrintingKubeClient) Create(resources kube.ResourceList) (*kube.Result, error) { 38 _, err := io.Copy(p.Out, bufferize(resources)) 39 if err != nil { 40 return nil, err 41 } 42 return &kube.Result{Created: resources}, nil 43 } 44 45 func (p *PrintingKubeClient) Wait(resources kube.ResourceList, _ time.Duration) error { 46 _, err := io.Copy(p.Out, bufferize(resources)) 47 return err 48 } 49 50 // Delete implements KubeClient delete. 51 // 52 // It only prints out the content to be deleted. 53 func (p *PrintingKubeClient) Delete(resources kube.ResourceList) (*kube.Result, []error) { 54 _, err := io.Copy(p.Out, bufferize(resources)) 55 if err != nil { 56 return nil, []error{err} 57 } 58 return &kube.Result{Deleted: resources}, nil 59 } 60 61 // WatchUntilReady implements KubeClient WatchUntilReady. 62 func (p *PrintingKubeClient) WatchUntilReady(resources kube.ResourceList, _ time.Duration) error { 63 _, err := io.Copy(p.Out, bufferize(resources)) 64 return err 65 } 66 67 // Update implements KubeClient Update. 68 func (p *PrintingKubeClient) Update(_, modified kube.ResourceList, _ bool) (*kube.Result, error) { 69 _, err := io.Copy(p.Out, bufferize(modified)) 70 if err != nil { 71 return nil, err 72 } 73 // TODO: This doesn't completely mock out have some that get created, 74 // updated, and deleted. I don't think these are used in any unit tests, but 75 // we may want to refactor a way to handle future tests 76 return &kube.Result{Updated: modified}, nil 77 } 78 79 // Build implements KubeClient Build. 80 func (p *PrintingKubeClient) Build(_ io.Reader) (kube.ResourceList, error) { 81 return []*resource.Info{}, nil 82 } 83 84 // WaitAndGetCompletedPodPhase implements KubeClient WaitAndGetCompletedPodPhase. 85 func (p *PrintingKubeClient) WaitAndGetCompletedPodPhase(_ string, _ time.Duration) (v1.PodPhase, error) { 86 return v1.PodSucceeded, nil 87 } 88 89 func bufferize(resources kube.ResourceList) io.Reader { 90 var builder strings.Builder 91 for _, info := range resources { 92 builder.WriteString(info.String() + "\n") 93 } 94 return strings.NewReader(builder.String()) 95 }