forked from weaveworks/weave-gitops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
168 lines (134 loc) · 3.49 KB
/
common.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
160
161
162
163
164
165
166
167
168
package utils
import (
"bytes"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"time"
"github.com/tomwright/dasel"
yaml "gopkg.in/yaml.v3"
validation "k8s.io/apimachinery/pkg/api/validation"
)
// WaitUntil runs checkDone until an error is NOT returned, or a timeout is reached.
// To continue polling, return an error.
func WaitUntil(out io.Writer, poll, timeout time.Duration, checkDone func() error) error {
_, err := timedRepeat(
out,
time.Now(),
poll,
timeout,
func(currentTime time.Time) time.Time {
time.Sleep(poll)
return time.Now()
},
checkDone)
return err
}
// timedRepeat runs checkDone until a timeout is reached by updating the current time via a specified operation
func timedRepeat(out io.Writer, start time.Time, poll, timeout time.Duration, updater func(currentTime time.Time) time.Time, checkDone func() error) (time.Time, error) {
currentTime := start
endTime := currentTime.Add(timeout)
for ; currentTime.Before(endTime); currentTime = updater(currentTime) {
err := checkDone()
if err == nil {
return currentTime, nil
}
fmt.Fprintf(out, "error occurred %s, retrying in %s\n", err, poll.String())
}
return currentTime, fmt.Errorf("timeout reached %s", timeout.String())
}
func URLToRepoName(url string) string {
return strings.TrimSuffix(filepath.Base(url), ".git")
}
func ValidateNamespace(ns string) error {
if errList := validation.ValidateNamespaceName(ns, false); len(errList) != 0 {
return fmt.Errorf("invalid namespace: %s", strings.Join(errList, ", "))
}
return nil
}
const (
coreManifestCount = 2
coreManifestName = "ww-gitops"
)
type ConfigStatus int
const (
Missing ConfigStatus = iota
Partial
Embedded
Valid
)
func (cs ConfigStatus) String() string {
switch cs {
case Missing:
return "Missing"
case Partial:
return "Partial"
case Embedded:
return "Embedded"
case Valid:
return "Valid"
default:
return "UnknownStatus"
}
}
type WalkResult struct {
Status ConfigStatus
Path string
}
func (wr WalkResult) Error() string {
return fmt.Sprintf("found %s: with status: %s", wr.Path, wr.Status)
}
func FindCoreConfig(dir string) WalkResult {
err := filepath.WalkDir(dir,
func(path string, _ fs.DirEntry, err error) error {
if err != nil {
return nil
}
if !strings.HasSuffix(path, ".yml") && !strings.HasSuffix(path, ".yaml") {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
return nil
}
r := bytes.NewReader(data)
decoder := yaml.NewDecoder(r)
docs := []map[string]interface{}{}
for {
var entry map[string]interface{}
if err := decoder.Decode(&entry); err == io.EOF {
break
}
docs = append(docs, entry)
}
rootNode := dasel.New(docs)
foundPartial := false
_, err = rootNode.QueryMultiple(fmt.Sprintf(".(kind=HelmRelease)(.metadata.name=%s)", coreManifestName))
if err == nil {
foundPartial = true
}
_, err = rootNode.QueryMultiple(fmt.Sprintf(".(kind=HelmRepository)(.metadata.name=%s)", coreManifestName))
if err != nil {
if foundPartial {
return WalkResult{Status: Partial, Path: path}
}
return nil
}
// retrieve the number of top-level entries from the file
val, err := rootNode.Query(".[#]")
if err != nil {
return nil
}
if val.InterfaceValue() != coreManifestCount {
return WalkResult{Status: Embedded, Path: path}
}
return WalkResult{Status: Valid, Path: path}
})
if val, ok := err.(WalkResult); ok {
return val
}
return WalkResult{Status: Missing, Path: ""}
}