Migrate pkg/proxy/util to structured logging (#104908)

* Migrate to Structured Logs in `pkg/proxy/util`

* Minor fixes

* change key to cidr and remove namespace arg

* Update key from cidr to CIDR

Co-authored-by: JUN YANG <69306452+yangjunmyfm192085@users.noreply.github.com>

* Update key cidr to CIDR

Co-authored-by: JUN YANG <69306452+yangjunmyfm192085@users.noreply.github.com>

* Update key ip to IP

Co-authored-by: JUN YANG <69306452+yangjunmyfm192085@users.noreply.github.com>

* Update key ip to IP

Co-authored-by: JUN YANG <69306452+yangjunmyfm192085@users.noreply.github.com>

* Interchange svcNamespace and svcName

* Change first letter of all messages to capital

* Change key names in endpoints.go

* Change all keynames to lower bumby caps convention

Co-authored-by: JUN YANG <69306452+yangjunmyfm192085@users.noreply.github.com>
This commit is contained in:
Pritish Samal 2021-09-21 02:24:35 +05:30 committed by GitHub
parent b34a735bbe
commit 060f5b88d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 13 deletions

View File

@ -36,13 +36,13 @@ func IPPart(s string) string {
// Must be IP:port
host, _, err := net.SplitHostPort(s)
if err != nil {
klog.Errorf("Error parsing '%s': %v", s, err)
klog.ErrorS(err, "Failed to parse host-port", "input", s)
return ""
}
// Check if host string is a valid IP address
ip := netutils.ParseIPSloppy(host)
if ip == nil {
klog.Errorf("invalid IP part '%s'", host)
klog.ErrorS(nil, "Failed to parse IP", "input", host)
return ""
}
return ip.String()
@ -53,12 +53,12 @@ func PortPart(s string) (int, error) {
// Must be IP:port
_, port, err := net.SplitHostPort(s)
if err != nil {
klog.Errorf("Error parsing '%s': %v", s, err)
klog.ErrorS(err, "Failed to parse host-port", "input", s)
return -1, err
}
portNumber, err := strconv.Atoi(port)
if err != nil {
klog.Errorf("Error parsing '%s': %v", port, err)
klog.ErrorS(err, "Failed to parse port", "input", port)
return -1, err
}
return portNumber, nil

View File

@ -81,12 +81,12 @@ func (d *detectLocalByCIDR) IsImplemented() bool {
func (d *detectLocalByCIDR) JumpIfLocal(args []string, toChain string) []string {
line := append(args, "-s", d.cidr, "-j", toChain)
klog.V(4).Info("[DetectLocalByCIDR (", d.cidr, ")", " Jump Local: ", line)
klog.V(4).InfoS("Detect Local By CIDR", "cidr", d.cidr, "jumpLocal", line)
return line
}
func (d *detectLocalByCIDR) JumpIfNotLocal(args []string, toChain string) []string {
line := append(args, "!", "-s", d.cidr, "-j", toChain)
klog.V(4).Info("[DetectLocalByCIDR (", d.cidr, ")]", " Jump Not Local: ", line)
klog.V(4).InfoS("Detect Local By CIDR", "cidr", d.cidr, "jumpNotLocal", line)
return line
}

View File

@ -176,12 +176,12 @@ func GetLocalAddrSet() netutils.IPSet {
func ShouldSkipService(service *v1.Service) bool {
// if ClusterIP is "None" or empty, skip proxying
if !helper.IsServiceIPSet(service) {
klog.V(3).Infof("Skipping service %s in namespace %s due to clusterIP = %q", service.Name, service.Namespace, service.Spec.ClusterIP)
klog.V(3).InfoS("Skipping service due to cluster IP", "service", klog.KObj(service), "clusterIP", service.Spec.ClusterIP)
return true
}
// Even if ClusterIP is set, ServiceTypeExternalName services don't get proxied
if service.Spec.Type == v1.ServiceTypeExternalName {
klog.V(3).Infof("Skipping service %s in namespace %s due to Type=ExternalName", service.Name, service.Namespace)
klog.V(3).InfoS("Skipping service due to Type=ExternalName", "service", klog.KObj(service))
return true
}
return false
@ -254,7 +254,7 @@ func GetNodeAddresses(cidrs []string, nw NetworkInterfacer) (sets.String, error)
// LogAndEmitIncorrectIPVersionEvent logs and emits incorrect IP version event.
func LogAndEmitIncorrectIPVersionEvent(recorder events.EventRecorder, fieldName, fieldValue, svcNamespace, svcName string, svcUID types.UID) {
errMsg := fmt.Sprintf("%s in %s has incorrect IP version", fieldValue, fieldName)
klog.Errorf("%s (service %s/%s).", errMsg, svcNamespace, svcName)
klog.ErrorS(nil, "Incorrect IP version", "service", klog.KRef(svcNamespace, svcName), "field", fieldName, "value", fieldValue)
if recorder != nil {
recorder.Eventf(
&v1.ObjectReference{
@ -274,7 +274,7 @@ func MapIPsByIPFamily(ipStrings []string) map[v1.IPFamily][]string {
if ipFamily, err := getIPFamilyFromIP(ip); err == nil {
ipFamilyMap[ipFamily] = append(ipFamilyMap[ipFamily], ip)
} else {
klog.Errorf("Skipping invalid IP: %s", ip)
klog.ErrorS(nil, "Skipping invalid IP", "ip", ip)
}
}
return ipFamilyMap
@ -288,7 +288,7 @@ func MapCIDRsByIPFamily(cidrStrings []string) map[v1.IPFamily][]string {
if ipFamily, err := getIPFamilyFromCIDR(cidr); err == nil {
ipFamilyMap[ipFamily] = append(ipFamilyMap[ipFamily], cidr)
} else {
klog.Errorf("Skipping invalid cidr: %s", cidr)
klog.ErrorS(nil, "Skipping invalid CIDR", "cidr", cidr)
}
}
return ipFamilyMap
@ -367,7 +367,7 @@ func EnsureSysctl(sysctl utilsysctl.Interface, name string, newVal int) error {
if err := sysctl.SetSysctl(name, newVal); err != nil {
return fmt.Errorf("can't set sysctl %s to %d: %v", name, newVal, err)
}
klog.V(1).Infof("Changed sysctl %q: %d -> %d", name, oldVal, newVal)
klog.V(1).InfoS("Changed sysctl", "name", name, "before", oldVal, "after", newVal)
}
return nil
}
@ -496,7 +496,7 @@ func RevertPorts(replacementPortsMap, originalPortsMap map[netutils.LocalPort]ne
for k, v := range replacementPortsMap {
// Only close newly opened local ports - leave ones that were open before this update
if originalPortsMap[k] == nil {
klog.V(2).Infof("Closing local port %s", k.String())
klog.V(2).InfoS("Closing local port", "port", k.String())
v.Close()
}
}