Skip to content

Commit

Permalink
Add check extra check for systemd service in the daemon
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ykulazhenkov committed Oct 31, 2023
1 parent f00830a commit 2be5408
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
8 changes: 4 additions & 4 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,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 {
log.Log.Error(err, "nodeStateSyncHandler(): failed to check if sriov-config service exist on host")
return err
Expand All @@ -475,8 +475,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 {
Expand All @@ -495,7 +496,6 @@ func (dn *Daemon) nodeStateSyncHandler() error {
<-dn.syncCh
return nil
}
return nil
}
log.Log.V(0).Info("nodeStateSyncHandler(): Interface not changed")
if latestState.Status.LastSyncError != "" ||
Expand Down
6 changes: 3 additions & 3 deletions pkg/plugins/k8s/k8s_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,13 @@ func (p *K8sPlugin) switchdevServiceStateUpdate() error {

func (p *K8sPlugin) sriovServiceStateUpdate() error {
log.Log.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 exist or is not enabled
if !isServiceEnabled {
p.updateTarget.sriovScript = true
} else {
p.updateTarget.sriovScript = p.isSystemServiceNeedUpdate(p.sriovService)
Expand Down
21 changes: 20 additions & 1 deletion pkg/service/service_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

type ServiceManager interface {
IsServiceExist(string) (bool, error)
IsServiceEnabled(string) (bool, error)
ReadService(string) (*Service, error)
EnableService(service *Service) error
}
Expand All @@ -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 {
Expand All @@ -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))
Expand Down

0 comments on commit 2be5408

Please sign in to comment.