...

Source file src/k8s.io/client-go/tools/remotecommand/errorstream.go

Documentation: k8s.io/client-go/tools/remotecommand

     1  /*
     2  Copyright 2016 The Kubernetes 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 remotecommand
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  
    23  	"k8s.io/apimachinery/pkg/util/runtime"
    24  )
    25  
    26  // errorStreamDecoder interprets the data on the error channel and creates a go error object from it.
    27  type errorStreamDecoder interface {
    28  	decode(message []byte) error
    29  }
    30  
    31  // watchErrorStream watches the errorStream for remote command error data,
    32  // decodes it with the given errorStreamDecoder, sends the decoded error (or nil if the remote
    33  // command exited successfully) to the returned error channel, and closes it.
    34  // This function returns immediately.
    35  func watchErrorStream(errorStream io.Reader, d errorStreamDecoder) chan error {
    36  	errorChan := make(chan error)
    37  
    38  	go func() {
    39  		defer runtime.HandleCrash()
    40  
    41  		message, err := io.ReadAll(errorStream)
    42  		switch {
    43  		case err != nil && err != io.EOF:
    44  			errorChan <- fmt.Errorf("error reading from error stream: %s", err)
    45  		case len(message) > 0:
    46  			errorChan <- d.decode(message)
    47  		default:
    48  			errorChan <- nil
    49  		}
    50  		close(errorChan)
    51  	}()
    52  
    53  	return errorChan
    54  }
    55  

View as plain text