Skip to content

Commit

Permalink
Fix golang-ci lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ReToCode committed Mar 15, 2024
1 parent cb21626 commit 87020aa
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 76 deletions.
4 changes: 2 additions & 2 deletions pkg/reconciler/ingress/config/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ package config
import (
"testing"

. "knative.dev/pkg/configmap/testing"
ktesting "knative.dev/pkg/configmap/testing"
)

func TestGateway(t *testing.T) {
cm, example := ConfigMapsFromTestFile(t, GatewayConfigName)
cm, example := ktesting.ConfigMapsFromTestFile(t, GatewayConfigName)

if _, err := NewGatewayFromConfigMap(cm); err != nil {
t.Error("NewContourFromConfigMap(actual) =", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/ingress/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ import (
_ "knative.dev/networking/pkg/client/injection/informers/networking/v1alpha1/ingress/fake"
_ "knative.dev/pkg/client/injection/kube/informers/core/v1/endpoints/fake"

. "knative.dev/pkg/reconciler/testing"
ktesting "knative.dev/pkg/reconciler/testing"
)

func TestNew(t *testing.T) {
ctx, _ := SetupFakeContext(t)
ctx, _ := ktesting.SetupFakeContext(t)

c := NewController(ctx, configmap.NewStaticWatcher(&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Expand Down
80 changes: 40 additions & 40 deletions pkg/reconciler/ingress/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientgotesting "k8s.io/client-go/testing"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"

fakegwapiclientset "knative.dev/net-gateway-api/pkg/client/injection/client/fake"
"knative.dev/net-gateway-api/pkg/reconciler/ingress/config"
Expand All @@ -46,8 +46,8 @@ import (

gatewayapi "sigs.k8s.io/gateway-api/apis/v1beta1"

. "knative.dev/net-gateway-api/pkg/reconciler/testing"
. "knative.dev/pkg/reconciler/testing"
gwtesting "knative.dev/net-gateway-api/pkg/reconciler/testing"
ktesting "knative.dev/pkg/reconciler/testing"
)

var (
Expand All @@ -57,9 +57,9 @@ var (
privateSvc = network.GetServiceHostname(privateName, testNamespace)

gatewayRef = gatewayapi.ParentReference{
Group: (*gatewayapi.Group)(pointer.String("gateway.networking.k8s.io")),
Kind: (*gatewayapi.Kind)(pointer.String("Gateway")),
Namespace: (*gatewayapi.Namespace)(pointer.String("istio-system")),
Group: (*gatewayapi.Group)(ptr.To("gateway.networking.k8s.io")),
Kind: (*gatewayapi.Kind)(ptr.To("Gateway")),
Namespace: (*gatewayapi.Namespace)(ptr.To("istio-system")),
Name: gatewayapi.ObjectName("istio-gateway"),
}
)
Expand Down Expand Up @@ -137,7 +137,7 @@ var (

// TODO: Add more tests - e.g. invalid ingress, delete ingress, etc.
func TestReconcile(t *testing.T) {
table := TableTest{{
table := ktesting.TableTest{{
Name: "bad workqueue key",
Key: "too/many/parts",
}, {
Expand Down Expand Up @@ -187,8 +187,8 @@ func TestReconcile(t *testing.T) {
Patch: []byte(`{"metadata":{"finalizers":["ingresses.networking.internal.knative.dev"],"resourceVersion":""}}`),
}},
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "name" finalizers`),
Eventf(corev1.EventTypeNormal, "Created", "Created HTTPRoute \"example.com\""),
ktesting.Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "name" finalizers`),
ktesting.Eventf(corev1.EventTypeNormal, "Created", "Created HTTPRoute \"example.com\""),
},
}, {
Name: "reconcile ready ingress",
Expand All @@ -200,7 +200,7 @@ func TestReconcile(t *testing.T) {
// no extra update
}}

table.Test(t, MakeFactory(func(ctx context.Context, listers *Listers, cmw configmap.Watcher) controller.Reconciler {
table.Test(t, gwtesting.MakeFactory(func(ctx context.Context, listers *gwtesting.Listers, _ configmap.Watcher) controller.Reconciler {
r := &Reconciler{
gwapiclient: fakegwapiclientset.Get(ctx),
// Listers index properties about resources
Expand All @@ -225,7 +225,7 @@ func TestReconcile(t *testing.T) {
}

func TestReconcileProberNotReady(t *testing.T) {
table := TableTest{{
table := ktesting.TableTest{{
Name: "first reconcile basic ingress wth prober",
Key: "ns/name",
Objects: append([]runtime.Object{
Expand All @@ -246,12 +246,12 @@ func TestReconcileProberNotReady(t *testing.T) {
Patch: []byte(`{"metadata":{"finalizers":["ingresses.networking.internal.knative.dev"],"resourceVersion":""}}`),
}},
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "name" finalizers`),
Eventf(corev1.EventTypeNormal, "Created", "Created HTTPRoute \"example.com\""),
ktesting.Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "name" finalizers`),
ktesting.Eventf(corev1.EventTypeNormal, "Created", "Created HTTPRoute \"example.com\""),
},
}}

table.Test(t, MakeFactory(func(ctx context.Context, listers *Listers, cmw configmap.Watcher) controller.Reconciler {
table.Test(t, gwtesting.MakeFactory(func(ctx context.Context, listers *gwtesting.Listers, _ configmap.Watcher) controller.Reconciler {
r := &Reconciler{
gwapiclient: fakegwapiclientset.Get(ctx),
// Listers index properties about resources
Expand All @@ -277,7 +277,7 @@ func TestReconcileTLS(t *testing.T) {
secretName := "name-WE-STICK-A-LONG-UID-HERE"
nsName := "ns"
deleteTime := time.Now().Add(-10 * time.Second)
table := TableTest{{
table := ktesting.TableTest{{
Name: "Happy TLS",
Key: "ns/name",
Objects: []runtime.Object{
Expand Down Expand Up @@ -312,8 +312,8 @@ func TestReconcileTLS(t *testing.T) {
}),
}},
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "name" finalizers`),
Eventf(corev1.EventTypeNormal, "Created", `Created HTTPRoute "example.com"`),
ktesting.Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "name" finalizers`),
ktesting.Eventf(corev1.EventTypeNormal, "Created", `Created HTTPRoute "example.com"`),
},
}, {
Name: "Already Configured",
Expand Down Expand Up @@ -390,10 +390,10 @@ func TestReconcileTLS(t *testing.T) {
}),
}},
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "name" finalizers`),
Eventf(corev1.EventTypeNormal, "Created", `Created HTTPRoute "example.com"`),
Eventf(corev1.EventTypeWarning, "GatewayMissing", `Unable to update Gateway istio-system/istio-gateway`),
Eventf(corev1.EventTypeWarning, "InternalError", `Gateway istio-system/istio-gateway does not exist: gateway.gateway.networking.k8s.io "istio-gateway" not found`),
ktesting.Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "name" finalizers`),
ktesting.Eventf(corev1.EventTypeNormal, "Created", `Created HTTPRoute "example.com"`),
ktesting.Eventf(corev1.EventTypeWarning, "GatewayMissing", `Unable to update Gateway istio-system/istio-gateway`),
ktesting.Eventf(corev1.EventTypeWarning, "InternalError", `Gateway istio-system/istio-gateway does not exist: gateway.gateway.networking.k8s.io "istio-gateway" not found`),
},
}, {
Name: "TLS ingress with httpOption redirected",
Expand Down Expand Up @@ -432,13 +432,13 @@ func TestReconcileTLS(t *testing.T) {
Patch: []byte(`{"metadata":{"finalizers":["ingresses.networking.internal.knative.dev"],"resourceVersion":""}}`),
}},
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "name" finalizers`),
Eventf(corev1.EventTypeNormal, "Created", "Created HTTPRoute \"example.com\""),
Eventf(corev1.EventTypeNormal, "Created", "Created HTTPRoute \"example.com-redirect\""),
ktesting.Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "name" finalizers`),
ktesting.Eventf(corev1.EventTypeNormal, "Created", "Created HTTPRoute \"example.com\""),
ktesting.Eventf(corev1.EventTypeNormal, "Created", "Created HTTPRoute \"example.com-redirect\""),
},
}}

table.Test(t, GatewayFactory(func(ctx context.Context, listers *Listers, cmw configmap.Watcher, tr *TableRow) controller.Reconciler {
table.Test(t, GatewayFactory(func(ctx context.Context, listers *gwtesting.Listers, _ configmap.Watcher, tr *ktesting.TableRow) controller.Reconciler {
r := &Reconciler{
gwapiclient: fakegwapiclientset.Get(ctx),
httprouteLister: listers.GetHTTPRouteLister(),
Expand Down Expand Up @@ -475,7 +475,7 @@ func TestReconcileTLS(t *testing.T) {
func TestReconcileProbeError(t *testing.T) {
theError := errors.New("this is the error")

table := TableTest{{
table := ktesting.TableTest{{
Name: "first reconcile basic ingress",
Key: "ns/name",
WantErr: true,
Expand All @@ -497,13 +497,13 @@ func TestReconcileProbeError(t *testing.T) {
Patch: []byte(`{"metadata":{"finalizers":["ingresses.networking.internal.knative.dev"],"resourceVersion":""}}`),
}},
WantEvents: []string{
Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "name" finalizers`),
Eventf(corev1.EventTypeNormal, "Created", "Created HTTPRoute \"example.com\""),
Eventf(corev1.EventTypeWarning, "InternalError", fmt.Sprintf("failed to probe Ingress: %v", theError)),
ktesting.Eventf(corev1.EventTypeNormal, "FinalizerUpdate", `Updated "name" finalizers`),
ktesting.Eventf(corev1.EventTypeNormal, "Created", "Created HTTPRoute \"example.com\""),
ktesting.Eventf(corev1.EventTypeWarning, "InternalError", fmt.Sprintf("failed to probe Ingress: %v", theError)),
},
}}

table.Test(t, MakeFactory(func(ctx context.Context, listers *Listers, cmw configmap.Watcher) controller.Reconciler {
table.Test(t, gwtesting.MakeFactory(func(ctx context.Context, listers *gwtesting.Listers, _ configmap.Watcher) controller.Reconciler {
r := &Reconciler{
gwapiclient: fakegwapiclientset.Get(ctx),
// Listers index properties about resources
Expand Down Expand Up @@ -562,7 +562,7 @@ type HTTPRouteOption func(h *gatewayapi.HTTPRoute)

func withSectionName(sectionName string) HTTPRouteOption {
return func(h *gatewayapi.HTTPRoute) {
h.Spec.CommonRouteSpec.ParentRefs[0].SectionName = (*gatewayapi.SectionName)(pointer.String(sectionName))
h.Spec.CommonRouteSpec.ParentRefs[0].SectionName = (*gatewayapi.SectionName)(ptr.To(sectionName))
}
}

Expand All @@ -589,14 +589,14 @@ func (t *testConfigStore) ToContext(ctx context.Context) context.Context {
}

// We need to inject the row's `Objects` to work-around improper pluralization in UnsafeGuessKindToResource
func GatewayFactory(ctor func(context.Context, *Listers, configmap.Watcher, *TableRow) controller.Reconciler) Factory {
return func(t *testing.T, r *TableRow) (
controller.Reconciler, ActionRecorderList, EventList,
func GatewayFactory(ctor func(context.Context, *gwtesting.Listers, configmap.Watcher, *ktesting.TableRow) controller.Reconciler) ktesting.Factory {
return func(t *testing.T, r *ktesting.TableRow) (
controller.Reconciler, ktesting.ActionRecorderList, ktesting.EventList,
) {
shim := func(c context.Context, l *Listers, cw configmap.Watcher) controller.Reconciler {
shim := func(c context.Context, l *gwtesting.Listers, cw configmap.Watcher) controller.Reconciler {
return ctor(c, l, cw, r)
}
return MakeFactory(shim)(t, r)
return gwtesting.MakeFactory(shim)(t, r)
}
}

Expand Down Expand Up @@ -634,18 +634,18 @@ func tlsListener(hostname, nsName, secretName string) GatewayOption {
Port: 443,
Protocol: "HTTPS",
TLS: &gatewayapi.GatewayTLSConfig{
Mode: (*gatewayapi.TLSModeType)(pointer.String("Terminate")),
Mode: (*gatewayapi.TLSModeType)(ptr.To("Terminate")),
CertificateRefs: []gatewayapi.SecretObjectReference{{
Group: (*gatewayapi.Group)(pointer.String("")),
Kind: (*gatewayapi.Kind)(pointer.String("Secret")),
Group: (*gatewayapi.Group)(ptr.To("")),
Kind: (*gatewayapi.Kind)(ptr.To("Secret")),
Name: gatewayapi.ObjectName(secretName),
Namespace: (*gatewayapi.Namespace)(&nsName),
}},
Options: map[gatewayapi.AnnotationKey]gatewayapi.AnnotationValue{},
},
AllowedRoutes: &gatewayapi.AllowedRoutes{
Namespaces: &gatewayapi.RouteNamespaces{
From: (*gatewayapi.FromNamespaces)(pointer.String("Selector")),
From: (*gatewayapi.FromNamespaces)(ptr.To("Selector")),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"kubernetes.io/metadata.name": nsName,
Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/ingress/lister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"knative.dev/networking/pkg/status"
"knative.dev/pkg/kmeta"

. "knative.dev/net-gateway-api/pkg/reconciler/testing"
ingress "knative.dev/net-gateway-api/pkg/reconciler/testing"
)

var (
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestListProbeTargets(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tl := NewListers(test.objects)
tl := ingress.NewListers(test.objects)

l := &gatewayPodTargetLister{
endpointsLister: tl.GetEndpointsLister(),
Expand Down
41 changes: 20 additions & 21 deletions pkg/reconciler/ingress/reconcile_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapi "sigs.k8s.io/gateway-api/apis/v1beta1"
Expand All @@ -46,7 +45,7 @@ func (c *Reconciler) reconcileWorkloadRoute(
gatewayConfig := config.FromContext(ctx).Gateway.Gateways[rule.Visibility]
gatewayRef := gatewayapi.ParentReference{
Group: (*gatewayapi.Group)(&gatewayapi.GroupVersion.Group),
Kind: (*gatewayapi.Kind)(pointer.String("Gateway")),
Kind: (*gatewayapi.Kind)(ptr.To("Gateway")),
Namespace: (*gatewayapi.Namespace)(&gatewayConfig.Gateway.Namespace),
Name: gatewayapi.ObjectName(gatewayConfig.Gateway.Name),
}
Expand Down Expand Up @@ -79,7 +78,7 @@ func (c *Reconciler) reconcileRedirectHTTPRoute(
gatewayConfig := config.FromContext(ctx).Gateway.Gateways[rule.Visibility]
gatewayRef := gatewayapi.ParentReference{
Group: (*gatewayapi.Group)(&gatewayapi.GroupVersion.Group),
Kind: (*gatewayapi.Kind)(pointer.String("Gateway")),
Kind: (*gatewayapi.Kind)(ptr.To("Gateway")),
Namespace: (*gatewayapi.Namespace)(&gatewayConfig.Gateway.Namespace),
Name: gatewayapi.ObjectName(gatewayConfig.Gateway.Name),

Expand Down Expand Up @@ -114,25 +113,25 @@ func (c *Reconciler) reconcileHTTPRoute(ctx context.Context,
return httpRoute, nil
} else if err != nil {
return nil, err
} else {
if !equality.Semantic.DeepEqual(httpRoute.Spec, desired.Spec) ||
!equality.Semantic.DeepEqual(httpRoute.Annotations, desired.Annotations) ||
!equality.Semantic.DeepEqual(httpRoute.Labels, desired.Labels) {

// Don't modify the informers copy.
origin := httpRoute.DeepCopy()
origin.Spec = desired.Spec
origin.Annotations = desired.Annotations
origin.Labels = desired.Labels

updated, err := c.gwapiclient.GatewayV1beta1().HTTPRoutes(origin.Namespace).Update(
ctx, origin, metav1.UpdateOptions{})
if err != nil {
recorder.Eventf(ing, corev1.EventTypeWarning, "UpdateFailed", "Failed to update HTTPRoute: %v", err)
return nil, fmt.Errorf("failed to update HTTPRoute: %w", err)
}
return updated, nil
}

Check warning on line 116 in pkg/reconciler/ingress/reconcile_resources.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/ingress/reconcile_resources.go#L116

Added line #L116 was not covered by tests

if !equality.Semantic.DeepEqual(httpRoute.Spec, desired.Spec) ||
!equality.Semantic.DeepEqual(httpRoute.Annotations, desired.Annotations) ||
!equality.Semantic.DeepEqual(httpRoute.Labels, desired.Labels) {

// Don't modify the informers copy.
origin := httpRoute.DeepCopy()
origin.Spec = desired.Spec
origin.Annotations = desired.Annotations
origin.Labels = desired.Labels

updated, err := c.gwapiclient.GatewayV1beta1().HTTPRoutes(origin.Namespace).Update(
ctx, origin, metav1.UpdateOptions{})
if err != nil {
recorder.Eventf(ing, corev1.EventTypeWarning, "UpdateFailed", "Failed to update HTTPRoute: %v", err)
return nil, fmt.Errorf("failed to update HTTPRoute: %w", err)

Check warning on line 132 in pkg/reconciler/ingress/reconcile_resources.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/ingress/reconcile_resources.go#L121-L132

Added lines #L121 - L132 were not covered by tests
}
return updated, nil

Check warning on line 134 in pkg/reconciler/ingress/reconcile_resources.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconciler/ingress/reconcile_resources.go#L134

Added line #L134 was not covered by tests
}

return httpRoute, err
Expand Down
3 changes: 1 addition & 2 deletions pkg/reconciler/ingress/resources/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"net/http"
"sort"

"k8s.io/utils/pointer"
"knative.dev/pkg/kmap"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -244,7 +243,7 @@ func matchesFromRulePath(path netv1alpha1.HTTPIngressPath) []gatewayapi.HTTPRout
}
pathMatch := gatewayapi.HTTPPathMatch{
Type: ptr.To(gatewayapiv1.PathMatchPathPrefix),
Value: pointer.String(pathPrefix),
Value: ptr.To(pathPrefix),
}

var headerMatchList []gatewayapi.HTTPHeaderMatch

Check failure on line 249 in pkg/reconciler/ingress/resources/httproute.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

Consider pre-allocating `headerMatchList` (prealloc)
Expand Down
Loading

0 comments on commit 87020aa

Please sign in to comment.