sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/plugin/version.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package plugin 18 19 import ( 20 "errors" 21 "fmt" 22 "strconv" 23 "strings" 24 25 "sigs.k8s.io/kubebuilder/v3/pkg/model/stage" 26 ) 27 28 var ( 29 errNegative = errors.New("plugin version number must be positive") 30 errEmpty = errors.New("plugin version is empty") 31 ) 32 33 // Version is a plugin version containing a positive integer and a stage value that represents stability. 34 type Version struct { 35 // Number denotes the current version of a plugin. Two different numbers between versions 36 // indicate that they are incompatible. 37 Number int 38 // Stage indicates stability. 39 Stage stage.Stage 40 } 41 42 // Parse parses version inline, assuming it adheres to format: (v)?[0-9]*(-(alpha|beta))? 43 func (v *Version) Parse(version string) error { 44 version = strings.TrimPrefix(version, "v") 45 if len(version) == 0 { 46 return errEmpty 47 } 48 49 substrings := strings.SplitN(version, "-", 2) 50 51 var err error 52 if v.Number, err = strconv.Atoi(substrings[0]); err != nil { 53 // Lets check if the `-` belonged to a negative number 54 if n, err := strconv.Atoi(version); err == nil && n < 0 { 55 return errNegative 56 } 57 return err 58 } 59 60 if len(substrings) > 1 { 61 if err = v.Stage.Parse(substrings[1]); err != nil { 62 return err 63 } 64 } 65 66 return nil 67 } 68 69 // String returns the string representation of v. 70 func (v Version) String() string { 71 stageStr := v.Stage.String() 72 if len(stageStr) == 0 { 73 return fmt.Sprintf("v%d", v.Number) 74 } 75 return fmt.Sprintf("v%d-%s", v.Number, stageStr) 76 } 77 78 // Validate ensures that the version number is positive and the stage is one of the valid stages. 79 func (v Version) Validate() error { 80 if v.Number < 0 { 81 return errNegative 82 } 83 84 return v.Stage.Validate() 85 } 86 87 // Compare returns -1 if v < other, 0 if v == other, and 1 if v > other. 88 func (v Version) Compare(other Version) int { 89 if v.Number > other.Number { 90 return 1 91 } else if v.Number < other.Number { 92 return -1 93 } 94 95 return v.Stage.Compare(other.Stage) 96 } 97 98 // IsStable returns true if v is stable. 99 func (v Version) IsStable() bool { 100 // Plugin version 0 is not considered stable 101 if v.Number == 0 { 102 return false 103 } 104 105 // Any other version than 0 depends on its stage field 106 return v.Stage.IsStable() 107 }