Skip to content

Commit

Permalink
only read insecure flags when explicitly requested
Browse files Browse the repository at this point in the history
  • Loading branch information
bverschueren committed Aug 28, 2024
1 parent bc3056c commit 699a514
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
22 changes: 19 additions & 3 deletions cmd/logs/logreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"log"
"os"
"path/filepath"
"strings"
)

// logReader reads (current/previous/rotated) logs from a tree in a base directory:
Expand Down Expand Up @@ -63,7 +64,19 @@ func (l *LogReader) WithFilter(llf logLineFilter) {
}

func (l *LogReader) FromPrevious() {
l.files = &[]string{previousLogFile, previousInsecureLogFile}
l.files = &[]string{previousLogFile}
}

func (l *LogReader) FromInsecure() {
// iterate selected logs and read from its insecure siblings instead
insecure := []string{}
for _, f := range *l.files {
nf := strings.TrimSuffix(f, ".log")
if f != nf {
insecure = append(insecure, nf+".insecure.log")
}
}
*l.files = insecure
}

func (l *LogReader) FromRotated() {
Expand All @@ -77,7 +90,10 @@ func (l *LogReader) Read(w io.Writer) {
for _, filename := range *l.files {
reader, err := open(l.dirname + "/" + filename)
if err != nil {
fmt.Println(err)
if !os.IsNotExist(err) {
// since we're scanning through logs dynamically don't error out if log files do not exist
fmt.Errorf("failed to open log file: %v", err)
}
continue
}
defer reader.Close()
Expand Down Expand Up @@ -117,7 +133,7 @@ func (l *LogReader) applyFilter(raw []byte) []byte {
func open(filename string) (io.ReadCloser, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("failed to open log file: %v", err)
return nil, err
}
var reader io.ReadCloser
reader, err = gzip.NewReader(file)
Expand Down
31 changes: 30 additions & 1 deletion cmd/logs/logreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,35 @@ func TestRotatedFiles(t *testing.T) {
}
}

func TestFromInsecure(t *testing.T) {
tests := []struct {
name string
logReader *LogReader
expected *[]string
}{
{
name: "Handle insecure logs given a list of requested log files",
logReader: NewLogReader(""), // defaults to current.log
expected: &[]string{"current.insecure.log"},
},
{
name: "Handle insecure logs but only touch files suffixed .log",
logReader: &LogReader{"", &[]string{"current.log", "current.fakelog"}, nil},
expected: &[]string{"current.insecure.log"},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.logReader.FromInsecure()

if !reflect.DeepEqual(tc.logReader.files, tc.expected) {
t.Fatalf("Expected : %v, got: %v", tc.expected, tc.logReader.files)
}
})
}
}

func TestOpen(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -86,7 +115,7 @@ func TestOpen(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := open(tc.fixture)
if err == nil && tc.expectedError {
if tc.expectedError && err == nil {
t.Errorf("Expected error, got %v", got)
}
expectedType := reflect.TypeOf(tc.expected)
Expand Down
9 changes: 5 additions & 4 deletions cmd/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var Logs = &cobra.Command{
containerName, _ := cmd.Flags().GetString("container")
previousFlag, _ := cmd.Flags().GetBool("previous")
rotatedFlag, _ := cmd.Flags().GetBool("rotated")
insecureFlag, _ := cmd.Flags().GetBool("insecure")
allContainersFlag, _ := cmd.Flags().GetBool("all-containers")
logLevels := []string{}
if LogLevel != "" {
Expand All @@ -85,10 +86,10 @@ var Logs = &cobra.Command{
fmt.Fprintln(os.Stderr, "arguments in resource/name form must have a single resource and name")
os.Exit(1)
}
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels)
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels, insecureFlag)
} else {
podName = s[0]
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels)
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels, insecureFlag)
}
}
if len(args) == 2 {
Expand All @@ -103,7 +104,7 @@ var Logs = &cobra.Command{
os.Exit(1)
}
containerName = args[1]
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels)
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels, insecureFlag)
}
} else {
if containerName != "" {
Expand All @@ -112,7 +113,7 @@ var Logs = &cobra.Command{
} else {
podName = args[0]
containerName = args[1]
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels)
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels, insecureFlag)
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/logs/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"sigs.k8s.io/yaml"
)

func logsPods(currentContextPath string, defaultConfigNamespace string, podName string, containerName string, previousFlag bool, rotatedFlag bool, allContainersFlag bool, logLevels []string) {
func logsPods(currentContextPath string, defaultConfigNamespace string, podName string, containerName string, previousFlag bool, rotatedFlag bool, allContainersFlag bool, logLevels []string, insecureFlag bool) {
var logFilter logLineFilter = NewCRILogFilter(logLevels, nil)
var _Items v1.PodList
CurrentNamespacePath := currentContextPath + "/namespaces/" + defaultConfigNamespace
Expand Down Expand Up @@ -70,6 +70,9 @@ func logsPods(currentContextPath string, defaultConfigNamespace string, podName
if rotatedFlag {
log.FromRotated()
}
if insecureFlag {
log.FromInsecure()
}
log.Read(os.Stdout)
}
return
Expand Down Expand Up @@ -111,6 +114,9 @@ func logsPods(currentContextPath string, defaultConfigNamespace string, podName
if rotatedFlag {
log.FromRotated()
}
if insecureFlag {
log.FromInsecure()
}
log.Read(os.Stdout)
}
}
Expand Down

0 comments on commit 699a514

Please sign in to comment.