exec.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package exec
  14. import (
  15. "bytes"
  16. "k8s.io/kubernetes/pkg/kubelet/util/ioutils"
  17. "k8s.io/kubernetes/pkg/probe"
  18. "k8s.io/klog"
  19. "k8s.io/utils/exec"
  20. )
  21. const (
  22. maxReadLength = 10 * 1 << 10 // 10KB
  23. )
  24. // New creates a Prober.
  25. func New() Prober {
  26. return execProber{}
  27. }
  28. // Prober is an interface defining the Probe object for container readiness/liveness checks.
  29. type Prober interface {
  30. Probe(e exec.Cmd) (probe.Result, string, error)
  31. }
  32. type execProber struct{}
  33. // Probe executes a command to check the liveness/readiness of container
  34. // from executing a command. Returns the Result status, command output, and
  35. // errors if any.
  36. func (pr execProber) Probe(e exec.Cmd) (probe.Result, string, error) {
  37. var dataBuffer bytes.Buffer
  38. writer := ioutils.LimitWriter(&dataBuffer, maxReadLength)
  39. e.SetStderr(writer)
  40. e.SetStdout(writer)
  41. err := e.Start()
  42. if err == nil {
  43. err = e.Wait()
  44. }
  45. data := dataBuffer.Bytes()
  46. klog.V(4).Infof("Exec probe response: %q", string(data))
  47. if err != nil {
  48. exit, ok := err.(exec.ExitError)
  49. if ok {
  50. if exit.ExitStatus() == 0 {
  51. return probe.Success, string(data), nil
  52. }
  53. return probe.Failure, string(data), nil
  54. }
  55. return probe.Unknown, "", err
  56. }
  57. return probe.Success, string(data), nil
  58. }