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 committed Dec 20, 2021
1 parent c400742 commit 8d105de
Showing 1 changed file with 106 additions and 46 deletions.
152 changes: 106 additions & 46 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,24 @@ 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
}
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,30 +254,53 @@ func (p *K8sPlugin) readManifestFiles() error {

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

// Check switchdev run script
data, err := ioutil.ReadFile(path.Join(chroot, p.switchdevRunScript.Path))
data, err := ioutil.ReadFile(path.Join(chroot, p.switchdevBeforeNMRunScript.Path))
if err != nil {
if !os.IsNotExist(err) {
return err
}
p.updateTarget.switchdevBeforeNMRunScript = true
} else if string(data) != p.switchdevBeforeNMRunScript.Contents.Inline {
p.updateTarget.switchdevBeforeNMRunScript = true
}
data, err = ioutil.ReadFile(path.Join(chroot, p.switchdevAfterNMRunScript.Path))
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
p.updateTarget.switchdevAfterNMRunScript = true
} else if string(data) != p.switchdevAfterNMRunScript.Contents.Inline {
p.updateTarget.switchdevAfterNMRunScript = true
}

// Check switchdev udev script
Expand Down Expand Up @@ -330,17 +375,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 8d105de

Please sign in to comment.