|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "github.com/haproxytech/kubernetes-ingress/pkg/annotations" |
| 7 | + "github.com/haproxytech/kubernetes-ingress/pkg/haproxy" |
| 8 | + "github.com/haproxytech/kubernetes-ingress/pkg/haproxy/instance" |
| 9 | + "github.com/haproxytech/kubernetes-ingress/pkg/k8s" |
| 10 | + "github.com/haproxytech/kubernetes-ingress/pkg/store" |
| 11 | +) |
| 12 | + |
| 13 | +type PrometheusEndpoint struct { |
| 14 | + EventChan chan k8s.SyncDataEvent |
| 15 | + PodNs string |
| 16 | +} |
| 17 | + |
| 18 | +//nolint:golint, stylecheck |
| 19 | +const ( |
| 20 | + PROMETHEUS_BACKEND_NAME = "haproxy-controller_prometheus_http" |
| 21 | + PROMETHEUS_URL_PATH = "/metrics" |
| 22 | +) |
| 23 | + |
| 24 | +func (handler PrometheusEndpoint) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (err error) { |
| 25 | + if handler.PodNs == "" { |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + status := store.EMPTY |
| 30 | + var secret *store.Secret |
| 31 | + _, errBackend := h.BackendGet(PROMETHEUS_BACKEND_NAME) |
| 32 | + backendExists := errBackend == nil |
| 33 | + |
| 34 | + annSecret := annotations.String("prometheus-endpoint-auth-secret", k.ConfigMaps.Main.Annotations) |
| 35 | + var secretExists, secretChanged, userListChanged bool |
| 36 | + userListExists, _ := h.UserListExistsByGroup("haproxy-controller-prometheus") |
| 37 | + |
| 38 | + // Does the secret exist in store ? ... |
| 39 | + if annSecret != "" { |
| 40 | + secretFQN := strings.Split(annSecret, "/") |
| 41 | + if len(secretFQN) == 2 { |
| 42 | + ns := k.Namespaces[secretFQN[0]] |
| 43 | + if ns != nil { |
| 44 | + secret = ns.Secret[secretFQN[1]] |
| 45 | + secretExists = secret != nil && secret.Status != store.DELETED |
| 46 | + secretChanged = secret.Status == store.MODIFIED |
| 47 | + } |
| 48 | + } |
| 49 | + userListChanged = secretChanged || !userListExists |
| 50 | + } else { |
| 51 | + userListChanged = userListExists |
| 52 | + } |
| 53 | + |
| 54 | + if !backendExists { |
| 55 | + status = store.ADDED |
| 56 | + } |
| 57 | + |
| 58 | + if !userListChanged && status == store.EMPTY && (!secretExists || (secretExists && secret.Status == store.EMPTY)) { |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + prometheusSvcName := "prometheus" |
| 63 | + svc := &store.Service{ |
| 64 | + Namespace: handler.PodNs, |
| 65 | + Name: prometheusSvcName, |
| 66 | + Status: status, |
| 67 | + Annotations: k.ConfigMaps.Main.Annotations, |
| 68 | + Ports: []store.ServicePort{ |
| 69 | + { |
| 70 | + Name: "http", |
| 71 | + Protocol: "http", |
| 72 | + Port: 8765, |
| 73 | + Status: status, |
| 74 | + }, |
| 75 | + }, |
| 76 | + Faked: true, |
| 77 | + } |
| 78 | + endpoints := &store.Endpoints{ |
| 79 | + Namespace: handler.PodNs, |
| 80 | + Service: prometheusSvcName, |
| 81 | + SliceName: prometheusSvcName, |
| 82 | + Status: status, |
| 83 | + Ports: map[string]*store.PortEndpoints{ |
| 84 | + "http": { |
| 85 | + Port: int64(h.Env.ControllerPort), |
| 86 | + Addresses: map[string]struct{}{"127.0.0.1": {}}, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + if status != store.EMPTY { |
| 92 | + k.EventService(k.GetNamespace(svc.Namespace), svc) |
| 93 | + k.EventEndpoints(k.GetNamespace(endpoints.Namespace), endpoints, func(*store.RuntimeBackend, bool) error { return nil }) |
| 94 | + } |
| 95 | + |
| 96 | + ing := &store.Ingress{ |
| 97 | + Status: status, |
| 98 | + Faked: true, |
| 99 | + IngressCore: store.IngressCore{ |
| 100 | + Namespace: handler.PodNs, |
| 101 | + Name: "prometheus", |
| 102 | + Rules: map[string]*store.IngressRule{"": { |
| 103 | + Paths: map[string]*store.IngressPath{ |
| 104 | + PROMETHEUS_URL_PATH: { |
| 105 | + SvcNamespace: svc.Namespace, |
| 106 | + SvcName: svc.Name, |
| 107 | + Path: PROMETHEUS_URL_PATH, |
| 108 | + SvcPortString: "http", |
| 109 | + PathTypeMatch: store.PATH_TYPE_IMPLEMENTATION_SPECIFIC, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }}, |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + if secretExists { |
| 117 | + ing.Annotations = map[string]string{ |
| 118 | + "auth-type": "basic-auth", |
| 119 | + "auth-secret": annSecret, |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if userListChanged || status != store.EMPTY || secretExists && secret.Status != store.EMPTY { |
| 124 | + k.EventIngress(k.GetNamespace(ing.Namespace), ing) |
| 125 | + } |
| 126 | + |
| 127 | + instance.ReloadIf(status != store.EMPTY, "creation/modification of prometheus endpoint") |
| 128 | + |
| 129 | + return nil |
| 130 | +} |
0 commit comments