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

spidermultusconfig: validate customcniconfig if is valid #2153

Merged
merged 1 commit into from
Aug 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/multuscniconfig/multusconfig_informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,12 @@ func generateNetAttachDef(netAttachName string, multusConf *spiderpoolv2beta1.Sp
}
case CustomType:
if multusConfSpec.CustomCNIConfig != nil && len(*multusConfSpec.CustomCNIConfig) > 0 {
if !json.Valid([]byte(*multusConfSpec.CustomCNIConfig)) {
return nil, fmt.Errorf("customCniConfig isn't a valid JSON encoding")
}
confStr = *multusConfSpec.CustomCNIConfig
}

default:
// It's impossible get into the default branch
return nil, fmt.Errorf("%w: unrecognized CNI type %s", constant.ErrWrongInput, multusConfSpec.CniType)
Expand Down
7 changes: 7 additions & 0 deletions pkg/multuscniconfig/multusconfig_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package multuscniconfig

import (
"encoding/json"
"fmt"

"k8s.io/apimachinery/pkg/util/validation/field"
Expand Down Expand Up @@ -84,6 +85,12 @@ func validateCNIConfig(multusConfig *spiderpoolv2beta1.SpiderMultusConfig) *fiel
if multusConfig.Spec.MacvlanConfig != nil || multusConfig.Spec.IPVlanConfig != nil || multusConfig.Spec.SriovConfig != nil {
return field.Forbidden(cniTypeField, fmt.Sprintf("the cniType %s only supports %s, please remove other CNI configs", CustomType, customCniConfigField.String()))
}

if multusConfig.Spec.CustomCNIConfig != nil && *multusConfig.Spec.CustomCNIConfig != "" {
if !json.Valid([]byte(*multusConfig.Spec.CustomCNIConfig)) {
return field.Forbidden(customCniConfigField, "customCniConfig isn't a valid JSON encoding")
}
}
}

if multusConfig.Spec.CoordinatorConfig != nil {
Expand Down
1 change: 1 addition & 0 deletions test/doc/spidermultus.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
| M00002 | testing creating spiderMultusConfig with cniType: ipvlan and checking the net-attach-conf config if works | p1 | smoke | | |
| M00003 | testing creating spiderMultusConfig with cniType: sriov and checking the net-attach-conf config if works | p1 | smoke | | |
| M00004 | testing creating spiderMultusConfig with cniType: custom and checking the net-attach-conf config if works | p1 | smoke | | |
| M00004 | testing creating spiderMultusConfig with cniType: custom and invalid json config, expect error happened | p1 | smoke | | |
| M00005 | testing creating spiderMultusConfig with cniType: macvlan with vlanId with one master and checking the net-attach-conf config if works | p1 | smoke | | |
| M00006 | testing creating spiderMultusConfig with cniType: macvlan with vlanId with two master with bond config and checking the net-attach-conf config if works | p1 | smoke | | |
| M00007 | After deleting spiderMultusConfig, the corresponding net-attach-conf will also be deleted | p1 | smoke | | |
Expand Down