This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
/
routes.go
129 lines (111 loc) · 6.85 KB
/
routes.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
package controller
import (
"net/http"
v1 "github.com/acorn-io/acorn/pkg/apis/internal.acorn.io/v1"
"github.com/acorn-io/acorn/pkg/controller/acornimagebuildinstance"
"github.com/acorn-io/acorn/pkg/controller/appdefinition"
"github.com/acorn-io/acorn/pkg/controller/appstatus"
"github.com/acorn-io/acorn/pkg/controller/builder"
"github.com/acorn-io/acorn/pkg/controller/config"
"github.com/acorn-io/acorn/pkg/controller/defaults"
"github.com/acorn-io/acorn/pkg/controller/devsession"
"github.com/acorn-io/acorn/pkg/controller/eventinstance"
"github.com/acorn-io/acorn/pkg/controller/gc"
"github.com/acorn-io/acorn/pkg/controller/images"
"github.com/acorn-io/acorn/pkg/controller/ingress"
"github.com/acorn-io/acorn/pkg/controller/jobs"
"github.com/acorn-io/acorn/pkg/controller/namespace"
"github.com/acorn-io/acorn/pkg/controller/networkpolicy"
"github.com/acorn-io/acorn/pkg/controller/pvc"
"github.com/acorn-io/acorn/pkg/controller/quota"
"github.com/acorn-io/acorn/pkg/controller/scheduling"
"github.com/acorn-io/acorn/pkg/controller/secrets"
"github.com/acorn-io/acorn/pkg/controller/service"
"github.com/acorn-io/acorn/pkg/controller/tls"
"github.com/acorn-io/acorn/pkg/event"
"github.com/acorn-io/acorn/pkg/labels"
"github.com/acorn-io/acorn/pkg/system"
"github.com/acorn-io/acorn/pkg/volume"
"github.com/acorn-io/baaah/pkg/router"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
policyv1 "k8s.io/api/policy/v1"
rbacv1 "k8s.io/api/rbac/v1"
storagev1 "k8s.io/api/storage/v1"
klabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/rest"
)
var (
managedSelector = klabels.SelectorFromSet(map[string]string{
labels.AcornManaged: "true",
})
)
func routes(router *router.Router, cfg *rest.Config, registryTransport http.RoundTripper, recorder event.Recorder) error {
jobsHandler, err := jobs.NewHandler(cfg)
if err != nil {
return err
}
router.OnErrorHandler = appdefinition.OnError
appRouter := router.Type(&v1.AppInstance{}).Middleware(devsession.OverlayDevSession).IncludeFinalizing()
appRouter.HandlerFunc(appstatus.PrepareStatus)
appRouter.HandlerFunc(appdefinition.AssignNamespace)
appRouter.HandlerFunc(appdefinition.CheckImageAllowedHandler(registryTransport))
appRouter.HandlerFunc(appdefinition.PullAppImage(registryTransport, recorder))
appRouter.HandlerFunc(images.CreateImages)
appRouter.HandlerFunc(appdefinition.ParseAppImage)
appRouter.HandlerFunc(tls.ProvisionCerts) // Provision TLS certificates for port bindings with user-defined (valid) domains
appRouter.Middleware(appdefinition.FilterLabelsAndAnnotationsConfig).HandlerFunc(namespace.AddNamespace)
appRouter.Middleware(jobs.NeedsDestroyJobFinalization).FinalizeFunc(jobs.DestroyJobFinalizer, jobs.FinalizeDestroyJob)
// DeploySpec will create the namespace, so ensure it runs before anything that requires a namespace
appHasNamespace := appRouter.Middleware(appdefinition.RequireNamespace, appdefinition.IgnoreTerminatingNamespace, appdefinition.FilterLabelsAndAnnotationsConfig)
appHasNamespace.HandlerFunc(defaults.Calculate)
appHasNamespace.HandlerFunc(scheduling.Calculate)
appHasNamespace.HandlerFunc(quota.EnsureQuotaRequest)
appHasNamespace.HandlerFunc(quota.WaitForAllocation)
appMeetsPreconditions := appHasNamespace.Middleware(appstatus.CheckStatus)
appMeetsPreconditions.Middleware(appdefinition.ImagePulled).HandlerFunc(appdefinition.DeploySpec)
appMeetsPreconditions.Middleware(appdefinition.ImagePulled).HandlerFunc(secrets.CreateSecrets)
appMeetsPreconditions.HandlerFunc(appstatus.SetStatus)
appMeetsPreconditions.HandlerFunc(appstatus.ReadyStatus)
appMeetsPreconditions.HandlerFunc(networkpolicy.ForApp)
appMeetsPreconditions.HandlerFunc(appdefinition.AddAcornProjectLabel)
appMeetsPreconditions.HandlerFunc(appdefinition.UpdateObservedFields)
appRouter.HandlerFunc(appstatus.CLIStatus)
router.Type(&v1.DevSessionInstance{}).HandlerFunc(devsession.ExpireDevSession)
router.Type(&v1.ServiceInstance{}).HandlerFunc(service.RenderServices)
router.Type(&v1.BuilderInstance{}).HandlerFunc(builder.SetRegion)
router.Type(&v1.BuilderInstance{}).HandlerFunc(builder.DeployBuilder)
router.Type(&v1.AcornImageBuildInstance{}).HandlerFunc(acornimagebuildinstance.SetRegion)
router.Type(&v1.AcornImageBuildInstance{}).HandlerFunc(acornimagebuildinstance.MarkRecorded)
router.Type(&v1.ServiceInstance{}).HandlerFunc(gc.GCOrphans)
router.Type(&v1.EventInstance{}).HandlerFunc(eventinstance.GCExpired())
router.Type(&batchv1.Job{}).Selector(managedSelector).HandlerFunc(jobs.JobCleanup)
router.Type(&rbacv1.ClusterRole{}).Selector(managedSelector).HandlerFunc(gc.GCOrphans)
router.Type(&rbacv1.ClusterRoleBinding{}).Selector(managedSelector).HandlerFunc(gc.GCOrphans)
router.Type(&corev1.PersistentVolumeClaim{}).Selector(managedSelector).HandlerFunc(pvc.MarkAndSave)
router.Type(&corev1.PersistentVolume{}).Selector(managedSelector).HandlerFunc(appdefinition.ReleaseVolume)
router.Type(&corev1.Namespace{}).Selector(managedSelector).HandlerFunc(namespace.DeleteOrphaned)
router.Type(&appsv1.DaemonSet{}).Namespace(system.ImagesNamespace).HandlerFunc(gc.GCOrphans)
router.Type(&appsv1.Deployment{}).Namespace(system.ImagesNamespace).HandlerFunc(gc.GCOrphans)
router.Type(&corev1.Service{}).Selector(managedSelector).HandlerFunc(gc.GCOrphans)
router.Type(&policyv1.PodDisruptionBudget{}).Namespace(system.ImagesNamespace).HandlerFunc(gc.GCOrphans)
router.Type(&corev1.Pod{}).Selector(managedSelector).HandlerFunc(gc.GCOrphans)
router.Type(&corev1.Pod{}).Selector(managedSelector).HandlerFunc(jobs.JobPodOrphanCleanup)
router.Type(&corev1.Pod{}).Selector(managedSelector).HandlerFunc(jobsHandler.SaveJobOutput)
router.Type(&netv1.Ingress{}).Selector(managedSelector).Namespace(system.ImagesNamespace).HandlerFunc(gc.GCOrphans)
router.Type(&netv1.Ingress{}).Selector(managedSelector).Middleware(ingress.RequireLBs).Handler(ingress.NewDNSHandler())
router.Type(&corev1.Secret{}).Selector(managedSelector).Middleware(tls.RequireSecretTypeTLS).HandlerFunc(tls.RenewCert) // renew (expired) TLS certificates, including the oss-acorn.io wildcard cert
router.Type(&storagev1.StorageClass{}).HandlerFunc(volume.SyncVolumeClasses)
router.Type(&corev1.Service{}).Selector(managedSelector).HandlerFunc(networkpolicy.ForService)
router.Type(&netv1.Ingress{}).Selector(managedSelector).HandlerFunc(networkpolicy.ForIngress)
router.Type(&appsv1.Deployment{}).Namespace(system.ImagesNamespace).HandlerFunc(networkpolicy.ForBuilder)
router.Type(&netv1.NetworkPolicy{}).Selector(managedSelector).HandlerFunc(gc.GCOrphans)
configRouter := router.Type(&corev1.ConfigMap{}).Namespace(system.Namespace).Name(system.ConfigName)
configRouter.Handler(config.NewDNSConfigHandler())
configRouter.HandlerFunc(builder.DeployRegistry)
configRouter.HandlerFunc(config.HandleAutoUpgradeInterval)
configRouter.HandlerFunc(volume.CreateEphemeralVolumeClass)
return nil
}