This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
build_controller.go
223 lines (188 loc) · 7.52 KB
/
build_controller.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
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"code.cloudfoundry.org/capi-k8s-release/src/cf-api-controllers/cf/model"
"code.cloudfoundry.org/capi-k8s-release/src/cf-api-controllers/image_registry"
"github.com/buildpacks/lifecycle"
"github.com/buildpacks/lifecycle/launch"
"github.com/go-logr/logr"
buildv1alpha1 "github.com/pivotal/kpack/pkg/apis/build/v1alpha1"
corev1alpha1 "github.com/pivotal/kpack/pkg/apis/core/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)
const BuildGUIDLabel = "cloudfoundry.org/build_guid"
const BuildReasonAnnotation = "image.kpack.io/reason"
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o fake/controller_runtime_client.go --fake-name ControllerRuntimeClient sigs.k8s.io/controller-runtime/pkg/client.Client
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o fake/cf_build_updater.go --fake-name CFBuildUpdater . CfBuildUpdater
type CfBuildUpdater interface {
UpdateBuild(buildGUID string, build model.Build) error
}
// BuildReconciler reconciles a Build object
type BuildReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
CFClient CfBuildUpdater
image_registry.ImageConfigFetcher
}
// +kubebuilder:rbac:groups=kpack.io,resources=builds,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=kpack.io,resources=builds/status,verbs=get;update;patch
func (r *BuildReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
var build buildv1alpha1.Build
err := r.Get(ctx, req.NamespacedName, &build)
if err != nil {
r.Log.WithValues("request", req.NamespacedName).Error(err, "failed initial build fetch")
if apierrors.IsNotFound(err) {
r.Log.WithValues("request", req.NamespacedName).Info("Attempted to reconcile build which no longer exists")
return ctrl.Result{Requeue: false}, nil
}
return ctrl.Result{}, err
}
logger := r.Log.WithValues(
"buildName", types.NamespacedName{Name: build.Name, Namespace: build.Namespace},
BuildGUIDLabel, build.GetLabels()[BuildGUIDLabel],
"status", build.Status,
)
condition := build.Status.GetCondition(corev1alpha1.ConditionSucceeded)
if condition.IsTrue() {
return r.reconcileSuccessfulBuild(&build, logger)
}
failureMessage := fmt.Sprintf(
"Kpack build unsuccessful: Build failure reason: '%s', message: '%s'.",
condition.Reason,
condition.Message,
)
failedContainerState := findAnyFailedContainerState(build.Status.StepStates)
if failedContainerState != nil {
failureMessage = fmt.Sprintf(
"Kpack build failed during container execution: Step failure reason: '%s', message: '%s'.",
failedContainerState.Terminated.Reason,
failedContainerState.Terminated.Message,
)
}
return r.reconcileFailedBuild(&build, failureMessage, logger)
}
func (r *BuildReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(new(buildv1alpha1.Build)).
WithEventFilter(predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
r.Log.WithValues("requestLink", e.Meta.GetSelfLink()).
V(1).Info("Build create event received")
return r.buildFilter(e.Object)
},
UpdateFunc: func(e event.UpdateEvent) bool {
r.Log.WithValues("requestLink", e.MetaNew.GetSelfLink()).
V(1).Info("Build update event received")
return r.buildFilter(e.ObjectNew)
},
DeleteFunc: func(_ event.DeleteEvent) bool { return false },
GenericFunc: func(_ event.GenericEvent) bool { return false },
}).
Complete(r)
}
var BuildFilterError = errors.New("Received a build event with a non-build runtime.Object")
func (r *BuildReconciler) buildFilter(e runtime.Object) bool {
newBuild, ok := e.(*buildv1alpha1.Build)
if !ok {
r.Log.WithValues("event", e).Error(BuildFilterError, "ignoring event")
return false
}
if _, isGuidPresent := newBuild.ObjectMeta.Labels[BuildGUIDLabel]; !isGuidPresent {
r.Log.WithValues("build", newBuild).V(1).Info("received update event for a non-CF Build resource, ignoring event")
return false
}
buildReason, ok := newBuild.ObjectMeta.Annotations[BuildReasonAnnotation]
if !ok {
r.Log.WithValues("build", newBuild).V(1).Info("received update event that was missing the build reason, ignoring event")
return false
}
return !newBuild.Status.GetCondition(corev1alpha1.ConditionSucceeded).IsUnknown() && buildReason != "STACK"
}
func (r *BuildReconciler) extractProcessTypes(build *buildv1alpha1.Build) (map[string]string, error) {
imageConfig, err := r.FetchImageConfig(build.Status.LatestImage, build.Spec.ServiceAccount, build.Namespace)
if err != nil {
return nil, err
}
var buildMetadata lifecycle.BuildMetadata
if err = json.Unmarshal([]byte(imageConfig.Labels[lifecycle.BuildMetadataLabel]), &buildMetadata); err != nil {
return nil, err
}
ret := make(map[string]string)
for _, process := range buildMetadata.Processes {
ret[process.Type] = extractFullCommand(process)
}
return ret, nil
}
func extractFullCommand(process launch.Process) string {
commandWithArgs := append([]string{process.Command}, process.Args...)
return strings.Join(commandWithArgs, " ")
}
func (r *BuildReconciler) reconcileSuccessfulBuild(build *buildv1alpha1.Build, logger logr.Logger) (ctrl.Result, error) {
logger.V(1).Info("Build completed successfully, marking as staged")
processTypes, err := r.extractProcessTypes(build)
if err != nil {
logger.Error(err, "Failed to fetch image config")
return r.reconcileFailedBuild(
build,
fmt.Sprintf(
"Failed to handle successful kpack build: %s",
err,
),
logger,
)
}
updateBuildRequest := model.NewBuildFromKpackBuild(build)
updateBuildRequest.Lifecycle.Data.ProcessTypes = processTypes
err = r.CFClient.UpdateBuild(build.GetLabels()[BuildGUIDLabel], updateBuildRequest)
if err != nil {
logger.Error(err, "Failed to send request to CF API")
// TODO: should we limit number of requeues? [story: #173573889]
return ctrl.Result{Requeue: true}, err
}
return ctrl.Result{}, nil
}
func (r *BuildReconciler) reconcileFailedBuild(build *buildv1alpha1.Build, errorMessage string, logger logr.Logger) (ctrl.Result, error) {
logger.V(1).Info("Build failed, marking as failed staging")
err := r.CFClient.UpdateBuild(build.GetLabels()[BuildGUIDLabel], model.Build{
State: model.BuildFailedState,
Error: errorMessage,
})
if err != nil {
logger.Error(err, "Failed to send request to CF API")
return ctrl.Result{Requeue: true}, err
}
return ctrl.Result{}, nil
}
// returns true if any container has terminated with a non-zero exit code
func findAnyFailedContainerState(containerStates []corev1.ContainerState) *corev1.ContainerState {
for _, container := range containerStates {
if container.Terminated != nil && container.Terminated.ExitCode != 0 {
return &container
}
}
return nil
}