-
Notifications
You must be signed in to change notification settings - Fork 164
/
builder.go
279 lines (238 loc) · 8.79 KB
/
builder.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package builder
import (
"context"
corev1 "k8s.io/api/core/v1"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"knative.dev/pkg/logging/logkey"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/equality"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/cache"
"knative.dev/pkg/controller"
buildapi "github.com/pivotal/kpack/pkg/apis/build/v1alpha2"
corev1alpha1 "github.com/pivotal/kpack/pkg/apis/core/v1alpha1"
"github.com/pivotal/kpack/pkg/client/clientset/versioned"
buildinformers "github.com/pivotal/kpack/pkg/client/informers/externalversions/build/v1alpha2"
buildlisters "github.com/pivotal/kpack/pkg/client/listers/build/v1alpha2"
"github.com/pivotal/kpack/pkg/cnb"
"github.com/pivotal/kpack/pkg/reconciler"
"github.com/pivotal/kpack/pkg/registry"
"github.com/pivotal/kpack/pkg/tracker"
)
const (
ReconcilerName = "Builders"
)
type BuilderCreator interface {
CreateBuilder(ctx context.Context, builderKeychain authn.Keychain, keychain authn.Keychain, fetcher cnb.RemoteBuildpackFetcher, clusterStack *buildapi.ClusterStack, spec buildapi.BuilderSpec, serviceAccountSecrets []*corev1.Secret) (buildapi.BuilderRecord, error)
}
type Fetcher interface {
SecretsForServiceAccount(context.Context, string, string) ([]*corev1.Secret, error)
}
func NewController(
ctx context.Context,
opt reconciler.Options,
builderInformer buildinformers.BuilderInformer,
builderCreator BuilderCreator,
keychainFactory registry.KeychainFactory,
clusterStoreInformer buildinformers.ClusterStoreInformer,
buildpackInformer buildinformers.BuildpackInformer,
clusterBuildpackInformer buildinformers.ClusterBuildpackInformer,
clusterStackInformer buildinformers.ClusterStackInformer,
extensionInformer buildinformers.ExtensionInformer,
clusterExtensionInformer buildinformers.ClusterExtensionInformer,
secretFetcher Fetcher,
) (*controller.Impl, func()) {
c := &Reconciler{
Client: opt.Client,
BuilderLister: builderInformer.Lister(),
BuilderCreator: builderCreator,
KeychainFactory: keychainFactory,
ClusterStoreLister: clusterStoreInformer.Lister(),
BuildpackLister: buildpackInformer.Lister(),
ClusterBuildpackLister: clusterBuildpackInformer.Lister(),
ClusterStackLister: clusterStackInformer.Lister(),
ExtensionLister: extensionInformer.Lister(),
ClusterExtensionLister: clusterExtensionInformer.Lister(),
SecretFetcher: secretFetcher,
}
logger := opt.Logger.With(
zap.String(logkey.Kind, buildapi.BuilderCRName),
)
impl := controller.NewContext(
ctx,
&reconciler.NetworkErrorReconciler{
Reconciler: c,
},
controller.ControllerOptions{WorkQueueName: ReconcilerName, Logger: logger},
)
builderInformer.Informer().AddEventHandler(controller.HandleAll(impl.Enqueue))
c.Tracker = tracker.New(impl.EnqueueKey, opt.TrackerResyncPeriod())
clusterStoreInformer.Informer().AddEventHandler(controller.HandleAll(
controller.EnsureTypeMeta(
c.Tracker.OnChanged,
buildapi.SchemeGroupVersion.WithKind(buildapi.ClusterStoreKind)),
))
clusterStackInformer.Informer().AddEventHandler(controller.HandleAll(
controller.EnsureTypeMeta(
c.Tracker.OnChanged,
buildapi.SchemeGroupVersion.WithKind(buildapi.ClusterStackKind)),
))
buildpackInformer.Informer().AddEventHandler(controller.HandleAll(
controller.EnsureTypeMeta(
c.Tracker.OnChanged,
buildapi.SchemeGroupVersion.WithKind(buildapi.BuildpackKind)),
))
clusterBuildpackInformer.Informer().AddEventHandler(controller.HandleAll(
controller.EnsureTypeMeta(
c.Tracker.OnChanged,
buildapi.SchemeGroupVersion.WithKind(buildapi.ClusterBuildpackKind)),
))
return impl, func() {
impl.GlobalResync(builderInformer.Informer())
}
}
type Reconciler struct {
Client versioned.Interface
BuilderLister buildlisters.BuilderLister
BuilderCreator BuilderCreator
KeychainFactory registry.KeychainFactory
Tracker reconciler.Tracker
ClusterStoreLister buildlisters.ClusterStoreLister
BuildpackLister buildlisters.BuildpackLister
ClusterBuildpackLister buildlisters.ClusterBuildpackLister
ExtensionLister buildlisters.ExtensionLister
ClusterExtensionLister buildlisters.ClusterExtensionLister
ClusterStackLister buildlisters.ClusterStackLister
SecretFetcher Fetcher
}
func (c *Reconciler) Reconcile(ctx context.Context, key string) error {
namespace, builderName, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
builder, err := c.BuilderLister.Builders(namespace).Get(builderName)
if k8serrors.IsNotFound(err) {
return nil
} else if err != nil {
return err
}
builder = builder.DeepCopy()
builderRecord, creationError := c.reconcileBuilder(ctx, builder)
if creationError != nil {
builder.Status.ErrorCreate(creationError)
err := c.updateStatus(ctx, builder)
if err != nil {
return err
}
return creationError
}
builder.Status.BuilderRecord(builderRecord)
return c.updateStatus(ctx, builder)
}
func (c *Reconciler) reconcileBuilder(ctx context.Context, builder *buildapi.Builder) (buildapi.BuilderRecord, error) {
c.Tracker.Track(reconciler.Key{
NamespacedName: types.NamespacedName{
Name: builder.Spec.Stack.Name,
Namespace: metav1.NamespaceAll,
},
GroupKind: schema.GroupKind{
Group: "kpack.io",
Kind: buildapi.ClusterStackKind,
},
}, builder.NamespacedName())
var (
clusterStore *buildapi.ClusterStore
err error
)
if builder.Spec.Store.Name != "" {
c.Tracker.Track(reconciler.Key{
NamespacedName: types.NamespacedName{
Name: builder.Spec.Store.Name,
Namespace: metav1.NamespaceAll,
},
GroupKind: schema.GroupKind{
Group: "kpack.io",
Kind: buildapi.ClusterStoreKind,
},
}, builder.NamespacedName())
clusterStore, err = c.ClusterStoreLister.Get(builder.Spec.Store.Name)
if err != nil {
return buildapi.BuilderRecord{}, err
}
}
c.Tracker.TrackKind(schema.GroupKind{
Group: "kpack.io",
Kind: buildapi.BuildpackKind,
}, builder.NamespacedName())
c.Tracker.TrackKind(schema.GroupKind{
Group: "kpack.io",
Kind: buildapi.ClusterBuildpackKind,
}, builder.NamespacedName())
buildpacks, err := c.BuildpackLister.Buildpacks(builder.Namespace).List(labels.Everything())
if err != nil {
return buildapi.BuilderRecord{}, err
}
extensions, err := c.ExtensionLister.Extensions(builder.Namespace).List(labels.Everything())
if err != nil {
return buildapi.BuilderRecord{}, err
}
clusterBuildpacks, err := c.ClusterBuildpackLister.List(labels.Everything())
if err != nil {
return buildapi.BuilderRecord{}, err
}
clusterExtensions, err := c.ClusterExtensionLister.List(labels.Everything())
if err != nil {
return buildapi.BuilderRecord{}, err
}
clusterStack, err := c.ClusterStackLister.Get(builder.Spec.Stack.Name)
if err != nil {
return buildapi.BuilderRecord{}, err
}
if !clusterStack.Status.GetCondition(corev1alpha1.ConditionReady).IsTrue() {
return buildapi.BuilderRecord{}, errors.Errorf("Error: clusterstack '%s' is not ready", clusterStack.Name)
}
builderKeychain, err := c.KeychainFactory.KeychainForSecretRef(ctx, registry.SecretRef{
ServiceAccount: builder.Spec.ServiceAccount(),
Namespace: builder.Namespace,
})
if err != nil {
return buildapi.BuilderRecord{}, err
}
stackKeychain := builderKeychain
if clusterStack.Spec.ServiceAccountRef != nil {
stackKeychain, err = c.KeychainFactory.KeychainForSecretRef(ctx, registry.SecretRef{
ServiceAccount: clusterStack.Spec.ServiceAccountRef.Name,
Namespace: clusterStack.Spec.ServiceAccountRef.Namespace,
})
if err != nil {
return buildapi.BuilderRecord{}, err
}
}
fetcher := cnb.NewRemoteBuildpackFetcher(c.KeychainFactory, clusterStore, buildpacks, clusterBuildpacks, extensions, clusterExtensions)
serviceAccountSecrets, err := c.SecretFetcher.SecretsForServiceAccount(ctx, builder.Spec.ServiceAccount(), builder.Namespace)
if err != nil {
return buildapi.BuilderRecord{}, err
}
buildRecord, err := c.BuilderCreator.CreateBuilder(ctx, builderKeychain, stackKeychain, fetcher, clusterStack, builder.Spec.BuilderSpec, serviceAccountSecrets)
if err != nil {
return buildapi.BuilderRecord{}, err
}
return buildRecord, nil
}
func (c *Reconciler) updateStatus(ctx context.Context, desired *buildapi.Builder) error {
desired.Status.ObservedGeneration = desired.Generation
original, err := c.BuilderLister.Builders(desired.Namespace).Get(desired.Name)
if err != nil {
return err
}
if equality.Semantic.DeepEqual(desired.Status, original.Status) {
return nil
}
_, err = c.Client.KpackV1alpha2().Builders(desired.Namespace).UpdateStatus(ctx, desired, metav1.UpdateOptions{})
return err
}