-
Notifications
You must be signed in to change notification settings - Fork 492
/
annotations.go
162 lines (140 loc) · 4.79 KB
/
annotations.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
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package utils
import (
"crypto/rand"
"fmt"
"io"
"strings"
"github.com/juju/errors"
"github.com/juju/juju/caas/kubernetes/provider/constants"
"github.com/juju/juju/core/annotations"
"github.com/juju/juju/environs/tags"
)
type AnnotationKeySupplier func() string
// AnnotationsForStorage provides the annotations that should be placed on a
// storage object. The annotations returned by this function are storage
// specific only and should be combined with other annotations where
// appropriate.
func AnnotationsForStorage(name string, legacy bool) annotations.Annotation {
return annotations.Annotation{
AnnotationJujuStorageKey(legacy): name,
}
}
// AnnotationJujuStorageKey returns the key used in annotations
// to describe the storage UUID.
func AnnotationJujuStorageKey(legacy bool) string {
if legacy {
return "juju-storage"
}
return annotationKey("storage", "name", false)
}
// AnnotationsForVersion provides the annotations that should be placed on an
// object that requires juju version information. The annotations returned by
// this function are version specific and may need to be combined with other
// annotations for a complete set.
func AnnotationsForVersion(vers string, legacy bool) annotations.Annotation {
return annotations.Annotation{
AnnotationVersionKey(legacy): vers,
}
}
// AnnotationVersionKey returns the key used in annotations to describe the
// Juju version. Legacy controls if the key returns is a legacy annotation key
// or newer style.
func AnnotationVersionKey(legacy bool) string {
if legacy {
return "juju-version"
}
return annotationKey("", "version", false)
}
// MakeK8sDomain builds and returns a Kubernetes resource domain for the
// provided components. Func is idempotent
func MakeK8sDomain(components ...string) string {
var parts []string
for _, v := range components {
if v != "" {
parts = append(parts, v)
}
}
return strings.Join(append(parts, constants.Domain), ".")
}
func annotationKey(name, suffix string, legacy bool) string {
if legacy {
return constants.LegacyDomain + "/" + name
}
return MakeK8sDomain(name) + "/" + suffix
}
// AnnotationModelUUIDKey returns the key used in annotations
// to describe the model UUID.
func AnnotationModelUUIDKey(legacy bool) string {
if legacy {
return "juju-model"
}
return annotationKey("model", "id", legacy)
}
// AnnotationControllerUUIDKey returns the key used in annotations
// to describe the controller UUID.
func AnnotationControllerUUIDKey(legacy bool) string {
return annotationKey("controller", "id", legacy)
}
// AnnotationControllerIsControllerKey returns the key used in annotations
// to describe if this pod is a controller pod.
func AnnotationControllerIsControllerKey(legacy bool) string {
if legacy {
return annotationKey("is-controller", "", true)
}
return annotationKey("controller", "is-controller", false)
}
// AnnotationUnitKey returns the key used in annotations
// to describe the Juju unit.
func AnnotationUnitKey(legacy bool) string {
return annotationKey("unit", "id", legacy)
}
// AnnotationCharmModifiedVersionKey returns the key used in annotations
// to describe the charm modified version.
func AnnotationCharmModifiedVersionKey(legacy bool) string {
if legacy {
return annotationKey("charm-modified-version", "", true)
}
return annotationKey("charm", "modified-version", false)
}
// AnnotationDisableNameKey returns the key used in annotations
// to describe the disabled name prefix.
func AnnotationDisableNameKey(legacy bool) string {
if legacy {
return annotationKey("disable-name-prefix", "", true)
}
return annotationKey("model", "disable-prefix", false)
}
// AnnotationKeyApplicationUUID is the key of annotation for recording pvc unique ID.
func AnnotationKeyApplicationUUID(legacy bool) string {
if legacy {
return "juju-app-uuid"
}
return MakeK8sDomain("app") + "/uuid"
}
// ResourceTagsToAnnotations creates annotations from the resource tags.
func ResourceTagsToAnnotations(in map[string]string, legacy bool) annotations.Annotation {
tagsAnnotationsMap := map[string]string{
tags.JujuController: AnnotationControllerUUIDKey(legacy),
tags.JujuModel: AnnotationModelUUIDKey(legacy),
}
out := annotations.New(nil)
for k, v := range in {
if annotationKey, ok := tagsAnnotationsMap[k]; ok {
k = annotationKey
}
out.Add(k, v)
}
return out
}
// RandomPrefixFunc defines a function used to generate a random hex string.
type RandomPrefixFunc func() (string, error)
// RandomPrefix returns a random string for storage related annotations.
func RandomPrefix() (string, error) {
var randPrefixBytes [4]byte
if _, err := io.ReadFull(rand.Reader, randPrefixBytes[0:4]); err != nil {
return "", errors.Trace(err)
}
return fmt.Sprintf("%x", randPrefixBytes), nil
}