generated from giantswarm/template-operator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
annotation.go
117 lines (84 loc) · 2.71 KB
/
annotation.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
package meta
import (
"encoding/json"
"os"
"os/user"
"github.com/giantswarm/k8smetadata/pkg/annotation"
"github.com/giantswarm/microerror"
"github.com/giantswarm/config-controller/api/v1alpha1"
"github.com/giantswarm/config-controller/pkg/project"
)
var (
configVersionAnnotation = annotation.ConfigVersion
xAppInfoAnnotation = project.Name() + ".x-giantswarm.io/app-info"
xCreatorAnnotation = project.Name() + ".x-giantswarm.io/creator"
xInstallationAnnotation = project.Name() + ".x-giantswarm.io/installation"
xObjectHashAnnotation = project.Name() + ".x-giantswarm.io/object-hash"
xPreviousConfigAnnotation = project.Name() + ".x-giantswarm.io/previous-config"
xProjectVersionAnnotation = project.Name() + ".x-giantswarm.io/project-version"
)
type ConfigVersion struct{}
func (ConfigVersion) Key() string { return configVersionAnnotation }
type XPreviousConfig struct{}
func (XPreviousConfig) Key() string { return xPreviousConfigAnnotation }
func (XPreviousConfig) Get(o Object) (v1alpha1.ConfigStatusConfig, error) {
a := o.GetAnnotations()
if a == nil {
return v1alpha1.ConfigStatusConfig{}, nil
}
raw, ok := a[xPreviousConfigAnnotation]
if !ok {
return v1alpha1.ConfigStatusConfig{}, nil
}
var c v1alpha1.ConfigStatusConfig
err := json.Unmarshal([]byte(raw), &c)
if err != nil {
return v1alpha1.ConfigStatusConfig{}, microerror.Mask(err)
}
return c, nil
}
func (XPreviousConfig) Set(o Object, c v1alpha1.ConfigStatusConfig) error {
bs, err := json.Marshal(c)
if err != nil {
return microerror.Mask(err)
}
a := o.GetAnnotations()
if a == nil {
a = make(map[string]string, 1)
}
a[xPreviousConfigAnnotation] = string(bs)
o.SetAnnotations(a)
return nil
}
type XAppInfo struct{}
func (XAppInfo) Key() string { return xAppInfoAnnotation }
func (XAppInfo) Val(catalog, app, version string) string {
return catalog + "/" + app + "@" + version
}
func (XAppInfo) ValFromConfig(c *v1alpha1.Config) string {
return XAppInfo{}.Val(c.Spec.App.Catalog, c.Spec.App.Name, c.Spec.App.Version)
}
type XCreator struct{}
func (XCreator) Key() string { return xCreatorAnnotation }
func (XCreator) Default() string {
u, err := user.Current()
if err != nil {
return u.Username
}
if os.Getenv("USER") != "" {
return os.Getenv("USER")
}
return os.Getenv("USERNAME") // Windows
}
type XInstallation struct{}
func (XInstallation) Key() string { return xInstallationAnnotation }
type XObjectHash struct{}
func (XObjectHash) Key() string { return xObjectHashAnnotation }
type XProjectVersion struct{}
func (XProjectVersion) Key() string { return xProjectVersionAnnotation }
func (XProjectVersion) Val(unique bool) string {
if unique {
return "0.0.0"
}
return project.Version()
}