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

fix(metrics): be aware when an ingress is deleted and remove it corre… #10

Merged
merged 2 commits into from
Sep 2, 2024
Merged
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
35 changes: 33 additions & 2 deletions pkg/envoy/ingress_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ import (
v1 "k8s.io/api/core/v1"
)

type UpstreamInfo struct {
RuleHost string
Upstream string
Namespace string
Class string
ClusterName string
IngressName string
}

var previousUpstreams = make(map[string]UpstreamInfo)

func sortCluster(clusters []*cluster) {
sort.Slice(clusters, func(i int, j int) bool {
return clusters[i].identity() < clusters[j].identity()
Expand Down Expand Up @@ -378,6 +389,7 @@ func translateIngresses(ingresses []*k8s.Ingress, syncSecrets bool, secrets []*v
cfg := &envoyConfiguration{}
envoyIngresses := map[string]*envoyIngress{}
ruleHostToIngresses := map[string][]*k8s.Ingress{}
currentUpstreams := make(map[string]UpstreamInfo)

for _, i := range ingresses {
for _, ruleHost := range i.RulesHosts {
Expand Down Expand Up @@ -429,12 +441,21 @@ func translateIngresses(ingresses []*k8s.Ingress, syncSecrets bool, secrets []*v
if weight64, err := strconv.ParseUint(ingress.Annotations["yggdrasil.uswitch.com/weight"], 10, 32); err == nil {
if weight64 != 0 {
envoyIngress.addUpstream(j, uint32(weight64))
EnvoyUpstreamInfo.WithLabelValues(strings.ReplaceAll(ruleHost, ".", "_"), j, ingress.Namespace, class, ingress.KubernetesClusterName, ingress.Name).Set(float64(1))
}
} else {
envoyIngress.addUpstream(j, 1)
EnvoyUpstreamInfo.WithLabelValues(strings.ReplaceAll(ruleHost, ".", "_"), j, ingress.Namespace, class, ingress.KubernetesClusterName, ingress.Name).Set(float64(1))
}
upstreamKey := fmt.Sprintf("%s-%s", ruleHost, j)
currentUpstreams[upstreamKey] = UpstreamInfo{
RuleHost: strings.ReplaceAll(ruleHost, ".", "_"),
Upstream: j,
Namespace: ingress.Namespace,
Class: class,
ClusterName: ingress.KubernetesClusterName,
IngressName: ingress.Name,
}

EnvoyUpstreamInfo.WithLabelValues(strings.ReplaceAll(ruleHost, ".", "_"), j, ingress.Namespace, class, ingress.KubernetesClusterName, ingress.Name).Set(float64(1))
} else {
logrus.Warnf("Endpoint is in maintenance mode, upstream %s will not be added for host %s", j, ruleHost)
}
Expand Down Expand Up @@ -507,6 +528,16 @@ func translateIngresses(ingresses []*k8s.Ingress, syncSecrets bool, secrets []*v
}
}

// Identify and remove upstreams that no longer exist
for upstreamKey, info := range previousUpstreams {
if _, exists := currentUpstreams[upstreamKey]; !exists {
EnvoyUpstreamInfo.DeleteLabelValues(info.RuleHost, info.Upstream, info.Namespace, info.Class, info.ClusterName, info.IngressName)
}
}

// Update the previous state
previousUpstreams = currentUpstreams

for _, ingress := range envoyIngresses {
cfg.Clusters = append(cfg.Clusters, ingress.cluster)
cfg.VirtualHosts = append(cfg.VirtualHosts, ingress.vhost)
Expand Down
Loading