Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Validate the linkType correctly when it can and cannot be changed #19

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions pkg/host/configvalidation.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,26 @@ func (v *configValidationImpl) ConstructNvParamMapFromTemplate(
desiredParameters[consts.SriovNumOfVfsParam] = strconv.Itoa(template.NumVfs)
}

if template.LinkType != "" {
// Link type change is not allowed on some devices
if _, found := defaultValues[consts.LinkTypeP1Param]; found {
linkType := nvParamLinkTypeFromName(string(template.LinkType))
desiredParameters[consts.LinkTypeP1Param] = linkType
if secondPortPresent {
desiredParameters[consts.LinkTypeP2Param] = linkType
}
// Link type change is not allowed on some devices
_, canChangeLinkType := defaultValues[consts.LinkTypeP1Param]
if canChangeLinkType {
linkType := nvParamLinkTypeFromName(string(template.LinkType))
desiredParameters[consts.LinkTypeP1Param] = linkType
if secondPortPresent {
desiredParameters[consts.LinkTypeP2Param] = linkType
}
if device.Status.Ports[0].NetworkInterface != "" &&
v.utils.GetLinkType(device.Status.Ports[0].NetworkInterface) !=
string(device.Spec.Configuration.Template.LinkType) {
err := types.IncorrectSpecError(
fmt.Sprintf(
"device doesn't support link type change, wrong link type provided in the template, should be: %s",
v.utils.GetLinkType(pciAddr)))
log.Log.Error(err, "incorrect spec", "device", device.Name)
return desiredParameters, err
} else {
desiredLinkType := string(device.Spec.Configuration.Template.LinkType)

for _, port := range device.Status.Ports {
if port.NetworkInterface != "" && v.utils.GetLinkType(port.NetworkInterface) != desiredLinkType {
err := types.IncorrectSpecError(
fmt.Sprintf(
"device doesn't support link type change, wrong link type provided in the template, should be: %s",
v.utils.GetLinkType(port.NetworkInterface)))
log.Log.Error(err, "incorrect spec", "device", device.Name)
return desiredParameters, err
}
}
}

Expand Down
107 changes: 107 additions & 0 deletions pkg/host/configvalidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,113 @@ var _ = Describe("ConfigValidationImpl", func() {
Expect(nvParams).To(HaveKeyWithValue("TEST_P1", "test"))
Expect(nvParams).To(HaveKeyWithValue("TEST_P2", "test"))
})
It("should report an error when LinkType cannot be changed and template differs from the actual status", func() {
mockHostUtils.On("GetLinkType", mock.Anything).Return(consts.Ethernet)
mockHostUtils.On("GetPCILinkSpeed", mock.Anything).Return(16, nil)

device := &v1alpha1.NicDevice{
Spec: v1alpha1.NicDeviceSpec{
Configuration: &v1alpha1.NicDeviceConfigurationSpec{
Template: &v1alpha1.ConfigurationTemplateSpec{
NumVfs: 0,
LinkType: consts.Infiniband,
PciPerformanceOptimized: &v1alpha1.PciPerformanceOptimizedSpec{
Enabled: true,
},
},
},
},
Status: v1alpha1.NicDeviceStatus{
Ports: []v1alpha1.NicDevicePortSpec{
{
PCI: "0000:03:00.0",
NetworkInterface: "enp3s0f0np0",
},
{
PCI: "0000:03:00.1",
NetworkInterface: "enp3s0f1np1",
},
},
},
}

defaultValues := map[string]string{}

_, err := validator.ConstructNvParamMapFromTemplate(device, defaultValues)
Expect(err).To(MatchError("incorrect spec: device doesn't support link type change, wrong link type provided in the template, should be: Ethernet"))
})
It("should not report an error when LinkType can be changed and template differs from the actual status", func() {
mockHostUtils.On("GetLinkType", mock.Anything).Return(consts.Ethernet)
mockHostUtils.On("GetPCILinkSpeed", mock.Anything).Return(16, nil)

device := &v1alpha1.NicDevice{
Spec: v1alpha1.NicDeviceSpec{
Configuration: &v1alpha1.NicDeviceConfigurationSpec{
Template: &v1alpha1.ConfigurationTemplateSpec{
NumVfs: 0,
LinkType: consts.Infiniband,
PciPerformanceOptimized: &v1alpha1.PciPerformanceOptimizedSpec{
Enabled: true,
},
},
},
},
Status: v1alpha1.NicDeviceStatus{
Ports: []v1alpha1.NicDevicePortSpec{
{
PCI: "0000:03:00.0",
NetworkInterface: "enp3s0f0np0",
},
{
PCI: "0000:03:00.1",
NetworkInterface: "enp3s0f1np1",
},
},
},
}

defaultValues := map[string]string{
consts.LinkTypeP1Param: consts.Ethernet,
}

_, err := validator.ConstructNvParamMapFromTemplate(device, defaultValues)
Expect(err).NotTo(HaveOccurred())
})
It("should not report an error when LinkType cannot be changed and template matches the actual status", func() {
mockHostUtils.On("GetLinkType", mock.Anything).Return(consts.Infiniband)
mockHostUtils.On("GetPCILinkSpeed", mock.Anything).Return(16, nil)

device := &v1alpha1.NicDevice{
Spec: v1alpha1.NicDeviceSpec{
Configuration: &v1alpha1.NicDeviceConfigurationSpec{
Template: &v1alpha1.ConfigurationTemplateSpec{
NumVfs: 0,
LinkType: consts.Infiniband,
PciPerformanceOptimized: &v1alpha1.PciPerformanceOptimizedSpec{
Enabled: true,
},
},
},
},
Status: v1alpha1.NicDeviceStatus{
Ports: []v1alpha1.NicDevicePortSpec{
{
PCI: "0000:03:00.0",
NetworkInterface: "enp3s0f0np0",
},
{
PCI: "0000:03:00.1",
NetworkInterface: "enp3s0f1np1",
},
},
},
}

defaultValues := map[string]string{}

_, err := validator.ConstructNvParamMapFromTemplate(device, defaultValues)
Expect(err).NotTo(HaveOccurred())
})
})

Describe("ValidateResetToDefault", func() {
Expand Down
Loading