github.com/uhthomas/helm@v3.0.0-beta.3+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  // 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  // Delete implements KubeClient delete.
    56  //
    57  // It only prints out the content to be deleted.
    58  func (p *PrintingKubeClient) Delete(resources kube.ResourceList) (*kube.Result, []error) {
    59  	_, err := io.Copy(p.Out, bufferize(resources))
    60  	if err != nil {
    61  		return nil, []error{err}
    62  	}
    63  	return &kube.Result{Deleted: resources}, nil
    64  }
    65  
    66  // WatchUntilReady implements KubeClient WatchUntilReady.
    67  func (p *PrintingKubeClient) WatchUntilReady(resources kube.ResourceList, _ time.Duration) error {
    68  	_, err := io.Copy(p.Out, bufferize(resources))
    69  	return err
    70  }
    71  
    72  // Update implements KubeClient Update.
    73  func (p *PrintingKubeClient) Update(_, modified kube.ResourceList, _ bool) (*kube.Result, error) {
    74  	_, err := io.Copy(p.Out, bufferize(modified))
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	// TODO: This doesn't completely mock out have some that get created,
    79  	// updated, and deleted. I don't think these are used in any unit tests, but
    80  	// we may want to refactor a way to handle future tests
    81  	return &kube.Result{Updated: modified}, nil
    82  }
    83  
    84  // Build implements KubeClient Build.
    85  func (p *PrintingKubeClient) Build(_ io.Reader) (kube.ResourceList, error) {
    86  	return []*resource.Info{}, nil
    87  }
    88  
    89  // WaitAndGetCompletedPodPhase implements KubeClient WaitAndGetCompletedPodPhase.
    90  func (p *PrintingKubeClient) WaitAndGetCompletedPodPhase(_ string, _ time.Duration) (v1.PodPhase, error) {
    91  	return v1.PodSucceeded, nil
    92  }
    93  
    94  func bufferize(resources kube.ResourceList) io.Reader {
    95  	var builder strings.Builder
    96  	for _, info := range resources {
    97  		builder.WriteString(info.String() + "\n")
    98  	}
    99  	return strings.NewReader(builder.String())
   100  }