Skip to content

Commit

Permalink
sort mapped domains related to the same redirect, fix metric registry (
Browse files Browse the repository at this point in the history
  • Loading branch information
ziollek committed Oct 11, 2023
1 parent 6534c7a commit 618ad8a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
3 changes: 2 additions & 1 deletion vaas/vaas/cluster/models.py
Expand Up @@ -143,7 +143,8 @@ def mapped_domains(self, cluster: LogicalCluster) -> List[str]:
for mapping, required_labels in self.__parse_placeholders().items():
if not required_labels.difference(cluster_labels):
result.append(mapping.format(**cluster.parsed_labels()))
return result
# sort domains in order to enforce stable rendering of vcl content for the same input
return sorted(result)

def is_cluster_related_by_labels(self, cluster: LogicalCluster) -> bool:
result = False
Expand Down
14 changes: 6 additions & 8 deletions vaas/vaas/metrics/prometheus.py
Expand Up @@ -21,15 +21,13 @@ def __init__(self) -> None:
self.registry: CollectorRegistry = CollectorRegistry()

def get_or_create(self, full_name: str, kind: Type[Kind]) -> Kind:
metric: Optional[Kind] = self.metrics_bucket.get(full_name)
name, labels = self._split_dotted_name_to_short_name_and_labels(full_name)
metric: Optional[Kind] = self.metrics_bucket.get(name)
if not metric:
name, labels = self._split_dotted_name_to_short_name_and_labels(full_name)
if labels:
new_metrics: Kind = kind(name, name, labels.keys(), registry=self.registry).labels(**labels)
else:
new_metrics: Kind = kind(name, name, registry=self.registry)
self.metrics_bucket[full_name] = new_metrics
return self.metrics_bucket[full_name]
self.metrics_bucket[name] = kind(name, name, labels.keys(), registry=self.registry)
if labels:
return self.metrics_bucket[name].labels(**labels)
return self.metrics_bucket[name]

def _split_dotted_name_to_short_name_and_labels(self, name: str) -> Tuple[str, Dict[str, str]]:
"""
Expand Down
14 changes: 7 additions & 7 deletions vaas/vaas/vcl/tests/expected-vcl-4.0-canary.vcl
Expand Up @@ -339,7 +339,7 @@ sub vcl_synth {
if (resp.status == 989) {
set resp.status = 200;
set resp.http.Content-Type = "application/json";
synthetic ( {"{ "vcl_version" : "70175", "varnish_status": "disabled" }"} );
synthetic ( {"{ "vcl_version" : "0bbef", "varnish_status": "disabled" }"} );
return (deliver);
}
}
Expand Down Expand Up @@ -411,16 +411,16 @@ sub vcl_recv {
unset req.http.x-canary-random;

# Flexible REDIRECT
if (req.http.host == "example.prod.org") {
if (req.http.host == "example.prod.com") {
if (req.url ~ "/source") {
set req.http.x-redirect = "2";
set req.http.x-destination = "http://example.prod.org/new_destination";
set req.http.x-destination = "http://example.prod.com/new_destination";
set req.http.x-response-code = "301";
set req.http.x-action = "redirect";
}
else if (req.url ~ "/source") {
set req.http.x-redirect = "1";
set req.http.x-destination = "http://example.prod.org/destination";
set req.http.x-destination = "http://example.prod.com/destination";
set req.http.x-response-code = "301";
set req.http.x-action = "redirect";
}
Expand All @@ -431,16 +431,16 @@ sub vcl_recv {
set req.http.x-action = "redirect";
}
}
if (req.http.host == "example.prod.com") {
if (req.http.host == "example.prod.org") {
if (req.url ~ "/source") {
set req.http.x-redirect = "2";
set req.http.x-destination = "http://example.prod.com/new_destination";
set req.http.x-destination = "http://example.prod.org/new_destination";
set req.http.x-response-code = "301";
set req.http.x-action = "redirect";
}
else if (req.url ~ "/source") {
set req.http.x-redirect = "1";
set req.http.x-destination = "http://example.prod.com/destination";
set req.http.x-destination = "http://example.prod.org/destination";
set req.http.x-response-code = "301";
set req.http.x-action = "redirect";
}
Expand Down
14 changes: 7 additions & 7 deletions vaas/vaas/vcl/tests/expected-vcl-4.0.vcl
Expand Up @@ -366,7 +366,7 @@ sub vcl_synth {
if (resp.status == 989) {
set resp.status = 200;
set resp.http.Content-Type = "application/json";
synthetic ( {"{ "vcl_version" : "34535", "varnish_status": "disabled" }"} );
synthetic ( {"{ "vcl_version" : "d584a", "varnish_status": "disabled" }"} );
return (deliver);
}
}
Expand Down Expand Up @@ -438,16 +438,16 @@ sub vcl_recv {
unset req.http.x-canary-random;

# Flexible REDIRECT
if (req.http.host == "example.prod.org") {
if (req.http.host == "example.prod.com") {
if (req.url ~ "/source") {
set req.http.x-redirect = "2";
set req.http.x-destination = "http://example.prod.org/new_destination";
set req.http.x-destination = "http://example.prod.com/new_destination";
set req.http.x-response-code = "301";
set req.http.x-action = "redirect";
}
else if (req.url ~ "/source") {
set req.http.x-redirect = "1";
set req.http.x-destination = "http://example.prod.org/destination";
set req.http.x-destination = "http://example.prod.com/destination";
set req.http.x-response-code = "301";
set req.http.x-action = "redirect";
}
Expand All @@ -458,16 +458,16 @@ sub vcl_recv {
set req.http.x-action = "redirect";
}
}
if (req.http.host == "example.prod.com") {
if (req.http.host == "example.prod.org") {
if (req.url ~ "/source") {
set req.http.x-redirect = "2";
set req.http.x-destination = "http://example.prod.com/new_destination";
set req.http.x-destination = "http://example.prod.org/new_destination";
set req.http.x-response-code = "301";
set req.http.x-action = "redirect";
}
else if (req.url ~ "/source") {
set req.http.x-redirect = "1";
set req.http.x-destination = "http://example.prod.com/destination";
set req.http.x-destination = "http://example.prod.org/destination";
set req.http.x-response-code = "301";
set req.http.x-action = "redirect";
}
Expand Down

0 comments on commit 618ad8a

Please sign in to comment.