-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathkubernetesversion.go
More file actions
104 lines (84 loc) · 2.63 KB
/
Copy pathkubernetesversion.go
File metadata and controls
104 lines (84 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package kubernetesversion contains important structs and method for kubernetes version.
package kubernetesversion
import (
"errors"
"fmt"
"strconv"
"strings"
)
// KubernetesVersion contains major and minor version.
type KubernetesVersion struct {
Major int
Minor int
}
var (
// ErrInvalidFormat is used for invalid format.
ErrInvalidFormat = errors.New("invalid format")
// ErrInvalidMajorVersion is used for invalid major version.
ErrInvalidMajorVersion = errors.New("invalid major version")
// ErrInvalidMinorVersion is used for invalid minor version.
ErrInvalidMinorVersion = errors.New("invalid minor version")
)
// New returns a kubernetes version from specified major and minor version.
func New(majorStr, minorStr string) (KubernetesVersion, error) {
major, err := strconv.Atoi(majorStr)
if err != nil {
return KubernetesVersion{}, ErrInvalidMajorVersion
}
minor, err := strconv.Atoi(minorStr)
if err != nil {
return KubernetesVersion{}, ErrInvalidMinorVersion
}
return KubernetesVersion{
Major: major,
Minor: minor,
}, nil
}
// NewFromString returns a kubernetes version from a specified input.
func NewFromString(str string) (KubernetesVersion, error) {
splitted := strings.Split(str, ".")
if len(splitted) != 2 {
return KubernetesVersion{}, ErrInvalidFormat
}
major, err := strconv.Atoi(splitted[0])
if err != nil {
return KubernetesVersion{}, ErrInvalidMajorVersion
}
minor, err := strconv.Atoi(splitted[1])
if err != nil {
return KubernetesVersion{}, ErrInvalidMinorVersion
}
return KubernetesVersion{
Major: major,
Minor: minor,
}, nil
}
// Validate validates a kubernetes version.
func (kv KubernetesVersion) Validate() error {
if kv.Major == 0 {
return ErrInvalidMajorVersion
}
if kv.Minor == 0 {
return ErrInvalidMinorVersion
}
return nil
}
func (kv KubernetesVersion) String() string {
return fmt.Sprintf("%v-%v", kv.Major, kv.Minor)
}
// StringWithDot returns the Kubernetes version in the format <major>.<minor>, e.g. 1.27.
func (kv KubernetesVersion) StringWithDot() string {
return fmt.Sprintf("%v.%v", kv.Major, kv.Minor)
}