...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/util/initsystem/initsystem_unix.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/util/initsystem

     1  //go:build !windows
     2  // +build !windows
     3  
     4  /*
     5  Copyright 2017 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package initsystem
    21  
    22  import (
    23  	"fmt"
    24  	"os/exec"
    25  	"strings"
    26  
    27  	"github.com/pkg/errors"
    28  )
    29  
    30  // OpenRCInitSystem defines openrc
    31  type OpenRCInitSystem struct{}
    32  
    33  // ServiceStart tries to start a specific service
    34  func (openrc OpenRCInitSystem) ServiceStart(service string) error {
    35  	args := []string{service, "start"}
    36  	return exec.Command("rc-service", args...).Run()
    37  }
    38  
    39  // ServiceStop tries to stop a specific service
    40  func (openrc OpenRCInitSystem) ServiceStop(service string) error {
    41  	args := []string{service, "stop"}
    42  	return exec.Command("rc-service", args...).Run()
    43  }
    44  
    45  // ServiceRestart tries to reload the environment and restart the specific service
    46  func (openrc OpenRCInitSystem) ServiceRestart(service string) error {
    47  	args := []string{service, "restart"}
    48  	return exec.Command("rc-service", args...).Run()
    49  }
    50  
    51  // ServiceExists ensures the service is defined for this init system.
    52  // openrc writes to stderr if a service is not found or not enabled
    53  // this is in contrast to systemd which only writes to stdout.
    54  // Hence, we use the Combinedoutput, and ignore the error.
    55  func (openrc OpenRCInitSystem) ServiceExists(service string) bool {
    56  	args := []string{service, "status"}
    57  	outBytes, _ := exec.Command("rc-service", args...).CombinedOutput()
    58  	return !strings.Contains(string(outBytes), "does not exist")
    59  }
    60  
    61  // ServiceIsEnabled ensures the service is enabled to start on each boot.
    62  func (openrc OpenRCInitSystem) ServiceIsEnabled(service string) bool {
    63  	args := []string{"show", "default"}
    64  	outBytes, _ := exec.Command("rc-update", args...).Output()
    65  	return strings.Contains(string(outBytes), service)
    66  }
    67  
    68  // ServiceIsActive ensures the service is running, or attempting to run. (crash looping in the case of kubelet)
    69  func (openrc OpenRCInitSystem) ServiceIsActive(service string) bool {
    70  	args := []string{service, "status"}
    71  	outBytes, _ := exec.Command("rc-service", args...).CombinedOutput()
    72  	outStr := string(outBytes)
    73  	return !strings.Contains(outStr, "stopped") && !strings.Contains(outStr, "does not exist")
    74  }
    75  
    76  // EnableCommand return a string describing how to enable a service
    77  func (openrc OpenRCInitSystem) EnableCommand(service string) string {
    78  	return fmt.Sprintf("rc-update add %s default", service)
    79  }
    80  
    81  // SystemdInitSystem defines systemd
    82  type SystemdInitSystem struct{}
    83  
    84  // EnableCommand return a string describing how to enable a service
    85  func (sysd SystemdInitSystem) EnableCommand(service string) string {
    86  	return fmt.Sprintf("systemctl enable %s.service", service)
    87  }
    88  
    89  // reloadSystemd reloads the systemd daemon
    90  func (sysd SystemdInitSystem) reloadSystemd() error {
    91  	if err := exec.Command("systemctl", "daemon-reload").Run(); err != nil {
    92  		return errors.Wrap(err, "failed to reload systemd")
    93  	}
    94  	return nil
    95  }
    96  
    97  // ServiceStart tries to start a specific service
    98  func (sysd SystemdInitSystem) ServiceStart(service string) error {
    99  	// Before we try to start any service, make sure that systemd is ready
   100  	if err := sysd.reloadSystemd(); err != nil {
   101  		return err
   102  	}
   103  	args := []string{"start", service}
   104  	return exec.Command("systemctl", args...).Run()
   105  }
   106  
   107  // ServiceRestart tries to reload the environment and restart the specific service
   108  func (sysd SystemdInitSystem) ServiceRestart(service string) error {
   109  	// Before we try to restart any service, make sure that systemd is ready
   110  	if err := sysd.reloadSystemd(); err != nil {
   111  		return err
   112  	}
   113  	args := []string{"restart", service}
   114  	return exec.Command("systemctl", args...).Run()
   115  }
   116  
   117  // ServiceStop tries to stop a specific service
   118  func (sysd SystemdInitSystem) ServiceStop(service string) error {
   119  	args := []string{"stop", service}
   120  	return exec.Command("systemctl", args...).Run()
   121  }
   122  
   123  // ServiceExists ensures the service is defined for this init system.
   124  func (sysd SystemdInitSystem) ServiceExists(service string) bool {
   125  	args := []string{"status", service}
   126  	outBytes, _ := exec.Command("systemctl", args...).Output()
   127  	output := string(outBytes)
   128  	return !strings.Contains(output, "Loaded: not-found") && !strings.Contains(output, "could not be found")
   129  }
   130  
   131  // ServiceIsEnabled ensures the service is enabled to start on each boot.
   132  func (sysd SystemdInitSystem) ServiceIsEnabled(service string) bool {
   133  	args := []string{"is-enabled", service}
   134  	err := exec.Command("systemctl", args...).Run()
   135  	return err == nil
   136  }
   137  
   138  // ServiceIsActive will check is the service is "active". In the case of
   139  // crash looping services (kubelet in our case) status will return as
   140  // "activating", so we will consider this active as well.
   141  func (sysd SystemdInitSystem) ServiceIsActive(service string) bool {
   142  	args := []string{"is-active", service}
   143  	// Ignoring error here, command returns non-0 if in "activating" status:
   144  	outBytes, _ := exec.Command("systemctl", args...).Output()
   145  	output := strings.TrimSpace(string(outBytes))
   146  	if output == "active" || output == "activating" {
   147  		return true
   148  	}
   149  	return false
   150  }
   151  
   152  // GetInitSystem returns an InitSystem for the current system, or nil
   153  // if we cannot detect a supported init system.
   154  // This indicates we will skip init system checks, not an error.
   155  func GetInitSystem() (InitSystem, error) {
   156  	// Assume existence of systemctl in path implies this is a systemd system:
   157  	_, err := exec.LookPath("systemctl")
   158  	if err == nil {
   159  		return &SystemdInitSystem{}, nil
   160  	}
   161  	_, err = exec.LookPath("openrc")
   162  	if err == nil {
   163  		return &OpenRCInitSystem{}, nil
   164  	}
   165  
   166  	return nil, errors.New("no supported init system detected, skipping checking for services")
   167  }
   168  

View as plain text