exec.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. "k8s.io/kubernetes/pkg/probe"
  16. "k8s.io/utils/exec"
  17. "k8s.io/klog"
  18. )
  19. // New creates a Prober.
  20. func New() Prober {
  21. return execProber{}
  22. }
  23. // Prober is an interface defining the Probe object for container readiness/liveness checks.
  24. type Prober interface {
  25. Probe(e exec.Cmd) (probe.Result, string, error)
  26. }
  27. type execProber struct{}
  28. // Probe executes a command to check the liveness/readiness of container
  29. // from executing a command. Returns the Result status, command output, and
  30. // errors if any.
  31. func (pr execProber) Probe(e exec.Cmd) (probe.Result, string, error) {
  32. data, err := e.CombinedOutput()
  33. klog.V(4).Infof("Exec probe response: %q", string(data))
  34. if err != nil {
  35. exit, ok := err.(exec.ExitError)
  36. if ok {
  37. if exit.ExitStatus() == 0 {
  38. return probe.Success, string(data), nil
  39. }
  40. return probe.Failure, string(data), nil
  41. }
  42. return probe.Unknown, "", err
  43. }
  44. return probe.Success, string(data), nil
  45. }