Skip to content

Commit 8c6edb4

Browse files
authored
Merge pull request #37 from deverton-godaddy/deverton/netreap-labels
Move netreap specific labels to separate package.
2 parents 96dbb44 + ba031ef commit 8c6edb4

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
netreap
1+
./netreap
22
.op

internal/netreap/netreap.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package netreap
2+
3+
const (
4+
5+
// LabelKeyNomadJobID is the name of the policy label which refers to the
6+
// Cilium policy name
7+
LabelKeyCiliumPolicyName = "cilium.policy.name"
8+
9+
// LabelKeyNomadJobID is the Nomad Job ID
10+
LabelKeyNomadJobID = "nomad.job_id"
11+
12+
// LabelKeyNomadJobID is the Nomad Namespace of the job
13+
LabelKeyNomadNamespace = "nomad.namespace"
14+
15+
// LabelKeyNomadTaskGroupID is the Nomad task group of the job allocation
16+
LabelKeyNomadTaskGroupID = "nomad.task_group_id"
17+
18+
// LabelSourceNetreap is a label imported from Netreap
19+
LabelSourceNetreap = "netreap"
20+
21+
// LabelSourceNomadKeyPrefix is prefix of a Netreap label
22+
LabelSourceNetreapKeyPrefix = LabelSourceNetreap + "."
23+
24+
// LabelSourceNomad is a label imported from Nomad
25+
LabelSourceNomad = "nomad"
26+
27+
// LabelSourceNomadKeyPrefix is prefix of a Nomad label
28+
LabelSourceNomadKeyPrefix = LabelSourceNomad + "."
29+
)
30+
31+
var (
32+
LabelCiliumPolicyName = LabelKeyCiliumPolicyName + "." + LabelSourceNetreap
33+
)

reapers/endpoints.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,13 @@ import (
1010

1111
"github.com/cilium/cilium/api/v1/models"
1212
endpoint_id "github.com/cilium/cilium/pkg/endpoint/id"
13+
"github.com/cosmonic/netreap/internal/netreap"
1314
nomad_api "github.com/hashicorp/nomad/api"
1415
"go.uber.org/zap"
1516

1617
backoff "github.com/cenkalti/backoff/v4"
1718
)
1819

19-
const (
20-
netreapLabelPrefix = "netreap"
21-
nomadLabelPrefix = "nomad"
22-
jobIDLabel = "nomad.job_id"
23-
taskGroupLabel = "nomad.task_group_id"
24-
namespaceLabel = "nomad.namespace"
25-
)
26-
2720
type EndpointReaper struct {
2821
cilium EndpointUpdater
2922
nomadAllocations AllocationInfo
@@ -271,9 +264,9 @@ func stringArrayEqual(left []string, right []string) bool {
271264
}
272265

273266
func (e *EndpointReaper) labelEndpoint(endpoint *models.Endpoint, allocation *nomad_api.Allocation) {
274-
newLabels := models.Labels{fmt.Sprintf("%s:%s=%s", netreapLabelPrefix, jobIDLabel, allocation.JobID)}
275-
newLabels = append(newLabels, fmt.Sprintf("%s:%s=%s", netreapLabelPrefix, namespaceLabel, allocation.Namespace))
276-
newLabels = append(newLabels, fmt.Sprintf("%s:%s=%s", netreapLabelPrefix, taskGroupLabel, allocation.TaskGroup))
267+
newLabels := models.Labels{fmt.Sprintf("%s:%s=%s", netreap.LabelSourceNetreap, netreap.LabelKeyNomadJobID, allocation.JobID)}
268+
newLabels = append(newLabels, fmt.Sprintf("%s:%s=%s", netreap.LabelSourceNetreap, netreap.LabelKeyNomadNamespace, allocation.Namespace))
269+
newLabels = append(newLabels, fmt.Sprintf("%s:%s=%s", netreap.LabelSourceNetreap, netreap.LabelKeyNomadTaskGroupID, allocation.TaskGroup))
277270

278271
// Combine the metadata from the job and the task group with the task group taking precedence
279272
metadata := make(map[string]string)
@@ -290,7 +283,7 @@ func (e *EndpointReaper) labelEndpoint(endpoint *models.Endpoint, allocation *no
290283
}
291284

292285
for k, v := range metadata {
293-
newLabels = append(newLabels, fmt.Sprintf("%s:%s=%s", nomadLabelPrefix, k, v))
286+
newLabels = append(newLabels, fmt.Sprintf("%s:%s=%s", netreap.LabelSourceNomad, k, v))
294287
}
295288

296289
oldLabels := models.Labels{}
@@ -308,6 +301,13 @@ func (e *EndpointReaper) labelEndpoint(endpoint *models.Endpoint, allocation *no
308301
return
309302
}
310303

304+
zap.L().Info("Patching labels on endpoint",
305+
zap.String("container-id", allocation.ID),
306+
zap.Int64("endpoint-id", endpoint.ID),
307+
zap.Strings("new-labels", newLabels),
308+
zap.Strings("old-labels", oldLabels),
309+
)
310+
311311
ecr := &models.EndpointChangeRequest{
312312
ContainerID: allocation.ID,
313313
ContainerName: allocation.Name,

0 commit comments

Comments
 (0)