plugin.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "strconv"
  20. "strings"
  21. )
  22. // PluginInfo reports information about CNI versioning
  23. type PluginInfo interface {
  24. // SupportedVersions returns one or more CNI spec versions that the plugin
  25. // supports. If input is provided in one of these versions, then the plugin
  26. // promises to use the same CNI version in its response
  27. SupportedVersions() []string
  28. // Encode writes this CNI version information as JSON to the given Writer
  29. Encode(io.Writer) error
  30. }
  31. type pluginInfo struct {
  32. CNIVersion_ string `json:"cniVersion"`
  33. SupportedVersions_ []string `json:"supportedVersions,omitempty"`
  34. }
  35. // pluginInfo implements the PluginInfo interface
  36. var _ PluginInfo = &pluginInfo{}
  37. func (p *pluginInfo) Encode(w io.Writer) error {
  38. return json.NewEncoder(w).Encode(p)
  39. }
  40. func (p *pluginInfo) SupportedVersions() []string {
  41. return p.SupportedVersions_
  42. }
  43. // PluginSupports returns a new PluginInfo that will report the given versions
  44. // as supported
  45. func PluginSupports(supportedVersions ...string) PluginInfo {
  46. if len(supportedVersions) < 1 {
  47. panic("programmer error: you must support at least one version")
  48. }
  49. return &pluginInfo{
  50. CNIVersion_: Current(),
  51. SupportedVersions_: supportedVersions,
  52. }
  53. }
  54. // PluginDecoder can decode the response returned by a plugin's VERSION command
  55. type PluginDecoder struct{}
  56. func (*PluginDecoder) Decode(jsonBytes []byte) (PluginInfo, error) {
  57. var info pluginInfo
  58. err := json.Unmarshal(jsonBytes, &info)
  59. if err != nil {
  60. return nil, fmt.Errorf("decoding version info: %s", err)
  61. }
  62. if info.CNIVersion_ == "" {
  63. return nil, fmt.Errorf("decoding version info: missing field cniVersion")
  64. }
  65. if len(info.SupportedVersions_) == 0 {
  66. if info.CNIVersion_ == "0.2.0" {
  67. return PluginSupports("0.1.0", "0.2.0"), nil
  68. }
  69. return nil, fmt.Errorf("decoding version info: missing field supportedVersions")
  70. }
  71. return &info, nil
  72. }
  73. // ParseVersion parses a version string like "3.0.1" or "0.4.5" into major,
  74. // minor, and micro numbers or returns an error
  75. func ParseVersion(version string) (int, int, int, error) {
  76. var major, minor, micro int
  77. if version == "" {
  78. return -1, -1, -1, fmt.Errorf("invalid version %q: the version is empty", version)
  79. }
  80. parts := strings.Split(version, ".")
  81. if len(parts) >= 4 {
  82. return -1, -1, -1, fmt.Errorf("invalid version %q: too many parts", version)
  83. }
  84. major, err := strconv.Atoi(parts[0])
  85. if err != nil {
  86. return -1, -1, -1, fmt.Errorf("failed to convert major version part %q: %v", parts[0], err)
  87. }
  88. if len(parts) >= 2 {
  89. minor, err = strconv.Atoi(parts[1])
  90. if err != nil {
  91. return -1, -1, -1, fmt.Errorf("failed to convert minor version part %q: %v", parts[1], err)
  92. }
  93. }
  94. if len(parts) >= 3 {
  95. micro, err = strconv.Atoi(parts[2])
  96. if err != nil {
  97. return -1, -1, -1, fmt.Errorf("failed to convert micro version part %q: %v", parts[2], err)
  98. }
  99. }
  100. return major, minor, micro, nil
  101. }
  102. // GreaterThanOrEqualTo takes two string versions, parses them into major/minor/micro
  103. // numbers, and compares them to determine whether the first version is greater
  104. // than or equal to the second
  105. func GreaterThanOrEqualTo(version, otherVersion string) (bool, error) {
  106. firstMajor, firstMinor, firstMicro, err := ParseVersion(version)
  107. if err != nil {
  108. return false, err
  109. }
  110. secondMajor, secondMinor, secondMicro, err := ParseVersion(otherVersion)
  111. if err != nil {
  112. return false, err
  113. }
  114. if firstMajor > secondMajor {
  115. return true, nil
  116. } else if firstMajor == secondMajor {
  117. if firstMinor > secondMinor {
  118. return true, nil
  119. } else if firstMinor == secondMinor && firstMicro >= secondMicro {
  120. return true, nil
  121. }
  122. }
  123. return false, nil
  124. }