...

Source file src/k8s.io/kubernetes/pkg/probe/exec/exec.go

Documentation: k8s.io/kubernetes/pkg/probe/exec

     1  /*
     2  Copyright 2015 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 exec
    18  
    19  import (
    20  	"bytes"
    21  
    22  	utilfeature "k8s.io/apiserver/pkg/util/feature"
    23  	"k8s.io/kubernetes/pkg/features"
    24  	"k8s.io/kubernetes/pkg/kubelet/util/ioutils"
    25  	"k8s.io/kubernetes/pkg/probe"
    26  
    27  	"k8s.io/klog/v2"
    28  	"k8s.io/utils/exec"
    29  )
    30  
    31  const (
    32  	maxReadLength = 10 * 1 << 10 // 10KB
    33  )
    34  
    35  // New creates a Prober.
    36  func New() Prober {
    37  	return execProber{}
    38  }
    39  
    40  // Prober is an interface defining the Probe object for container readiness/liveness checks.
    41  type Prober interface {
    42  	Probe(e exec.Cmd) (probe.Result, string, error)
    43  }
    44  
    45  type execProber struct{}
    46  
    47  // Probe executes a command to check the liveness/readiness of container
    48  // from executing a command. Returns the Result status, command output, and
    49  // errors if any.
    50  func (pr execProber) Probe(e exec.Cmd) (probe.Result, string, error) {
    51  	var dataBuffer bytes.Buffer
    52  	writer := ioutils.LimitWriter(&dataBuffer, maxReadLength)
    53  
    54  	e.SetStderr(writer)
    55  	e.SetStdout(writer)
    56  	err := e.Start()
    57  	if err == nil {
    58  		err = e.Wait()
    59  	}
    60  	data := dataBuffer.Bytes()
    61  
    62  	klog.V(4).Infof("Exec probe response: %q", string(data))
    63  	if err != nil {
    64  		exit, ok := err.(exec.ExitError)
    65  		if ok {
    66  			if exit.ExitStatus() == 0 {
    67  				return probe.Success, string(data), nil
    68  			}
    69  			return probe.Failure, string(data), nil
    70  		}
    71  
    72  		timeoutErr, ok := err.(*TimeoutError)
    73  		if ok {
    74  			if utilfeature.DefaultFeatureGate.Enabled(features.ExecProbeTimeout) {
    75  				// When exec probe timeout, data is empty, so we should return timeoutErr.Error() as the stdout.
    76  				return probe.Failure, timeoutErr.Error(), nil
    77  			}
    78  
    79  			klog.Warningf("Exec probe timed out after %s but ExecProbeTimeout feature gate was disabled", timeoutErr.Timeout())
    80  		}
    81  
    82  		return probe.Unknown, "", err
    83  	}
    84  	return probe.Success, string(data), nil
    85  }
    86  

View as plain text