| 1 |
// Copyright 2016 CNI authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package convert |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
|
| 20 |
"github.com/containernetworking/cni/pkg/types" |
| 21 |
) |
| 22 |
|
| 23 |
// ConvertFn should convert from the given arbitrary Result type into a |
| 24 |
// Result implementing CNI specification version passed in toVersion. |
| 25 |
// The function is guaranteed to be passed a Result type matching the |
| 26 |
// fromVersion it was registered with, and is guaranteed to be |
| 27 |
// passed a toVersion matching one of the toVersions it was registered with. |
| 28 |
type ConvertFn func(from types.Result, toVersion string) (types.Result, error) |
| 29 |
|
| 30 |
type converter struct { |
| 31 |
// fromVersion is the CNI Result spec version that convertFn accepts |
| 32 |
fromVersion string |
| 33 |
// toVersions is a list of versions that convertFn can convert to |
| 34 |
toVersions []string |
| 35 |
convertFn ConvertFn |
| 36 |
} |
| 37 |
|
| 38 |
var converters []*converter |
| 39 |
|
| 40 |
func findConverter(fromVersion, toVersion string) *converter { |
| 41 |
for _, c := range converters { |
| 42 |
if c.fromVersion == fromVersion { |
| 43 |
for _, v := range c.toVersions { |
| 44 |
if v == toVersion { |
| 45 |
return c |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
return nil |
| 51 |
} |
| 52 |
|
| 53 |
// Convert converts a CNI Result to the requested CNI specification version, |
| 54 |
// or returns an error if the conversion could not be performed or failed |
| 55 |
func Convert(from types.Result, toVersion string) (types.Result, error) { |
| 56 |
if toVersion == "" { |
| 57 |
toVersion = "0.1.0" |
| 58 |
} |
| 59 |
|
| 60 |
fromVersion := from.Version() |
| 61 |
|
| 62 |
// Shortcut for same version |
| 63 |
if fromVersion == toVersion { |
| 64 |
return from, nil |
| 65 |
} |
| 66 |
|
| 67 |
// Otherwise find the right converter |
| 68 |
c := findConverter(fromVersion, toVersion) |
| 69 |
if c == nil { |
| 70 |
return nil, fmt.Errorf("no converter for CNI result version %s to %s", |
| 71 |
fromVersion, toVersion) |
| 72 |
} |
| 73 |
return c.convertFn(from, toVersion) |
| 74 |
} |
| 75 |
|
| 76 |
// RegisterConverter registers a CNI Result converter. SHOULD NOT BE CALLED |
| 77 |
// EXCEPT FROM CNI ITSELF. |
| 78 |
func RegisterConverter(fromVersion string, toVersions []string, convertFn ConvertFn) { |
| 79 |
// Make sure there is no converter already registered for these |
| 80 |
// from and to versions |
| 81 |
for _, v := range toVersions { |
| 82 |
if findConverter(fromVersion, v) != nil { |
| 83 |
panic(fmt.Sprintf("converter already registered for %s to %s", |
| 84 |
fromVersion, v)) |
| 85 |
} |
| 86 |
} |
| 87 |
converters = append(converters, &converter{ |
| 88 |
fromVersion: fromVersion, |
| 89 |
toVersions: toVersions, |
| 90 |
convertFn: convertFn, |
| 91 |
}) |
| 92 |
} |