plugin.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. package version
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "io"
  19. )
  20. // PluginInfo reports information about CNI versioning
  21. type PluginInfo interface {
  22. // SupportedVersions returns one or more CNI spec versions that the plugin
  23. // supports. If input is provided in one of these versions, then the plugin
  24. // promises to use the same CNI version in its response
  25. SupportedVersions() []string
  26. // Encode writes this CNI version information as JSON to the given Writer
  27. Encode(io.Writer) error
  28. }
  29. type pluginInfo struct {
  30. CNIVersion_ string `json:"cniVersion"`
  31. SupportedVersions_ []string `json:"supportedVersions,omitempty"`
  32. }
  33. // pluginInfo implements the PluginInfo interface
  34. var _ PluginInfo = &pluginInfo{}
  35. func (p *pluginInfo) Encode(w io.Writer) error {
  36. return json.NewEncoder(w).Encode(p)
  37. }
  38. func (p *pluginInfo) SupportedVersions() []string {
  39. return p.SupportedVersions_
  40. }
  41. // PluginSupports returns a new PluginInfo that will report the given versions
  42. // as supported
  43. func PluginSupports(supportedVersions ...string) PluginInfo {
  44. if len(supportedVersions) < 1 {
  45. panic("programmer error: you must support at least one version")
  46. }
  47. return &pluginInfo{
  48. CNIVersion_: Current(),
  49. SupportedVersions_: supportedVersions,
  50. }
  51. }
  52. // PluginDecoder can decode the response returned by a plugin's VERSION command
  53. type PluginDecoder struct{}
  54. func (*PluginDecoder) Decode(jsonBytes []byte) (PluginInfo, error) {
  55. var info pluginInfo
  56. err := json.Unmarshal(jsonBytes, &info)
  57. if err != nil {
  58. return nil, fmt.Errorf("decoding version info: %s", err)
  59. }
  60. if info.CNIVersion_ == "" {
  61. return nil, fmt.Errorf("decoding version info: missing field cniVersion")
  62. }
  63. if len(info.SupportedVersions_) == 0 {
  64. if info.CNIVersion_ == "0.2.0" {
  65. return PluginSupports("0.1.0", "0.2.0"), nil
  66. }
  67. return nil, fmt.Errorf("decoding version info: missing field supportedVersions")
  68. }
  69. return &info, nil
  70. }