Skip to content

Commit

Permalink
switch the bash files implementation with golang configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Sch <sebassch@gmail.com>
  • Loading branch information
SchSeba committed Feb 14, 2023
1 parent eec7d58 commit 6a1bb44
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 93 deletions.
75 changes: 0 additions & 75 deletions bindata/scripts/enable-rdma.sh

This file was deleted.

16 changes: 0 additions & 16 deletions bindata/scripts/load-kmod.sh

This file was deleted.

3 changes: 2 additions & 1 deletion pkg/service/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/coreos/go-systemd/v22/unit"
"github.com/golang/glog"
"gopkg.in/yaml.v2"
)

Expand All @@ -28,7 +29,7 @@ OUTER:
continue OUTER
}
}

glog.Infof("DEBUG: %+v %v", optsA, *optB)
return true, nil
}

Expand Down
288 changes: 288 additions & 0 deletions pkg/systemd/systemd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package systemd

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/golang/glog"
"gopkg.in/yaml.v3"

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

const (
SriovSystemdConfigPath = utils.SriovConfBasePath + "/sriov-interface-config.yaml"
SriovSystemdResultPath = utils.SriovConfBasePath + "/sriov-interface-result.yaml"
sriovSystemdSupportedNicPath = utils.SriovConfBasePath + "/sriov-supported-nics-ids.yaml"
sriovSystemdServiceBinaryPath = "/var/lib/sriov/sriov-network-config-daemon"

SriovHostSystemdConfigPath = "/host" + SriovSystemdConfigPath
SriovHostSystemdResultPath = "/host" + SriovSystemdResultPath
sriovHostSystemdSupportedNicPath = "/host" + sriovSystemdSupportedNicPath
sriovHostSystemdServiceBinaryPath = "/host" + sriovSystemdServiceBinaryPath

SriovServicePath = "/etc/systemd/system/sriov-config.service"
SriovHostServicePath = "/host" + SriovServicePath
)

type SriovConfig struct {
Spec sriovnetworkv1.SriovNetworkNodeStateSpec `yaml:"spec"`
UnsupportedNics bool `yaml:"unsupportedNics"`
PlatformType utils.PlatformType `yaml:"platformType"`
}

type SriovResult struct {
SyncStatus string `yaml:"syncStatus"`
LastSyncError string `yaml:"lastSyncError"`
}

func ReadConfFile() (spec *SriovConfig, err error) {
rawConfig, err := ioutil.ReadFile(SriovSystemdConfigPath)
if err != nil {
return nil, err
}

err = yaml.Unmarshal(rawConfig, &spec)

return spec, err
}

func WriteConfFile(newState *sriovnetworkv1.SriovNetworkNodeState, unsupportedNics bool, platformType utils.PlatformType) (bool, error) {
newFile := false
// remove the device plugin revision as we don't need it here
newState.Spec.DpConfigVersion = ""

sriovConfig := &SriovConfig{
newState.Spec,
unsupportedNics,
platformType,
}

_, err := os.Stat(SriovHostSystemdConfigPath)
if err != nil {
if os.IsNotExist(err) {
// Create the sriov-operator folder on the host if it doesn't exist
if _, err := os.Stat(utils.HostSriovConfBasePath); os.IsNotExist(err) {
err = os.Mkdir(utils.HostSriovConfBasePath, os.ModeDir)
if err != nil {
glog.Errorf("WriteConfFile(): fail to create sriov-operator folder: %v", err)
return false, err
}
}

glog.V(2).Infof("WriteConfFile(): file not existed, create it")
_, err = os.Create(SriovHostSystemdConfigPath)
if err != nil {
glog.Errorf("WriteConfFile(): fail to create file: %v", err)
return false, err
}
newFile = true
} else {
return false, err
}
}

oldContent, err := ioutil.ReadFile(SriovHostSystemdConfigPath)
if err != nil {
glog.Errorf("WriteConfFile(): fail to read file: %v", err)
return false, err
}

oldContentObj := &SriovConfig{}
err = yaml.Unmarshal(oldContent, oldContentObj)
if err != nil {
glog.Errorf("WriteConfFile(): fail to unmarshal old file: %v", err)
return false, err
}

var newContent []byte
newContent, err = yaml.Marshal(sriovConfig)
if err != nil {
glog.Errorf("WriteConfFile(): fail to marshal config: %v", err)
return false, err
}

if bytes.Equal(newContent, oldContent) {
glog.V(2).Info("WriteConfFile(): no update")
return false, nil
}
glog.V(2).Infof("WriteConfFile(): previews configuration is not equal: old config:\n%s\nnew config:\n%s\n", string(oldContent), string(newContent))

glog.V(2).Infof("WriteConfFile(): write '%s' to %s", newContent, SriovHostSystemdConfigPath)
err = ioutil.WriteFile(SriovHostSystemdConfigPath, newContent, 0644)
if err != nil {
glog.Errorf("WriteConfFile(): fail to write file: %v", err)
return false, err
}

// this will be used to mark the first time we create this file.
// this helps to avoid the first reboot after installation
if newFile && len(sriovConfig.Spec.Interfaces) == 0 {
glog.V(2).Info("WriteConfFile(): first file creation and no interfaces to configure returning reboot false")
return false, nil
}

return true, nil
}

func WriteSriovResult(result *SriovResult) error {
_, err := os.Stat(SriovSystemdResultPath)
if err != nil {
if os.IsNotExist(err) {
glog.V(2).Infof("WriteSriovResult(): file not existed, create it")
_, err = os.Create(SriovSystemdResultPath)
if err != nil {
glog.Errorf("WriteSriovResult(): failed to create sriov result file on path %s: %v", SriovSystemdResultPath, err)
return err
}
} else {
glog.Errorf("WriteSriovResult(): failed to check sriov result file on path %s: %v", SriovSystemdResultPath, err)
return err
}
}

out, err := yaml.Marshal(result)
if err != nil {
glog.Errorf("WriteSriovResult(): failed to marshal sriov result file: %v", err)
return err
}

glog.V(2).Infof("WriteSriovResult(): write '%s' to %s", string(out), SriovSystemdResultPath)
err = ioutil.WriteFile(SriovSystemdResultPath, out, 0644)
if err != nil {
glog.Errorf("WriteSriovResult(): failed to write sriov result file on path %s: %v", SriovSystemdResultPath, err)
return err
}

return nil
}

func ReadSriovResult() (*SriovResult, error) {
_, err := os.Stat(SriovHostSystemdResultPath)
if err != nil {
if os.IsNotExist(err) {
glog.V(2).Infof("ReadSriovResult(): file not existed, return empty result")
return &SriovResult{}, nil
} else {
glog.Errorf("ReadSriovResult(): failed to check sriov result file on path %s: %v", SriovHostSystemdResultPath, err)
return nil, err
}
}

rawConfig, err := ioutil.ReadFile(SriovHostSystemdResultPath)
if err != nil {
glog.Errorf("ReadSriovResult(): failed to read sriov result file on path %s: %v", SriovHostSystemdResultPath, err)
return nil, err
}

result := &SriovResult{}
err = yaml.Unmarshal(rawConfig, &result)
if err != nil {
glog.Errorf("ReadSriovResult(): failed to unmarshal sriov result file on path %s: %v", SriovHostSystemdResultPath, err)
return nil, err
}
return result, err
}

func WriteSriovSupportedNics() error {
_, err := os.Stat(sriovHostSystemdSupportedNicPath)
if err != nil {
if os.IsNotExist(err) {
glog.V(2).Infof("WriteSriovSupportedNics(): file not existed, create it")
_, err = os.Create(sriovHostSystemdSupportedNicPath)
if err != nil {
glog.Errorf("WriteSriovSupportedNics(): failed to create sriov supporter nics ids file on path %s: %v", sriovHostSystemdSupportedNicPath, err)
return err
}
} else {
glog.Errorf("WriteSriovSupportedNics(): failed to check sriov supporter nics ids file on path %s: %v", sriovHostSystemdSupportedNicPath, err)
return err
}
}

rawNicList := []byte{}
for _, line := range sriovnetworkv1.NicIDMap {
rawNicList = append(rawNicList, []byte(fmt.Sprintf("%s\n", line))...)
}

err = ioutil.WriteFile(sriovHostSystemdSupportedNicPath, rawNicList, 0644)
if err != nil {
glog.Errorf("WriteSriovSupportedNics(): failed to write sriov supporter nics ids file on path %s: %v", sriovHostSystemdSupportedNicPath, err)
return err
}

return nil
}

func ReadSriovSupportedNics() ([]string, error) {
_, err := os.Stat(sriovSystemdSupportedNicPath)
if err != nil {
if os.IsNotExist(err) {
glog.V(2).Infof("ReadSriovSupportedNics(): file not existed, return empty result")
return nil, err
} else {
glog.Errorf("ReadSriovSupportedNics(): failed to check sriov supporter nics file on path %s: %v", sriovSystemdSupportedNicPath, err)
return nil, err
}
}

rawConfig, err := ioutil.ReadFile(sriovSystemdSupportedNicPath)
if err != nil {
glog.Errorf("ReadSriovSupportedNics(): failed to read sriov supporter nics file on path %s: %v", sriovSystemdSupportedNicPath, err)
return nil, err
}

lines := strings.Split(string(rawConfig), "\n")
return lines, nil
}

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

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

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

err = os.Remove(sriovHostSystemdServiceBinaryPath)
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)
if err != nil && !os.IsNotExist(err) {
return err
}
}

return nil
}
Loading

0 comments on commit 6a1bb44

Please sign in to comment.