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

Do not target services with port that does not exist #458

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Unreleased section should follow [Release Toolkit](https://github.com/newrelic/r

## Unreleased

### Enhancement
- Skip prometheus services with non-existing port

## v2.19.0 - 2023-11-02

### 🚀 Enhancements
Expand Down
13 changes: 11 additions & 2 deletions internal/pkg/endpoints/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,16 @@ func serviceTargets(s *corev1.Service) []Target {
return nil
}

availablePorts := make(map[string]bool)
for _, port := range s.Spec.Ports {
availablePorts[fmt.Sprintf("%d", port.Port)] = true
}

if port != "" {
if !availablePorts[port] {
klog.WithError(err).Warnf("Port %s is not exposed on service %s/%s", port, s.Namespace, s.Name)
return nil
}
u := url.URL{
Scheme: scheme,
Host: net.JoinHostPort(fmt.Sprintf("%s.%s.svc", s.Name, s.Namespace), port),
Expand All @@ -356,10 +365,10 @@ func serviceTargets(s *corev1.Service) []Target {

// No port specified so return a target for each Port defined for the Service.
var targets []Target
for _, port := range s.Spec.Ports {
for port := range availablePorts {
u := url.URL{
Scheme: scheme,
Host: net.JoinHostPort(fmt.Sprintf("%s.%s.svc", s.Name, s.Namespace), fmt.Sprintf("%d", port.Port)),
Host: net.JoinHostPort(fmt.Sprintf("%s.%s.svc", s.Name, s.Namespace), port),
Path: path,
RawQuery: query,
}
Expand Down
37 changes: 37 additions & 0 deletions internal/pkg/endpoints/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,13 @@ func populateFakeEndpointsDataSinglePort(clientset *fake.Clientset) error {
"prometheus.io/port": "1",
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Port: 1,
},
},
},
}

_, err := clientset.CoreV1().Services("test-ns").Create(context.TODO(), s, metav1.CreateOptions{})
Expand Down Expand Up @@ -1640,6 +1647,36 @@ func TestServiceTargetsInvalidPortAnnotaion(t *testing.T) {
)
}

func TestServiceTargetsNonExistingPort(t *testing.T) {
t.Parallel()

assert.ElementsMatch(
t,
serviceTargets(&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "my-service",
Namespace: "test-ns",
Annotations: map[string]string{
"prometheus.io/port": "5000",
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "http-app",
Port: 80,
},
{
Name: "http-app2",
Port: 8080,
},
},
},
}),
[]Target{},
)
}

func TestServiceTargetsNoPort(t *testing.T) {
t.Parallel()

Expand Down
Loading