versions.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "fmt"
  16. "strings"
  17. "github.com/blang/semver"
  18. )
  19. // EtcdVersion specifies an etcd server binaries SemVer.
  20. type EtcdVersion struct {
  21. semver.Version
  22. }
  23. // ParseEtcdVersion parses a SemVer string to an EtcdVersion.
  24. func ParseEtcdVersion(s string) (*EtcdVersion, error) {
  25. v, err := semver.Make(s)
  26. if err != nil {
  27. return nil, err
  28. }
  29. return &EtcdVersion{v}, nil
  30. }
  31. // MustParseEtcdVersion parses a SemVer string to an EtcdVersion and panics if the parse fails.
  32. func MustParseEtcdVersion(s string) *EtcdVersion {
  33. return &EtcdVersion{semver.MustParse(s)}
  34. }
  35. // String returns the version in SemVer string format.
  36. func (v *EtcdVersion) String() string {
  37. return v.Version.String()
  38. }
  39. // Equals returns true if the versions are exactly equal.
  40. func (v *EtcdVersion) Equals(o *EtcdVersion) bool {
  41. return v.Version.Equals(o.Version)
  42. }
  43. // MajorMinorEquals returns true if the major and minor parts of the versions are equal;
  44. // if only patch versions differ, this returns true.
  45. func (v *EtcdVersion) MajorMinorEquals(o *EtcdVersion) bool {
  46. return v.Major == o.Major && v.Minor == o.Minor
  47. }
  48. // EtcdStorageVersion identifies the storage version of an etcd data directory.
  49. type EtcdStorageVersion int
  50. const (
  51. storageUnknown EtcdStorageVersion = iota
  52. storageEtcd2
  53. storageEtcd3
  54. )
  55. // ParseEtcdStorageVersion parses an etcd storage version string to an EtcdStorageVersion.
  56. func ParseEtcdStorageVersion(s string) (EtcdStorageVersion, error) {
  57. switch s {
  58. case "etcd2":
  59. return storageEtcd2, nil
  60. case "etcd3":
  61. return storageEtcd3, nil
  62. default:
  63. return storageUnknown, fmt.Errorf("unrecognized storage version: %s", s)
  64. }
  65. }
  66. // MustParseEtcdStorageVersion parses an etcd storage version string to an EtcdStorageVersion and
  67. // panics if the parse fails.
  68. func MustParseEtcdStorageVersion(s string) EtcdStorageVersion {
  69. version, err := ParseEtcdStorageVersion(s)
  70. if err != nil {
  71. panic(err)
  72. }
  73. return version
  74. }
  75. // String returns the text representation of the EtcdStorageVersion, 'etcd2' or 'etcd3'.
  76. func (v EtcdStorageVersion) String() string {
  77. switch v {
  78. case storageEtcd2:
  79. return "etcd2"
  80. case storageEtcd3:
  81. return "etcd3"
  82. default:
  83. panic(fmt.Sprintf("enum value %d missing from EtcdStorageVersion String() function", v))
  84. }
  85. }
  86. // EtcdVersionPair is composed of an etcd version and storage version.
  87. type EtcdVersionPair struct {
  88. version *EtcdVersion
  89. storageVersion EtcdStorageVersion
  90. }
  91. // ParseEtcdVersionPair parses a "<version>/<storage-version>" string to an EtcdVersionPair.
  92. func ParseEtcdVersionPair(s string) (*EtcdVersionPair, error) {
  93. parts := strings.Split(s, "/")
  94. if len(parts) != 2 {
  95. return nil, fmt.Errorf("malformed version file, expected <major>.<minor>.<patch>/<storage> but got %s", s)
  96. }
  97. version, err := ParseEtcdVersion(parts[0])
  98. if err != nil {
  99. return nil, err
  100. }
  101. storageVersion, err := ParseEtcdStorageVersion(parts[1])
  102. if err != nil {
  103. return nil, err
  104. }
  105. return &EtcdVersionPair{version, storageVersion}, nil
  106. }
  107. // MustParseEtcdVersionPair parses a "<version>/<storage-version>" string to an EtcdVersionPair
  108. // or panics if the parse fails.
  109. func MustParseEtcdVersionPair(s string) *EtcdVersionPair {
  110. pair, err := ParseEtcdVersionPair(s)
  111. if err != nil {
  112. panic(err)
  113. }
  114. return pair
  115. }
  116. // String returns "<version>/<storage-version>" string of the EtcdVersionPair.
  117. func (vp *EtcdVersionPair) String() string {
  118. return fmt.Sprintf("%s/%s", vp.version, vp.storageVersion)
  119. }
  120. // Equals returns true if both the versions and storage versions are exactly equal.
  121. func (vp *EtcdVersionPair) Equals(o *EtcdVersionPair) bool {
  122. return vp.version.Equals(o.version) && vp.storageVersion == o.storageVersion
  123. }
  124. // SupportedVersions provides a list of etcd versions that are "supported" for some purpose.
  125. // The list must be sorted from lowest semantic version to high.
  126. type SupportedVersions []*EtcdVersion
  127. // NextVersion returns the next supported version after the given current version, or nil if no
  128. // next version exists.
  129. func (sv SupportedVersions) NextVersion(current *EtcdVersion) *EtcdVersion {
  130. var nextVersion *EtcdVersion
  131. for i, supportedVersion := range sv {
  132. if current.MajorMinorEquals(supportedVersion) && len(sv) > i+1 {
  133. nextVersion = sv[i+1]
  134. }
  135. }
  136. return nextVersion
  137. }
  138. // NextVersionPair returns the next supported version after the given current version and infers
  139. // the storage version from the major version part of the next version.
  140. func (sv SupportedVersions) NextVersionPair(current *EtcdVersionPair) *EtcdVersionPair {
  141. nextVersion := sv.NextVersion(current.version)
  142. if nextVersion == nil {
  143. return nil
  144. }
  145. storageVersion := storageEtcd3
  146. if nextVersion.Major == 2 {
  147. storageVersion = storageEtcd2
  148. }
  149. return &EtcdVersionPair{version: nextVersion, storageVersion: storageVersion}
  150. }
  151. // ParseSupportedVersions parses a comma separated list of etcd versions.
  152. func ParseSupportedVersions(s string) (SupportedVersions, error) {
  153. var err error
  154. list := strings.Split(s, ",")
  155. versions := make(SupportedVersions, len(list))
  156. for i, v := range list {
  157. versions[i], err = ParseEtcdVersion(strings.TrimSpace(v))
  158. if err != nil {
  159. return nil, err
  160. }
  161. }
  162. return versions, nil
  163. }
  164. // MustParseSupportedVersions parses a comma separated list of etcd versions or panics if the parse fails.
  165. func MustParseSupportedVersions(s string) SupportedVersions {
  166. versions, err := ParseSupportedVersions(s)
  167. if err != nil {
  168. panic(err)
  169. }
  170. return versions
  171. }