...

Source file src/k8s.io/kubernetes/pkg/kubelet/kubelet_server_journal_linux.go

Documentation: k8s.io/kubernetes/pkg/kubelet

     1  //go:build linux
     2  
     3  /*
     4  Copyright 2022 The Kubernetes Authors.
     5  
     6  Licensed under the Apache License, Version 2.0 (the "License");
     7  you may not use this file except in compliance with the License.
     8  You may obtain a copy of the License at
     9  
    10      http://www.apache.org/licenses/LICENSE-2.0
    11  
    12  Unless required by applicable law or agreed to in writing, software
    13  distributed under the License is distributed on an "AS IS" BASIS,
    14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  See the License for the specific language governing permissions and
    16  limitations under the License.
    17  */
    18  
    19  package kubelet
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"os/exec"
    25  	"strings"
    26  )
    27  
    28  // getLoggingCmd returns the journalctl cmd and arguments for the given nodeLogQuery and boot. Note that
    29  // services are explicitly passed here to account for the heuristics
    30  func getLoggingCmd(n *nodeLogQuery, services []string) (string, []string, error) {
    31  	args := []string{
    32  		"--utc",
    33  		"--no-pager",
    34  		"--output=short-precise",
    35  	}
    36  	if n.SinceTime != nil {
    37  		args = append(args, fmt.Sprintf("--since=%s", n.SinceTime.Format(dateLayout)))
    38  	}
    39  	if n.UntilTime != nil {
    40  		args = append(args, fmt.Sprintf("--until=%s", n.UntilTime.Format(dateLayout)))
    41  	}
    42  	if n.TailLines != nil {
    43  		args = append(args, "--pager-end", fmt.Sprintf("--lines=%d", *n.TailLines))
    44  	}
    45  	for _, service := range services {
    46  		if len(service) > 0 {
    47  			args = append(args, "--unit="+service)
    48  		}
    49  	}
    50  	if len(n.Pattern) > 0 {
    51  		args = append(args, "--grep="+n.Pattern)
    52  	}
    53  
    54  	if n.Boot != nil {
    55  		args = append(args, "--boot", fmt.Sprintf("%d", *n.Boot))
    56  	}
    57  
    58  	return "journalctl", args, nil
    59  }
    60  
    61  // checkForNativeLogger checks journalctl output for a service
    62  func checkForNativeLogger(ctx context.Context, service string) bool {
    63  	// This will return all the journald units
    64  	cmd := exec.CommandContext(ctx, "journalctl", []string{"--field", "_SYSTEMD_UNIT"}...)
    65  	output, err := cmd.CombinedOutput()
    66  	if err != nil {
    67  		// Returning false to allow checking if the service is logging to a file
    68  		return false
    69  	}
    70  
    71  	// journalctl won't return an error if we try to fetch logs for a non-existent service,
    72  	// hence we search for it in the list of services known to journalctl
    73  	return strings.Contains(string(output), service+".service")
    74  }
    75  

View as plain text