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 4 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},
}
}
16 changes: 16 additions & 0 deletions pkg/templates/danglingservice/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@
return []diagnostic.Diagnostic{{
Message: "service has no selector specified",
}}
} else {
// Check if Service is linked to Endpoints
for _, obj := range lintCtx.Objects() {

endpoints, ok := object.K8sObject.(*v1.Endpoints)
if !ok {
continue
}
if endpoints.Namespace != obj.K8sObject.GetNamespace() {
continue

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

View check run for this annotation

Codecov / codecov/patch

pkg/templates/danglingservice/template.go#L54-L55

Added lines #L54 - L55 were not covered by tests
}
if endpoints.Name == obj.GetK8sObjectName().Name {

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

View check run for this annotation

Codecov / codecov/patch

pkg/templates/danglingservice/template.go#L57

Added line #L57 was not covered by tests
// Found!
return nil

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

View check run for this annotation

Codecov / codecov/patch

pkg/templates/danglingservice/template.go#L59

Added line #L59 was not covered by tests
}
}
}

for _, ignoredLabel := range p.IgnoredLabels {
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(service2)

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
Loading