/
backend.go
215 lines (181 loc) · 5.62 KB
/
backend.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package k8simpl
import (
"context"
"fmt"
"github.com/traPtitech/neoshowcase/pkg/util/retry"
"strings"
"sync"
certmanagerv1 "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned"
"github.com/friendsofgo/errors"
log "github.com/sirupsen/logrus"
traefikv1alpha1 "github.com/traefik/traefik/v2/pkg/provider/kubernetes/crd/generated/clientset/versioned/typed/traefikio/v1alpha1"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/util/ds"
"github.com/traPtitech/neoshowcase/pkg/util/hash"
)
const (
// managedLabel (always "true") indicates the resource is managed by NeoShowcase.
managedLabel = "ns.trap.jp/managed"
// appIDLabel indicates the related application ID.
appIDLabel = "ns.trap.jp/app-id"
// appRestartAnnotation instructs StatefulSets to restart pods when necessary.
appRestartAnnotation = "ns.trap.jp/restarted-at"
// resourceHashAnnotation is hex-encoded 64-bit XXH3 hash of the resource before this annotation is applied.
resourceHashAnnotation = "ns.trap.jp/hash"
// fieldManager is the name of this controller.
fieldManager = "neoshowcase"
)
var _ domain.Backend = (*Backend)(nil)
type Backend struct {
restConfig *rest.Config
client *kubernetes.Clientset
traefikClient *traefikv1alpha1.TraefikV1alpha1Client
certManagerClient *certmanagerv1.Clientset
config Config
eventSubs domain.PubSub[*domain.ContainerEvent]
stopWatcher func()
reloadLock sync.Mutex
}
func NewK8SBackend(
restConfig *rest.Config,
k8sCSet *kubernetes.Clientset,
traefikClient *traefikv1alpha1.TraefikV1alpha1Client,
certManagerClient *certmanagerv1.Clientset,
config Config,
) (*Backend, error) {
err := config.Validate()
if err != nil {
return nil, err
}
b := &Backend{
restConfig: restConfig,
client: k8sCSet,
traefikClient: traefikClient,
certManagerClient: certManagerClient,
config: config,
}
return b, nil
}
func (b *Backend) Start(_ context.Context) error {
ctx, cancel := context.WithCancel(context.Background())
b.stopWatcher = cancel
go retry.Do(ctx, b.eventListener, "pod watcher")
return nil
}
func (b *Backend) eventListener(ctx context.Context) error {
podWatcher, err := b.client.CoreV1().Pods(b.config.Namespace).Watch(ctx, metav1.ListOptions{
LabelSelector: metav1.FormatLabelSelector(&metav1.LabelSelector{MatchLabels: map[string]string{
managedLabel: "true",
}}),
})
if err != nil {
return errors.Wrap(err, "failed to watch pods")
}
defer podWatcher.Stop()
for ev := range podWatcher.ResultChan() {
p, ok := ev.Object.(*apiv1.Pod)
if !ok {
log.Warnf("unexpected type: %v", ev)
continue
}
appID, ok := p.Labels[appIDLabel]
if !ok {
continue
}
b.eventSubs.Publish(&domain.ContainerEvent{ApplicationID: appID})
}
return nil
}
func (b *Backend) Dispose(_ context.Context) error {
b.stopWatcher()
return nil
}
func (b *Backend) AvailableDomains() domain.AvailableDomainSlice {
return ds.Map(b.config.Domains, (*domainConf).toDomainAD)
}
func (b *Backend) targetAuth(fqdn string) *domainAuthConf {
for _, dc := range b.config.Domains {
if dc.Auth.Available && dc.toDomainAD().Match(fqdn) {
return dc.Auth
}
}
return nil
}
func (b *Backend) AvailablePorts() domain.AvailablePortSlice {
return ds.Map(b.config.Ports, (*portConf).toDomainAP)
}
func (b *Backend) ListenContainerEvents() (sub <-chan *domain.ContainerEvent, unsub func()) {
return b.eventSubs.Subscribe()
}
// generalLabelWithoutManagement returns labels that indicates not directly managed by this backend
func (b *Backend) generalLabelWithoutManagement() map[string]string {
return b.config.labels()
}
func (b *Backend) generalLabel() map[string]string {
return ds.MergeMap(b.config.labels(), map[string]string{
managedLabel: "true",
})
}
func (b *Backend) appLabel(appID string) map[string]string {
return ds.MergeMap(b.config.labels(), map[string]string{
managedLabel: "true",
appIDLabel: appID,
})
}
func toSelectorString(matchLabels map[string]string) string {
return metav1.FormatLabelSelector(&metav1.LabelSelector{
MatchLabels: matchLabels,
})
}
func allSelector() map[string]string {
return map[string]string{
managedLabel: "true",
}
}
func appSelector(appID string) map[string]string {
return map[string]string{
appIDLabel: appID,
}
}
func deploymentName(appID string) string {
return fmt.Sprintf("nsapp-%s", appID)
}
func deploymentNameWithDiscriminator(appID string, content []byte) string {
return fmt.Sprintf("nsapp-%s-%s", appID, hash.XXH3Hex(content)[:6])
}
func generatedPodName(appID string) string {
return deploymentName(appID) + "-0"
}
const podContainerName = "app"
func serviceName(website *domain.Website) string {
return fmt.Sprintf("nsapp-%s", website.ID)
}
func portServiceName(port *domain.PortPublication) string {
return fmt.Sprintf("nsapp-port-%s-%d", port.Protocol, port.InternetPort)
}
func stripMiddlewareName(website *domain.Website) string {
return serviceName(website) + "-strip"
}
func ssHeaderMiddlewareName(ss *domain.StaticSite) string {
return fmt.Sprintf("nsapp-ss-header-%s", ss.Application.ID)
}
func certificateName(fqdn string) string {
wildcard := strings.HasPrefix(fqdn, "*.")
if wildcard {
fqdn = strings.TrimPrefix(fqdn, "*.")
}
fqdn = strings.ReplaceAll(fqdn, "-", "--")
fqdn = strings.ReplaceAll(fqdn, ".", "-")
if wildcard {
return fmt.Sprintf("nsapp-%s-wildcard", fqdn)
} else {
return fmt.Sprintf("nsapp-%s", fqdn)
}
}
func tlsSecretName(fqdn string) string {
return certificateName(fqdn) + "-tls"
}