-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Philip Laine <[email protected]>
- Loading branch information
1 parent
3c8dcb8
commit 042c5cd
Showing
19 changed files
with
804 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
// Package dns contains DNS related functionality. | ||
package dns | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
var ( | ||
// localClusterServiceRegex is used to match the local cluster service format: | ||
localClusterServiceRegex = regexp.MustCompile(`^(?P<name>[^\.]+)\.(?P<namespace>[^\.]+)\.svc\.cluster\.local$`) | ||
) | ||
|
||
// IsServiceURL returns true if the give url complies with the service url format. | ||
func IsServiceURL(serviceURL string) bool { | ||
_, _, _, err := ParseServiceURL(serviceURL) | ||
return err == nil | ||
} | ||
|
||
// ParseServiceURL takes a serviceURL and parses it to find the service info for connecting to the cluster. The string is expected to follow the following format: | ||
// Example serviceURL: http://{SERVICE_NAME}.{NAMESPACE}.svc.cluster.local:{PORT}. | ||
func ParseServiceURL(serviceURL string) (string, string, int, error) { | ||
if serviceURL == "" { | ||
return "", "", 0, errors.New("service url cannot be empty") | ||
} | ||
parsedURL, err := url.Parse(serviceURL) | ||
if err != nil { | ||
return "", "", 0, err | ||
} | ||
if parsedURL.Port() == "" { | ||
return "", "", 0, errors.New("service url does not have a port") | ||
} | ||
remotePort, err := strconv.Atoi(parsedURL.Port()) | ||
if err != nil { | ||
return "", "", 0, err | ||
} | ||
matches := localClusterServiceRegex.FindStringSubmatch(parsedURL.Hostname()) | ||
if len(matches) != 3 { | ||
return "", "", 0, fmt.Errorf("invalid service url %s", serviceURL) | ||
} | ||
return matches[2], matches[1], remotePort, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
package dns | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestServiceURL(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
serviceURL string | ||
expectedErr string | ||
expectedNamespace string | ||
expectedName string | ||
expectedPort int | ||
}{ | ||
{ | ||
name: "correct service url", | ||
serviceURL: "http://foo.bar.svc.cluster.local:5000", | ||
expectedNamespace: "bar", | ||
expectedName: "foo", | ||
expectedPort: 5000, | ||
}, | ||
{ | ||
name: "invalid service url without port", | ||
serviceURL: "http://google.com", | ||
expectedErr: "service url does not have a port", | ||
}, | ||
{ | ||
name: "invalid service url with port", | ||
serviceURL: "http://google.com:3000", | ||
expectedErr: "invalid service url http://google.com:3000", | ||
}, | ||
{ | ||
name: "empty service url", | ||
serviceURL: "", | ||
expectedErr: "service url cannot be empty", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
isServiceURL := IsServiceURL(tt.serviceURL) | ||
namespace, name, port, err := ParseServiceURL(tt.serviceURL) | ||
if tt.expectedErr != "" { | ||
require.False(t, isServiceURL) | ||
require.EqualError(t, err, tt.expectedErr) | ||
return | ||
} | ||
require.True(t, isServiceURL) | ||
require.Equal(t, tt.expectedNamespace, namespace) | ||
require.Equal(t, tt.expectedName, name) | ||
require.Equal(t, tt.expectedPort, port) | ||
}) | ||
} | ||
} |
Oops, something went wrong.