protocolsforobject.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 polymorphichelpers
  14. import (
  15. "fmt"
  16. "strconv"
  17. appsv1 "k8s.io/api/apps/v1"
  18. appsv1beta1 "k8s.io/api/apps/v1beta1"
  19. appsv1beta2 "k8s.io/api/apps/v1beta2"
  20. corev1 "k8s.io/api/core/v1"
  21. extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. )
  24. func protocolsForObject(object runtime.Object) (map[string]string, error) {
  25. // TODO: replace with a swagger schema based approach (identify pod selector via schema introspection)
  26. switch t := object.(type) {
  27. case *corev1.ReplicationController:
  28. return getProtocols(t.Spec.Template.Spec), nil
  29. case *corev1.Pod:
  30. return getProtocols(t.Spec), nil
  31. case *corev1.Service:
  32. return getServiceProtocols(t.Spec), nil
  33. case *extensionsv1beta1.Deployment:
  34. return getProtocols(t.Spec.Template.Spec), nil
  35. case *appsv1.Deployment:
  36. return getProtocols(t.Spec.Template.Spec), nil
  37. case *appsv1beta2.Deployment:
  38. return getProtocols(t.Spec.Template.Spec), nil
  39. case *appsv1beta1.Deployment:
  40. return getProtocols(t.Spec.Template.Spec), nil
  41. case *extensionsv1beta1.ReplicaSet:
  42. return getProtocols(t.Spec.Template.Spec), nil
  43. case *appsv1.ReplicaSet:
  44. return getProtocols(t.Spec.Template.Spec), nil
  45. case *appsv1beta2.ReplicaSet:
  46. return getProtocols(t.Spec.Template.Spec), nil
  47. default:
  48. return nil, fmt.Errorf("cannot extract protocols from %T", object)
  49. }
  50. }
  51. func getProtocols(spec corev1.PodSpec) map[string]string {
  52. result := make(map[string]string)
  53. for _, container := range spec.Containers {
  54. for _, port := range container.Ports {
  55. // Empty protocol must be defaulted (TCP)
  56. if len(port.Protocol) == 0 {
  57. port.Protocol = corev1.ProtocolTCP
  58. }
  59. result[strconv.Itoa(int(port.ContainerPort))] = string(port.Protocol)
  60. }
  61. }
  62. return result
  63. }
  64. // Extracts the protocols exposed by a service from the given service spec.
  65. func getServiceProtocols(spec corev1.ServiceSpec) map[string]string {
  66. result := make(map[string]string)
  67. for _, servicePort := range spec.Ports {
  68. // Empty protocol must be defaulted (TCP)
  69. if len(servicePort.Protocol) == 0 {
  70. servicePort.Protocol = corev1.ProtocolTCP
  71. }
  72. result[strconv.Itoa(int(servicePort.Port))] = string(servicePort.Protocol)
  73. }
  74. return result
  75. }