Newer
Older
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
/*
Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.gnu.org/licenses/agpl-3.0.html
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package meta
import (
"fmt"
"k8s.io/apimachinery/pkg/labels"
)
// TODO remove meta in interface, not needed
// Instance interface can work with the commons app.kubernetes.io labels - https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/#labels
type Instance interface {
Meta
// The name of the application - app.kubernetes.io/name
GetApplication() string
SetApplication(string)
// A unique name identifying the instance of an application - app.kubernetes.io/instance
GetInstance() string
SetInstance(string)
// The current version of the application (e.g., a semantic version, revision hash, etc.) - app.kubernetes.io/version
GetVersion() string
SetVersion(string)
// The component within the architecture - app.kubernetes.io/component
GetComponent() string
SetComponent(string)
// The name of a higher level application this one is part of - app.kubernetes.io/part-of
GetPartOf() string
SetPartOf(string)
// The tool being used to manage the operation of an application - app.kubernetes.io/managed-by
GetManagedBy() string
SetManagedBy(string)
}
// Instance labels returns the app.kubernetes.io labels
func InstanceLabels(m Instance) map[string]string {
labels := labels.Set{
"app.kubernetes.io/name": m.GetApplication(),
"app.kubernetes.io/instance": m.GetInstance(),
"app.kubernetes.io/version": m.GetVersion(),
"app.kubernetes.io/component": m.GetComponent(),
"app.kubernetes.io/part-of": m.GetPartOf(),
"app.kubernetes.io/managed-by": m.GetManagedBy(),
}
return labels
}
// TODO use Object interface and rename Merge ?
func SetObjectMeta(src Instance, dest Instance) {
dest.SetLabels(labels.Merge(dest.GetLabels(), InstanceLabels(src)))
if len(dest.GetName()) == 0 {
dest.SetName(fmt.Sprintf("%v-%v", src.GetInstance(), dest.GetComponent()))
}
if len(dest.GetNamespace()) == 0 {
dest.SetNamespace(src.GetNamespace())
}
}
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
func SetObjectMetaFromInstance(src Instance, dest Instance) {
if len(dest.GetInstance()) == 0 {
dest.SetInstance(src.GetInstance())
}
if len(dest.GetApplication()) == 0 {
dest.SetApplication(src.GetApplication())
}
if len(dest.GetVersion()) == 0 {
dest.SetVersion(src.GetVersion())
}
if len(dest.GetPartOf()) == 0 {
dest.SetPartOf(src.GetPartOf())
}
if len(dest.GetManagedBy()) == 0 {
dest.SetManagedBy(src.GetManagedBy())
}
/* if len(dest.GetName()) == 0 {
dest.SetName(fmt.Sprintf("%v-%v", src.GetInstance(), dest.GetComponent()))
} */
if len(dest.GetNamespace()) == 0 {
dest.SetNamespace(src.GetNamespace())
}
}
func MergeObjectMeta(src Instance, dest Instance) {
dest.SetLabels(labels.Merge(dest.GetLabels(), InstanceLabels(src)))
if len(dest.GetName()) == 0 {
dest.SetName(fmt.Sprintf("%v-%v", src.GetInstance(), dest.GetComponent()))
}
if len(dest.GetNamespace()) == 0 {
dest.SetNamespace(src.GetNamespace())
}
}