Cannot match regex capture group within same expression

I’m using the discovery.relabel component on kubernetes service targets from the discovery.kubernetes component, in order to filter targets to only the services which have a particular annotation. These annotations contain the URL path for blackbox health checks. This part is working fine.

I then want to filter the service targets by a given port (given in another service annotation), so that I’m only left with the services whose ports match the port in the annotation.

I’m trying to create a discovery.relabel rule to do this. I created the regex shown below for this purpose. It’s designed to check the two port numbers are equal, and write ‘true’ to a new label.

image

I’ve attempted to put this regex into a relabel rule but can’t get it to work, see below (note that I have removed the ^/$ from the start/end for testing).

For the reference to the capture group in the regex arg, I’ve tried using \1, \\1, $1 and ${1}, but cannot get anything to work here. It may simply be that this regex engine doesn’t support using capture groups within the same expression.

Can anyone help with this? Have I just done something wrong? Or are there any alternatives to do what I want to do here?

Below are some of the targets where I would like to make sure the service port is equal to the blackbox port.

Thanks :slight_smile:

Ahhh yes I think I’ve just found out that Golang’s RE2 does not support backreferences. Does anyone know a different way to achieve this in Alloy?

In fact, I’ve figured it out now. I hadn’t realised that the keepequal action compares two label values. The below works how I want it :slight_smile:

rule {
  // Drop all services where the service port is not equal to the blackbox port
  source_labels = ["__meta_kubernetes_service_port_number"]
  action = "keepequal"
  target_label = "__meta_kubernetes_service_annotation_blackbox_match_port"
}
1 Like