Skip to content

Commit

Permalink
types: implement convert module and make types use it
Browse files Browse the repository at this point in the history
Moving to spec 1.0.0 requires more complicated conversions, so
put the converter determination logic in a separate module that
any of the types can call to convert between arbitrary versions.
This is necessary to ensure the types don't have import cycles.

This also implements downconversion, which we never claimed
*not* to support, but didn't implement.

Signed-off-by: Dan Williams <dcbw@redhat.com>
  • Loading branch information
dcbw committed Aug 13, 2020
1 parent e915473 commit fd6dbbb
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 114 deletions.
67 changes: 58 additions & 9 deletions pkg/types/020/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,23 @@ import (
"os"

"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/convert"
)

const ImplementedSpecVersion string = "0.2.0"

var SupportedVersions = []string{"", "0.1.0", ImplementedSpecVersion}

// Register converters for all versions less than the implemented spec version
func init() {
convert.Register("0.1.0", []string{ImplementedSpecVersion}, convertFrom010)
convert.Register(ImplementedSpecVersion, []string{"0.1.0"}, convertTo010)
}

// Compatibility types for CNI version 0.1.0 and 0.2.0

// NewResult creates a new Result object from JSON data. The JSON data
// must be compatible with the CNI versions implemented by this type.
func NewResult(data []byte) (types.Result, error) {
result := &Result{}
if err := json.Unmarshal(data, result); err != nil {
Expand All @@ -38,9 +47,10 @@ func NewResult(data []byte) (types.Result, error) {
return result, nil
}

// GetResult converts the given Result object to the ImplementedSpecVersion
// and returns the concrete type or an error
func GetResult(r types.Result) (*Result, error) {
// We expect version 0.1.0/0.2.0 results
result020, err := r.GetAsVersion(ImplementedSpecVersion)
result020, err := convert.Convert(r, ImplementedSpecVersion)
if err != nil {
return nil, err
}
Expand All @@ -51,6 +61,39 @@ func GetResult(r types.Result) (*Result, error) {
return result, nil
}

func copyIPConfig(from *IPConfig) *IPConfig {
if from == nil {
return nil
}
return from.Copy()
}

func convertFrom010(from types.Result, toVersion string) (types.Result, error) {
if toVersion != "0.1.0" {
panic("only converts to version 0.1.0")
}
fromResult := from.(*Result)
return &Result{
CNIVersion: ImplementedSpecVersion,
IP4: copyIPConfig(fromResult.IP4),
IP6: copyIPConfig(fromResult.IP6),
DNS: types.CopyDNS(fromResult.DNS),
}, nil
}

func convertTo010(from types.Result, toVersion string) (types.Result, error) {
if toVersion != "0.2.0" {
panic("only converts to version 0.2.0")
}
fromResult := from.(*Result)
return &Result{
CNIVersion: "0.1.0",
IP4: copyIPConfig(fromResult.IP4),
IP6: copyIPConfig(fromResult.IP6),
DNS: types.CopyDNS(fromResult.DNS),
}, nil
}

// Result is what gets returned from the plugin (via stdout) to the caller
type Result struct {
CNIVersion string `json:"cniVersion,omitempty"`
Expand All @@ -64,13 +107,7 @@ func (r *Result) Version() string {
}

func (r *Result) GetAsVersion(version string) (types.Result, error) {
for _, supportedVersion := range SupportedVersions {
if version == supportedVersion {
r.CNIVersion = version
return r, nil
}
}
return nil, fmt.Errorf("cannot convert version %q to %s", SupportedVersions, version)
return convert.Convert(r, version)
}

func (r *Result) Print() error {
Expand All @@ -93,6 +130,18 @@ type IPConfig struct {
Routes []types.Route
}

func (i *IPConfig) Copy() *IPConfig {
var routes []types.Route
for _, fromRoute := range i.Routes {
routes = append(routes, *fromRoute.Copy())
}
return &IPConfig{
IP: i.IP,
Gateway: i.Gateway,
Routes: routes,
}
}

// net.IPNet is not JSON (un)marshallable so this duality is needed
// for our custom IPNet type

Expand Down
88 changes: 88 additions & 0 deletions pkg/types/convert/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2016 CNI authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package convert

import (
"fmt"

"github.com/containernetworking/cni/pkg/types"
)

// ConvertFn should convert from the given arbitrary Result type into a
// Result implementing CNI specification version passed in toVersion.
// The function is guaranteed to be passed a Result type matching the
// fromVersion it was registered with, and is guaranteed to be
// passed a toVersion matching one of the toVersions it was registered with.
type ConvertFn func(from types.Result, toVersion string) (types.Result, error)

type converter struct {
// fromVersion is the CNI Result spec version that convertFn accepts
fromVersion string
// toVersions is a list of versions that convertFn can convert to
toVersions []string
convertFn ConvertFn
}

var converters []*converter

func findConverter(fromVersion, toVersion string) *converter {
for _, c := range converters {
if c.fromVersion == fromVersion {
for _, v := range c.toVersions {
if v == toVersion {
return c
}
}
}
}
return nil
}

// Convert converts a CNI Result to the requested CNI specification version,
// or returns an error if the converstion could not be performed or failed
func Convert(from types.Result, toVersion string) (types.Result, error) {
fromVersion := from.Version()

// Shortcut for same version
if fromVersion == toVersion {
return from, nil
}

// Otherwise find the right converter
c := findConverter(fromVersion, toVersion)
if c == nil {
return nil, fmt.Errorf("no converter for CNI result version %s to %s",
fromVersion, toVersion)
}
return c.convertFn(from, toVersion)
}

// Register registers a CNI Result converter. SHOULD NOT BE CALLED
// EXCEPT FROM CNI ITSELF.
func Register(fromVersion string, toVersions []string, convertFn ConvertFn) {
// Make sure there is no converter already registered for these
// from and to versions
for _, v := range toVersions {
if findConverter(fromVersion, v) != nil {
panic(fmt.Sprintf("converter already registered for %s to %s",
fromVersion, v))
}
}
converters = append(converters, &converter{
fromVersion: fromVersion,
toVersions: toVersions,
convertFn: convertFn,
})
}
Loading

0 comments on commit fd6dbbb

Please sign in to comment.