Skip to content

Commit

Permalink
Refact controllerapi to use IPs from status of nginx CRD
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Feb 15, 2024
1 parent faf2433 commit fe33045
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 75 deletions.
68 changes: 17 additions & 51 deletions pkg/controllerapi/controllerapi.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package controllerapi

import (
"context"
"encoding/json"
"net/http"

"github.com/tsuru/nginx-operator/api/v1alpha1"
coreV1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
sigsk8sclient "sigs.k8s.io/controller-runtime/pkg/client"
)

Expand All @@ -17,54 +14,27 @@ 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 {
func rpaasTargetGroups(nginxInstance *v1alpha1.Nginx) []TargetGroup {
targetGroups := []TargetGroup{}
ips := []string{}

for _, service := range nginxInstance.Status.Services {
ips = append(ips, service.IPs...)
}

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
for _, ingress := range nginxInstance.Status.Ingresses {
ips = append(ips, ingress.IPs...)
}

namespace := svc.ObjectMeta.Namespace
serviceInstance := svc.Labels["rpaas.extensions.tsuru.io/instance-name"]
service := svc.Labels["rpaas.extensions.tsuru.io/service-name"]
teamOwner := svc.Labels["rpaas.extensions.tsuru.io/team-owner"]
for _, ip := range ips {
namespace := nginxInstance.ObjectMeta.Namespace
serviceInstance := nginxInstance.Labels["rpaas.extensions.tsuru.io/instance-name"]
service := nginxInstance.Labels["rpaas.extensions.tsuru.io/service-name"]
teamOwner := nginxInstance.Labels["rpaas.extensions.tsuru.io/team-owner"]

targetGroups = append(targetGroups, TargetGroup{
Targets: []string{
"http://" + svcIP + healthcheckPath,
"http://" + ip + healthcheckPath,
},
Labels: map[string]string{
"namespace": namespace,
Expand All @@ -78,7 +48,7 @@ func rpaasTargetGroups(svcMap map[sigsk8sclient.ObjectKey]*coreV1.Service, nginx
for _, host := range tls.Hosts {
targetGroups = append(targetGroups, TargetGroup{
Targets: []string{
"https://" + svcIP + healthcheckPath,
"https://" + ip + healthcheckPath,
},
Labels: map[string]string{
"namespace": namespace,
Expand All @@ -103,22 +73,18 @@ type TargetGroup struct {

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{})
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)...)
targetGroups = append(targetGroups, rpaasTargetGroups(&nginxInstance)...)
}

w.Header().Set("Content-Type", "application/json")
Expand Down
32 changes: 8 additions & 24 deletions pkg/controllerapi/controllerapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tsuru/nginx-operator/api/v1alpha1"
coreV1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

Expand All @@ -23,6 +22,12 @@ func TestPrometheusDiscover(t *testing.T) {
ObjectMeta: metaV1.ObjectMeta{
Name: "test",
Namespace: "default",
Labels: map[string]string{
"nginx.tsuru.io/app": "nginx",
"rpaas.extensions.tsuru.io/instance-name": "test",
"rpaas.extensions.tsuru.io/service-name": "rpaasv2",
"rpaas.extensions.tsuru.io/team-owner": "team01",
},
},
Spec: v1alpha1.NginxSpec{
TLS: []v1alpha1.NginxTLS{
Expand All @@ -38,35 +43,14 @@ func TestPrometheusDiscover(t *testing.T) {
Services: []v1alpha1.ServiceStatus{
{
Name: "test",
},
},
},
}

svc1 := &coreV1.Service{
ObjectMeta: metaV1.ObjectMeta{
Name: "test",
Namespace: "default",
Labels: map[string]string{
"nginx.tsuru.io/app": "nginx",
"rpaas.extensions.tsuru.io/instance-name": "test",
"rpaas.extensions.tsuru.io/service-name": "rpaasv2",
"rpaas.extensions.tsuru.io/team-owner": "team01",
},
},
Status: coreV1.ServiceStatus{
LoadBalancer: coreV1.LoadBalancerStatus{
Ingress: []coreV1.LoadBalancerIngress{
{
IP: "1.1.1.1",
},
IPs: []string{"1.1.1.1"},
},
},
},
}

scheme := extensionsruntime.NewScheme()
client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(nginx1, svc1).Build()
client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(nginx1).Build()
api := controllerapi.New(client)

w := httptest.NewRecorder()
Expand Down

0 comments on commit fe33045

Please sign in to comment.