Skip to content

Commit

Permalink
multus: thick: disallow using cni version <= 0.3.1 with >= 0.4.0
Browse files Browse the repository at this point in the history
Similarly to disallowing incompatible versions in entrypoint.sh,
add the same logic in go for the thick plugin.
  • Loading branch information
bn222 committed Dec 15, 2021
1 parent a374543 commit e5ac226
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module gopkg.in/k8snetworkplumbingwg/multus-cni.v3
go 1.16

require (
github.com/blang/semver v3.5.1+incompatible
github.com/containernetworking/cni v0.8.1
github.com/containernetworking/plugins v0.9.1
github.com/fsnotify/fsnotify v1.4.9
Expand Down
36 changes: 36 additions & 0 deletions pkg/config/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ package config

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"strings"
"time"

"github.com/blang/semver"
)

const (
Expand Down Expand Up @@ -70,6 +73,39 @@ func NewMultusConfig(pluginName string, cniVersion string, kubeconfig string, co
// Generate generates the multus configuration from whatever state is currently
// held
func (mc *MultusConf) Generate() (string, error) {
versionFmt := "delegate cni version is %s while top level cni version is %s"
v040, _ := semver.Make("0.4.0")
v031, _ := semver.Make("0.3.1")
topLevel, err := semver.Make(mc.CNIVersion)

if err != nil {
s := "Error in top level cni version"
return "", errors.New(s)
}

if topLevel.GTE(v040) {
for _, delegate := range mc.Delegates {
delegatesMap, ok := delegate.(map[string]interface{})
if !ok {
s := "Couldn't get cni version of delegate"
return "", errors.New(s)
}
delegateVersion, ok := delegatesMap["cniVersion"].(string)
if !ok {
s := "Couldn't get cni version of delegate"
return "", errors.New(s)
}
v, err := semver.Make(delegateVersion)
if err != nil {
return "", err
}
if v.LTE(v031) {
s := fmt.Sprintf(versionFmt, delegateVersion, mc.CNIVersion)
return "", errors.New(s)
}
}
}

data, err := json.Marshal(mc)
return string(data), err
}
Expand Down
1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ github.com/Microsoft/go-winio/pkg/guid
# github.com/beorn7/perks v1.0.1
github.com/beorn7/perks/quantile
# github.com/blang/semver v3.5.1+incompatible
## explicit
github.com/blang/semver
# github.com/cespare/xxhash/v2 v2.1.1
github.com/cespare/xxhash/v2
Expand Down

0 comments on commit e5ac226

Please sign in to comment.