From 188236a6b027a85e57ac5e7403e322cab3a5c990 Mon Sep 17 00:00:00 2001 From: Yury Kulazhenkov Date: Thu, 26 Oct 2023 12:23:45 +0300 Subject: [PATCH] Add check extra check for systemd service in the daemon When systemd configuration mode is used we need to make sure that the systemd service in not only exist but also is in enabled state. Signed-off-by: Yury Kulazhenkov --- pkg/daemon/daemon.go | 8 ++++---- pkg/plugins/k8s/k8s_plugin.go | 6 +++--- pkg/service/service_manager.go | 21 ++++++++++++++++++++- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index 207685180c..c35f72fccb 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -457,7 +457,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { if dn.nodeState.GetGeneration() == latest { if dn.useSystemdService { - serviceExist, err := dn.serviceManager.IsServiceExist(systemd.SriovServicePath) + serviceEnabled, err := dn.serviceManager.IsServiceEnabled(systemd.SriovServicePath) if err != nil { glog.Errorf("nodeStateSyncHandler(): failed to check if sriov-config service exist on host: %v", err) return err @@ -466,8 +466,9 @@ func (dn *Daemon) nodeStateSyncHandler() error { // if the service doesn't exist we should continue to let the k8s plugin to create the service files // this is only for k8s base environments, for openshift the sriov-operator creates a machine config to will apply // the system service and reboot the node the config-daemon doesn't need to do anything. - if !serviceExist { - sriovResult = &systemd.SriovResult{SyncStatus: syncStatusFailed, LastSyncError: "sriov-config systemd service doesn't exist on node"} + if !serviceEnabled { + sriovResult = &systemd.SriovResult{SyncStatus: syncStatusFailed, + LastSyncError: "sriov-config systemd service is not available on node"} } else { sriovResult, err = systemd.ReadSriovResult() if err != nil { @@ -486,7 +487,6 @@ func (dn *Daemon) nodeStateSyncHandler() error { <-dn.syncCh return nil } - return nil } glog.V(0).Infof("nodeStateSyncHandler(): Interface not changed") if latestState.Status.LastSyncError != "" || diff --git a/pkg/plugins/k8s/k8s_plugin.go b/pkg/plugins/k8s/k8s_plugin.go index cbe48153fa..43385be3ac 100644 --- a/pkg/plugins/k8s/k8s_plugin.go +++ b/pkg/plugins/k8s/k8s_plugin.go @@ -322,13 +322,13 @@ func (p *K8sPlugin) switchdevServiceStateUpdate() error { func (p *K8sPlugin) sriovServiceStateUpdate() error { glog.Info("sriovServiceStateUpdate()") - exist, err := p.serviceManager.IsServiceExist(p.sriovService.Path) + isServiceEnabled, err := p.serviceManager.IsServiceEnabled(p.sriovService.Path) if err != nil { return err } - // create the service if it doesn't exist - if !exist { + // create and enable the service if it doesn't or is not enabled + if !isServiceEnabled { p.updateTarget.sriovScript = true } else { p.updateTarget.sriovScript = p.isSystemServiceNeedUpdate(p.sriovService) diff --git a/pkg/service/service_manager.go b/pkg/service/service_manager.go index 6fef6fe505..29653fe10a 100644 --- a/pkg/service/service_manager.go +++ b/pkg/service/service_manager.go @@ -11,6 +11,7 @@ import ( type ServiceManager interface { IsServiceExist(string) (bool, error) + IsServiceEnabled(string) (bool, error) ReadService(string) (*Service, error) EnableService(service *Service) error } @@ -27,7 +28,7 @@ func NewServiceManager(chroot string) ServiceManager { return &serviceManager{root} } -// ReadService read service from given path +// IsServiceExist check if service unit exist func (sm *serviceManager) IsServiceExist(servicePath string) (bool, error) { _, err := os.Stat(path.Join(sm.chroot, servicePath)) if err != nil { @@ -40,6 +41,24 @@ func (sm *serviceManager) IsServiceExist(servicePath string) (bool, error) { return true, nil } +// IsServiceEnabled check if service exist and enabled +func (sm *serviceManager) IsServiceEnabled(servicePath string) (bool, error) { + exist, err := sm.IsServiceExist(servicePath) + if err != nil || !exist { + return false, err + } + serviceName := filepath.Base(servicePath) + // Change root dir + exit, err := utils.Chroot(sm.chroot) + if err != nil { + return false, err + } + defer exit() + + cmd := exec.Command("systemctl", "is-enabled", serviceName) + return cmd.Run() == nil, nil +} + // ReadService read service from given path func (sm *serviceManager) ReadService(servicePath string) (*Service, error) { data, err := os.ReadFile(path.Join(sm.chroot, servicePath))