This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
forked from atlassian/smith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
197 lines (174 loc) · 7 KB
/
app.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
package app
import (
"context"
"errors"
"log"
"time"
"github.com/atlassian/smith"
smith_v1 "github.com/atlassian/smith/pkg/apis/smith/v1"
"github.com/atlassian/smith/pkg/cleanup"
clean_types "github.com/atlassian/smith/pkg/cleanup/types"
"github.com/atlassian/smith/pkg/client"
smithClientset "github.com/atlassian/smith/pkg/client/clientset_generated/clientset"
smithClient_v1 "github.com/atlassian/smith/pkg/client/clientset_generated/clientset/typed/smith/v1"
"github.com/atlassian/smith/pkg/client/smart"
"github.com/atlassian/smith/pkg/controller"
"github.com/atlassian/smith/pkg/readychecker"
ready_types "github.com/atlassian/smith/pkg/readychecker/types"
"github.com/atlassian/smith/pkg/resources"
"github.com/atlassian/smith/pkg/speccheck"
"github.com/atlassian/smith/pkg/store"
"github.com/ash2k/stager"
sc_v1a1 "github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1alpha1"
scClientset "github.com/kubernetes-incubator/service-catalog/pkg/client/clientset_generated/clientset"
crdClientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
crdInformers "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
api_v1 "k8s.io/client-go/pkg/api/v1"
apps_v1b1 "k8s.io/client-go/pkg/apis/apps/v1beta1"
ext_v1b1 "k8s.io/client-go/pkg/apis/extensions/v1beta1"
settings_v1a1 "k8s.io/client-go/pkg/apis/settings/v1alpha1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
var (
crdCreationBackoff = wait.Backoff{
Duration: 1 * time.Second,
Factor: 1.2,
Jitter: 0.1,
Steps: 7,
}
)
type App struct {
RestConfig *rest.Config
ServiceCatalogConfig *rest.Config
ResyncPeriod time.Duration
Namespace string
DisablePodPreset bool
Workers int
}
func (a *App) Run(ctx context.Context) error {
// Clients
clientset, err := kubernetes.NewForConfig(a.RestConfig)
if err != nil {
return err
}
bundleClient, err := smithClientset.NewForConfig(a.RestConfig)
if err != nil {
return err
}
var scClient scClientset.Interface
if a.ServiceCatalogConfig != nil {
scClient, err = scClientset.NewForConfig(a.ServiceCatalogConfig)
if err != nil {
return err
}
}
crdClient, err := crdClientset.NewForConfig(a.RestConfig)
if err != nil {
return err
}
sc := smart.NewClient(a.RestConfig, a.ServiceCatalogConfig, clientset, scClient)
scheme, err := FullScheme(a.ServiceCatalogConfig != nil)
if err != nil {
return err
}
// Stager will perform ordered, graceful shutdown
stgr := stager.New()
defer stgr.Shutdown()
// Multi store
multiStore := store.NewMulti(scheme.DeepCopy)
// Informers
bundleInf := client.BundleInformer(bundleClient.SmithV1(), a.Namespace, a.ResyncPeriod)
multiStore.AddInformer(smith_v1.BundleGVK, bundleInf)
informerFactory := crdInformers.NewSharedInformerFactory(crdClient, a.ResyncPeriod)
crdInf := informerFactory.Apiextensions().V1beta1().CustomResourceDefinitions().Informer()
crdStore, err := store.NewCrd(crdInf, scheme.DeepCopy)
if err != nil {
return err
}
multiStore.AddInformer(crdGVK, crdInf)
stage := stgr.NextStage()
stage.StartWithChannel(crdInf.Run) // Must be after store.AddInformer()
// We must wait for crdInf to populate its cache to avoid reading from an empty cache
// in Ready Checker and in EnsureCrdExists().
if !cache.WaitForCacheSync(ctx.Done(), crdInf.HasSynced) {
return errors.New("wait for CRD Informer was cancelled")
}
// Ensure CRD Bundle exists
crdLister := informerFactory.Apiextensions().V1beta1().CustomResourceDefinitions().Lister()
err = wait.ExponentialBackoff(crdCreationBackoff, func() (bool /*done*/, error) {
if err := resources.EnsureCrdExistsAndIsEstablished(ctx, scheme, crdClient, crdLister, resources.BundleCrd()); err != nil {
// TODO be smarter about what is retried
if err == context.Canceled || err == context.DeadlineExceeded {
return true, err
}
log.Printf("Failed to create CRD %s: %v", smith_v1.BundleResourceName, err)
return false, nil
}
return true, nil
})
if err != nil {
return err
}
// Controller
bs, err := store.NewBundle(bundleInf, multiStore, scheme.DeepCopy)
if err != nil {
return err
}
resourceInfs := a.resourceInformers(clientset, scClient)
cntrlr := a.controller(bundleInf, crdInf, bundleClient, bs, crdStore, sc, scheme, multiStore, resourceInfs)
// Add all informers to Multi store and start them
for gvk, inf := range resourceInfs {
multiStore.AddInformer(gvk, inf)
stage.StartWithChannel(inf.Run) // Must be after AddInformer()
}
stage.StartWithChannel(bundleInf.Run)
cntrlr.Run(ctx)
return ctx.Err()
}
func (a *App) controller(bundleInf, crdInf cache.SharedIndexInformer, bundleClient smithClient_v1.BundlesGetter, bundleStore controller.BundleStore, crdStore readychecker.CrdStore,
sc smith.SmartClient, scheme *runtime.Scheme, cStore controller.Store, resourceInfs map[schema.GroupVersionKind]cache.SharedIndexInformer) *controller.BundleController {
// Ready Checker
readyTypes := []map[schema.GroupKind]readychecker.IsObjectReady{ready_types.MainKnownTypes}
if a.ServiceCatalogConfig != nil {
readyTypes = append(readyTypes, ready_types.ServiceCatalogKnownTypes)
}
rc := readychecker.New(crdStore, readyTypes...)
// Object cleanup
cleanupTypes := []map[schema.GroupKind]cleanup.SpecCleanup{clean_types.MainKnownTypes}
if a.ServiceCatalogConfig != nil {
cleanupTypes = append(cleanupTypes, clean_types.ServiceCatalogKnownTypes)
}
oc := cleanup.New(cleanupTypes...)
// Spec check
specCheck := &speccheck.SpecCheck{
Scheme: scheme,
Cleaner: oc,
}
queue := workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "bundle")
return controller.New(bundleInf, crdInf, bundleClient, bundleStore, sc, rc, scheme, cStore, specCheck, queue, a.Workers, a.ResyncPeriod, resourceInfs)
}
func (a *App) resourceInformers(mainClient kubernetes.Interface, scClient scClientset.Interface) map[schema.GroupVersionKind]cache.SharedIndexInformer {
// Core API types
infs := map[schema.GroupVersionKind]cache.SharedIndexInformer{
ext_v1b1.SchemeGroupVersion.WithKind("Ingress"): a.ingressInformer(mainClient),
api_v1.SchemeGroupVersion.WithKind("Service"): a.serviceInformer(mainClient),
api_v1.SchemeGroupVersion.WithKind("ConfigMap"): a.configMapInformer(mainClient),
api_v1.SchemeGroupVersion.WithKind("Secret"): a.secretInformer(mainClient),
apps_v1b1.SchemeGroupVersion.WithKind("Deployment"): a.deploymentAppsInformer(mainClient),
}
if !a.DisablePodPreset {
infs[settings_v1a1.SchemeGroupVersion.WithKind("PodPreset")] = a.podPresetInformer(mainClient)
}
// Service Catalog types
if scClient != nil {
infs[sc_v1a1.SchemeGroupVersion.WithKind("ServiceInstanceCredential")] = a.serviceInstanceCredentialInformer(scClient)
infs[sc_v1a1.SchemeGroupVersion.WithKind("ServiceInstance")] = a.serviceInstanceCredentialInformer(scClient)
}
return infs
}