Skip to content

Commit 4c3af70

Browse files
committed
Drop unknown services and unnamed services.
This filters the kustomize parsed resources, to drop ones where we were unable to extract a service name from the labels on resources. This also drops services where the extracted label name is not in the services for an environment.
1 parent f1abd4c commit 4c3af70

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

pkg/httpapi/application.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,20 @@ func parseServicesFromResources(env *environment, res []*parser.Resource) ([]res
7373
services := []responseService{}
7474
for k, v := range serviceImages {
7575
svc := env.findService(k)
76+
// If the extracted service name is not known within this environment,
77+
// this drops it from the response.
78+
if svc == nil {
79+
continue
80+
}
7681
svcRepo := ""
7782
if svc != nil {
7883
svcRepo = svc.SourceURL
7984
}
85+
// This skips services where we haven't extracted a name from the
86+
// labels.
87+
if k == "" {
88+
continue
89+
}
8090
rs := responseService{
8191
Name: k,
8292
Images: keys(v),

pkg/httpapi/application_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,81 @@ func TestParseServicesFromResourcesReturnsSetOfImages(t *testing.T) {
170170
t.Fatalf("parseServicesFromResources got\n%s", diff)
171171
}
172172
}
173+
174+
func TestParseServicesFromResourcesIgnoresEmptyServices(t *testing.T) {
175+
res := []*parser.Resource{
176+
{
177+
Group: "apps", Version: "v1", Kind: "Deployment", Name: "go-demo-http",
178+
Labels: map[string]string{},
179+
Images: []string{"bigkevmcd/go-demo:876ecb3"},
180+
},
181+
{
182+
Version: "v1", Kind: "Service", Name: "go-demo-http",
183+
Labels: map[string]string{},
184+
},
185+
}
186+
env := &environment{
187+
Name: "test-env",
188+
Cluster: "https://cluster.local",
189+
Apps: []*application{
190+
{
191+
Name: "my-app",
192+
Services: []service{
193+
{
194+
Name: "go-demo",
195+
SourceURL: testSourceURL,
196+
},
197+
},
198+
},
199+
},
200+
}
201+
202+
svcs, err := parseServicesFromResources(env, res)
203+
if err != nil {
204+
t.Fatal(err)
205+
}
206+
207+
want := []responseService{}
208+
if diff := cmp.Diff(want, svcs); diff != "" {
209+
t.Fatalf("parseServicesFromResources got\n%s", diff)
210+
}
211+
}
212+
213+
func TestParseServicesFromResourcesIgnoresUnknownServices(t *testing.T) {
214+
res := []*parser.Resource{
215+
{
216+
Group: "apps", Version: "v1", Kind: "Deployment", Name: "go-demo-http",
217+
Labels: map[string]string{
218+
nameLabel: "unknown",
219+
partOfLabel: "unknown",
220+
},
221+
Images: []string{"bigkevmcd/go-demo:876ecb3"},
222+
},
223+
}
224+
225+
env := &environment{
226+
Name: "test-env",
227+
Cluster: "https://cluster.local",
228+
Apps: []*application{
229+
{
230+
Name: "my-app",
231+
Services: []service{
232+
{
233+
Name: "go-demo",
234+
SourceURL: testSourceURL,
235+
},
236+
},
237+
},
238+
},
239+
}
240+
241+
svcs, err := parseServicesFromResources(env, res)
242+
if err != nil {
243+
t.Fatal(err)
244+
}
245+
246+
want := []responseService{}
247+
if diff := cmp.Diff(want, svcs); diff != "" {
248+
t.Fatalf("parseServicesFromResources got\n%s", diff)
249+
}
250+
}

0 commit comments

Comments
 (0)