Skip to content

Commit a71c807

Browse files
committed
use constant variable instead of hard code string
1 parent 40313eb commit a71c807

File tree

9 files changed

+30
-22
lines changed

9 files changed

+30
-22
lines changed

internal/adc/translator/apisixroute.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/apache/apisix-ingress-controller/internal/utils"
4141
"github.com/apache/apisix-ingress-controller/pkg/id"
4242
pkgutils "github.com/apache/apisix-ingress-controller/pkg/utils"
43+
k8sTypes "github.com/apache/apisix-ingress-controller/internal/types"
4344
)
4445

4546
func (t *Translator) TranslateApisixRoute(tctx *provider.TranslateContext, ar *apiv2.ApisixRoute) (result *TranslateResult, err error) {
@@ -405,7 +406,7 @@ func (t *Translator) translateApisixRouteBackendResolveGranularityEndpoint(tctx
405406
backendRef := gatewayv1.BackendRef{
406407
BackendObjectReference: gatewayv1.BackendObjectReference{
407408
Group: (*gatewayv1.Group)(&apiv2.GroupVersion.Group),
408-
Kind: (*gatewayv1.Kind)(ptr.To("Service")),
409+
Kind: (*gatewayv1.Kind)(ptr.To(k8sTypes.KindService)),
409410
Name: gatewayv1.ObjectName(backend.ServiceName),
410411
Namespace: (*gatewayv1.Namespace)(&arNN.Namespace),
411412
Port: (*gatewayv1.PortNumber)(&port),

internal/adc/translator/gateway.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/apache/apisix-ingress-controller/internal/id"
3838
"github.com/apache/apisix-ingress-controller/internal/provider"
3939
"github.com/apache/apisix-ingress-controller/internal/utils"
40+
k8sTypes "github.com/apache/apisix-ingress-controller/internal/types"
4041
)
4142

4243
func (t *Translator) TranslateGateway(tctx *provider.TranslateContext, obj *gatewayv1.Gateway) (*TranslateResult, error) {
@@ -86,7 +87,7 @@ func (t *Translator) translateSecret(tctx *provider.TranslateContext, listener g
8687
if ref.Namespace != nil {
8788
ns = string(*ref.Namespace)
8889
}
89-
if listener.TLS.CertificateRefs[0].Kind != nil && *listener.TLS.CertificateRefs[0].Kind == "Secret" {
90+
if listener.TLS.CertificateRefs[0].Kind != nil && *listener.TLS.CertificateRefs[0].Kind == k8sTypes.KindSecret {
9091
sslObj := &adctypes.SSL{
9192
Snis: []string{},
9293
}

internal/adc/translator/ingress.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/apache/apisix-ingress-controller/internal/controller/label"
3131
"github.com/apache/apisix-ingress-controller/internal/id"
3232
"github.com/apache/apisix-ingress-controller/internal/provider"
33+
k8sTypes "github.com/apache/apisix-ingress-controller/internal/types"
3334
)
3435

3536
func (t *Translator) translateIngressTLS(ingressTLS *networkingv1.IngressTLS, secret *corev1.Secret, labels map[string]string) (*adctypes.SSL, error) {
@@ -123,7 +124,7 @@ func (t *Translator) TranslateIngress(tctx *provider.TranslateContext, obj *netw
123124
// get the EndpointSlice of the backend service
124125
backendService := path.Backend.Service
125126
if backendService != nil {
126-
backendRef := convertBackendRef(obj.Namespace, backendService.Name, "Service")
127+
backendRef := convertBackendRef(obj.Namespace, backendService.Name, k8sTypes.KindService)
127128
t.AttachBackendTrafficPolicyToUpstream(backendRef, tctx.BackendTrafficPolicies, upstream)
128129
}
129130

internal/controller/gateway_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"github.com/apache/apisix-ingress-controller/internal/controller/status"
4343
"github.com/apache/apisix-ingress-controller/internal/provider"
4444
"github.com/apache/apisix-ingress-controller/internal/utils"
45+
k8sTypes "github.com/apache/apisix-ingress-controller/internal/types"
4546
)
4647

4748
// GatewayReconciler reconciles a Gateway object.
@@ -316,7 +317,7 @@ func (r *GatewayReconciler) listGatewaysForHTTPRoute(ctx context.Context, obj cl
316317
if parentRef.Group != nil && *parentRef.Group != gatewayv1.GroupName {
317318
continue
318319
}
319-
if parentRef.Kind != nil && *parentRef.Kind != "Gateway" {
320+
if parentRef.Kind != nil && *parentRef.Kind != k8sTypes.KindGateway {
320321
continue
321322
}
322323
if parentRef.Namespace != nil {

internal/controller/httproute_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ func (r *HTTPRouteReconciler) processHTTPRouteBackendRefs(tctx *provider.Transla
456456
targetNN.Namespace = string(*backend.Namespace)
457457
}
458458

459-
if backend.Kind != nil && *backend.Kind != "Service" {
459+
if backend.Kind != nil && *backend.Kind != types.KindService {
460460
terr = types.NewInvalidKindError(*backend.Kind)
461461
continue
462462
}
@@ -560,7 +560,7 @@ func (r *HTTPRouteReconciler) processHTTPRoute(tctx *provider.TranslateContext,
560560
}
561561
}
562562
for _, backend := range rule.BackendRefs {
563-
if backend.Kind != nil && *backend.Kind != "Service" {
563+
if backend.Kind != nil && *backend.Kind != types.KindService {
564564
terror = types.NewInvalidKindError(*backend.Kind)
565565
continue
566566
}

internal/controller/indexer/indexer.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131

3232
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
3333
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
34+
k8sTypes "github.com/apache/apisix-ingress-controller/internal/types"
3435
)
3536

3637
const (
@@ -431,7 +432,7 @@ func GatewaySecretIndexFunc(rawObj client.Object) (keys []string) {
431432
continue
432433
}
433434
for _, ref := range listener.TLS.CertificateRefs {
434-
if ref.Kind == nil || *ref.Kind != "Secret" {
435+
if ref.Kind == nil || *ref.Kind != k8sTypes.KindSecret {
435436
continue
436437
}
437438
namespace := gateway.GetNamespace()
@@ -486,7 +487,7 @@ func HTTPRouteServiceIndexFunc(rawObj client.Object) []string {
486487
for _, rule := range hr.Spec.Rules {
487488
for _, backend := range rule.BackendRefs {
488489
namespace := hr.GetNamespace()
489-
if backend.Kind != nil && *backend.Kind != "Service" {
490+
if backend.Kind != nil && *backend.Kind != k8sTypes.KindService {
490491
continue
491492
}
492493
if backend.Namespace != nil {
@@ -620,7 +621,7 @@ func HTTPRouteExtensionIndexFunc(rawObj client.Object) []string {
620621
if filter.Type != gatewayv1.HTTPRouteFilterExtensionRef || filter.ExtensionRef == nil {
621622
continue
622623
}
623-
if filter.ExtensionRef.Kind == "PluginConfig" {
624+
if filter.ExtensionRef.Kind == k8sTypes.KindPluginConfig {
624625
keys = append(keys, GenIndexKey(hr.GetNamespace(), string(filter.ExtensionRef.Name)))
625626
}
626627
}
@@ -646,7 +647,7 @@ func GatewayParametersRefIndexFunc(rawObj client.Object) []string {
646647
gw := rawObj.(*gatewayv1.Gateway)
647648
if gw.Spec.Infrastructure != nil && gw.Spec.Infrastructure.ParametersRef != nil {
648649
// now we only care about kind: GatewayProxy
649-
if gw.Spec.Infrastructure.ParametersRef.Kind == "GatewayProxy" {
650+
if gw.Spec.Infrastructure.ParametersRef.Kind == k8sTypes.KindGatewayProxy {
650651
name := gw.Spec.Infrastructure.ParametersRef.Name
651652
return []string{GenIndexKey(gw.GetNamespace(), name)}
652653
}
@@ -676,7 +677,7 @@ func IngressClassParametersRefIndexFunc(rawObj client.Object) []string {
676677
if ingressClass.Spec.Parameters != nil &&
677678
ingressClass.Spec.Parameters.APIGroup != nil &&
678679
*ingressClass.Spec.Parameters.APIGroup == v1alpha1.GroupVersion.Group &&
679-
ingressClass.Spec.Parameters.Kind == "GatewayProxy" {
680+
ingressClass.Spec.Parameters.Kind == k8sTypes.KindGatewayProxy {
680681
ns := ingressClass.GetNamespace()
681682
if ingressClass.Spec.Parameters.Namespace != nil {
682683
ns = *ingressClass.Spec.Parameters.Namespace

test/conformance/suite_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
"github.com/apache/apisix-ingress-controller/test/e2e/framework"
3535
"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
36+
"github.com/apache/apisix-ingress-controller/internal/types"
3637
)
3738

3839
var gatewayClassName = "apisix"
@@ -225,7 +226,7 @@ func patchGatewaysForConformanceTest(ctx context.Context, k8sClient client.Clien
225226
gateway.Spec.Infrastructure = &gatewayv1.GatewayInfrastructure{
226227
ParametersRef: &gatewayv1.LocalParametersReference{
227228
Group: "apisix.apache.org",
228-
Kind: "GatewayProxy",
229+
Kind: types.KindGatewayProxy,
229230
Name: "conformance-gateway-proxy",
230231
},
231232
}

test/e2e/gatewayapi/controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/stretchr/testify/assert"
2727

2828
"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
29+
"github.com/apache/apisix-ingress-controller/internal/types"
2930
)
3031

3132
var _ = Describe("Check if controller cache gets synced with correct resources", Label("networking.k8s.io", "basic"), func() {
@@ -111,7 +112,7 @@ spec:
111112
time.Sleep(10 * time.Second)
112113

113114
By("check GatewayClass condition")
114-
gcyaml, err := s.GetResourceYamlFromNamespace("GatewayClass", gatewayClassName, s.Namespace())
115+
gcyaml, err := s.GetResourceYamlFromNamespace(types.KindGatewayClass, gatewayClassName, s.Namespace())
115116
Expect(err).NotTo(HaveOccurred(), "getting GatewayClass yaml")
116117
Expect(gcyaml).To(ContainSubstring(`status: "True"`), "checking GatewayClass condition status")
117118
Expect(gcyaml).To(ContainSubstring("message: the gatewayclass has been accepted by the apisix-ingress-controller"), "checking GatewayClass condition message")
@@ -122,7 +123,7 @@ spec:
122123
time.Sleep(10 * time.Second)
123124

124125
By("check Gateway condition")
125-
gwyaml, err := s.GetResourceYamlFromNamespace("Gateway", gatewayName, gatewayName)
126+
gwyaml, err := s.GetResourceYamlFromNamespace(types.KindGateway, gatewayName, gatewayName)
126127
Expect(err).NotTo(HaveOccurred(), "getting Gateway yaml")
127128
Expect(gwyaml).To(ContainSubstring(`status: "True"`), "checking Gateway condition status")
128129
Expect(gwyaml).To(ContainSubstring("message: the gateway has been accepted by the apisix-ingress-controller"), "checking Gateway condition message")
@@ -163,7 +164,7 @@ spec:
163164
beforeEach(s1)
164165
})
165166
It("Apply resource ", func() {
166-
ResourceApplied(s1, "HTTPRoute", "httpbin", s1.Namespace(), fmt.Sprintf(route1, s1.Namespace()), 1)
167+
ResourceApplied(s1, types.KindHTTPRoute, "httpbin", s1.Namespace(), fmt.Sprintf(route1, s1.Namespace()), 1)
167168

168169
s1.RetryAssertion(func() int {
169170
routes, _ := s1.DefaultDataplaneResource().Route().List(s1.Context)
@@ -210,7 +211,7 @@ spec:
210211
beforeEach(s2)
211212
})
212213
It("Apply resource ", func() {
213-
ResourceApplied(s2, "HTTPRoute", "httpbin2", s2.Namespace(), fmt.Sprintf(route2, s2.Namespace(), s2.Namespace()), 1)
214+
ResourceApplied(s2, types.KindHTTPRoute, "httpbin2", s2.Namespace(), fmt.Sprintf(route2, s2.Namespace(), s2.Namespace()), 1)
214215
time.Sleep(5 * time.Second)
215216
routes, err := s2.DefaultDataplaneResource().Route().List(s2.Context)
216217
Expect(err).NotTo(HaveOccurred())

test/e2e/gatewayapi/gatewayproxy.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
. "github.com/onsi/gomega"
2626

2727
"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
28+
"github.com/apache/apisix-ingress-controller/internal/types"
2829
)
2930

3031
var _ = Describe("Test GatewayProxy", Label("apisix.apache.org", "v1alpha1", "gatewayproxy"), func() {
@@ -135,7 +136,7 @@ spec:
135136
time.Sleep(5 * time.Second)
136137

137138
By("Check GatewayClass condition")
138-
gcYaml, err := s.GetResourceYaml("GatewayClass", gatewayClassName)
139+
gcYaml, err := s.GetResourceYaml(types.KindGatewayClass, gatewayClassName)
139140
Expect(err).NotTo(HaveOccurred(), "getting GatewayClass yaml")
140141
Expect(gcYaml).To(ContainSubstring(`status: "True"`), "checking GatewayClass condition status")
141142
Expect(gcYaml).To(ContainSubstring("message: the gatewayclass has been accepted by the apisix-ingress-controller"), "checking GatewayClass condition message")
@@ -151,7 +152,7 @@ spec:
151152
time.Sleep(5 * time.Second)
152153

153154
By("check Gateway condition")
154-
gwyaml, err := s.GetResourceYaml("Gateway", s.Namespace())
155+
gwyaml, err := s.GetResourceYaml(types.KindGateway, s.Namespace())
155156
Expect(err).NotTo(HaveOccurred(), "getting Gateway yaml")
156157
Expect(gwyaml).To(ContainSubstring(`status: "True"`), "checking Gateway condition status")
157158
Expect(gwyaml).To(ContainSubstring("message: the gateway has been accepted by the apisix-ingress-controller"), "checking Gateway condition message")
@@ -160,7 +161,7 @@ spec:
160161
Context("Test Gateway with enabled GatewayProxy plugin", func() {
161162
It("Should apply plugin configuration when enabled", func() {
162163
By("Create HTTPRoute for Gateway with GatewayProxy")
163-
s.ResourceApplied("HTTPRoute", "test-route", fmt.Sprintf(httpRouteForTest, s.Namespace()), 1)
164+
s.ResourceApplied(types.KindHTTPRoute, "test-route", fmt.Sprintf(httpRouteForTest, s.Namespace()), 1)
164165

165166
By("Check if the plugin is applied")
166167
s.RequestAssert(&scaffold.RequestAssert{
@@ -178,7 +179,7 @@ spec:
178179
Expect(err).NotTo(HaveOccurred(), "updating GatewayProxy with disabled plugin")
179180

180181
By("Create HTTPRoute for Gateway with GatewayProxy")
181-
s.ResourceApplied("HTTPRoute", "test-route", fmt.Sprintf(httpRouteForTest, s.Namespace()), 1)
182+
s.ResourceApplied(types.KindHTTPRoute, "test-route", fmt.Sprintf(httpRouteForTest, s.Namespace()), 1)
182183

183184
By("Check if the plugin is not applied")
184185
s.RequestAssert(&scaffold.RequestAssert{
@@ -215,7 +216,7 @@ spec:
215216
Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy with enabled plugin")
216217

217218
By("Create HTTPRoute")
218-
s.ResourceApplied("HTTPRoute", "test-route", fmt.Sprintf(httpRouteForTest, s.Namespace()), 1)
219+
s.ResourceApplied(types.KindHTTPRoute, "test-route", fmt.Sprintf(httpRouteForTest, s.Namespace()), 1)
219220

220221
s.RequestAssert(&scaffold.RequestAssert{
221222
Method: "GET",
@@ -286,7 +287,7 @@ spec:
286287
Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy with valid provider")
287288

288289
s.RetryAssertion(func() string {
289-
gpYaml, _ := s.GetResourceYaml("GatewayProxy", s.Namespace())
290+
gpYaml, _ := s.GetResourceYaml(types.KindGatewayProxy, s.Namespace())
290291
return gpYaml
291292
}).Should(ContainSubstring(`"type":"ControlPlane"`), "checking GatewayProxy is applied")
292293
})

0 commit comments

Comments
 (0)