github.com/john-lin/cni@v0.6.0-rc1.0.20170712150331-b69e640cc0e2/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  	"fmt"
    19  
    20  	"github.com/containernetworking/cni/pkg/types"
    21  	"github.com/containernetworking/cni/pkg/types/020"
    22  	"github.com/containernetworking/cni/pkg/types/current"
    23  )
    24  
    25  // Current reports the version of the CNI spec implemented by this library
    26  func Current() string {
    27  	return "0.3.1"
    28  }
    29  
    30  // Legacy PluginInfo describes a plugin that is backwards compatible with the
    31  // CNI spec version 0.1.0.  In particular, a runtime compiled against the 0.1.0
    32  // library ought to work correctly with a plugin that reports support for
    33  // Legacy versions.
    34  //
    35  // Any future CNI spec versions which meet this definition should be added to
    36  // this list.
    37  var Legacy = PluginSupports("0.1.0", "0.2.0")
    38  var All = PluginSupports("0.1.0", "0.2.0", "0.3.0", "0.3.1")
    39  
    40  var resultFactories = []struct {
    41  	supportedVersions []string
    42  	newResult         types.ResultFactoryFunc
    43  }{
    44  	{current.SupportedVersions, current.NewResult},
    45  	{types020.SupportedVersions, types020.NewResult},
    46  }
    47  
    48  // Finds a Result object matching the requested version (if any) and asks
    49  // that object to parse the plugin result, returning an error if parsing failed.
    50  func NewResult(version string, resultBytes []byte) (types.Result, error) {
    51  	reconciler := &Reconciler{}
    52  	for _, resultFactory := range resultFactories {
    53  		err := reconciler.CheckRaw(version, resultFactory.supportedVersions)
    54  		if err == nil {
    55  			// Result supports this version
    56  			return resultFactory.newResult(resultBytes)
    57  		}
    58  	}
    59  
    60  	return nil, fmt.Errorf("unsupported CNI result version %q", version)
    61  }