github.com/projectcalico/api@v0.0.0-20231218190037-9183ab93f33e/pkg/lib/numorstring/protocol.go (about) 1 // Copyright (c) 2016-2020 Tigera, Inc. All rights reserved. 2 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package numorstring 16 17 import "strings" 18 19 const ( 20 ProtocolUDP = "UDP" 21 ProtocolTCP = "TCP" 22 ProtocolICMP = "ICMP" 23 ProtocolICMPv6 = "ICMPv6" 24 ProtocolSCTP = "SCTP" 25 ProtocolUDPLite = "UDPLite" 26 27 ProtocolUDPV1 = "udp" 28 ProtocolTCPV1 = "tcp" 29 ProtocolSCTPV1 = "sctp" 30 ) 31 32 var ( 33 allProtocolNames = []string{ 34 ProtocolUDP, 35 ProtocolTCP, 36 ProtocolICMP, 37 ProtocolICMPv6, 38 ProtocolSCTP, 39 ProtocolUDPLite, 40 } 41 ) 42 43 type Protocol Uint8OrString 44 45 // ProtocolFromInt creates a Protocol struct from an integer value. 46 func ProtocolFromInt(p uint8) Protocol { 47 return Protocol( 48 Uint8OrString{Type: NumOrStringNum, NumVal: p}, 49 ) 50 } 51 52 // ProtocolV3FromProtocolV1 creates a v3 Protocol from a v1 Protocol, 53 // while handling case conversion. 54 func ProtocolV3FromProtocolV1(p Protocol) Protocol { 55 if p.Type == NumOrStringNum { 56 return p 57 } 58 59 for _, n := range allProtocolNames { 60 if strings.EqualFold(n, p.StrVal) { 61 return Protocol( 62 Uint8OrString{Type: NumOrStringString, StrVal: n}, 63 ) 64 } 65 } 66 67 return p 68 } 69 70 // ProtocolFromString creates a Protocol struct from a string value. 71 func ProtocolFromString(p string) Protocol { 72 for _, n := range allProtocolNames { 73 if strings.EqualFold(n, p) { 74 return Protocol( 75 Uint8OrString{Type: NumOrStringString, StrVal: n}, 76 ) 77 } 78 } 79 80 // Unknown protocol - return the value unchanged. Validation should catch this. 81 return Protocol( 82 Uint8OrString{Type: NumOrStringString, StrVal: p}, 83 ) 84 } 85 86 // ProtocolFromStringV1 creates a Protocol struct from a string value (for the v1 API) 87 func ProtocolFromStringV1(p string) Protocol { 88 return Protocol( 89 Uint8OrString{Type: NumOrStringString, StrVal: strings.ToLower(p)}, 90 ) 91 } 92 93 // UnmarshalJSON implements the json.Unmarshaller interface. 94 func (p *Protocol) UnmarshalJSON(b []byte) error { 95 return (*Uint8OrString)(p).UnmarshalJSON(b) 96 } 97 98 // MarshalJSON implements the json.Marshaller interface. 99 func (p Protocol) MarshalJSON() ([]byte, error) { 100 return Uint8OrString(p).MarshalJSON() 101 } 102 103 // String returns the string value, or the Itoa of the int value. 104 func (p Protocol) String() string { 105 return (Uint8OrString)(p).String() 106 } 107 108 // String returns the string value, or the Itoa of the int value. 109 func (p Protocol) ToV1() Protocol { 110 if p.Type == NumOrStringNum { 111 return p 112 } 113 return ProtocolFromStringV1(p.StrVal) 114 } 115 116 // NumValue returns the NumVal if type Int, or if 117 // it is a String, will attempt a conversion to int. 118 func (p Protocol) NumValue() (uint8, error) { 119 return (Uint8OrString)(p).NumValue() 120 } 121 122 // SupportsProtocols returns whether this protocol supports ports. This returns true if 123 // the numerical or string version of the protocol indicates TCP (6), UDP (17), or SCTP (132). 124 func (p Protocol) SupportsPorts() bool { 125 num, err := p.NumValue() 126 if err == nil { 127 return num == 6 || num == 17 || num == 132 128 } else { 129 switch p.StrVal { 130 case ProtocolTCP, ProtocolUDP, ProtocolTCPV1, ProtocolUDPV1, ProtocolSCTP, ProtocolSCTPV1: 131 return true 132 } 133 return false 134 } 135 } 136 137 // OpenAPISchemaType is used by the kube-openapi generator when constructing 138 // the OpenAPI spec of this type. 139 // See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators 140 func (_ Protocol) OpenAPISchemaType() []string { return []string{"string"} } 141 142 // OpenAPISchemaFormat is used by the kube-openapi generator when constructing 143 // the OpenAPI spec of this type. 144 // See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators 145 func (_ Protocol) OpenAPISchemaFormat() string { return "int-or-string" }