github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/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 "github.com/stefanmcshane/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 // IsReachable checks if the cluster is reachable 37 func (p *PrintingKubeClient) IsReachable() error { 38 return nil 39 } 40 41 // Create prints the values of what would be created with a real KubeClient. 42 func (p *PrintingKubeClient) Create(resources kube.ResourceList) (*kube.Result, error) { 43 _, err := io.Copy(p.Out, bufferize(resources)) 44 if err != nil { 45 return nil, err 46 } 47 return &kube.Result{Created: resources}, nil 48 } 49 50 func (p *PrintingKubeClient) Wait(resources kube.ResourceList, _ time.Duration) error { 51 _, err := io.Copy(p.Out, bufferize(resources)) 52 return err 53 } 54 55 func (p *PrintingKubeClient) WaitWithJobs(resources kube.ResourceList, _ time.Duration) error { 56 _, err := io.Copy(p.Out, bufferize(resources)) 57 return err 58 } 59 60 func (p *PrintingKubeClient) WaitForDelete(resources kube.ResourceList, _ time.Duration) error { 61 _, err := io.Copy(p.Out, bufferize(resources)) 62 return err 63 } 64 65 // Delete implements KubeClient delete. 66 // 67 // It only prints out the content to be deleted. 68 func (p *PrintingKubeClient) Delete(resources kube.ResourceList) (*kube.Result, []error) { 69 _, err := io.Copy(p.Out, bufferize(resources)) 70 if err != nil { 71 return nil, []error{err} 72 } 73 return &kube.Result{Deleted: resources}, nil 74 } 75 76 // WatchUntilReady implements KubeClient WatchUntilReady. 77 func (p *PrintingKubeClient) WatchUntilReady(resources kube.ResourceList, _ time.Duration) error { 78 _, err := io.Copy(p.Out, bufferize(resources)) 79 return err 80 } 81 82 // Update implements KubeClient Update. 83 func (p *PrintingKubeClient) Update(_, modified kube.ResourceList, _ bool) (*kube.Result, error) { 84 _, err := io.Copy(p.Out, bufferize(modified)) 85 if err != nil { 86 return nil, err 87 } 88 // TODO: This doesn't completely mock out have some that get created, 89 // updated, and deleted. I don't think these are used in any unit tests, but 90 // we may want to refactor a way to handle future tests 91 return &kube.Result{Updated: modified}, nil 92 } 93 94 // Build implements KubeClient Build. 95 func (p *PrintingKubeClient) Build(_ io.Reader, _ bool) (kube.ResourceList, error) { 96 return []*resource.Info{}, nil 97 } 98 99 // WaitAndGetCompletedPodPhase implements KubeClient WaitAndGetCompletedPodPhase. 100 func (p *PrintingKubeClient) WaitAndGetCompletedPodPhase(_ string, _ time.Duration) (v1.PodPhase, error) { 101 return v1.PodSucceeded, nil 102 } 103 104 func bufferize(resources kube.ResourceList) io.Reader { 105 var builder strings.Builder 106 for _, info := range resources { 107 builder.WriteString(info.String() + "\n") 108 } 109 return strings.NewReader(builder.String()) 110 }