Skip to content

Commit

Permalink
Merge pull request #2153 from cyclinder/spidermultusconfig/custom
Browse files Browse the repository at this point in the history
spidermultusconfig: validate customcniconfig if is valid
  • Loading branch information
weizhoublue authored Aug 8, 2023
2 parents 6c2581a + c767310 commit 14a02fe
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
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

0 comments on commit 14a02fe

Please sign in to comment.