-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild_messenger.go
More file actions
244 lines (238 loc) · 8.71 KB
/
build_messenger.go
File metadata and controls
244 lines (238 loc) · 8.71 KB
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
package messenger
import (
"context"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/go-logr/logr"
"github.com/uselagoon/machinery/api/schema"
lagooncrd "github.com/uselagoon/remote-controller/api/lagoon/v1beta2"
"github.com/uselagoon/remote-controller/internal/helpers"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// buildStatusLogsToLagoonLogs sends the logs to lagoon-logs message queue, used for general messaging
func (m *Messenger) BuildStatusLogsToLagoonLogs(
ctx context.Context,
mq bool,
opLog logr.Logger,
lagoonBuild *lagooncrd.LagoonBuild,
buildCondition lagooncrd.BuildStatusType,
targetName, buildStep string,
) {
if mq {
msg := schema.LagoonLog{
Severity: "info",
Project: lagoonBuild.Spec.Project.Name,
Event: "task:builddeploy-kubernetes:" + buildCondition.ToLower(), // @TODO: this probably needs to be changed to a new task event for the controller
Meta: &schema.LagoonLogMeta{
ProjectName: lagoonBuild.Spec.Project.Name,
BranchName: lagoonBuild.Spec.Project.Environment,
BuildStatus: buildCondition.ToLower(), // same as buildstatus label
BuildName: lagoonBuild.Name,
BuildStep: buildStep,
LogLink: lagoonBuild.Spec.Project.UILink,
Cluster: targetName,
},
Message: fmt.Sprintf("*[%s]* %s Build `%s` %s",
lagoonBuild.Spec.Project.Name,
lagoonBuild.Spec.Project.Environment,
lagoonBuild.Name,
buildCondition.ToLower(),
),
}
route, routes, err := helpers.GetLagoonEnvRoutes(ctx, opLog, m.Client, lagoonBuild.Namespace)
// if we aren't being provided the lagoon config, we can skip adding the routes etc
if err == nil {
msg.Meta.Route = route
msg.Meta.Routes = routes
}
msgBytes, err := json.Marshal(msg)
if err != nil {
opLog.Error(err, "unable to encode message as JSON")
}
// @TODO: if we can't publish the message because we are deleting the resource, then should we even
// bother to patch the resource??
// leave it for now cause the resource will just be deleted anyway
if err := m.Publish("lagoon-logs", msgBytes); err != nil {
// if we can't publish the message, just return
return
}
}
}
// updateDeploymentAndEnvironmentTask sends the status of the build and deployment to the controllerhandler message queue in lagoon,
// this is for the handler in lagoon to process.
func (m *Messenger) UpdateDeploymentAndEnvironmentTask(
ctx context.Context,
mq bool,
opLog logr.Logger,
lagoonBuild *lagooncrd.LagoonBuild,
checkLagoonEnv bool,
buildCondition lagooncrd.BuildStatusType,
targetName, buildStep string,
) {
namespace := helpers.GenerateNamespaceName(
lagoonBuild.Spec.Project.NamespacePattern, // the namespace pattern or `openshiftProjectPattern` from Lagoon is never received by the controller
lagoonBuild.Spec.Project.Environment,
lagoonBuild.Spec.Project.Name,
m.NamespacePrefix,
m.ControllerNamespace,
m.RandomNamespacePrefix,
)
if mq {
ns := &corev1.Namespace{}
if err := m.Client.Get(ctx, types.NamespacedName{Name: namespace}, ns); err != nil {
if helpers.IgnoreNotFound(err) != nil {
opLog.Error(err, "namespace %s not found", namespace)
return
}
}
envName := ns.Labels["lagoon.sh/environment"]
eID, _ := strconv.Atoi(ns.Labels["lagoon.sh/environmentId"])
envID := helpers.UintPtr(uint(eID))
projectName := ns.Labels["lagoon.sh/project"]
pID, _ := strconv.Atoi(ns.Labels["lagoon.sh/projectId"])
projectID := helpers.UintPtr(uint(pID))
msg := schema.LagoonMessage{
Type: "build",
Namespace: namespace,
Meta: &schema.LagoonLogMeta{
Environment: envName,
Project: projectName,
EnvironmentID: envID,
ProjectID: projectID,
BuildStatus: buildCondition.ToLower(),
BuildStep: buildStep,
BuildName: lagoonBuild.Name,
LogLink: lagoonBuild.Spec.Project.UILink,
RemoteID: string(lagoonBuild.UID),
Cluster: targetName,
},
}
lagoonServices := &corev1.ConfigMap{}
if err := m.Client.Get(ctx, types.NamespacedName{Namespace: lagoonBuild.Namespace, Name: "lagoon-services"}, lagoonServices); err != nil {
if helpers.IgnoreNotFound(err) != nil {
opLog.Error(err, "configmap lagoon-services not found")
return
}
// if configmap doesn't exist, fall back to previous service check behaviour
labelRequirements1, _ := labels.NewRequirement("lagoon.sh/service", selection.NotIn, []string{"faketest"})
listOption := (&client.ListOptions{}).ApplyOptions([]client.ListOption{
client.InNamespace(lagoonBuild.Namespace),
client.MatchingLabelsSelector{
Selector: labels.NewSelector().Add(*labelRequirements1),
},
})
depList := &appsv1.DeploymentList{}
serviceNames := []string{}
services := []schema.EnvironmentService{}
if err := m.Client.List(context.TODO(), depList, listOption); err == nil {
// generate the list of services to add or update to the environment
for _, deployment := range depList.Items {
var serviceName, serviceType string
containers := []schema.ServiceContainer{}
if name, ok := deployment.Labels["lagoon.sh/service"]; ok {
serviceName = name
serviceNames = append(serviceNames, serviceName)
for _, container := range deployment.Spec.Template.Spec.Containers {
containers = append(containers, schema.ServiceContainer{Name: container.Name})
}
}
if sType, ok := deployment.Labels["lagoon.sh/service-type"]; ok {
serviceType = sType
}
// probably need to collect dbaas consumers too at some stage
services = append(services, schema.EnvironmentService{
Name: serviceName,
Type: serviceType,
Containers: containers,
})
}
msg.Meta.Services = serviceNames
msg.Meta.EnvironmentServices = services
}
} else {
// otherwise get the values from configmap
if val, ok := lagoonServices.Data["post-deploy"]; ok {
serviceConfig := helpers.LagoonServices{}
err := json.Unmarshal([]byte(val), &serviceConfig)
if err == nil {
msg.Meta.EnvironmentServices = serviceConfig.Services
}
}
}
if checkLagoonEnv {
route, routes, err := helpers.GetLagoonEnvRoutes(ctx, opLog, m.Client, lagoonBuild.Namespace)
// if we aren't being provided the lagoon config, we can skip adding the routes etc
if err == nil {
msg.Meta.Route = route
msg.Meta.Routes = routes
}
}
if buildCondition.ToLower() == "failed" || buildCondition.ToLower() == "complete" || buildCondition.ToLower() == "cancelled" {
msg.Meta.EndTime = time.Now().UTC().Format("2006-01-02 15:04:05")
}
msgBytes, err := json.Marshal(msg)
if err != nil {
opLog.Error(err, "unable to encode message as JSON")
}
// @TODO: if we can't publish the message because we are deleting the resource, then should we even
// bother to patch the resource??
// leave it for now cause the resource will just be deleted anyway
if err := m.Publish("lagoon-tasks:controller", msgBytes); err != nil {
// if we can't publish the message, just return
return
}
}
}
// buildLogsToLagoonLogs sends the build logs to the lagoon-logs message queue
// it contains the actual pod log output that is sent to elasticsearch, it is what eventually is displayed in the UI
func (m *Messenger) BuildLogsToLagoonLogs(
mq bool,
opLog logr.Logger,
lagoonBuild *lagooncrd.LagoonBuild,
logs []byte,
buildCondition lagooncrd.BuildStatusType,
targetName string,
) {
if mq {
condition := buildCondition
buildStep := "queued"
if condition == lagooncrd.BuildStatusCancelled {
buildStep = "cancelled"
}
msg := schema.LagoonLog{
Severity: "info",
Project: lagoonBuild.Spec.Project.Name,
Event: "build-logs:builddeploy-kubernetes:" + lagoonBuild.Name,
Meta: &schema.LagoonLogMeta{
JobName: lagoonBuild.Name, // @TODO: remove once lagoon is corrected in controller-handler
BuildName: lagoonBuild.Name,
BuildStatus: buildCondition.ToLower(), // same as buildstatus label
BuildStep: buildStep,
BranchName: lagoonBuild.Spec.Project.Environment,
RemoteID: string(lagoonBuild.UID),
LogLink: lagoonBuild.Spec.Project.UILink,
Cluster: targetName,
},
}
// add the actual build log message
msg.Message = string(logs)
msgBytes, err := json.Marshal(msg)
if err != nil {
opLog.Error(err, "unable to encode message as JSON")
}
// @TODO: if we can't publish the message because we are deleting the resource, then should we even
// bother to patch the resource??
// leave it for now cause the resource will just be deleted anyway
if err := m.Publish("lagoon-logs", msgBytes); err != nil {
// if we can't publish the message, just return
return
}
}
}