Skip to content

Commit

Permalink
log -> klog
Browse files Browse the repository at this point in the history
  • Loading branch information
mcluseau committed Jul 1, 2021
1 parent 34019a1 commit 1423a48
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 123 deletions.
96 changes: 3 additions & 93 deletions backends/nft/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ import (
"bufio"
"bytes"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
Expand Down Expand Up @@ -81,7 +79,7 @@ func PreRun() {
for _, cidr := range *clusterCIDRsFlag {
ip, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
log.Fatalf("bad CIDR given: %q: %v", cidr, err)
klog.Fatalf("bad CIDR given: %q: %v", cidr, err)
}

if ip.To4() == nil {
Expand All @@ -90,97 +88,9 @@ func PreRun() {
clusterCIDRsV4 = append(clusterCIDRsV4, ipNet.String())
}
}
log.Print("cluster CIDRs V4: ", clusterCIDRsV4)
log.Print("cluster CIDRs V6: ", clusterCIDRsV6)
}

func checkIPTableVersion() {
cmdArr := [2]string{"ip6tables", "iptables"}
for _, value := range cmdArr {
cmd := exec.Command(value, "-V")
stdout, err := cmd.Output()
if err != nil && errors.Unwrap(err) != exec.ErrNotFound {
klog.Warningf("cmd (%v) throws error: %v", cmd, err)
continue
}
if bytes.Contains(stdout, []byte("legacy")) {
klog.Warning("legacy ", value, " found")
}
}
}

func checkMapIndexBug() {
if *forceNFTHashBug {
hasNFTHashBug = true
return
}

klog.Info("checking for NFT hash bug")

// check the nft vmap bug (0.9.5 but protect against the whole class)
nft := exec.Command("nft", "-f", "-")
nft.Stdin = bytes.NewBuffer([]byte(`
table ip k8s_test_vmap_bug
delete table ip k8s_test_vmap_bug
table ip k8s_test_vmap_bug {
map m1 {
typeof numgen random mod 2 : ip daddr
elements = { 1 : 10.0.0.1, 2 : 10.0.0.2 }
}
}
`))
if err := nft.Run(); err != nil {
klog.Warning("failed to test nft bugs: ", err)
}

// cleanup on return
defer func() {
nft = exec.Command("nft", "-f", "-")
nft.Stdin = bytes.NewBuffer([]byte(`
delete table ip k8s_test_vmap_bug
`))
nft.Stdout = os.Stdout
nft.Stderr = os.Stderr
err := nft.Run()
if err != nil {
klog.Warning("failed to delete test table k8s_test_vmap_bug: ", err)
}
}()

// get the recorded map
nft = exec.Command("nft", "list", "map", "ip", "k8s_test_vmap_bug", "m1")
output, err := nft.Output()
if err != nil {
klog.Warning("failed to test nft bugs: ", err)
return
}

if len(bytes.TrimSpace(output)) == 0 {
klog.Warning(`!!! WARNING !!! NFT is blind, can't auto-detect hash bug; to manually check:
-> run in this container:
# nft -f - <<EOF
table ip k8s_test_vmap_bug
delete table ip k8s_test_vmap_bug
table ip k8s_test_vmap_bug {
map m1 {
typeof numgen random mod 2 : ip daddr
elements = { 1 : 10.0.0.1, 2 : 10.0.0.2 }
}
}
EOF
-> run on your system:
# nft list map ip k8s_test_vmap_bug m1
-> if the output map is { 16777216 : 10.0.0.1, 33554432 : 10.0.0.2 }
then add --force-nft-hash-workaround=true here`)
}

hasNFTHashBug = bytes.Contains(output, []byte("16777216")) || bytes.Contains(output, []byte("0x01000000"))

if hasNFTHashBug {
klog.Info("nft vmap bug found, map indices will be affected by the workaround (0x01 will become 0x01000000)")
}
klog.Info("cluster CIDRs V4: ", clusterCIDRsV4)
klog.Info("cluster CIDRs V6: ", clusterCIDRsV6)
}

func Callback(ch <-chan *client.ServiceEndpoints) {
Expand Down
8 changes: 4 additions & 4 deletions cmd/kpng-backend-torture/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"flag"
"fmt"
"log"
"math/rand"
"net"
"strconv"
Expand All @@ -29,6 +28,7 @@ import (
"github.com/gogo/protobuf/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"k8s.io/klog"

"sigs.k8s.io/kpng/pkg/api/localnetv1"
"sigs.k8s.io/kpng/pkg/diffstore"
Expand Down Expand Up @@ -101,7 +101,7 @@ func injectState(rev uint64, w *watchstate.WatchState) {
args := flag.Args()

if int(rev) >= len(args) {
log.Print("waiting forever (rev: ", rev, ")")
klog.Info("waiting forever (rev: ", rev, ")")
select {} // tests finished, sleep forever
}

Expand All @@ -111,10 +111,10 @@ func injectState(rev uint64, w *watchstate.WatchState) {

_, err := fmt.Sscanf(spec, "%d:%d", &nSvc, &nEpPerSvc)
if err != nil {
log.Fatal("failed to parse arg: ", spec, ": ", err)
klog.Fatal("failed to parse arg: ", spec, ": ", err)
}

log.Print("sending spec ", spec, " (rev: ", rev, ")")
klog.Info("sending spec ", spec, " (rev: ", rev, ")")

svcIP := ipGen(net.ParseIP("10.0.0.0"))
epIP := ipGen(net.ParseIP("10.128.0.0"))
Expand Down
12 changes: 6 additions & 6 deletions cmd/kpng-globallog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ package main
import (
"context"
"fmt"
"log"
"time"

"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/connectivity"
"google.golang.org/protobuf/proto"
"k8s.io/klog"

"sigs.k8s.io/kpng/client"
"sigs.k8s.io/kpng/pkg/api/localnetv1"
Expand Down Expand Up @@ -66,7 +66,7 @@ func run() {
if isCanceled(err) {
return
} else if err != nil {
log.Print("failed to connect: ", err)
klog.Info("failed to connect: ", err)
time.Sleep(time.Second)
return
}
Expand All @@ -82,7 +82,7 @@ func run() {
if isCanceled(err) {
return
} else if err != nil {
log.Print("failed to start the watch: ", err)
klog.Info("failed to start the watch: ", err)
time.Sleep(time.Second)
return
}
Expand All @@ -92,7 +92,7 @@ func run() {
if isCanceled(err) {
return
} else if err != nil {
log.Print("watch request failed: ", err)
klog.Info("watch request failed: ", err)
time.Sleep(time.Second)
return
}
Expand Down Expand Up @@ -146,14 +146,14 @@ loop:
v = &localnetv1.ServiceInfo{}

default:
log.Print("unknown set: ", set.Ref.Set)
klog.Info("unknown set: ", set.Ref.Set)
continue loop
}

if v != nil {
err = proto.Unmarshal(set.Bytes, v)
if err != nil {
log.Print("failed to parse value: ", err)
klog.Info("failed to parse value: ", err)
v = nil
}
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/kpng-nodelog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package main

import (
"fmt"
"log"
"time"

"github.com/spf13/cobra"
"google.golang.org/protobuf/proto"
"k8s.io/klog"

"sigs.k8s.io/kpng/client"
"sigs.k8s.io/kpng/localsink"
Expand Down Expand Up @@ -89,13 +89,13 @@ func (s *sink) Send(op *localnetv1.OpItem) (err error) {
v = &localnetv1.ServiceInfo{}

default:
log.Print("unknown set: ", set.Ref.Set)
klog.Info("unknown set: ", set.Ref.Set)
}

if v != nil {
err = proto.Unmarshal(set.Bytes, v)
if err != nil {
log.Print("failed to parse value: ", err)
klog.Info("failed to parse value: ", err)
v = nil
}
}
Expand Down
8 changes: 4 additions & 4 deletions examples/iptables-extip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
"bytes"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path"
"sort"
"strings"

"k8s.io/klog"
"sigs.k8s.io/kpng/client"
)

Expand Down Expand Up @@ -178,11 +178,11 @@ func handleEndpoints(items []*client.ServiceEndpoints) {
// continue
// }

log.Print("ext-iptables: rules have changed, updating")
klog.Info("ext-iptables: rules have changed, updating")
rules := ipt.Bytes()

if *dryRun {
log.Printf("would have applied those rules:\n%s", ipt.String())
klog.Infof("would have applied those rules:\n%s", ipt.String())
return
}

Expand All @@ -195,7 +195,7 @@ func handleEndpoints(items []*client.ServiceEndpoints) {

err := cmd.Run()
if err != nil {
log.Print("ext-iptables: failed to restore iptables rules: ", err, "\n", string(rules))
klog.Info("ext-iptables: failed to restore iptables rules: ", err, "\n", string(rules))
}
}

Expand Down
6 changes: 3 additions & 3 deletions global-state.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ services:
ips:
v4:
- 11.0.1.2
v6: []
v6: [ "fd2b::1:2" ]
conditions:
ready: true
topology:
Expand All @@ -101,7 +101,7 @@ services:
ips:
v4:
- 11.0.2.2
v6: []
v6: [ "fd2b::2:2" ]
conditions:
ready: true
topology:
Expand All @@ -116,7 +116,7 @@ services:
ips:
v4:
- 11.0.3.2
v6: []
v6: [ "fd2b::3:2" ]
conditions:
ready: true
topology:
Expand Down
20 changes: 10 additions & 10 deletions jobs/file2store/file2store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package file2store
import (
"context"
"io/ioutil"
"log"
"os"
"path"
"time"

"github.com/cespare/xxhash"
"github.com/gogo/protobuf/proto"
"gopkg.in/yaml.v2"
"k8s.io/klog"
"sigs.k8s.io/kpng/jobs/store2file"
"sigs.k8s.io/kpng/pkg/api/localnetv1"
"sigs.k8s.io/kpng/pkg/diffstore"
Expand Down Expand Up @@ -51,7 +51,7 @@ func (j *Job) Run(ctx context.Context) {

stat, err := os.Stat(configPath)
if err != nil {
log.Print("failed to stat config: ", err)
klog.Info("failed to stat config: ", err)
continue
}

Expand All @@ -63,14 +63,14 @@ func (j *Job) Run(ctx context.Context) {

configBytes, err := ioutil.ReadFile(configPath)
if err != nil {
log.Print("failed to read config: ", err)
klog.Info("failed to read config: ", err)
continue
}

state := &store2file.GlobalState{}
err = yaml.UnmarshalStrict(configBytes, state)
if err != nil {
log.Print("failed to parse config: ", err)
klog.Info("failed to parse config: ", err)
continue
}

Expand Down Expand Up @@ -119,34 +119,34 @@ func (j *Job) Run(ctx context.Context) {

store.Update(func(tx *proxystore.Tx) {
for _, u := range diffNodes.Updated() {
log.Print("U node ", string(u.Key))
klog.Info("U node ", string(u.Key))
tx.SetNode(u.Value.(*localnetv1.Node))
}
for _, u := range diffSvcs.Updated() {
log.Print("U service ", string(u.Key))
klog.Info("U service ", string(u.Key))
si := u.Value.(*localnetv1.ServiceInfo)
tx.SetService(si.Service, si.TopologyKeys)
}
for _, u := range diffEPs.Updated() {
log.Print("U endpoints ", string(u.Key))
klog.Info("U endpoints ", string(u.Key))
key := string(u.Key)
eis := u.Value.([]*localnetv1.EndpointInfo)

tx.SetEndpointsOfSource(path.Dir(key), path.Base(key), eis)
}

for _, d := range diffEPs.Deleted() {
log.Print("D endpoints ", string(d.Key))
klog.Info("D endpoints ", string(d.Key))
key := string(d.Key)
tx.DelEndpointsOfSource(path.Dir(key), path.Base(key))
}
for _, d := range diffSvcs.Deleted() {
log.Print("D service ", string(d.Key))
klog.Info("D service ", string(d.Key))
key := string(d.Key)
tx.DelService(path.Dir(key), path.Base(key))
}
for _, d := range diffNodes.Deleted() {
log.Print("D node ", string(d.Key))
klog.Info("D node ", string(d.Key))
tx.DelNode(string(d.Key))
}

Expand Down

0 comments on commit 1423a48

Please sign in to comment.