Skip to content

Commit

Permalink
Update pkg/systemd to support running in host and in container
Browse files Browse the repository at this point in the history
Signed-off-by: Yury Kulazhenkov <ykulazhenkov@nvidia.com>
  • Loading branch information
ykulazhenkov committed Jan 31, 2024
1 parent 65e8053 commit 67155e3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 52 deletions.
96 changes: 44 additions & 52 deletions pkg/systemd/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
)

Expand All @@ -35,17 +36,8 @@ const (
sriovSystemdSupportedNicPath = consts.SriovConfBasePath + "/sriov-supported-nics-ids.yaml"
sriovSystemdServiceBinaryPath = "/var/lib/sriov/sriov-network-config-daemon"

SriovHostSystemdConfigPath = consts.Host + SriovSystemdConfigPath
SriovHostSystemdResultPath = consts.Host + SriovSystemdResultPath
sriovHostSystemdSupportedNicPath = consts.Host + sriovSystemdSupportedNicPath
sriovHostSystemdServiceBinaryPath = consts.Host + sriovSystemdServiceBinaryPath

SriovServicePath = "/etc/systemd/system/sriov-config.service"
SriovPostNetworkServicePath = "/etc/systemd/system/sriov-config-post-network.service"
SriovHostServicePath = consts.Host + SriovServicePath
SriovHostPostNetworkServicePath = consts.Host + SriovPostNetworkServicePath

HostSriovConfBasePath = consts.Host + consts.SriovConfBasePath
SriovServicePath = "/etc/systemd/system/sriov-config.service"
SriovPostNetworkServicePath = "/etc/systemd/system/sriov-config-post-network.service"
)

// TODO: move this to the host interface also
Expand All @@ -62,7 +54,7 @@ type SriovResult struct {
}

func ReadConfFile() (spec *SriovConfig, err error) {
rawConfig, err := os.ReadFile(SriovSystemdConfigPath)
rawConfig, err := os.ReadFile(utils.GetHostExtensionPath(SriovSystemdConfigPath))
if err != nil {
return nil, err
}
Expand All @@ -83,22 +75,22 @@ func WriteConfFile(newState *sriovnetworkv1.SriovNetworkNodeState) (bool, error)
vars.PlatformType,
}

_, err := os.Stat(SriovHostSystemdConfigPath)
_, err := os.Stat(utils.GetHostExtensionPath(SriovSystemdConfigPath))
if err != nil {
if os.IsNotExist(err) {
// Create the sriov-operator folder on the host if it doesn't exist
if _, err := os.Stat(HostSriovConfBasePath); os.IsNotExist(err) {
err = os.Mkdir(HostSriovConfBasePath, os.ModeDir)
if _, err := os.Stat(utils.GetHostExtensionPath(consts.SriovConfBasePath)); os.IsNotExist(err) {
err = os.Mkdir(utils.GetHostExtensionPath(consts.SriovConfBasePath), os.ModeDir)
if err != nil {
log.Log.Error(err, "WriteConfFile(): fail to create sriov-operator folder",
"path", HostSriovConfBasePath)
"path", utils.GetHostExtensionPath(consts.SriovConfBasePath))
return false, err
}
}

log.Log.V(2).Info("WriteConfFile(): file not existed, create it",
"path", SriovHostSystemdConfigPath)
_, err = os.Create(SriovHostSystemdConfigPath)
"path", utils.GetHostExtensionPath(SriovSystemdConfigPath))
_, err = os.Create(utils.GetHostExtensionPath(SriovSystemdConfigPath))
if err != nil {
log.Log.Error(err, "WriteConfFile(): fail to create file")
return false, err
Expand All @@ -109,9 +101,9 @@ func WriteConfFile(newState *sriovnetworkv1.SriovNetworkNodeState) (bool, error)
}
}

oldContent, err := os.ReadFile(SriovHostSystemdConfigPath)
oldContent, err := os.ReadFile(utils.GetHostExtensionPath(SriovSystemdConfigPath))
if err != nil {
log.Log.Error(err, "WriteConfFile(): fail to read file", "path", SriovHostSystemdConfigPath)
log.Log.Error(err, "WriteConfFile(): fail to read file", "path", utils.GetHostExtensionPath(SriovSystemdConfigPath))
return false, err
}

Expand All @@ -137,8 +129,8 @@ func WriteConfFile(newState *sriovnetworkv1.SriovNetworkNodeState) (bool, error)
"old", string(oldContent), "new", string(newContent))

log.Log.V(2).Info("WriteConfFile(): write content to file",
"content", newContent, "path", SriovHostSystemdConfigPath)
err = os.WriteFile(SriovHostSystemdConfigPath, newContent, 0644)
"content", newContent, "path", utils.GetHostExtensionPath(SriovSystemdConfigPath))
err = os.WriteFile(utils.GetHostExtensionPath(SriovSystemdConfigPath), newContent, 0644)
if err != nil {
log.Log.Error(err, "WriteConfFile(): fail to write file")
return false, err
Expand All @@ -155,17 +147,17 @@ func WriteConfFile(newState *sriovnetworkv1.SriovNetworkNodeState) (bool, error)
}

func WriteSriovResult(result *SriovResult) error {
_, err := os.Stat(SriovSystemdResultPath)
_, err := os.Stat(utils.GetHostExtensionPath(SriovSystemdResultPath))
if err != nil {
if os.IsNotExist(err) {
log.Log.V(2).Info("WriteSriovResult(): file not existed, create it")
_, err = os.Create(SriovSystemdResultPath)
_, err = os.Create(utils.GetHostExtensionPath(SriovSystemdResultPath))
if err != nil {
log.Log.Error(err, "WriteSriovResult(): failed to create sriov result file", "path", SriovSystemdResultPath)
log.Log.Error(err, "WriteSriovResult(): failed to create sriov result file", "path", utils.GetHostExtensionPath(SriovSystemdResultPath))
return err
}
} else {
log.Log.Error(err, "WriteSriovResult(): failed to check sriov result file", "path", SriovSystemdResultPath)
log.Log.Error(err, "WriteSriovResult(): failed to check sriov result file", "path", utils.GetHostExtensionPath(SriovSystemdResultPath))
return err
}
}
Expand All @@ -177,70 +169,70 @@ func WriteSriovResult(result *SriovResult) error {
}

log.Log.V(2).Info("WriteSriovResult(): write results",
"content", string(out), "path", SriovSystemdResultPath)
err = os.WriteFile(SriovSystemdResultPath, out, 0644)
"content", string(out), "path", utils.GetHostExtensionPath(SriovSystemdResultPath))
err = os.WriteFile(utils.GetHostExtensionPath(SriovSystemdResultPath), out, 0644)
if err != nil {
log.Log.Error(err, "WriteSriovResult(): failed to write sriov result file", "path", SriovSystemdResultPath)
log.Log.Error(err, "WriteSriovResult(): failed to write sriov result file", "path", utils.GetHostExtensionPath(SriovSystemdResultPath))
return err
}

return nil
}

func ReadSriovResult() (*SriovResult, error) {
_, err := os.Stat(SriovHostSystemdResultPath)
_, err := os.Stat(utils.GetHostExtensionPath(SriovSystemdResultPath))
if err != nil {
if os.IsNotExist(err) {
log.Log.V(2).Info("ReadSriovResult(): file does not exist, return empty result")
return &SriovResult{}, nil
} else {
log.Log.Error(err, "ReadSriovResult(): failed to check sriov result file", "path", SriovHostSystemdResultPath)
log.Log.Error(err, "ReadSriovResult(): failed to check sriov result file", "path", utils.GetHostExtensionPath(SriovSystemdResultPath))
return nil, err
}
}

rawConfig, err := os.ReadFile(SriovHostSystemdResultPath)
rawConfig, err := os.ReadFile(utils.GetHostExtensionPath(SriovSystemdResultPath))
if err != nil {
log.Log.Error(err, "ReadSriovResult(): failed to read sriov result file", "path", SriovHostSystemdResultPath)
log.Log.Error(err, "ReadSriovResult(): failed to read sriov result file", "path", utils.GetHostExtensionPath(SriovSystemdResultPath))
return nil, err
}

result := &SriovResult{}
err = yaml.Unmarshal(rawConfig, &result)
if err != nil {
log.Log.Error(err, "ReadSriovResult(): failed to unmarshal sriov result file", "path", SriovHostSystemdResultPath)
log.Log.Error(err, "ReadSriovResult(): failed to unmarshal sriov result file", "path", utils.GetHostExtensionPath(SriovSystemdResultPath))
return nil, err
}
return result, err
}

func RemoveSriovResult() error {
err := os.Remove(SriovHostSystemdResultPath)
err := os.Remove(utils.GetHostExtensionPath(SriovSystemdResultPath))
if err != nil {
if os.IsNotExist(err) {
log.Log.V(2).Info("RemoveSriovResult(): result file not found")
return nil
}
log.Log.Error(err, "RemoveSriovResult(): failed to remove sriov result file", "path", SriovHostSystemdResultPath)
log.Log.Error(err, "RemoveSriovResult(): failed to remove sriov result file", "path", utils.GetHostExtensionPath(SriovSystemdResultPath))
return err
}
log.Log.V(2).Info("RemoveSriovResult(): result file removed")
return nil
}

func WriteSriovSupportedNics() error {
_, err := os.Stat(sriovHostSystemdSupportedNicPath)
_, err := os.Stat(utils.GetHostExtensionPath(sriovSystemdSupportedNicPath))
if err != nil {
if os.IsNotExist(err) {
log.Log.V(2).Info("WriteSriovSupportedNics(): file does not exist, create it")
_, err = os.Create(sriovHostSystemdSupportedNicPath)
_, err = os.Create(utils.GetHostExtensionPath(sriovSystemdSupportedNicPath))
if err != nil {
log.Log.Error(err, "WriteSriovSupportedNics(): failed to create sriov supporter nics ids file",
"path", sriovHostSystemdSupportedNicPath)
"path", utils.GetHostExtensionPath(sriovSystemdSupportedNicPath))
return err
}
} else {
log.Log.Error(err, "WriteSriovSupportedNics(): failed to check sriov supported nics ids file", "path", sriovHostSystemdSupportedNicPath)
log.Log.Error(err, "WriteSriovSupportedNics(): failed to check sriov supported nics ids file", "path", utils.GetHostExtensionPath(sriovSystemdSupportedNicPath))
return err
}
}
Expand All @@ -250,31 +242,31 @@ func WriteSriovSupportedNics() error {
rawNicList = append(rawNicList, []byte(fmt.Sprintf("%s\n", line))...)
}

err = os.WriteFile(sriovHostSystemdSupportedNicPath, rawNicList, 0644)
err = os.WriteFile(utils.GetHostExtensionPath(sriovSystemdSupportedNicPath), rawNicList, 0644)
if err != nil {
log.Log.Error(err, "WriteSriovSupportedNics(): failed to write sriov supported nics ids file",
"path", sriovHostSystemdSupportedNicPath)
"path", utils.GetHostExtensionPath(sriovSystemdSupportedNicPath))
return err
}

return nil
}

func ReadSriovSupportedNics() ([]string, error) {
_, err := os.Stat(sriovSystemdSupportedNicPath)
_, err := os.Stat(utils.GetHostExtensionPath(sriovSystemdSupportedNicPath))
if err != nil {
if os.IsNotExist(err) {
log.Log.V(2).Info("ReadSriovSupportedNics(): file does not exist, return empty result")
return nil, err
} else {
log.Log.Error(err, "ReadSriovSupportedNics(): failed to check sriov supported nics file", "path", sriovSystemdSupportedNicPath)
log.Log.Error(err, "ReadSriovSupportedNics(): failed to check sriov supported nics file", "path", utils.GetHostExtensionPath(sriovSystemdSupportedNicPath))
return nil, err
}
}

rawConfig, err := os.ReadFile(sriovSystemdSupportedNicPath)
rawConfig, err := os.ReadFile(utils.GetHostExtensionPath(sriovSystemdSupportedNicPath))
if err != nil {
log.Log.Error(err, "ReadSriovSupportedNics(): failed to read sriov supported nics file", "path", sriovSystemdSupportedNicPath)
log.Log.Error(err, "ReadSriovSupportedNics(): failed to read sriov supported nics file", "path", utils.GetHostExtensionPath(sriovSystemdSupportedNicPath))
return nil, err
}

Expand All @@ -283,33 +275,33 @@ func ReadSriovSupportedNics() ([]string, error) {
}

func CleanSriovFilesFromHost(isOpenShift bool) error {
err := os.Remove(SriovHostSystemdConfigPath)
err := os.Remove(utils.GetHostExtensionPath(SriovSystemdConfigPath))
if err != nil && !os.IsNotExist(err) {
return err
}

err = os.Remove(SriovHostSystemdResultPath)
err = os.Remove(utils.GetHostExtensionPath(SriovSystemdResultPath))
if err != nil && !os.IsNotExist(err) {
return err
}

err = os.Remove(sriovHostSystemdSupportedNicPath)
err = os.Remove(utils.GetHostExtensionPath(sriovSystemdSupportedNicPath))
if err != nil && !os.IsNotExist(err) {
return err
}

err = os.Remove(sriovHostSystemdServiceBinaryPath)
err = os.Remove(utils.GetHostExtensionPath(sriovSystemdServiceBinaryPath))
if err != nil && !os.IsNotExist(err) {
return err
}

// in openshift we should not remove the systemd service it will be done by the machine config operator
if !isOpenShift {
err = os.Remove(SriovHostServicePath)
err = os.Remove(utils.GetHostExtensionPath(SriovServicePath))
if err != nil && !os.IsNotExist(err) {
return err
}
err = os.Remove(SriovHostPostNetworkServicePath)
err = os.Remove(utils.GetHostExtensionPath(SriovPostNetworkServicePath))
if err != nil && !os.IsNotExist(err) {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func GetHostExtension() string {
return filepath.Join(vars.FilesystemRoot, consts.Host)
}

func GetHostExtensionPath(path string) string {
return filepath.Join(GetHostExtension(), path)
}

func GetChrootExtension() string {
if vars.InChroot {
return vars.FilesystemRoot
Expand Down

0 comments on commit 67155e3

Please sign in to comment.