k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/proxy/util/endpoints.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "net" 21 22 "k8s.io/klog/v2" 23 netutils "k8s.io/utils/net" 24 ) 25 26 // IPPart returns just the IP part of an IP or IP:port or endpoint string. If the IP 27 // part is an IPv6 address enclosed in brackets (e.g. "[fd00:1::5]:9999"), 28 // then the brackets are stripped as well. 29 func IPPart(s string) string { 30 if ip := netutils.ParseIPSloppy(s); ip != nil { 31 // IP address without port 32 return s 33 } 34 // Must be IP:port 35 host, _, err := net.SplitHostPort(s) 36 if err != nil { 37 klog.ErrorS(err, "Failed to parse host-port", "input", s) 38 return "" 39 } 40 // Check if host string is a valid IP address 41 ip := netutils.ParseIPSloppy(host) 42 if ip == nil { 43 klog.ErrorS(nil, "Failed to parse IP", "input", host) 44 return "" 45 } 46 return ip.String() 47 }