-
Notifications
You must be signed in to change notification settings - Fork 88
/
kustomize.go
159 lines (130 loc) · 3.92 KB
/
kustomize.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package binaries
import (
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"github.com/blang/semver"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/logger"
)
var (
knownKustomizeVersions kustomizeVersions
)
// InitKustomize will discover kustomize versions from the environment and populate a list of known
// kustomize versions for later use.
func InitKustomize() (err error) {
knownKustomizeVersions, err = discoverKustomizeVersions(os.DirFS("/"))
if err != nil {
return errors.Wrap(err, "discover kustomize versions")
}
logger.Infof("Found kustomize binary versions %s", knownKustomizeVersions)
return nil
}
// GetKustomizePathForVersion gets the path to a known kustomize version that matches the provided
// semver or semver range.
func GetKustomizePathForVersion(userString string) (string, error) {
for _, knownVersion := range knownKustomizeVersions {
if knownVersion.Match(userString) {
return knownVersion.Path, nil
}
}
return "", errors.New("kustomize binary not found")
}
type kustomizeVersions []kustomizeFuzzyVersion
func (a kustomizeVersions) Len() int {
return len(a)
}
func (a kustomizeVersions) Less(i, j int) bool {
return a[i].Version.GT(a[j].Version) // sort in descending order
}
func (a kustomizeVersions) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
type kustomizeFuzzyVersion struct {
Version semver.Version
Path string
}
func newKustomizeFuzzyVersion(major uint64, path string) kustomizeFuzzyVersion {
return kustomizeFuzzyVersion{
Version: semver.Version{
Major: major,
Minor: 0,
Patch: 0,
},
Path: path,
}
}
func (v kustomizeFuzzyVersion) String() string {
if v.Version.Equals(semver.Version{}) {
return ""
}
return fmt.Sprintf("%d", v.Version.Major)
}
func (v kustomizeFuzzyVersion) Match(userString string) bool {
if v.Version.Equals(semver.Version{}) {
return true // catch all
}
if userString == "" || userString == "latest" {
return false
}
if userString == v.String() {
return true
}
// ignore error here as this could be a semver range
if exactVer, err := semver.Parse(userString); err == nil {
// fuzzy match major, not minor and patch
return exactVer.Major == v.Version.Major
}
rangeVer, err := semver.ParseRange(userString)
if err != nil {
return false
}
return rangeVer(v.Version)
}
func discoverKustomizeVersions(fileSystem fs.FS) ([]kustomizeFuzzyVersion, error) {
if binDirPath := os.Getenv("KOTS_KUSTOMIZE_BIN_DIR"); binDirPath != "" {
versions, err := discoverKustomizeVersionsFromDir(fileSystem, binDirPath)
return versions, errors.Wrap(err, "discover kustomize versions from dir")
}
versions, err := discoverKustomizeVersionsFromPath(fileSystem)
return versions, errors.Wrap(err, "discover kustomize versions from path")
}
var kustomizeVerRegexp = regexp.MustCompile(`^kustomize(\d+)?$`)
func discoverKustomizeVersionsFromDir(fileSystem fs.FS, dirPath string) ([]kustomizeFuzzyVersion, error) {
versions := []kustomizeFuzzyVersion{}
entries, err := fs.ReadDir(fileSystem, strings.TrimLeft(dirPath, "/"))
if err != nil {
return versions, errors.Wrap(err, "read dir")
}
for _, entry := range entries {
matches := kustomizeVerRegexp.FindStringSubmatch(entry.Name())
if len(matches) > 0 {
var major uint64
// default version has no version suffix
if matches[1] != "" {
var err error
major, err = strconv.ParseUint(matches[1], 10, 64)
if err != nil {
continue
}
}
versions = append(versions, newKustomizeFuzzyVersion(major, filepath.Join(dirPath, entry.Name())))
}
}
sort.Sort(kustomizeVersions(versions))
return versions, nil
}
func discoverKustomizeVersionsFromPath(fileSystem fs.FS) ([]kustomizeFuzzyVersion, error) {
// NOTE: exec.LookPath does not yet support io/fs
binPath, err := exec.LookPath("kustomize")
if err != nil {
return nil, errors.Wrap(err, "look path")
}
return []kustomizeFuzzyVersion{newKustomizeFuzzyVersion(0, binPath)}, nil
}