github.com/baraj55/containernetworking-cni@v0.7.2-0.20200219164625-56ace59a9e7f/pkg/version/version.go (about)

     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 version
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  
    21  	"github.com/containernetworking/cni/pkg/types"
    22  	"github.com/containernetworking/cni/pkg/types/020"
    23  	"github.com/containernetworking/cni/pkg/types/current"
    24  )
    25  
    26  // Current reports the version of the CNI spec implemented by this library
    27  func Current() string {
    28  	return "0.4.0"
    29  }
    30  
    31  // Legacy PluginInfo describes a plugin that is backwards compatible with the
    32  // CNI spec version 0.1.0.  In particular, a runtime compiled against the 0.1.0
    33  // library ought to work correctly with a plugin that reports support for
    34  // Legacy versions.
    35  //
    36  // Any future CNI spec versions which meet this definition should be added to
    37  // this list.
    38  var Legacy = PluginSupports("0.1.0", "0.2.0")
    39  var All = PluginSupports("0.1.0", "0.2.0", "0.3.0", "0.3.1", "0.4.0")
    40  
    41  var resultFactories = []struct {
    42  	supportedVersions []string
    43  	newResult         types.ResultFactoryFunc
    44  }{
    45  	{current.SupportedVersions, current.NewResult},
    46  	{types020.SupportedVersions, types020.NewResult},
    47  }
    48  
    49  // Finds a Result object matching the requested version (if any) and asks
    50  // that object to parse the plugin result, returning an error if parsing failed.
    51  func NewResult(version string, resultBytes []byte) (types.Result, error) {
    52  	reconciler := &Reconciler{}
    53  	for _, resultFactory := range resultFactories {
    54  		err := reconciler.CheckRaw(version, resultFactory.supportedVersions)
    55  		if err == nil {
    56  			// Result supports this version
    57  			return resultFactory.newResult(resultBytes)
    58  		}
    59  	}
    60  
    61  	return nil, fmt.Errorf("unsupported CNI result version %q", version)
    62  }
    63  
    64  // ParsePrevResult parses a prevResult in a NetConf structure and sets
    65  // the NetConf's PrevResult member to the parsed Result object.
    66  func ParsePrevResult(conf *types.NetConf) error {
    67  	if conf.RawPrevResult == nil {
    68  		return nil
    69  	}
    70  
    71  	resultBytes, err := json.Marshal(conf.RawPrevResult)
    72  	if err != nil {
    73  		return fmt.Errorf("could not serialize prevResult: %v", err)
    74  	}
    75  
    76  	conf.RawPrevResult = nil
    77  	conf.PrevResult, err = NewResult(conf.CNIVersion, resultBytes)
    78  	if err != nil {
    79  		return fmt.Errorf("could not parse prevResult: %v", err)
    80  	}
    81  
    82  	return nil
    83  }