fake_handler_runner.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. Copyright 2016 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 lifecycle
  14. import (
  15. "fmt"
  16. "sync"
  17. "k8s.io/kubernetes/pkg/api"
  18. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  19. "k8s.io/kubernetes/pkg/kubelet/util/format"
  20. )
  21. type FakeHandlerRunner struct {
  22. sync.Mutex
  23. HandlerRuns []string
  24. Err error
  25. }
  26. func NewFakeHandlerRunner() *FakeHandlerRunner {
  27. return &FakeHandlerRunner{HandlerRuns: []string{}}
  28. }
  29. func (hr *FakeHandlerRunner) Run(containerID kubecontainer.ContainerID, pod *api.Pod, container *api.Container, handler *api.Handler) (string, error) {
  30. hr.Lock()
  31. defer hr.Unlock()
  32. if hr.Err != nil {
  33. return "", hr.Err
  34. }
  35. switch {
  36. case handler.Exec != nil:
  37. hr.HandlerRuns = append(hr.HandlerRuns, fmt.Sprintf("exec on pod: %v, container: %v: %v", format.Pod(pod), container.Name, containerID.String()))
  38. case handler.HTTPGet != nil:
  39. hr.HandlerRuns = append(hr.HandlerRuns, fmt.Sprintf("http-get on pod: %v, container: %v: %v", format.Pod(pod), container.Name, containerID.String()))
  40. case handler.TCPSocket != nil:
  41. hr.HandlerRuns = append(hr.HandlerRuns, fmt.Sprintf("tcp-socket on pod: %v, container: %v: %v", format.Pod(pod), container.Name, containerID.String()))
  42. default:
  43. return "", fmt.Errorf("Invalid handler: %v", handler)
  44. }
  45. return "", nil
  46. }
  47. func (hr *FakeHandlerRunner) Reset() {
  48. hr.HandlerRuns = []string{}
  49. hr.Err = nil
  50. }