Skip to content

Commit afaa171

Browse files
authored
chore: use constant variable instead of hard code (#2560) (#269)
1 parent 41df985 commit afaa171

File tree

22 files changed

+115
-153
lines changed

22 files changed

+115
-153
lines changed

api/adc/types.go

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -29,54 +29,6 @@ import (
2929
"github.com/incubator4/go-resty-expr/expr"
3030
)
3131

32-
const (
33-
// HashOnVars means the hash scope is variable.
34-
HashOnVars = "vars"
35-
// HashOnVarsCombination means the hash scope is the
36-
// variable combination.
37-
HashOnVarsCombination = "vars_combinations"
38-
// HashOnHeader means the hash scope is HTTP request
39-
// headers.
40-
HashOnHeader = "header"
41-
// HashOnCookie means the hash scope is HTTP Cookie.
42-
HashOnCookie = "cookie"
43-
// HashOnConsumer means the hash scope is APISIX consumer.
44-
HashOnConsumer = "consumer"
45-
46-
// LbRoundRobin is the round robin load balancer.
47-
LBRoundRobin = "roundrobin"
48-
// LbConsistentHash is the consistent hash load balancer.
49-
LbConsistentHash = "chash"
50-
// LbEwma is the ewma load balancer.
51-
LbEwma = "ewma"
52-
// LbLeaseConn is the least connection load balancer.
53-
LbLeastConn = "least_conn"
54-
55-
// SchemeHTTP represents the HTTP protocol.
56-
SchemeHTTP = "http"
57-
// SchemeGRPC represents the GRPC protocol.
58-
SchemeGRPC = "grpc"
59-
// SchemeHTTPS represents the HTTPS protocol.
60-
SchemeHTTPS = "https"
61-
// SchemeGRPCS represents the GRPCS protocol.
62-
SchemeGRPCS = "grpcs"
63-
// SchemeTCP represents the TCP protocol.
64-
SchemeTCP = "tcp"
65-
// SchemeUDP represents the UDP protocol.
66-
SchemeUDP = "udp"
67-
68-
// DefaultUpstreamTimeout represents the default connect,
69-
// read and send timeout (in seconds) with upstreams.
70-
DefaultUpstreamTimeout = 60
71-
72-
// PassHostPass represents pass option for pass_host Upstream settings.
73-
PassHostPass = "pass"
74-
// PassHostPass represents node option for pass_host Upstream settings.
75-
PassHostNode = "node"
76-
// PassHostPass represents rewrite option for pass_host Upstream settings.
77-
PassHostRewrite = "rewrite"
78-
)
79-
8032
const (
8133
TypeRoute = "route"
8234
TypeService = "service"
@@ -392,11 +344,7 @@ const (
392344
type Scheme string
393345

394346
const (
395-
Grpc Scheme = "grpc"
396-
Grpcs Scheme = "grpcs"
397-
Kafka Scheme = "kafka"
398-
TLS Scheme = "tls"
399-
UDP Scheme = "udp"
347+
SchemeHTTP = "http"
400348
)
401349

402350
type UpstreamType string

internal/adc/cache/memdb.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,23 @@ func (c *dbCache) Delete(obj any) error {
8181

8282
func (c *dbCache) InsertRoute(r *types.Route) error {
8383
route := r.DeepCopy()
84-
return c.insert("route", route)
84+
return c.insert(types.TypeRoute, route)
8585
}
8686

8787
func (c *dbCache) InsertSSL(ssl *types.SSL) error {
88-
return c.insert("ssl", ssl.DeepCopy())
88+
return c.insert(types.TypeSSL, ssl.DeepCopy())
8989
}
9090

9191
func (c *dbCache) InsertService(u *types.Service) error {
92-
return c.insert("service", u.DeepCopy())
92+
return c.insert(types.TypeService, u.DeepCopy())
9393
}
9494

9595
func (c *dbCache) InsertConsumer(consumer *types.Consumer) error {
96-
return c.insert("consumer", consumer.DeepCopy())
96+
return c.insert(types.TypeConsumer, consumer.DeepCopy())
9797
}
9898

9999
func (c *dbCache) InsertGlobalRule(globalRule *types.GlobalRuleItem) error {
100-
return c.insert("global_rule", globalRule.DeepCopy())
100+
return c.insert(types.TypeGlobalRule, globalRule.DeepCopy())
101101
}
102102

103103
func (c *dbCache) insert(table string, obj any) error {
@@ -111,39 +111,39 @@ func (c *dbCache) insert(table string, obj any) error {
111111
}
112112

113113
func (c *dbCache) GetRoute(id string) (*types.Route, error) {
114-
obj, err := c.get("route", id)
114+
obj, err := c.get(types.TypeRoute, id)
115115
if err != nil {
116116
return nil, err
117117
}
118118
return obj.(*types.Route).DeepCopy(), nil
119119
}
120120

121121
func (c *dbCache) GetSSL(id string) (*types.SSL, error) {
122-
obj, err := c.get("ssl", id)
122+
obj, err := c.get(types.TypeSSL, id)
123123
if err != nil {
124124
return nil, err
125125
}
126126
return obj.(*types.SSL).DeepCopy(), nil
127127
}
128128

129129
func (c *dbCache) GetService(id string) (*types.Service, error) {
130-
obj, err := c.get("service", id)
130+
obj, err := c.get(types.TypeService, id)
131131
if err != nil {
132132
return nil, err
133133
}
134134
return obj.(*types.Service).DeepCopy(), nil
135135
}
136136

137137
func (c *dbCache) GetConsumer(username string) (*types.Consumer, error) {
138-
obj, err := c.get("consumer", username)
138+
obj, err := c.get(types.TypeConsumer, username)
139139
if err != nil {
140140
return nil, err
141141
}
142142
return obj.(*types.Consumer).DeepCopy(), nil
143143
}
144144

145145
func (c *dbCache) GetGlobalRule(id string) (*types.GlobalRuleItem, error) {
146-
obj, err := c.get("global_rule", id)
146+
obj, err := c.get(types.TypeGlobalRule, id)
147147
if err != nil {
148148
return nil, err
149149
}
@@ -175,7 +175,7 @@ func (c *dbCache) get(table, id string) (any, error) {
175175
}
176176

177177
func (c *dbCache) ListRoutes(opts ...ListOption) ([]*types.Route, error) {
178-
raws, err := c.list("route", opts...)
178+
raws, err := c.list(types.TypeRoute, opts...)
179179
if err != nil {
180180
return nil, err
181181
}
@@ -187,7 +187,7 @@ func (c *dbCache) ListRoutes(opts ...ListOption) ([]*types.Route, error) {
187187
}
188188

189189
func (c *dbCache) ListSSL(opts ...ListOption) ([]*types.SSL, error) {
190-
raws, err := c.list("ssl", opts...)
190+
raws, err := c.list(types.TypeSSL, opts...)
191191
if err != nil {
192192
return nil, err
193193
}
@@ -199,7 +199,7 @@ func (c *dbCache) ListSSL(opts ...ListOption) ([]*types.SSL, error) {
199199
}
200200

201201
func (c *dbCache) ListServices(opts ...ListOption) ([]*types.Service, error) {
202-
raws, err := c.list("service", opts...)
202+
raws, err := c.list(types.TypeService, opts...)
203203
if err != nil {
204204
return nil, err
205205
}
@@ -211,7 +211,7 @@ func (c *dbCache) ListServices(opts ...ListOption) ([]*types.Service, error) {
211211
}
212212

213213
func (c *dbCache) ListConsumers(opts ...ListOption) ([]*types.Consumer, error) {
214-
raws, err := c.list("consumer", opts...)
214+
raws, err := c.list(types.TypeConsumer, opts...)
215215
if err != nil {
216216
return nil, err
217217
}
@@ -223,7 +223,7 @@ func (c *dbCache) ListConsumers(opts ...ListOption) ([]*types.Consumer, error) {
223223
}
224224

225225
func (c *dbCache) ListGlobalRules(opts ...ListOption) ([]*types.GlobalRuleItem, error) {
226-
raws, err := c.list("global_rule", opts...)
226+
raws, err := c.list(types.TypeGlobalRule, opts...)
227227
if err != nil {
228228
return nil, err
229229
}
@@ -257,23 +257,23 @@ func (c *dbCache) list(table string, opts ...ListOption) ([]any, error) {
257257
}
258258

259259
func (c *dbCache) DeleteRoute(r *types.Route) error {
260-
return c.delete("route", r)
260+
return c.delete(types.TypeRoute, r)
261261
}
262262

263263
func (c *dbCache) DeleteSSL(ssl *types.SSL) error {
264-
return c.delete("ssl", ssl)
264+
return c.delete(types.TypeSSL, ssl)
265265
}
266266

267267
func (c *dbCache) DeleteService(u *types.Service) error {
268-
return c.delete("service", u)
268+
return c.delete(types.TypeService, u)
269269
}
270270

271271
func (c *dbCache) DeleteConsumer(consumer *types.Consumer) error {
272-
return c.delete("consumer", consumer)
272+
return c.delete(types.TypeConsumer, consumer)
273273
}
274274

275275
func (c *dbCache) DeleteGlobalRule(globalRule *types.GlobalRuleItem) error {
276-
return c.delete("global_rule", globalRule)
276+
return c.delete(types.TypeGlobalRule, globalRule)
277277
}
278278

279279
func (c *dbCache) delete(table string, obj any) error {

internal/adc/client/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func (c *Client) sync(ctx context.Context, task Task) error {
303303
pkgmetrics.RecordFileIODuration("prepare_sync_file", "failure", time.Since(fileIOStart).Seconds())
304304
return err
305305
}
306-
pkgmetrics.RecordFileIODuration("prepare_sync_file", "success", time.Since(fileIOStart).Seconds())
306+
pkgmetrics.RecordFileIODuration("prepare_sync_file", adctypes.StatusSuccess, time.Since(fileIOStart).Seconds())
307307
defer cleanup()
308308

309309
args := BuildADCExecuteArgs(syncFilePath, task.Labels, task.ResourceTypes)
@@ -319,7 +319,7 @@ func (c *Client) sync(ctx context.Context, task Task) error {
319319
err := c.executor.Execute(ctx, c.BackendMode, config, args)
320320
duration := time.Since(startTime).Seconds()
321321

322-
status := "success"
322+
status := adctypes.StatusSuccess
323323
if err != nil {
324324
status = "failure"
325325
log.Errorw("failed to execute adc command", zap.Error(err), zap.Any("config", config))

internal/adc/translator/apisixroute.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
3838
"github.com/apache/apisix-ingress-controller/internal/controller/label"
3939
"github.com/apache/apisix-ingress-controller/internal/provider"
40+
internaltypes "github.com/apache/apisix-ingress-controller/internal/types"
4041
"github.com/apache/apisix-ingress-controller/internal/utils"
4142
"github.com/apache/apisix-ingress-controller/pkg/id"
4243
pkgutils "github.com/apache/apisix-ingress-controller/pkg/utils"
@@ -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(internaltypes.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
@@ -36,6 +36,7 @@ import (
3636
"github.com/apache/apisix-ingress-controller/internal/controller/label"
3737
"github.com/apache/apisix-ingress-controller/internal/id"
3838
"github.com/apache/apisix-ingress-controller/internal/provider"
39+
internaltypes "github.com/apache/apisix-ingress-controller/internal/types"
3940
"github.com/apache/apisix-ingress-controller/internal/utils"
4041
)
4142

@@ -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 == internaltypes.KindSecret {
9091
sslObj := &adctypes.SSL{
9192
Snis: []string{},
9293
}

internal/adc/translator/httproute.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/apache/apisix-ingress-controller/internal/controller/label"
3838
"github.com/apache/apisix-ingress-controller/internal/id"
3939
"github.com/apache/apisix-ingress-controller/internal/provider"
40+
internaltypes "github.com/apache/apisix-ingress-controller/internal/types"
4041
)
4142

4243
func (t *Translator) fillPluginsFromHTTPRouteFilters(
@@ -70,7 +71,7 @@ func (t *Translator) fillPluginFromExtensionRef(plugins adctypes.Plugins, namesp
7071
if extensionRef == nil {
7172
return
7273
}
73-
if extensionRef.Kind == "PluginConfig" {
74+
if extensionRef.Kind == internaltypes.KindPluginConfig {
7475
pluginconfig := tctx.PluginConfigs[types.NamespacedName{
7576
Namespace: namespace,
7677
Name: string(extensionRef.Name),
@@ -300,7 +301,7 @@ func (t *Translator) fillHTTPRoutePoliciesForHTTPRoute(tctx *provider.TranslateC
300301
var policies []v1alpha1.HTTPRoutePolicy
301302
for _, policy := range tctx.HTTPRoutePolicies {
302303
for _, ref := range policy.Spec.TargetRefs {
303-
if string(ref.Kind) == "HTTPRoute" && (ref.SectionName == nil || *ref.SectionName == "" || ptr.Equal(ref.SectionName, rule.Name)) {
304+
if string(ref.Kind) == internaltypes.KindHTTPRoute && (ref.SectionName == nil || *ref.SectionName == "" || ptr.Equal(ref.SectionName, rule.Name)) {
304305
policies = append(policies, policy)
305306
break
306307
}
@@ -376,7 +377,7 @@ func (t *Translator) TranslateBackendRefWithFilter(tctx *provider.TranslateConte
376377
}
377378

378379
func (t *Translator) translateBackendRef(tctx *provider.TranslateContext, ref gatewayv1.BackendRef, endpointFilter func(*discoveryv1.Endpoint) bool) (adctypes.UpstreamNodes, error) {
379-
if ref.Kind != nil && *ref.Kind != "Service" {
380+
if ref.Kind != nil && *ref.Kind != internaltypes.KindService {
380381
return adctypes.UpstreamNodes{}, fmt.Errorf("kind %s is not supported", *ref.Kind)
381382
}
382383

@@ -549,7 +550,7 @@ func (t *Translator) TranslateHTTPRoute(tctx *provider.TranslateContext, httpRou
549550
port int32
550551
)
551552
if backend.Kind == nil {
552-
kind = "Service"
553+
kind = internaltypes.KindService
553554
} else {
554555
kind = string(*backend.Kind)
555556
}

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+
internaltypes "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, internaltypes.KindService)
127128
t.AttachBackendTrafficPolicyToUpstream(backendRef, tctx.BackendTrafficPolicies, upstream)
128129
}
129130

internal/controller/apisixroute_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ func (r *ApisixRouteReconciler) validateHTTPBackend(tctx *provider.TranslateCont
459459
return nil
460460
}
461461

462-
if backend.ResolveGranularity == "service" && service.Spec.ClusterIP == "" {
462+
if backend.ResolveGranularity == apiv2.ResolveGranularityService && service.Spec.ClusterIP == "" {
463463
r.Log.Error(errors.New("service has no ClusterIP"), "Service", serviceNN, "ResolveGranularity", backend.ResolveGranularity)
464464
return nil
465465
}

internal/controller/consumer_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/manager/readiness"
4444
"github.com/apache/apisix-ingress-controller/internal/provider"
45+
internaltypes "github.com/apache/apisix-ingress-controller/internal/types"
4546
"github.com/apache/apisix-ingress-controller/internal/utils"
4647
)
4748

@@ -193,7 +194,7 @@ func (r *ConsumerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
193194
consumer.Name = req.Name
194195

195196
consumer.TypeMeta = metav1.TypeMeta{
196-
Kind: "Consumer",
197+
Kind: internaltypes.KindConsumer,
197198
APIVersion: v1alpha1.GroupVersion.String(),
198199
}
199200

internal/controller/gateway_controller.go

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

@@ -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 != internaltypes.KindGateway {
320321
continue
321322
}
322323
if parentRef.Namespace != nil {

0 commit comments

Comments
 (0)