...

Source file src/helm.sh/helm/v3/pkg/kube/fake/printer.go

Documentation: helm.sh/helm/v3/pkg/kube/fake

     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  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"k8s.io/cli-runtime/pkg/resource"
    28  
    29  	"helm.sh/helm/v3/pkg/kube"
    30  )
    31  
    32  // PrintingKubeClient implements KubeClient, but simply prints the reader to
    33  // the given output.
    34  type PrintingKubeClient struct {
    35  	Out io.Writer
    36  }
    37  
    38  // IsReachable checks if the cluster is reachable
    39  func (p *PrintingKubeClient) IsReachable() error {
    40  	return nil
    41  }
    42  
    43  // Create prints the values of what would be created with a real KubeClient.
    44  func (p *PrintingKubeClient) Create(resources kube.ResourceList) (*kube.Result, error) {
    45  	_, err := io.Copy(p.Out, bufferize(resources))
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	return &kube.Result{Created: resources}, nil
    50  }
    51  
    52  func (p *PrintingKubeClient) Get(resources kube.ResourceList, _ bool) (map[string][]runtime.Object, error) {
    53  	_, err := io.Copy(p.Out, bufferize(resources))
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	return make(map[string][]runtime.Object), nil
    58  }
    59  
    60  func (p *PrintingKubeClient) Wait(resources kube.ResourceList, _ time.Duration) error {
    61  	_, err := io.Copy(p.Out, bufferize(resources))
    62  	return err
    63  }
    64  
    65  func (p *PrintingKubeClient) WaitWithJobs(resources kube.ResourceList, _ time.Duration) error {
    66  	_, err := io.Copy(p.Out, bufferize(resources))
    67  	return err
    68  }
    69  
    70  func (p *PrintingKubeClient) WaitForDelete(resources kube.ResourceList, _ time.Duration) error {
    71  	_, err := io.Copy(p.Out, bufferize(resources))
    72  	return err
    73  }
    74  
    75  // Delete implements KubeClient delete.
    76  //
    77  // It only prints out the content to be deleted.
    78  func (p *PrintingKubeClient) Delete(resources kube.ResourceList) (*kube.Result, []error) {
    79  	_, err := io.Copy(p.Out, bufferize(resources))
    80  	if err != nil {
    81  		return nil, []error{err}
    82  	}
    83  	return &kube.Result{Deleted: resources}, nil
    84  }
    85  
    86  // WatchUntilReady implements KubeClient WatchUntilReady.
    87  func (p *PrintingKubeClient) WatchUntilReady(resources kube.ResourceList, _ time.Duration) error {
    88  	_, err := io.Copy(p.Out, bufferize(resources))
    89  	return err
    90  }
    91  
    92  // Update implements KubeClient Update.
    93  func (p *PrintingKubeClient) Update(_, modified kube.ResourceList, _ bool) (*kube.Result, error) {
    94  	_, err := io.Copy(p.Out, bufferize(modified))
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	// TODO: This doesn't completely mock out have some that get created,
    99  	// updated, and deleted. I don't think these are used in any unit tests, but
   100  	// we may want to refactor a way to handle future tests
   101  	return &kube.Result{Updated: modified}, nil
   102  }
   103  
   104  // Build implements KubeClient Build.
   105  func (p *PrintingKubeClient) Build(_ io.Reader, _ bool) (kube.ResourceList, error) {
   106  	return []*resource.Info{}, nil
   107  }
   108  
   109  // BuildTable implements KubeClient BuildTable.
   110  func (p *PrintingKubeClient) BuildTable(_ io.Reader, _ bool) (kube.ResourceList, error) {
   111  	return []*resource.Info{}, nil
   112  }
   113  
   114  // WaitAndGetCompletedPodPhase implements KubeClient WaitAndGetCompletedPodPhase.
   115  func (p *PrintingKubeClient) WaitAndGetCompletedPodPhase(_ string, _ time.Duration) (v1.PodPhase, error) {
   116  	return v1.PodSucceeded, nil
   117  }
   118  
   119  // DeleteWithPropagationPolicy implements KubeClient delete.
   120  //
   121  // It only prints out the content to be deleted.
   122  func (p *PrintingKubeClient) DeleteWithPropagationPolicy(resources kube.ResourceList, _ metav1.DeletionPropagation) (*kube.Result, []error) {
   123  	_, err := io.Copy(p.Out, bufferize(resources))
   124  	if err != nil {
   125  		return nil, []error{err}
   126  	}
   127  	return &kube.Result{Deleted: resources}, nil
   128  }
   129  
   130  func bufferize(resources kube.ResourceList) io.Reader {
   131  	var builder strings.Builder
   132  	for _, info := range resources {
   133  		builder.WriteString(info.String() + "\n")
   134  	}
   135  	return strings.NewReader(builder.String())
   136  }
   137  

View as plain text