Skip to content

Commit

Permalink
feat: support extraction of host string slices
Browse files Browse the repository at this point in the history
status:
  url:
    - x
    - y

status:
  url: x
  • Loading branch information
aslakknutsen committed Jul 10, 2024
1 parent 3651d53 commit 3c32e28
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
14 changes: 11 additions & 3 deletions pkg/resource/authconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,22 @@ func (k *expressionHostExtractor) Extract(target *unstructured.Unstructured) []s
hosts := []string{}

for _, path := range k.paths {
host, found, err := unstructured.NestedString(target.Object, strings.Split(path, ".")...)
foundHost, found, err := unstructured.NestedString(target.Object, strings.Split(path, ".")...)
if err == nil && found {
// TODO log err?
parsedURL, err := url.Parse(host)
parsedURL, err := url.Parse(foundHost)
if err == nil {
hosts = append(hosts, parsedURL.Host)
}
}
foundHosts, found, err := unstructured.NestedStringSlice(target.Object, strings.Split(path, ".")...)

Check failure on line 152 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

assignments should only be cuddled with other assignments (wsl)

Check failure on line 152 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / golangci-lint

assignments should only be cuddled with other assignments (wsl)
if err == nil && found {

Check failure on line 153 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / test

only one cuddle assignment allowed before if statement (wsl)

Check failure on line 153 in pkg/resource/authconfig.go

View workflow job for this annotation

GitHub Actions / golangci-lint

only one cuddle assignment allowed before if statement (wsl)
for _, foundHost := range foundHosts {
parsedURL, err := url.Parse(foundHost)
if err == nil {
hosts = append(hosts, parsedURL.Host)
}
}
}
}

u := unique(hosts)
Expand Down
20 changes: 19 additions & 1 deletion pkg/resource/authconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var _ = Describe("AuthConfig functions", Label(labels.Unit), func() {

Context("Host extraction", func() {

It("should extract host from unstructured via paths", func() {
It("should extract host from unstructured via paths as string", func() {
// given
extractor := resource.NewExpressionHostExtractor([]string{"status.url"})
target := unstructured.Unstructured{
Expand All @@ -30,6 +30,24 @@ var _ = Describe("AuthConfig functions", Label(labels.Unit), func() {
Expect(hosts).To(HaveExactElements("test.com"))
})

It("should extract host from unstructured via paths as slice of strings", func() {
// given
extractor := resource.NewExpressionHostExtractor([]string{"status.url"})
target := unstructured.Unstructured{
Object: map[string]interface{}{
"status": map[string]interface{}{
"url": []interface{}{"http://test.com", "http://test2.com"},
},
},
}

// when
hosts := extractor.Extract(&target)

// then
Expect(hosts).To(ContainElements("test.com", "test2.com"))
})

})

})

0 comments on commit 3c32e28

Please sign in to comment.