Skip to content

Commit

Permalink
dnspolicy: various fixes
Browse files Browse the repository at this point in the history
* Remove unnecessary common functions
* Return empty array for GetRulesHostnames
* Add additional unit tests for k8s utils
  • Loading branch information
mikenairn committed Feb 9, 2024
1 parent bd00273 commit 9b4301d
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 26 deletions.
3 changes: 1 addition & 2 deletions api/v1alpha1/dnspolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ func (p *DNSPolicy) GetWrappedNamespace() gatewayapiv1.Namespace {
}

func (p *DNSPolicy) GetRulesHostnames() []string {
//TODO implement me
panic("implement me")
return make([]string, 0)
}

func (p *DNSPolicy) GetTargetRef() gatewayapiv1alpha2.PolicyTargetReference {
Expand Down
6 changes: 3 additions & 3 deletions controllers/dns_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func (dh *dnsHelper) getDNSHealthCheckProbes(ctx context.Context, gateway *gatew
return nil, err
}

return common.MapErr(list.Items, func(obj kuadrantdnsv1alpha1.DNSHealthCheckProbe) (*kuadrantdnsv1alpha1.DNSHealthCheckProbe, error) {
return &obj, nil
})
return common.Map(list.Items, func(obj kuadrantdnsv1alpha1.DNSHealthCheckProbe) *kuadrantdnsv1alpha1.DNSHealthCheckProbe {
return &obj
}), nil
}
3 changes: 2 additions & 1 deletion controllers/dnspolicy_healthchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"context"
"fmt"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -98,7 +99,7 @@ func (r *DNSPolicyReconciler) deleteUnexpectedGatewayHealthCheckProbes(ctx conte
return err
}
for i, p := range existingProbes.Items {
if !common.Contains(expectedProbes, func(expectedProbe *kuadrantdnsv1alpha1.DNSHealthCheckProbe) bool {
if !slices.ContainsFunc(expectedProbes, func(expectedProbe *kuadrantdnsv1alpha1.DNSHealthCheckProbe) bool {
return expectedProbe.Name == p.Name && expectedProbe.Namespace == p.Namespace
}) {
if err := r.Client().Delete(ctx, &existingProbes.Items[i]); err != nil {
Expand Down
20 changes: 0 additions & 20 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ func Find[T any](slice []T, match func(T) bool) (*T, bool) {
return nil, false
}

func Contains[T any](slice []T, match func(T) bool) bool {
_, ok := Find(slice, match)
return ok
}

// Map applies the given mapper function to each element in the input slice and returns a new slice with the results.
func Map[T, U any](slice []T, f func(T) U) []U {
arr := make([]U, len(slice))
Expand All @@ -132,21 +127,6 @@ func Map[T, U any](slice []T, f func(T) U) []U {
return arr
}

func MapErr[T, R any](slice []T, f func(T) (R, error)) ([]R, error) {
result := make([]R, len(slice))

for i, elem := range slice {
mapped, err := f(elem)
if err != nil {
return nil, err
}

result[i] = mapped
}

return result, nil
}

// Filter filters the input slice using the given predicate function and returns a new slice with the results.
func Filter[T any](slice []T, f func(T) bool) []T {
arr := make([]T, 0)
Expand Down
39 changes: 39 additions & 0 deletions pkg/common/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package common

import (
"errors"
"testing"

gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
)

func TestIsTargetNotFound(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{
name: "err is NewErrTargetNotFound",
err: NewErrTargetNotFound("foo", gatewayapiv1alpha2.PolicyTargetReference{}, errors.New("bar")),
want: true,
},
{
name: "err is NewErrInvalid",
err: NewErrInvalid("foo", errors.New("bar")),
want: false,
},
{
name: "err is standard error",
err: errors.New("bar"),
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsTargetNotFound(tt.err); got != tt.want {
t.Errorf("IsTargetNotFound() = %v, want %v", got, tt.want)
}
})
}
}
101 changes: 101 additions & 0 deletions pkg/common/k8s_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,3 +852,104 @@ func TestFindDeploymentStatusCondition(t *testing.T) {
})
}
}

func TestHasLabel(t *testing.T) {
testCases := []struct {
name string
obj metav1.Object
label string
expect bool
}{
{
name: "existing label found",
obj: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-object",
Labels: map[string]string{
"test-key": "value",
},
},
},
label: "test-key",
expect: true,
},
{
name: "existing label not found",
obj: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-object",
Labels: map[string]string{
"test-fail": "value",
},
},
},
label: "test-key",
expect: false,
},
{
name: "no labels",
obj: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-object",
Labels: nil,
},
},
label: "test-key",
expect: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := HasLabel(tc.obj, tc.label)
if got != tc.expect {
t.Errorf("expected '%v' got '%v'", tc.expect, got)
}
})
}
}

func TestGetLabel(t *testing.T) {
testCases := []struct {
name string
obj metav1.Object
label string
expect string
}{
{
name: "existing label found",
obj: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-object",
Labels: map[string]string{
"test-key": "value",
},
},
},
label: "test-key",
expect: "value",
},
{
name: "existing label not found",
obj: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-object",
Labels: map[string]string{
"test-fail": "value",
},
},
},
label: "test-key",
expect: "",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := GetLabel(tc.obj, tc.label)
if got != tc.expect {
t.Errorf("expected '%v' got '%v'", tc.expect, got)
}
})
}
}

0 comments on commit 9b4301d

Please sign in to comment.