Skip to content

Commit 7220e7b

Browse files
authored
feat(#3097): use TranslationFailureCollector in Parser (#3111)
It adds TranslationFailureCollector to the Parser. Build method is modified so that translation failures are returned from it. KongClient inspects the failures and in case any occurred, reports TranslationCount metric with SuccessKey=SuccessFalse.
1 parent 8e12adb commit 7220e7b

File tree

9 files changed

+236
-160
lines changed

9 files changed

+236
-160
lines changed

internal/dataplane/kong_client.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,11 @@ func (c *KongClient) Update(ctx context.Context) error {
297297

298298
// initialize a parser
299299
c.logger.Debug("parsing kubernetes objects into data-plane configuration")
300-
p := parser.NewParser(c.logger, storer)
300+
301+
p, err := parser.NewParser(c.logger, storer)
302+
if err != nil {
303+
return fmt.Errorf("failed to create parser: %w", err)
304+
}
301305
formatVersion := "1.1"
302306
if c.AreKubernetesObjectReportsEnabled() {
303307
p.EnableKubernetesObjectReports()
@@ -311,13 +315,18 @@ func (c *KongClient) Update(ctx context.Context) error {
311315
}
312316

313317
// parse the Kubernetes objects from the storer into Kong configuration
314-
kongstate := p.Build()
315-
// todo: does it still make sense to report TranslationCount when Build no longer returns an error?
316-
// https://github.com/Kong/kubernetes-ingress-controller/issues/1892
317-
c.prometheusMetrics.TranslationCount.With(prometheus.Labels{
318-
metrics.SuccessKey: metrics.SuccessTrue,
319-
}).Inc()
320-
c.logger.Debug("successfully built data-plane configuration")
318+
kongstate, translationFailures := p.Build()
319+
if failuresCount := len(translationFailures); failuresCount > 0 {
320+
c.prometheusMetrics.TranslationCount.With(prometheus.Labels{
321+
metrics.SuccessKey: metrics.SuccessFalse,
322+
}).Inc()
323+
c.logger.Debugf("%d translation failures have occurred when building data-plane configuration", failuresCount)
324+
} else {
325+
c.prometheusMetrics.TranslationCount.With(prometheus.Labels{
326+
metrics.SuccessKey: metrics.SuccessTrue,
327+
}).Inc()
328+
c.logger.Debug("successfully built data-plane configuration")
329+
}
321330

322331
// generate the deck configuration to be applied to the admin API
323332
c.logger.Debug("converting configuration to deck config")

internal/dataplane/parser/parser.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,35 @@ type Parser struct {
5151
featureEnabledCombinedServiceRoutes bool
5252

5353
flagEnabledRegexPathPrefix bool
54+
failuresCollector *TranslationFailuresCollector
5455
}
5556

5657
// NewParser produces a new Parser object provided a logging mechanism
5758
// and a Kubernetes object store.
5859
func NewParser(
5960
logger logrus.FieldLogger,
6061
storer store.Storer,
61-
) *Parser {
62-
return &Parser{
63-
logger: logger,
64-
storer: storer,
62+
) (*Parser, error) {
63+
failuresCollector, err := NewTranslationFailuresCollector(logger)
64+
if err != nil {
65+
return nil, fmt.Errorf("failed to create translation errors collector: %w", err)
6566
}
67+
68+
return &Parser{
69+
logger: logger,
70+
storer: storer,
71+
failuresCollector: failuresCollector,
72+
}, nil
6673
}
6774

6875
// -----------------------------------------------------------------------------
6976
// Parser - Public Methods
7077
// -----------------------------------------------------------------------------
7178

7279
// Build creates a Kong configuration from Ingress and Custom resources
73-
// defined in Kubernetes.
74-
func (p *Parser) Build() *kongstate.KongState {
80+
// defined in Kubernetes. It returns a slice of TranslationFailures which should
81+
// be used to provide users with feedback on Kubernetes objects validity.
82+
func (p *Parser) Build() (*kongstate.KongState, []TranslationFailure) {
7583
// parse and merge all rules together from all Kubernetes API sources
7684
ingressRules := mergeIngressRules(
7785
p.ingressRulesFromIngressV1beta1(),
@@ -120,7 +128,7 @@ func (p *Parser) Build() *kongstate.KongState {
120128
// populate CA certificates in Kong
121129
result.CACertificates = getCACerts(p.logger, p.storer, result.Plugins)
122130

123-
return &result
131+
return &result, p.popTranslationFailures()
124132
}
125133

126134
// -----------------------------------------------------------------------------
@@ -174,6 +182,10 @@ func (p *Parser) EnableRegexPathPrefix() {
174182
p.flagEnabledRegexPathPrefix = true
175183
}
176184

185+
func (p *Parser) popTranslationFailures() []TranslationFailure {
186+
return p.failuresCollector.PopTranslationFailures()
187+
}
188+
177189
// -----------------------------------------------------------------------------
178190
// Parser - Private Methods
179191
// -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)