2015-01-23 13:03:04 -05:00
|
|
|
/*
|
2016-06-02 20:25:58 -04:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-01-23 13:03:04 -05:00
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package exec
|
|
|
|
|
|
|
|
|
|
import (
|
2019-09-09 23:12:07 -04:00
|
|
|
"bytes"
|
|
|
|
|
|
2020-09-06 13:52:49 -04:00
|
|
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
|
|
|
|
"k8s.io/kubernetes/pkg/features"
|
2019-09-09 23:12:07 -04:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/util/ioutils"
|
2015-08-05 18:03:47 -04:00
|
|
|
"k8s.io/kubernetes/pkg/probe"
|
2015-01-23 13:03:04 -05:00
|
|
|
|
2020-04-17 15:25:06 -04:00
|
|
|
"k8s.io/klog/v2"
|
2019-09-09 23:12:07 -04:00
|
|
|
"k8s.io/utils/exec"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
maxReadLength = 10 * 1 << 10 // 10KB
|
2015-01-23 13:03:04 -05:00
|
|
|
)
|
|
|
|
|
|
2018-09-12 17:13:19 -04:00
|
|
|
// New creates a Prober.
|
|
|
|
|
func New() Prober {
|
2015-02-08 15:19:34 -05:00
|
|
|
return execProber{}
|
2015-01-27 13:00:21 -05:00
|
|
|
}
|
|
|
|
|
|
2018-09-12 17:13:19 -04:00
|
|
|
// Prober is an interface defining the Probe object for container readiness/liveness checks.
|
|
|
|
|
type Prober interface {
|
2015-05-13 20:30:37 -04:00
|
|
|
Probe(e exec.Cmd) (probe.Result, string, error)
|
2015-02-08 15:19:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type execProber struct{}
|
2015-01-27 13:00:21 -05:00
|
|
|
|
2018-09-12 17:13:19 -04:00
|
|
|
// Probe executes a command to check the liveness/readiness of container
|
|
|
|
|
// from executing a command. Returns the Result status, command output, and
|
|
|
|
|
// errors if any.
|
2015-05-13 20:30:37 -04:00
|
|
|
func (pr execProber) Probe(e exec.Cmd) (probe.Result, string, error) {
|
2019-09-09 23:12:07 -04:00
|
|
|
var dataBuffer bytes.Buffer
|
|
|
|
|
writer := ioutils.LimitWriter(&dataBuffer, maxReadLength)
|
|
|
|
|
|
|
|
|
|
e.SetStderr(writer)
|
|
|
|
|
e.SetStdout(writer)
|
|
|
|
|
err := e.Start()
|
|
|
|
|
if err == nil {
|
|
|
|
|
err = e.Wait()
|
|
|
|
|
}
|
|
|
|
|
data := dataBuffer.Bytes()
|
|
|
|
|
|
2018-11-09 13:49:10 -05:00
|
|
|
klog.V(4).Infof("Exec probe response: %q", string(data))
|
2015-01-23 13:03:04 -05:00
|
|
|
if err != nil {
|
2015-05-08 12:48:31 -04:00
|
|
|
exit, ok := err.(exec.ExitError)
|
|
|
|
|
if ok {
|
|
|
|
|
if exit.ExitStatus() == 0 {
|
2015-05-13 20:30:37 -04:00
|
|
|
return probe.Success, string(data), nil
|
2015-05-08 12:48:31 -04:00
|
|
|
}
|
2018-09-12 17:13:19 -04:00
|
|
|
return probe.Failure, string(data), nil
|
2015-05-08 12:48:31 -04:00
|
|
|
}
|
2020-09-06 13:52:49 -04:00
|
|
|
|
|
|
|
|
timeoutErr, ok := err.(*TimeoutError)
|
|
|
|
|
if ok {
|
|
|
|
|
if utilfeature.DefaultFeatureGate.Enabled(features.ExecProbeTimeout) {
|
2021-11-06 03:59:22 -04:00
|
|
|
// When exec probe timeout, data is empty, so we should return timeoutErr.Error() as the stdout.
|
|
|
|
|
return probe.Failure, timeoutErr.Error(), nil
|
2020-09-06 13:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
klog.Warningf("Exec probe timed out after %s but ExecProbeTimeout feature gate was disabled", timeoutErr.Timeout())
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-13 20:30:37 -04:00
|
|
|
return probe.Unknown, "", err
|
2015-01-23 13:03:04 -05:00
|
|
|
}
|
2015-05-13 20:30:37 -04:00
|
|
|
return probe.Success, string(data), nil
|
2015-01-23 13:03:04 -05:00
|
|
|
}
|