forked from apache/camel-k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
217 lines (181 loc) · 6.55 KB
/
build.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
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 integrationkit
import (
"context"
"fmt"
"strings"
"time"
"github.com/apache/camel-k/pkg/util/defaults"
"github.com/pkg/errors"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util/kubernetes"
)
// NewBuildAction creates a new build request handling action for the kit.
func NewBuildAction() Action {
return &buildAction{}
}
type buildAction struct {
baseAction
}
func (action *buildAction) Name() string {
return "build"
}
func (action *buildAction) CanHandle(kit *v1.IntegrationKit) bool {
return kit.Status.Phase == v1.IntegrationKitPhaseBuildSubmitted ||
kit.Status.Phase == v1.IntegrationKitPhaseBuildRunning
}
func (action *buildAction) Handle(ctx context.Context, kit *v1.IntegrationKit) (*v1.IntegrationKit, error) {
if kit.Status.Phase == v1.IntegrationKitPhaseBuildSubmitted {
return action.handleBuildSubmitted(ctx, kit)
} else if kit.Status.Phase == v1.IntegrationKitPhaseBuildRunning {
return action.handleBuildRunning(ctx, kit)
}
return nil, nil
}
func (action *buildAction) handleBuildSubmitted(ctx context.Context, kit *v1.IntegrationKit) (*v1.IntegrationKit, error) {
build, err := kubernetes.GetBuild(ctx, action.client, kit.Name, kit.Namespace)
if err != nil && !k8serrors.IsNotFound(err) {
return nil, err
}
if err != nil && k8serrors.IsNotFound(err) ||
build.Status.Phase == v1.BuildPhaseError ||
build.Status.Phase == v1.BuildPhaseInterrupted ||
build.Status.Phase == v1.BuildPhaseSucceeded {
env, err := trait.Apply(ctx, action.client, nil, kit)
if err != nil {
return nil, err
}
if env.CamelCatalog == nil {
return nil, errors.New("undefined camel catalog")
}
labels := kubernetes.FilterCamelCreatorLabels(kit.Labels)
labels[v1.IntegrationKitLayoutLabel] = kit.Labels[v1.IntegrationKitLayoutLabel]
annotations := make(map[string]string)
if v, ok := kit.Annotations[v1.PlatformSelectorAnnotation]; ok {
annotations[v1.PlatformSelectorAnnotation] = v
}
operatorID := defaults.OperatorID()
if operatorID != "" {
annotations[v1.OperatorIDAnnotation] = operatorID
}
timeout := env.Platform.Status.Build.GetTimeout()
if layout := labels[v1.IntegrationKitLayoutLabel]; env.Platform.Spec.Build.Timeout == nil && layout == v1.IntegrationKitLayoutNative {
// Increase the timeout to a sensible default
timeout = metav1.Duration{
Duration: 10 * time.Minute,
}
}
build = &v1.Build{
TypeMeta: metav1.TypeMeta{
APIVersion: v1.SchemeGroupVersion.String(),
Kind: v1.BuildKind,
},
ObjectMeta: metav1.ObjectMeta{
Namespace: kit.Namespace,
Name: kit.Name,
Labels: labels,
Annotations: annotations,
},
Spec: v1.BuildSpec{
Strategy: env.Platform.Status.Build.BuildStrategy,
Tasks: env.BuildTasks,
Timeout: timeout,
},
}
// Set the integration kit instance as the owner and controller
if err := controllerutil.SetControllerReference(kit, build, action.client.GetScheme()); err != nil {
return nil, err
}
err = action.client.Delete(ctx, build)
if err != nil && !k8serrors.IsNotFound(err) {
return nil, errors.Wrap(err, "cannot delete build")
}
err = action.client.Create(ctx, build)
if err != nil {
return nil, errors.Wrap(err, "cannot create build")
}
}
if build.Status.Phase == v1.BuildPhaseRunning {
kit.Status.Phase = v1.IntegrationKitPhaseBuildRunning
return kit, nil
}
return nil, nil
}
func (action *buildAction) handleBuildRunning(ctx context.Context, kit *v1.IntegrationKit) (*v1.IntegrationKit, error) {
build, err := kubernetes.GetBuild(ctx, action.client, kit.Name, kit.Namespace)
if err != nil {
return nil, err
}
switch build.Status.Phase {
case v1.BuildPhaseRunning:
action.L.Info("Build running")
case v1.BuildPhaseSucceeded:
// we should ensure that the integration kit is still in the right phase,
// if not there is a chance that the kit has been modified by the user
if kit.Status.Phase != v1.IntegrationKitPhaseBuildRunning {
return nil, fmt.Errorf("found kit %s not in the expected phase (expectd=%s, found=%s)",
kit.Name,
string(v1.IntegrationKitPhaseBuildRunning),
string(kit.Status.Phase),
)
}
kit.Status.BaseImage = build.Status.BaseImage
kit.Status.Image = build.Status.Image
// Address the image by repository digest instead of tag if possible
if build.Status.Digest != "" {
image := kit.Status.Image
i := strings.LastIndex(image, ":")
if i > 0 {
image = image[:i]
}
kit.Status.Image = fmt.Sprintf("%s@%s", image, build.Status.Digest)
} else {
// otherwise rely on repository tag
kit.Status.Image = build.Status.Image
}
kit.Status.Phase = v1.IntegrationKitPhaseReady
kit.Status.Artifacts = make([]v1.Artifact, 0, len(build.Status.Artifacts))
for _, a := range build.Status.Artifacts {
// do not include artifact location
kit.Status.Artifacts = append(kit.Status.Artifacts, v1.Artifact{
ID: a.ID,
Location: "",
Target: a.Target,
Checksum: a.Checksum,
})
}
return kit, err
case v1.BuildPhaseError, v1.BuildPhaseInterrupted:
// we should ensure that the integration kit is still in the right phase,
// if not there is a chance that the kit has been modified by the user
if kit.Status.Phase != v1.IntegrationKitPhaseBuildRunning {
return nil, fmt.Errorf("found kit %s not the an expected phase (expectd=%s, found=%s)",
kit.Name,
string(v1.IntegrationKitPhaseBuildRunning),
string(kit.Status.Phase),
)
}
// Let's copy the build failure to the integration kit status
kit.Status.Failure = build.Status.Failure
kit.Status.Phase = v1.IntegrationKitPhaseError
return kit, nil
}
return nil, nil
}