Skip to content

Commit

Permalink
sort labels and tags before creating netboxip
Browse files Browse the repository at this point in the history
  • Loading branch information
d-honeybadger committed Jun 29, 2022
1 parent ec8544e commit 1f277b5
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"net/netip"
"sort"
"strings"

netboxctrl "github.com/digitalocean/netbox-ip-controller"
Expand Down Expand Up @@ -61,12 +62,14 @@ type NetBoxIPConfig struct {
// given as input
func CreateNetBoxIPs(ips []string, config NetBoxIPConfig) (*IPs, error) {

labels := []string{fmt.Sprintf("namespace: %s", config.Object.GetNamespace())}
labels := make([]string, 0)
for key, value := range config.Object.GetLabels() {
if config.ReconcilerLabels[key] {
labels = append(labels, fmt.Sprintf("%s: %s", key, value))
}
}
sort.Strings(labels)
labels = append([]string{fmt.Sprintf("namespace: %s", config.Object.GetNamespace())}, labels...)

var tags []v1beta1.Tag
for _, tag := range config.ReconcilerTags {
Expand All @@ -75,6 +78,7 @@ func CreateNetBoxIPs(ips []string, config NetBoxIPConfig) (*IPs, error) {
Slug: tag.Slug,
})
}
sort.Slice(tags, func(i, j int) bool { return tags[i].Name < tags[j].Name })

var outputIPs IPs

Expand Down
143 changes: 143 additions & 0 deletions internal/controller/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package controller

import (
"net/netip"
"testing"

netboxctrl "github.com/digitalocean/netbox-ip-controller"
netboxcrd "github.com/digitalocean/netbox-ip-controller/api/netbox"
"github.com/digitalocean/netbox-ip-controller/api/netbox/v1beta1"
"github.com/digitalocean/netbox-ip-controller/internal/netbox"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

func TestCreateNetBoxIPs(t *testing.T) {
tests := []struct {
name string
ips []string
config NetBoxIPConfig
expectedIPs *IPs
}{{
name: "labels should be sorted",
ips: []string{"192.168.0.1"},
config: NetBoxIPConfig{
Object: &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "testpod",
Namespace: "testnamespace",
UID: types.UID("abc123"),
Labels: map[string]string{
"c": "foo",
"b": "bar",
"a": "baz",
"irrelevant": "",
},
},
},
ReconcilerLabels: map[string]bool{"a": true, "b": true, "c": true},
},
expectedIPs: &IPs{
IPv4: &v1beta1.NetBoxIP{
TypeMeta: metav1.TypeMeta{
Kind: netboxcrd.NetBoxIPKind,
APIVersion: "v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "pod-abc123-ipv4",
Namespace: "testnamespace",
Labels: map[string]string{
netboxctrl.NameLabel: "testpod",
},
Finalizers: []string{netboxctrl.IPFinalizer},
},
Spec: v1beta1.NetBoxIPSpec{
Address: netip.AddrFrom4([4]byte{192, 168, 0, 1}),
Description: "namespace: testnamespace, a: baz, b: bar, c: foo",
},
},
},
}, {
name: "tags should be sorted",
ips: []string{"192.168.0.1"},
config: NetBoxIPConfig{
Object: &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "testpod",
Namespace: "testnamespace",
UID: types.UID("abc123"),
},
},
ReconcilerTags: []netbox.Tag{{
Name: "ytest",
Slug: "1slug",
}, {
Name: "xtest",
Slug: "2slug",
}, {
Name: "ztest",
Slug: "3slug",
}},
ReconcilerLabels: map[string]bool{"a": true, "b": true, "c": true},
},
expectedIPs: &IPs{
IPv4: &v1beta1.NetBoxIP{
TypeMeta: metav1.TypeMeta{
Kind: netboxcrd.NetBoxIPKind,
APIVersion: "v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "pod-abc123-ipv4",
Namespace: "testnamespace",
Labels: map[string]string{
netboxctrl.NameLabel: "testpod",
},
Finalizers: []string{netboxctrl.IPFinalizer},
},
Spec: v1beta1.NetBoxIPSpec{
Address: netip.AddrFrom4([4]byte{192, 168, 0, 1}),
Tags: []v1beta1.Tag{{
Name: "xtest",
Slug: "2slug",
}, {
Name: "ytest",
Slug: "1slug",
}, {
Name: "ztest",
Slug: "3slug",
}},
Description: "namespace: testnamespace",
},
},
},
}}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ips, err := CreateNetBoxIPs(test.ips, test.config)
if err != nil {
t.Errorf("expected no error, got %q", err)
}

if diff := cmp.Diff(
test.expectedIPs,
ips,
cmpopts.IgnoreFields(metav1.ObjectMeta{}, "ResourceVersion"),
cmp.Comparer(func(x, y netip.Addr) bool { return x.Compare(y) == 0 }),
); diff != "" {
t.Errorf("IPs (-want, +got)\n%s", diff)
}
})
}
}

0 comments on commit 1f277b5

Please sign in to comment.