fake_host.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright 2014 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 testing
  14. // helper for testing plugins
  15. // a fake host is created here that can be used by plugins for testing
  16. import (
  17. "k8s.io/api/core/v1"
  18. clientset "k8s.io/client-go/kubernetes"
  19. "k8s.io/kubernetes/pkg/kubelet/dockershim/network/hostport"
  20. )
  21. type fakeNetworkHost struct {
  22. fakeNamespaceGetter
  23. FakePortMappingGetter
  24. kubeClient clientset.Interface
  25. Legacy bool
  26. }
  27. func NewFakeHost(kubeClient clientset.Interface) *fakeNetworkHost {
  28. host := &fakeNetworkHost{kubeClient: kubeClient, Legacy: true}
  29. return host
  30. }
  31. func (fnh *fakeNetworkHost) GetPodByName(name, namespace string) (*v1.Pod, bool) {
  32. return nil, false
  33. }
  34. func (fnh *fakeNetworkHost) GetKubeClient() clientset.Interface {
  35. return nil
  36. }
  37. func (nh *fakeNetworkHost) SupportsLegacyFeatures() bool {
  38. return nh.Legacy
  39. }
  40. type fakeNamespaceGetter struct {
  41. ns string
  42. }
  43. func (nh *fakeNamespaceGetter) GetNetNS(containerID string) (string, error) {
  44. return nh.ns, nil
  45. }
  46. type FakePortMappingGetter struct {
  47. PortMaps map[string][]*hostport.PortMapping
  48. }
  49. func (pm *FakePortMappingGetter) GetPodPortMappings(containerID string) ([]*hostport.PortMapping, error) {
  50. return pm.PortMaps[containerID], nil
  51. }