-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working on a internal api to provide way to integrate with prometheus…
… blackbox
- Loading branch information
Showing
2 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package controllerapi | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/tsuru/nginx-operator/api/v1alpha1" | ||
sigsk8sclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
coreV1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | ||
) | ||
|
||
const healthcheckPath = "/_nginx_healthcheck" | ||
|
||
type prometheusDiscoverHandler struct { | ||
client sigsk8sclient.Client | ||
} | ||
|
||
func (h *prometheusDiscoverHandler) svcMap(ctx context.Context) (map[sigsk8sclient.ObjectKey]*coreV1.Service, error) { | ||
svcMap := map[sigsk8sclient.ObjectKey]*coreV1.Service{} | ||
allNginxServices := &coreV1.ServiceList{} | ||
err := h.client.List(ctx, allNginxServices, &sigsk8sclient.ListOptions{ | ||
LabelSelector: labels.SelectorFromSet(labels.Set{ | ||
"nginx.tsuru.io/app": "nginx", | ||
}), | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for i, svc := range allNginxServices.Items { | ||
svcMap[sigsk8sclient.ObjectKeyFromObject(&svc)] = &allNginxServices.Items[i] | ||
} | ||
|
||
return svcMap, nil | ||
} | ||
|
||
func rpaasTargetGroups(svcMap map[sigsk8sclient.ObjectKey]*coreV1.Service, nginxInstance *v1alpha1.Nginx) []TargetGroup { | ||
targetGroups := []TargetGroup{} | ||
|
||
for _, service := range nginxInstance.Status.Services { | ||
|
||
svc := svcMap[sigsk8sclient.ObjectKey{ | ||
Namespace: nginxInstance.Namespace, | ||
Name: service.Name, | ||
}] | ||
|
||
if svc == nil { | ||
continue | ||
} | ||
|
||
if len(svc.Status.LoadBalancer.Ingress) == 0 { | ||
continue | ||
} | ||
|
||
svcIP := svc.Status.LoadBalancer.Ingress[0].IP | ||
|
||
serviceInstance := svc.Labels["rpaas.extensions.tsuru.io/instance-name"] | ||
service := svc.Labels["rpaas.extensions.tsuru.io/service-name"] | ||
|
||
targetGroups = append(targetGroups, TargetGroup{ | ||
Targets: []string{ | ||
"http://" + svcIP + healthcheckPath, | ||
}, | ||
Labels: map[string]string{ | ||
"service_instance": serviceInstance, | ||
"service": service, | ||
}, | ||
}) | ||
|
||
for _, tls := range nginxInstance.Spec.TLS { | ||
for _, host := range tls.Hosts { | ||
targetGroups = append(targetGroups, TargetGroup{ | ||
Targets: []string{ | ||
"https://" + svcIP + healthcheckPath, | ||
}, | ||
Labels: map[string]string{ | ||
"service_instance": serviceInstance, | ||
"service": service, | ||
"servername": host, | ||
}, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return targetGroups | ||
} | ||
|
||
// TargetGroup is a collection of related hosts that prometheus monitors | ||
type TargetGroup struct { | ||
Targets []string `json:"targets"` | ||
Labels map[string]string `json:"labels"` | ||
} | ||
|
||
func (h *prometheusDiscoverHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
svcMap, err := h.svcMap(ctx) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
allNginx := &v1alpha1.NginxList{} | ||
|
||
err = h.client.List(ctx, allNginx, &sigsk8sclient.ListOptions{}) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
targetGroups := []TargetGroup{} | ||
for _, nginxInstance := range allNginx.Items { | ||
targetGroups = append(targetGroups, rpaasTargetGroups(svcMap, &nginxInstance)...) | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
encoder := json.NewEncoder(w) | ||
encoder.SetIndent(" ", " ") | ||
err = encoder.Encode(targetGroups) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
} | ||
|
||
func New(client sigsk8sclient.Client) http.Handler { | ||
mux := http.NewServeMux() | ||
mux.Handle("/v1/prometheus/discover", &prometheusDiscoverHandler{client: client}) | ||
|
||
return mux | ||
} |