Skip to content

Commit

Permalink
Add switchdev-after-networkmanager services to k8s plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Zenghui Shi <[email protected]>
  • Loading branch information
zshi-redhat authored and SchSeba committed Dec 27, 2021
1 parent a491059 commit 66f3ae4
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 64 deletions.
192 changes: 128 additions & 64 deletions pkg/plugins/k8s/k8s_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,50 @@ import (
)

type K8sPlugin struct {
PluginName string
SpecVersion string
serviceManager service.ServiceManager
switchdevRunScript *service.ScriptManifestFile
switchdevUdevScript *service.ScriptManifestFile
switchdevService *service.Service
openVSwitchService *service.Service
networkManagerService *service.Service
updateTarget *k8sUpdateTarget
PluginName string
SpecVersion string
serviceManager service.ServiceManager
switchdevBeforeNMRunScript *service.ScriptManifestFile
switchdevAfterNMRunScript *service.ScriptManifestFile
switchdevUdevScript *service.ScriptManifestFile
switchdevBeforeNMService *service.Service
switchdevAfterNMService *service.Service
openVSwitchService *service.Service
networkManagerService *service.Service
updateTarget *k8sUpdateTarget
}

type k8sUpdateTarget struct {
switchdevService bool
switchdevRunScript bool
switchdevUdevScript bool
systemServices []*service.Service
switchdevBeforeNMService bool
switchdevAfterNMService bool
switchdevBeforeNMRunScript bool
switchdevAfterNMRunScript bool
switchdevUdevScript bool
systemServices []*service.Service
}

func (u *k8sUpdateTarget) needUpdate() bool {
return u.switchdevService || u.switchdevRunScript || u.switchdevUdevScript || len(u.systemServices) > 0
return u.switchdevBeforeNMService || u.switchdevAfterNMService || u.switchdevBeforeNMRunScript || u.switchdevAfterNMRunScript || u.switchdevUdevScript || len(u.systemServices) > 0
}

func (u *k8sUpdateTarget) needReboot() bool {
return u.switchdevBeforeNMService || u.switchdevAfterNMService || u.switchdevBeforeNMRunScript || u.switchdevAfterNMRunScript || u.switchdevUdevScript
}

func (u *k8sUpdateTarget) reset() {
u.switchdevService = false
u.switchdevRunScript = false
u.switchdevBeforeNMService = false
u.switchdevAfterNMService = false
u.switchdevBeforeNMRunScript = false
u.switchdevAfterNMRunScript = false
u.systemServices = []*service.Service{}
}

func (u *k8sUpdateTarget) String() string {
var updateList []string
if u.switchdevService {
if u.switchdevBeforeNMService || u.switchdevAfterNMService {
updateList = append(updateList, "SwitchdevService")
}
if u.switchdevRunScript {
if u.switchdevBeforeNMRunScript || u.switchdevAfterNMRunScript {
updateList = append(updateList, "SwitchdevRunScript")
}
if u.switchdevUdevScript {
Expand All @@ -63,13 +73,15 @@ func (u *k8sUpdateTarget) String() string {
}

const (
switchdevManifestPath = "bindata/manifests/switchdev-config/"
switchdevUnits = switchdevManifestPath + "switchdev-units/"
switchdevUnitFile = switchdevUnits + "switchdev-configuration.yaml"
networkManagerUnitFile = switchdevUnits + "NetworkManager.service.yaml"
ovsUnitFile = switchdevManifestPath + "ovs-units/ovs-vswitchd.service.yaml"
configuresSwitchdevScript = switchdevManifestPath + "files/configure-switchdev.sh.yaml"
switchdevRenamingUdevScript = switchdevManifestPath + "files/switchdev-vf-link-name.sh.yaml"
switchdevManifestPath = "bindata/manifests/switchdev-config/"
switchdevUnits = switchdevManifestPath + "switchdev-units/"
switchdevBeforeNMUnitFile = switchdevUnits + "switchdev-configuration-before-nm.yaml"
switchdevAfterNMUnitFile = switchdevUnits + "switchdev-configuration-after-nm.yaml"
networkManagerUnitFile = switchdevUnits + "NetworkManager.service.yaml"
ovsUnitFile = switchdevManifestPath + "ovs-units/ovs-vswitchd.service.yaml"
configuresSwitchdevBeforeNMScript = switchdevManifestPath + "files/switchdev-configuration-before-nm.sh.yaml"
configuresSwitchdevAfterNMScript = switchdevManifestPath + "files/switchdev-configuration-after-nm.sh.yaml"
switchdevRenamingUdevScript = switchdevManifestPath + "files/switchdev-vf-link-name.sh.yaml"

chroot = "/host"
)
Expand Down Expand Up @@ -131,7 +143,7 @@ func (p *K8sPlugin) OnNodeStateChange(old, new *sriovnetworkv1.SriovNetworkNodeS

if p.updateTarget.needUpdate() {
needDrain = true
if p.updateTarget.switchdevUdevScript {
if p.updateTarget.needReboot() {
needReboot = true
glog.Infof("k8s-plugin OnNodeStateChange(): needReboot to update %q", p.updateTarget)
} else {
Expand All @@ -145,7 +157,7 @@ func (p *K8sPlugin) OnNodeStateChange(old, new *sriovnetworkv1.SriovNetworkNodeS
// Apply config change
func (p *K8sPlugin) Apply() error {
glog.Info("k8s-plugin Apply()")
if err := p.updateSwichdevService(); err != nil {
if err := p.updateSwitchdevService(); err != nil {
return err
}

Expand All @@ -160,7 +172,11 @@ func (p *K8sPlugin) Apply() error {

func (p *K8sPlugin) readSwitchdevManifest() error {
// Read switchdev service
switchdevService, err := service.ReadServiceManifestFile(switchdevUnitFile)
switchdevBeforeNMService, err := service.ReadServiceManifestFile(switchdevBeforeNMUnitFile)
if err != nil {
return err
}
switchdevAfterNMService, err := service.ReadServiceManifestFile(switchdevAfterNMUnitFile)
if err != nil {
return err
}
Expand All @@ -171,18 +187,28 @@ func (p *K8sPlugin) readSwitchdevManifest() error {
Name: "ConditionPathExists",
Value: "!/etc/ignition-machine-config-encapsulated.json",
}
switchdevService, err = service.RemoveFromService(switchdevService, conditionOpt)
switchdevBeforeNMService, err = service.RemoveFromService(switchdevBeforeNMService, conditionOpt)
if err != nil {
return err
}
switchdevAfterNMService, err = service.RemoveFromService(switchdevAfterNMService, conditionOpt)
if err != nil {
return err
}
p.switchdevService = switchdevService
p.switchdevBeforeNMService = switchdevBeforeNMService
p.switchdevAfterNMService = switchdevAfterNMService

// Read switchdev run script
switchdevRunScript, err := service.ReadScriptManifestFile(configuresSwitchdevScript)
switchdevBeforeNMRunScript, err := service.ReadScriptManifestFile(configuresSwitchdevBeforeNMScript)
if err != nil {
return err
}
p.switchdevRunScript = switchdevRunScript
switchdevAfterNMRunScript, err := service.ReadScriptManifestFile(configuresSwitchdevAfterNMScript)
if err != nil {
return err
}
p.switchdevBeforeNMRunScript = switchdevBeforeNMRunScript
p.switchdevAfterNMRunScript = switchdevAfterNMRunScript

// Read switchdev udev script
switchdevUdevScript, err := service.ReadScriptManifestFile(switchdevRenamingUdevScript)
Expand Down Expand Up @@ -232,42 +258,35 @@ func (p *K8sPlugin) readManifestFiles() error {

func (p *K8sPlugin) switchdevServiceStateUpdate() error {
// Check switchdev service
swdService, err := p.serviceManager.ReadService(p.switchdevService.Path)
needUpdate, err := p.isSwitchdevServiceNeedUpdate(p.switchdevBeforeNMService)
if err != nil {
if !os.IsNotExist(err) {
return err
}
// service not exists
p.updateTarget.switchdevService = true
} else {
needChange, err := service.CompareServices(swdService, p.switchdevService)
if err != nil {
return err
}
p.updateTarget.switchdevService = needChange
return err
}
p.updateTarget.switchdevBeforeNMService = needUpdate
needUpdate, err = p.isSwitchdevServiceNeedUpdate(p.switchdevAfterNMService)
if err != nil {
return err
}
p.updateTarget.switchdevAfterNMService = needUpdate

// Check switchdev run script
data, err := ioutil.ReadFile(path.Join(chroot, p.switchdevRunScript.Path))
needUpdate, err = p.isSwitchdevScriptNeedUpdate(p.switchdevBeforeNMRunScript)
if err != nil {
if !os.IsNotExist(err) {
return err
}
p.updateTarget.switchdevRunScript = true
} else if string(data) != p.switchdevRunScript.Contents.Inline {
p.updateTarget.switchdevRunScript = true
return err
}
p.updateTarget.switchdevBeforeNMRunScript = needUpdate
needUpdate, err = p.isSwitchdevScriptNeedUpdate(p.switchdevAfterNMRunScript)
if err != nil {
return err
}
p.updateTarget.switchdevAfterNMRunScript = needUpdate

// Check switchdev udev script
data, err = ioutil.ReadFile(path.Join(chroot, p.switchdevUdevScript.Path))
needUpdate, err = p.isSwitchdevScriptNeedUpdate(p.switchdevUdevScript)
if err != nil {
if !os.IsNotExist(err) {
return err
}
p.updateTarget.switchdevUdevScript = true
} else if string(data) != p.switchdevUdevScript.Contents.Inline {
p.updateTarget.switchdevUdevScript = true
return err
}
p.updateTarget.switchdevUdevScript = needUpdate

return nil
}
Expand All @@ -276,6 +295,36 @@ func (p *K8sPlugin) getSystemServices() []*service.Service {
return []*service.Service{p.networkManagerService, p.openVSwitchService}
}

func (p *K8sPlugin) isSwitchdevScriptNeedUpdate(scriptObj *service.ScriptManifestFile) (needUpdate bool, err error) {
data, err := ioutil.ReadFile(path.Join(chroot, scriptObj.Path))
if err != nil {
if !os.IsNotExist(err) {
return false, err
}
return true, nil
} else if string(data) != scriptObj.Contents.Inline {
return true, nil
}
return false, nil
}

func (p *K8sPlugin) isSwitchdevServiceNeedUpdate(serviceObj *service.Service) (needUpdate bool, err error) {
swdService, err := p.serviceManager.ReadService(serviceObj.Path)
if err != nil {
if !os.IsNotExist(err) {
return false, err
}
// service not exists
return true, nil
} else {
needChange, err := service.CompareServices(swdService, serviceObj)
if err != nil {
return false, err
}
return needChange, nil
}
}

func (p *K8sPlugin) isSystemServiceNeedUpdate(serviceObj *service.Service) bool {
systemService, err := p.serviceManager.ReadService(serviceObj.Path)
if err != nil {
Expand Down Expand Up @@ -330,17 +379,32 @@ func (p *K8sPlugin) servicesStateUpdate() error {
return nil
}

func (p *K8sPlugin) updateSwichdevService() error {
if p.updateTarget.switchdevService {
err := p.serviceManager.EnableService(p.switchdevService)
func (p *K8sPlugin) updateSwitchdevService() error {
if p.updateTarget.switchdevBeforeNMService {
err := p.serviceManager.EnableService(p.switchdevBeforeNMService)
if err != nil {
return err
}
}

if p.updateTarget.switchdevAfterNMService {
err := p.serviceManager.EnableService(p.switchdevAfterNMService)
if err != nil {
return err
}
}

if p.updateTarget.switchdevBeforeNMRunScript {
err := ioutil.WriteFile(path.Join(chroot, p.switchdevBeforeNMRunScript.Path),
[]byte(p.switchdevBeforeNMRunScript.Contents.Inline), 0755)
if err != nil {
return err
}
}

if p.updateTarget.switchdevRunScript {
err := ioutil.WriteFile(path.Join(chroot, p.switchdevRunScript.Path),
[]byte(p.switchdevRunScript.Contents.Inline), 0755)
if p.updateTarget.switchdevAfterNMRunScript {
err := ioutil.WriteFile(path.Join(chroot, p.switchdevAfterNMRunScript.Path),
[]byte(p.switchdevAfterNMRunScript.Contents.Inline), 0755)
if err != nil {
return err
}
Expand Down

0 comments on commit 66f3ae4

Please sign in to comment.