Skip to content

Commit

Permalink
Fix Network Policy logging on Windows Nodes (#2052)
Browse files Browse the repository at this point in the history
On Windows Nodes the path should be /k/antrea/logs/networkpolicy/np.log
and not /var/log/antrea/networkpolicy/np.log, as decided by the log_dir
flag.

We unify the approach for support bundle and Network Policy logging.

Fixes #2050
  • Loading branch information
antoninbas authored Apr 9, 2021
1 parent b8046f8 commit bc6d66b
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 49 deletions.
26 changes: 12 additions & 14 deletions pkg/agent/controller/networkpolicy/packetin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"log"
"os"
"path/filepath"

"github.com/contiv/libOpenflow/openflow13"
"github.com/contiv/libOpenflow/protocol"
Expand All @@ -31,11 +32,12 @@ import (
"github.com/vmware-tanzu/antrea/pkg/agent/openflow"
binding "github.com/vmware-tanzu/antrea/pkg/ovs/openflow"
"github.com/vmware-tanzu/antrea/pkg/util/ip"
"github.com/vmware-tanzu/antrea/pkg/util/logdir"
)

const (
logDir string = "/var/log/antrea/networkpolicy/"
logfileName string = "np.log"
logfileSubdir string = "networkpolicy"
logfileName string = "np.log"

IPv4HdrLen uint16 = 20
IPv6HdrLen uint16 = 40
Expand Down Expand Up @@ -71,26 +73,22 @@ type logInfo struct {
// initLogger is called while newing Antrea network policy agent controller.
// Customize AntreaPolicyLogger specifically for Antrea Policies audit logging.
func initLogger() error {
// logging file should be /var/log/antrea/networkpolicy/np.log
logDir := filepath.Join(logdir.GetLogDir(), logfileSubdir)
logFile := filepath.Join(logDir, logfileName)
if _, err := os.Stat(logDir); os.IsNotExist(err) {
os.Mkdir(logDir, 0755)
}
file, err := os.OpenFile(logDir+logfileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
klog.Errorf("Failed to initialize logger to audit Antrea Policies %v", err)
return err
}

AntreaPolicyLogger = log.New(file, "", log.Ldate|log.Lmicroseconds)
// Use lumberjack log file rotation
AntreaPolicyLogger.SetOutput(&lumberjack.Logger{
Filename: logDir + logfileName,
// Use lumberjack log file rot
logOutput := &lumberjack.Logger{
Filename: logFile,
MaxSize: 500, // allow max 500 megabytes for one log file
MaxBackups: 3, // allow max 3 old log file backups
MaxAge: 28, // allow max 28 days maintenance of old log files
Compress: true, // compress the old log files for backup
})
klog.V(2).Info("Initialized Antrea-native Policy Logger for audit logging")
}
AntreaPolicyLogger = log.New(logOutput, "", log.Ldate|log.Lmicroseconds)
klog.V(2).Infof("Initialized Antrea-native Policy Logger for audit logging with log file '%s'", logFile)
return nil
}

Expand Down
28 changes: 16 additions & 12 deletions pkg/support/dump.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
// Copyright 2021 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package support

import (
"encoding/json"
"flag"
"fmt"
"io"
"os"
Expand All @@ -19,10 +32,9 @@ import (
controllerquerier "github.com/vmware-tanzu/antrea/pkg/controller/querier"
"github.com/vmware-tanzu/antrea/pkg/ovs/ovsctl"
"github.com/vmware-tanzu/antrea/pkg/querier"
"github.com/vmware-tanzu/antrea/pkg/util/logdir"
)

const antreaLinuxWellKnownLogDir = "/var/log/antrea"

// AgentDumper is the interface for dumping runtime information of the agent. Its
// functions should only work in an agent Pod or a Windows Node which has an agent
// installed.
Expand Down Expand Up @@ -151,15 +163,7 @@ func (d *controllerDumper) DumpNetworkPolicyResources(basedir string) error {
}

func (d *controllerDumper) DumpLog(basedir string) error {
logDirFlag := flag.CommandLine.Lookup("log_dir")
var logDir string
if logDirFlag == nil {
logDir = antreaLinuxWellKnownLogDir
} else if len(logDirFlag.Value.String()) == 0 {
logDir = logDirFlag.DefValue
} else {
logDir = logDirFlag.Value.String()
}
logDir := logdir.GetLogDir()
return fileCopy(d.fs, path.Join(basedir, "logs", "controller"), logDir, "antrea-controller")
}

Expand Down
12 changes: 2 additions & 10 deletions pkg/support/dump_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,17 @@
package support

import (
"flag"
"fmt"
"path"
"path/filepath"

"github.com/vmware-tanzu/antrea/pkg/agent/config"
"github.com/vmware-tanzu/antrea/pkg/agent/util/iptables"
"github.com/vmware-tanzu/antrea/pkg/util/logdir"
)

func (d *agentDumper) DumpLog(basedir string) error {
logDirFlag := flag.CommandLine.Lookup("log_dir")
var logDir string
if logDirFlag == nil {
logDir = antreaLinuxWellKnownLogDir
} else if len(logDirFlag.Value.String()) == 0 {
logDir = logDirFlag.DefValue
} else {
logDir = logDirFlag.Value.String()
}
logDir := logdir.GetLogDir()
if err := fileCopy(d.fs, path.Join(basedir, "logs", "agent"), logDir, "antrea-agent"); err != nil {
return err
}
Expand Down
18 changes: 5 additions & 13 deletions pkg/support/dump_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,22 @@
package support

import (
"flag"
"fmt"
"path"
"path/filepath"

"github.com/vmware-tanzu/antrea/pkg/util/logdir"
)

const (
antreaWindowsWellKnownLogDir = `C:\k\antrea\logs`
antreaWindowsOVSLogDir = `C:\openvswitch\var\log\openvswitch`
antreaWindowsKubeletLogDir = `C:\var\log\kubelet`
antreaWindowsOVSLogDir = `C:\openvswitch\var\log\openvswitch`
antreaWindowsKubeletLogDir = `C:\var\log\kubelet`
)

// Todo: Logs for OVS and kubelet are collected from the fixed path currently, more enhancements are needed to support
// collecting them from a configurable path in the future.
func (d *agentDumper) DumpLog(basedir string) error {
logDirFlag := flag.CommandLine.Lookup("log_dir")
var logDir string
if logDirFlag == nil {
logDir = antreaWindowsWellKnownLogDir
} else if len(logDirFlag.Value.String()) == 0 {
logDir = logDirFlag.DefValue
} else {
logDir = logDirFlag.Value.String()
}
logDir := logdir.GetLogDir()
if err := fileCopy(d.fs, path.Join(basedir, "logs", "agent"), logDir, "rancher-wins-antrea-agent"); err != nil {
return err
}
Expand Down
37 changes: 37 additions & 0 deletions pkg/util/logdir/logdir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2021 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logdir

import (
"flag"
"os"
)

// GetLogDir returns the path to the directory where Antrea logs are written.
// It will look for the value of the log_dir command-line flag, or default to a
// well-known OS-dependent location.
func GetLogDir() string {
logDirFlag := flag.CommandLine.Lookup("log_dir")
var logDir string
if logDirFlag == nil {
logDir = antreaWellKnownLogDir
} else if len(logDirFlag.Value.String()) == 0 {
// default log directory used by klog
logDir = os.TempDir()
} else {
logDir = logDirFlag.Value.String()
}
return logDir
}
19 changes: 19 additions & 0 deletions pkg/util/logdir/logdir_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// +build !windows

// Copyright 2021 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logdir

const antreaWellKnownLogDir = "/var/log/antrea"
19 changes: 19 additions & 0 deletions pkg/util/logdir/logdir_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// +build windows

// Copyright 2021 Antrea Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logdir

const antreaWellKnownLogDir = `C:\k\antrea\logs`

0 comments on commit bc6d66b

Please sign in to comment.