-
Notifications
You must be signed in to change notification settings - Fork 24
/
rpaasinstance.go
125 lines (101 loc) · 3.55 KB
/
rpaasinstance.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
// Copyright 2020 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package v1alpha1
import (
"fmt"
"sort"
)
const (
DefaultLabelKeyPrefix = "rpaas.extensions.tsuru.io"
RpaasOperatorInstanceNameLabelKey = DefaultLabelKeyPrefix + "/instance-name"
RpaasOperatorServiceNameLabelKey = DefaultLabelKeyPrefix + "/service-name"
RpaasOperatorPlanNameLabelKey = DefaultLabelKeyPrefix + "/plan-name"
RpaasOperatorTeamOwnerLabelKey = DefaultLabelKeyPrefix + "/team-owner"
RpaasOperatorClusterNameLabelKey = DefaultLabelKeyPrefix + "/cluster-name"
LegacyRpaasOperatorInstanceNameLabelKey = "rpaas_instance"
LegacyRpaasOperatorServiceNameLabelKey = "rpaas_service"
)
func (i *RpaasInstance) GetBaseLabels(labels map[string]string) map[string]string {
return mergeMap(map[string]string{
LegacyRpaasOperatorInstanceNameLabelKey: i.Name,
LegacyRpaasOperatorServiceNameLabelKey: i.Labels[RpaasOperatorServiceNameLabelKey],
RpaasOperatorInstanceNameLabelKey: i.Name,
RpaasOperatorServiceNameLabelKey: i.Labels[RpaasOperatorServiceNameLabelKey],
RpaasOperatorPlanNameLabelKey: i.Spec.PlanName,
RpaasOperatorTeamOwnerLabelKey: i.Labels[RpaasOperatorTeamOwnerLabelKey],
}, labels)
}
func (i *RpaasInstance) TeamOwner() string {
return i.Labels[RpaasOperatorTeamOwnerLabelKey]
}
func (i *RpaasInstance) ClusterName() string {
return i.Labels[RpaasOperatorClusterNameLabelKey]
}
func (i *RpaasInstance) SetTeamOwner(team string) {
newLabels := map[string]string{RpaasOperatorTeamOwnerLabelKey: team}
i.appendNewLabels(newLabels)
}
func (i *RpaasInstance) SetClusterName(clusterName string) {
newLabels := map[string]string{RpaasOperatorClusterNameLabelKey: clusterName}
i.appendNewLabels(newLabels)
}
func (i *RpaasInstance) BelongsToCluster(clusterName string) bool {
instanceCluster := i.Labels[RpaasOperatorClusterNameLabelKey]
if instanceCluster == "" {
return false
}
return clusterName == instanceCluster
}
func (i *RpaasInstance) CertManagerRequests() (reqs []CertManager) {
if i == nil || i.Spec.DynamicCertificates == nil {
return
}
uniqueCerts := make(map[string]*CertManager)
if req := i.Spec.DynamicCertificates.CertManager; req != nil {
r := req.DeepCopy()
r.DNSNames = r.dnsNames(i)
uniqueCerts[r.Issuer] = r
}
for _, req := range i.Spec.DynamicCertificates.CertManagerRequests {
r, found := uniqueCerts[req.Issuer]
if !found {
uniqueCerts[req.Issuer] = req.DeepCopy()
continue
}
r.DNSNames = append(r.DNSNames, req.dnsNames(i)...)
r.IPAddresses = append(r.IPAddresses, req.IPAddresses...)
}
for _, v := range uniqueCerts {
reqs = append(reqs, *v)
}
sort.Slice(reqs, func(i, j int) bool { return reqs[i].Issuer < reqs[j].Issuer })
return
}
func (c *CertManager) dnsNames(i *RpaasInstance) (names []string) {
if c == nil {
return
}
names = append(names, c.DNSNames...)
if c.DNSNamesDefault && i.Spec.DNS != nil && i.Spec.DNS.Zone != "" {
names = append(names, fmt.Sprintf("%s.%s", i.Name, i.Spec.DNS.Zone))
}
return
}
func (i *RpaasInstance) appendNewLabels(newLabels map[string]string) {
i.Labels = mergeMap(i.Labels, newLabels)
i.Annotations = mergeMap(i.Annotations, newLabels)
i.Spec.PodTemplate.Labels = mergeMap(i.Spec.PodTemplate.Labels, newLabels)
if i.Spec.Service != nil {
i.Spec.Service.Labels = mergeMap(i.Spec.Service.Labels, newLabels)
}
}
func mergeMap(a, b map[string]string) map[string]string {
if a == nil {
return b
}
for k, v := range b {
a[k] = v
}
return a
}