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

Check if Service has Endpoints for dangling service template #747

Closed
Closed
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
17 changes: 17 additions & 0 deletions pkg/lintcontext/mocks/endpoints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package mocks

import (
"testing"

"github.com/stretchr/testify/require"
coreV1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// AddMockEndpoints adds a mock Endpoint to LintContext
func (l *MockLintContext) AddMockEndpoints(t *testing.T, name string) {
require.NotEmpty(t, name)
l.objects[name] = &coreV1.Endpoints{
ObjectMeta: metaV1.ObjectMeta{Name: name},
}
}
14 changes: 14 additions & 0 deletions pkg/templates/danglingservice/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@
}
selector := service.Spec.Selector
if len(selector) == 0 {
// Check if Service is linked to Endpoints
for _, obj := range lintCtx.Objects() {
endpoints, ok := obj.K8sObject.(*v1.Endpoints)
if !ok {
continue
}
if endpoints.Namespace != object.K8sObject.GetNamespace() {
continue

Check warning on line 50 in pkg/templates/danglingservice/template.go

View check run for this annotation

Codecov / codecov/patch

pkg/templates/danglingservice/template.go#L50

Added line #L50 was not covered by tests
}
if endpoints.Name == object.GetK8sObjectName().Name {
// Found!
return nil

Check warning on line 54 in pkg/templates/danglingservice/template.go

View check run for this annotation

Codecov / codecov/patch

pkg/templates/danglingservice/template.go#L54

Added line #L54 was not covered by tests
}
}
return []diagnostic.Diagnostic{{
Message: "service has no selector specified",
}}
Expand Down
35 changes: 35 additions & 0 deletions pkg/templates/danglingservice/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

const (
endpoint1 = "endpoint1"
pod1 = "pod1"
pod2 = "pod2"
serviceNone = "service-matches-none"
Expand Down Expand Up @@ -52,6 +53,10 @@ func (s *DanglingServiceTestSuite) AddService(name string, podLabels map[string]
})
}

func (s *DanglingServiceTestSuite) AddEndpoints(name string) {
s.ctx.AddMockEndpoints(s.T(), name)
}

func (s *DanglingServiceTestSuite) AddDeploymentWithLabels(name string, labels map[string]string) {
s.ctx.AddMockDeployment(s.T(), name)
s.ctx.ModifyDeployment(s.T(), name, func(deployment *appsV1.Deployment) {
Expand Down Expand Up @@ -93,6 +98,36 @@ func (s *DanglingServiceTestSuite) TestNoDanglingServices() {
})
}

func (s *DanglingServiceTestSuite) TestNoDanglingServiceWithEndpoints() {
s.AddService(endpoint1, nil)
s.AddEndpoints(endpoint1)

s.Validate(s.ctx, []templates.TestCase{
{
Param: params.Params{},
Diagnostics: map[string][]diagnostic.Diagnostic{
endpoint1: {},
},
ExpectInstantiationError: false,
},
})
}

func (s *DanglingServiceTestSuite) TestDanglingServiceWithNoMatchingEndpoints() {
s.AddService(service1, nil)
s.AddEndpoints(endpoint1)

s.Validate(s.ctx, []templates.TestCase{
{
Param: params.Params{},
Diagnostics: map[string][]diagnostic.Diagnostic{
service1: {{Message: "service has no selector specified"}},
},
ExpectInstantiationError: false,
},
})
}

func (s *DanglingServiceTestSuite) TestOneServiceIsDangling() {
s.AddDeploymentWithLabels(pod2, labelselector2)
s.AddService(service1, labelselector1)
Expand Down
23 changes: 22 additions & 1 deletion tests/checks/dangling-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,25 @@ spec:
- name: 8080-tcp
port: 8080
selector:
app.kubernetes.io/name: app
app.kubernetes.io/name: app
---
kind: Endpoints
apiVersion: v1
metadata:
name: service-mt1
subsets:
- addresses:
- ip: 10.1.2.3
ports:
- name: https
port: 443
---
apiVersion: v1
kind: Service
metadata:
name: service-mt1
spec:
ports:
- name: https
port: 443
targetPort: 443
Loading