Skip to content

Commit

Permalink
Remove dead code and add test for SeparateSANs
Browse files Browse the repository at this point in the history
  • Loading branch information
eaudetcobello committed Mar 22, 2024
1 parent 326284b commit d6b8136
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/k8s/pkg/utils/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ package utils

import (
"net"
"strings"
)

func GetExtraSANsFromString(extraSANs string) []string {
// TODO: Add validation for the extraSANs
return strings.Split(extraSANs, ",")
}

func SeparateSANs(extraSANs []string) ([]net.IP, []string) {
var ipSANs []net.IP
var dnsSANs []string

for _, san := range extraSANs {
if san == "" {
continue
}

if ip := net.ParseIP(san); ip != nil {
ipSANs = append(ipSANs, ip)
} else {
Expand Down
31 changes: 31 additions & 0 deletions src/k8s/pkg/utils/certificate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package utils_test

import (
"testing"

"github.com/canonical/k8s/pkg/utils"

. "github.com/onsi/gomega"
)

func TestSeparateSANs(t *testing.T) {
tests := []string{"192.168.0.1", "::1", "cluster.local", "kubernetes.svc.local", "", "2001:db8:0:1:1:1:1:1"}

g := NewWithT(t)
gotIPs, gotDNSs := utils.SeparateSANs(tests)

// Convert cert.IPAddresses to a slice of string representations
ips := make([]string, len(gotIPs))
for i, ip := range gotIPs {
ips[i] = ip.String()
}

g.Expect(len(ips)).To(Equal(3))
g.Expect(ips).To(ContainElement("192.168.0.1"))
g.Expect(ips).To(ContainElement("::1"))
g.Expect(ips).To(ContainElement("2001:db8:0:1:1:1:1:1"))

g.Expect(len(gotDNSs)).To(Equal(2))
g.Expect(gotDNSs).To(ContainElement("cluster.local"))
g.Expect(gotDNSs).To(ContainElement("kubernetes.svc.local"))
}

0 comments on commit d6b8136

Please sign in to comment.