Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to specify list of ignored Nomad meta labels #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,11 @@ clients are available to Netreap.

### Configuring

| Flag | Env Var | Default | Description |
| ---------------------- | --------------------- | ----------------------------- | ------------------------------------------------------------------------------------------------------------- |
| `--debug` | `NETREAP_DEBUG` | `false` | Turns on debug logging |
| `--policy-key` | `NETREAP_POLICY_KEY` | `netreap.io/policy` | Consul key that Netreap watches for changes to the Cilium policy JSON value |
| Flag | Env Var | Default | Description |
| ---------------------------- | ------------------------------ | ----------------------------- | ------------------------------------------------------------------------------------------------------------- |
| `--debug` | `NETREAP_DEBUG` | `false` | Turns on debug logging |
| `--policy-key` | `NETREAP_POLICY_KEY` | `netreap.io/policy` | Consul key that Netreap watches for changes to the Cilium policy JSON value |
| `--ignore-nomad-meta-labels` | `NETREAP_IGNORED_META_LABELS` | `""` | Comma-separated list of Nomad's job metadata that not will be passed to Cilium Endpoint labels |

Please note that to configure the Nomad, Consul and Cilium clients that Netreap uses,
we leverage the well defined environment variables for
Expand Down
4 changes: 4 additions & 0 deletions internal/netreap/netreap.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ const (
var (
LabelCiliumPolicyName = LabelKeyCiliumPolicyName + "." + LabelSourceNetreap
)

type IgnoredLabels struct {
IgnoreMeta []string
}
16 changes: 14 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"os/signal"
"strings"

cilium_client "github.com/cilium/cilium/pkg/client"
cilium_logging "github.com/cilium/cilium/pkg/logging"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/urfave/cli/v2"
"go.uber.org/zap"

"github.com/cosmonic-labs/netreap/internal/netreap"
"github.com/cosmonic-labs/netreap/internal/policy"
"github.com/cosmonic-labs/netreap/internal/zaplogrus"
"github.com/cosmonic-labs/netreap/reapers"
Expand All @@ -23,7 +25,8 @@ import (
var Version = "unreleased"

type config struct {
policyKey string
policyKey string
ignoredLabels string
}

func main() {
Expand Down Expand Up @@ -55,6 +58,14 @@ func main() {
EnvVars: []string{"NETREAP_POLICY_KEY"},
Destination: &conf.policyKey,
},
&cli.StringFlag{
Name: "ignore-nomad-meta-labels",
Aliases: []string{"i"},
Value: "",
Usage: "List of ignored labels collected from Nomad's job metainfo.",
EnvVars: []string{"NETREAP_IGNORED_META_LABELS"},
Destination: &conf.ignoredLabels,
},
},
Before: func(ctx *cli.Context) error {
if debug {
Expand Down Expand Up @@ -137,8 +148,9 @@ func run(conf config) error {
return fmt.Errorf("unable to start node reaper: %s", err)
}

nomadIgnoredLables := netreap.IgnoredLabels{IgnoreMeta: strings.Split(conf.ignoredLabels, ",")}
zap.S().Debug("Starting endpoint reaper")
endpoint_reaper, err := reapers.NewEndpointReaper(cilium_client, nomad_client.Allocations(), nomad_client.EventStream(), nodeID)
endpoint_reaper, err := reapers.NewEndpointReaper(cilium_client, nomad_client.Allocations(), nomad_client.EventStream(), nodeID, nomadIgnoredLables)
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions reapers/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/cosmonic-labs/netreap/internal/netreap"
nomad_api "github.com/hashicorp/nomad/api"
"go.uber.org/zap"
"golang.org/x/exp/slices"

backoff "github.com/cenkalti/backoff/v4"
)
Expand All @@ -23,16 +24,18 @@ type EndpointReaper struct {
nomadAllocations AllocationInfo
nomadEventStream EventStreamer
nodeID string
ignoredLabels netreap.IgnoredLabels
}

// NewEndpointReaper creates a new EndpointReaper. This will run an initial reconciliation before
// returning the reaper
func NewEndpointReaper(ciliumClient EndpointUpdater, nomadAllocations AllocationInfo, nomadEventStream EventStreamer, nodeID string) (*EndpointReaper, error) {
func NewEndpointReaper(ciliumClient EndpointUpdater, nomadAllocations AllocationInfo, nomadEventStream EventStreamer, nodeID string, ignoredLabels netreap.IgnoredLabels) (*EndpointReaper, error) {
reaper := EndpointReaper{
cilium: ciliumClient,
nomadAllocations: nomadAllocations,
nomadEventStream: nomadEventStream,
nodeID: nodeID,
ignoredLabels: ignoredLabels,
}

// Do the initial reconciliation loop
Expand Down Expand Up @@ -290,7 +293,9 @@ func (e *EndpointReaper) labelEndpoint(endpoint *models.Endpoint, allocation *no
}

for k, v := range metadata {
newLabels = append(newLabels, fmt.Sprintf("%s:%s=%s", netreap.LabelSourceNomad, k, v))
if !slices.Contains(e.ignoredLabels.IgnoreMeta, k) {
newLabels = append(newLabels, fmt.Sprintf("%s:%s=%s", netreap.LabelSourceNomad, k, v))
}
}

oldLabels := models.Labels{}
Expand Down
3 changes: 2 additions & 1 deletion reapers/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/cilium/cilium/api/v1/models"
endpoint_id "github.com/cilium/cilium/pkg/endpoint/id"
"github.com/cosmonic-labs/netreap/internal/netreap"
nomad_api "github.com/hashicorp/nomad/api"
)

Expand Down Expand Up @@ -140,7 +141,7 @@ func TestEndpointReconcile(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
reaper, err := NewEndpointReaper(tt.cilium, tt.nomadAllocations, nil, "")
reaper, err := NewEndpointReaper(tt.cilium, tt.nomadAllocations, nil, "", netreap.IgnoredLabels{})
if err != nil {
t.Fatalf("unexpected error creating poller %v", err)
}
Expand Down