...

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

Documentation: k8s.io/kubernetes/pkg/kubelet

     1  //go:build windows
     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  const powershellExe = "PowerShell.exe"
    29  
    30  // getLoggingCmd returns the powershell cmd and arguments for the given nodeLogQuery and boot
    31  func getLoggingCmd(n *nodeLogQuery, services []string) (string, []string, error) {
    32  	args := []string{
    33  		"-NonInteractive",
    34  		"-ExecutionPolicy", "Bypass",
    35  		"-Command",
    36  	}
    37  
    38  	psCmd := "Get-WinEvent -FilterHashtable @{LogName='Application'"
    39  	if n.SinceTime != nil {
    40  		psCmd += fmt.Sprintf("; StartTime='%s'", n.SinceTime.Format(dateLayout))
    41  	}
    42  	if n.UntilTime != nil {
    43  		psCmd += fmt.Sprintf("; EndTime='%s'", n.UntilTime.Format(dateLayout))
    44  	}
    45  	var providers []string
    46  	for _, service := range services {
    47  		if len(service) > 0 {
    48  			providers = append(providers, "'"+service+"'")
    49  		}
    50  	}
    51  	if len(providers) > 0 {
    52  		psCmd += fmt.Sprintf("; ProviderName=%s", strings.Join(providers, ","))
    53  	}
    54  	psCmd += "}"
    55  	if n.TailLines != nil {
    56  		psCmd += fmt.Sprintf(" -MaxEvents %d", *n.TailLines)
    57  	}
    58  	psCmd += " | Sort-Object TimeCreated"
    59  	if len(n.Pattern) > 0 {
    60  		psCmd += fmt.Sprintf(" | Where-Object -Property Message -Match '%s'", n.Pattern)
    61  	}
    62  	psCmd += " | Format-Table -AutoSize -Wrap"
    63  
    64  	args = append(args, psCmd)
    65  
    66  	return powershellExe, args, nil
    67  }
    68  
    69  // checkForNativeLogger always returns true for Windows
    70  func checkForNativeLogger(ctx context.Context, service string) bool {
    71  	cmd := exec.CommandContext(ctx, powershellExe, []string{
    72  		"-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command",
    73  		fmt.Sprintf("Get-WinEvent -ListProvider %s | Format-Table -AutoSize", service)}...)
    74  
    75  	_, err := cmd.CombinedOutput()
    76  	if err != nil {
    77  		// Get-WinEvent will return ExitError if the service is not listed as a provider
    78  		if _, ok := err.(*exec.ExitError); ok {
    79  			return false
    80  		}
    81  		// Other errors imply that CombinedOutput failed before the command was executed,
    82  		// so lets to get the logs using Get-WinEvent at the call site instead of assuming
    83  		// the service is logging to a file
    84  	}
    85  	return true
    86  }
    87  

View as plain text