-
Notifications
You must be signed in to change notification settings - Fork 13
/
snapshot_template.go
119 lines (104 loc) · 3.15 KB
/
snapshot_template.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
package templates
import (
"text/template"
)
var ResourceGroupSnapshotTemplate = template.Must(template.New("resource_group_snapshot").Funcs(Funcs).Parse(
`package {{ .Project.ProjectConfig.Version }}
import (
"encoding/binary"
"fmt"
"hash"
"hash/fnv"
"log"
{{ .Imports }}
"github.com/solo-io/solo-kit/pkg/api/v1/resources"
"github.com/rotisserie/eris"
"github.com/solo-io/go-utils/hashutils"
"go.uber.org/zap"
)
type {{ .GoName }}Snapshot struct {
{{- range .Resources}}
{{ upper_camel .PluralName }} {{ .ImportPrefix }}{{ .Name }}List
{{- end}}
}
func (s {{ .GoName }}Snapshot) Clone() {{ .GoName }}Snapshot {
return {{ .GoName }}Snapshot{
{{- range .Resources}}
{{ upper_camel .PluralName }}: s.{{ upper_camel .PluralName }}.Clone(),
{{- end}}
}
}
func (s {{ .GoName }}Snapshot) Hash(hasher hash.Hash64) (uint64, error) {
if hasher == nil {
hasher = fnv.New64()
}
{{- range .Resources}}
if _, err := s.hash{{ upper_camel .PluralName }}(hasher); err != nil {
return 0, err
}
{{- end}}
return hasher.Sum64(), nil
}
{{- $ResourceGroup := . }}
{{- range .Resources }}
func (s {{ $ResourceGroup.GoName }}Snapshot) hash{{ upper_camel .PluralName }}(hasher hash.Hash64) (uint64, error) {
{{- if .SkipHashingAnnotations }}
clonedList := s.{{ upper_camel .PluralName }}.Clone()
for _, v := range clonedList {
v.Metadata.Annotations = nil
}
return hashutils.HashAllSafe(hasher, clonedList.AsInterfaces()...)
{{- else }}
return hashutils.HashAllSafe(hasher, s.{{ upper_camel .PluralName }}.AsInterfaces()...)
{{- end }}
}
{{- end}}
func (s {{ .GoName }}Snapshot) HashFields() []zap.Field {
var fields []zap.Field
hasher := fnv.New64()
{{- range .Resources}}
{{ upper_camel .PluralName }}Hash, err := s.hash{{ upper_camel .PluralName }}(hasher)
if err != nil {
log.Println(eris.Wrapf(err, "error hashing, this should never happen"))
}
fields = append(fields, zap.Uint64("{{ lower_camel .PluralName }}", {{ upper_camel .PluralName }}Hash ))
{{- end}}
snapshotHash, err := s.Hash(hasher)
if err != nil {
log.Println(eris.Wrapf(err, "error hashing, this should never happen"))
}
return append(fields, zap.Uint64("snapshotHash", snapshotHash))
}
type {{ .GoName }}SnapshotStringer struct {
Version uint64
{{- range .Resources}}
{{ upper_camel .PluralName }} []string
{{- end}}
}
func (ss {{ .GoName }}SnapshotStringer) String() string {
s := fmt.Sprintf("{{ .GoName }}Snapshot %v\n", ss.Version)
{{- range .Resources}}
s += fmt.Sprintf(" {{ upper_camel .PluralName }} %v\n", len(ss.{{ upper_camel .PluralName }}))
for _, name := range ss.{{ upper_camel .PluralName }} {
s += fmt.Sprintf(" %v\n", name)
}
{{- end}}
return s
}
func (s {{ .GoName }}Snapshot) Stringer() {{ .GoName }}SnapshotStringer {
snapshotHash, err := s.Hash(nil)
if err != nil {
log.Println(eris.Wrapf(err, "error hashing, this should never happen"))
}
return {{ .GoName }}SnapshotStringer{
Version: snapshotHash,
{{- range .Resources}}
{{- if .ClusterScoped }}
{{ upper_camel .PluralName }}: s.{{ upper_camel .PluralName }}.Names(),
{{- else }}
{{ upper_camel .PluralName }}: s.{{ upper_camel .PluralName }}.NamespacesDotNames(),
{{- end }}
{{- end}}
}
}
`))