...

Source file src/sigs.k8s.io/cli-utils/pkg/apply/task/printer_adapter.go

Documentation: sigs.k8s.io/cli-utils/pkg/apply/task

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package task
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  
    10  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    11  	"k8s.io/apimachinery/pkg/runtime"
    12  	"k8s.io/cli-runtime/pkg/printers"
    13  	"sigs.k8s.io/cli-utils/pkg/apply/event"
    14  	"sigs.k8s.io/cli-utils/pkg/object"
    15  )
    16  
    17  // KubectlPrinterAdapter is a workaround for capturing progress from
    18  // ApplyOptions. ApplyOptions were originally meant to print progress
    19  // directly using a configurable printer. The KubectlPrinterAdapter
    20  // plugs into ApplyOptions as a ToPrinter function, but instead of
    21  // printing the info, it emits it as an event on the provided channel.
    22  type KubectlPrinterAdapter struct {
    23  	ch        chan<- event.Event
    24  	groupName string
    25  }
    26  
    27  // resourcePrinterImpl implements the ResourcePrinter interface. But
    28  // instead of printing, it emits information on the provided channel.
    29  type resourcePrinterImpl struct {
    30  	applyStatus event.ApplyEventStatus
    31  	ch          chan<- event.Event
    32  	groupName   string
    33  }
    34  
    35  // PrintObj takes the provided object and operation and emits
    36  // it on the channel.
    37  func (r *resourcePrinterImpl) PrintObj(obj runtime.Object, _ io.Writer) error {
    38  	id, err := object.RuntimeToObjMeta(obj)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	r.ch <- event.Event{
    43  		Type: event.ApplyType,
    44  		ApplyEvent: event.ApplyEvent{
    45  			GroupName:  r.groupName,
    46  			Identifier: id,
    47  			Status:     r.applyStatus,
    48  			Resource:   obj.(*unstructured.Unstructured),
    49  		},
    50  	}
    51  	return nil
    52  }
    53  
    54  type toPrinterFunc func(string) (printers.ResourcePrinter, error)
    55  
    56  // toPrinterFunc returns a function of type toPrinterFunc. This
    57  // is the type required by the ApplyOptions.
    58  func (p *KubectlPrinterAdapter) toPrinterFunc() toPrinterFunc {
    59  	return func(operation string) (printers.ResourcePrinter, error) {
    60  		applyStatus, err := kubectlOperationToApplyStatus(operation)
    61  		return &resourcePrinterImpl{
    62  			ch:          p.ch,
    63  			applyStatus: applyStatus,
    64  			groupName:   p.groupName,
    65  		}, err
    66  	}
    67  }
    68  
    69  func kubectlOperationToApplyStatus(operation string) (event.ApplyEventStatus, error) {
    70  	switch operation {
    71  	case "serverside-applied":
    72  		return event.ApplySuccessful, nil
    73  	case "created":
    74  		return event.ApplySuccessful, nil
    75  	case "unchanged":
    76  		return event.ApplySuccessful, nil
    77  	case "configured":
    78  		return event.ApplySuccessful, nil
    79  	default:
    80  		return event.ApplyEventStatus(0), fmt.Errorf("unknown operation %s", operation)
    81  	}
    82  }
    83  

View as plain text