pod_port.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. Copyright 2018 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 util
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. )
  18. // LookupContainerPortNumberByName find containerPort number by its named port name
  19. func LookupContainerPortNumberByName(pod v1.Pod, name string) (int32, error) {
  20. for _, ctr := range pod.Spec.Containers {
  21. for _, ctrportspec := range ctr.Ports {
  22. if ctrportspec.Name == name {
  23. return ctrportspec.ContainerPort, nil
  24. }
  25. }
  26. }
  27. return int32(-1), fmt.Errorf("Pod '%s' does not have a named port '%s'", pod.Name, name)
  28. }