Skip to content

Commit f0f52d7

Browse files
r-vasquezjoejulian
authored andcommitted
k8s: bump golangci-lint to latest version
There was a change for nolint directives: golangci/golangci-lint#3109 (comment) And we are adding confidence of 0.8 in revive to avoid false positives. (cherry picked from commit bf5401e)
1 parent 523014a commit f0f52d7

40 files changed

+484
-105
lines changed

src/go/k8s/.golangci.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ linters-settings:
2626
misspell:
2727
locale: US
2828
nolintlint:
29-
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
3029
allow-unused: false # report any unused nolint directives
3130
require-explanation: false # don't require an explanation for nolint directives
3231
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
32+
revive:
33+
confidence: 0.8
3334

3435
linters:
3536
# please, do not use `enable-all`: it's deprecated and will be removed soon.

src/go/k8s/apis/redpanda/v1alpha1/cluster_types.go

+26-6
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ type RedpandaResourceRequirements struct {
5050
// * Is limited by 2Gi per core if requests.memory is set.
5151
//
5252
// Example:
53-
// in: minimum requirement per core, 2GB
54-
// in: Requests.Memory, 16GB
55-
// => maxAllowedCores = 8
56-
// if requestedCores == 8, set smp = 8 (with 2GB per core)
57-
// if requestedCores == 4, set smp = 4 (with 4GB per core)
53+
//
54+
// in: minimum requirement per core, 2GB
55+
// in: Requests.Memory, 16GB
56+
// => maxAllowedCores = 8
57+
// if requestedCores == 8, set smp = 8 (with 2GB per core)
58+
// if requestedCores == 4, set smp = 4 (with 4GB per core)
5859
func (r *RedpandaResourceRequirements) RedpandaCPU() *resource.Quantity {
5960
q := r.Redpanda.Cpu()
6061
if q == nil || q.IsZero() {
@@ -257,17 +258,33 @@ type StorageSpec struct {
257258
// used as a external listener. This port is tight to the autogenerated
258259
// host port. The collision between Kafka external, Kafka internal,
259260
// Admin, Pandaproxy, Schema Registry and RPC port is checked in the webhook.
261+
// An optional endpointTemplate can be used to configure advertised addresses
262+
// for Kafka API and Pandaproxy, while it is disallowed for other listeners.
260263
type ExternalConnectivityConfig struct {
261264
// Enabled enables the external connectivity feature
262265
Enabled bool `json:"enabled,omitempty"`
263266
// Subdomain can be used to change the behavior of an advertised
264267
// KafkaAPI. Each broker advertises Kafka API as follows
265-
// BROKER_ID.SUBDOMAIN:EXTERNAL_KAFKA_API_PORT.
268+
// ENDPOINT.SUBDOMAIN:EXTERNAL_KAFKA_API_PORT.
266269
// If Subdomain is empty then each broker advertises Kafka
267270
// API as PUBLIC_NODE_IP:EXTERNAL_KAFKA_API_PORT.
268271
// If TLS is enabled then this subdomain will be requested
269272
// as a subject alternative name.
270273
Subdomain string `json:"subdomain,omitempty"`
274+
// EndpointTemplate is a Golang template string that allows customizing each
275+
// broker advertised address.
276+
// Redpanda uses the format BROKER_ID.SUBDOMAIN:EXTERNAL_KAFKA_API_PORT by
277+
// default for advertised addresses. When an EndpointTemplate is
278+
// provided, then the BROKER_ID part is replaced with the endpoint
279+
// computed from the template.
280+
// The following variables are available to the template:
281+
// - Index: the Redpanda broker progressive number
282+
// - HostIP: the ip address of the Node, as reported in pod status
283+
//
284+
// Common template functions from Sprig (http://masterminds.github.io/sprig/)
285+
// are also available. The set of available functions is limited to hermetic
286+
// functions because template application needs to be deterministic.
287+
EndpointTemplate string `json:"endpointTemplate,omitempty"`
271288
// The preferred address type to be assigned to the external
272289
// advertised addresses. The valid types are ExternalDNS,
273290
// ExternalIP, InternalDNS, InternalIP, and Hostname.
@@ -908,12 +925,14 @@ type TLSConfig struct {
908925
// Kafka API
909926

910927
// GetPort returns API port
928+
//
911929
//nolint:gocritic // TODO KafkaAPI is now 81 bytes, consider a pointer
912930
func (k KafkaAPI) GetPort() int {
913931
return k.Port
914932
}
915933

916934
// GetTLS returns API TLSConfig
935+
//
917936
//nolint:gocritic // TODO KafkaAPI is now 81 bytes, consider a pointer
918937
func (k KafkaAPI) GetTLS() *TLSConfig {
919938
return &TLSConfig{
@@ -925,6 +944,7 @@ func (k KafkaAPI) GetTLS() *TLSConfig {
925944
}
926945

927946
// GetExternal returns API's ExternalConnectivityConfig
947+
//
928948
//nolint:gocritic // TODO KafkaAPI is now 81 bytes, consider a pointer
929949
func (k KafkaAPI) GetExternal() *ExternalConnectivityConfig {
930950
return &k.External

src/go/k8s/apis/redpanda/v1alpha1/cluster_types_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"k8s.io/utils/pointer"
2222
)
2323

24-
// nolint:funlen // this is ok for a test
24+
//nolint:funlen // this is ok for a test
2525
func TestRedpandaResourceRequirements(t *testing.T) {
2626
type test struct {
2727
name string

src/go/k8s/apis/redpanda/v1alpha1/cluster_webhook.go

+61
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"strconv"
1616

1717
cmmeta "github.com/jetstack/cert-manager/pkg/apis/meta/v1"
18+
"github.com/redpanda-data/redpanda/src/go/k8s/pkg/utils"
1819
corev1 "k8s.io/api/core/v1"
1920
apierrors "k8s.io/apimachinery/pkg/api/errors"
2021
"k8s.io/apimachinery/pkg/api/resource"
@@ -277,6 +278,12 @@ func (r *Cluster) validateAdminListeners() field.ErrorList {
277278
r.Spec.Configuration.AdminAPI,
278279
"bootstrap loadbalancer not available for http admin api"))
279280
}
281+
if externalAdmin != nil && externalAdmin.External.EndpointTemplate != "" {
282+
allErrs = append(allErrs,
283+
field.Invalid(field.NewPath("spec").Child("configuration").Child("adminApi"),
284+
r.Spec.Configuration.AdminAPI,
285+
"cannot provide an endpoint template for admin listener"))
286+
}
280287

281288
// for now only one listener can have TLS to be backward compatible with v1alpha1 API
282289
foundListenerWithTLS := false
@@ -306,6 +313,7 @@ func (r *Cluster) validateKafkaListeners() field.ErrorList {
306313
}
307314

308315
var external *KafkaAPI
316+
var externalIdx int
309317
for i, p := range r.Spec.Configuration.KafkaAPI {
310318
if p.External.Enabled {
311319
if external != nil {
@@ -315,6 +323,7 @@ func (r *Cluster) validateKafkaListeners() field.ErrorList {
315323
"only one kafka api listener can be marked as external"))
316324
}
317325
external = &r.Spec.Configuration.KafkaAPI[i]
326+
externalIdx = i
318327
}
319328
}
320329

@@ -357,10 +366,36 @@ func (r *Cluster) validateKafkaListeners() field.ErrorList {
357366
r.Spec.Configuration.KafkaAPI,
358367
"bootstrap port cannot be empty"))
359368
}
369+
//nolint:dupl // not identical
370+
if external != nil && external.External.EndpointTemplate != "" {
371+
if external.External.Subdomain == "" {
372+
allErrs = append(allErrs,
373+
field.Invalid(field.NewPath("spec").Child("configuration").Child("kafkaApi").Index(externalIdx).Child("external"),
374+
external.External,
375+
"endpointTemplate can only be used in combination with subdomain"))
376+
}
377+
378+
err := checkValidEndpointTemplate(external.External.EndpointTemplate)
379+
if err != nil {
380+
log.Error(err, "Invalid endpoint template received", "template", external.External.EndpointTemplate)
381+
allErrs = append(allErrs,
382+
field.Invalid(field.NewPath("spec").Child("configuration").Child("kafkaApi").Index(externalIdx).Child("external").Child("endpointTemplate"),
383+
external.External.EndpointTemplate,
384+
fmt.Sprintf("template is invalid: %v", err)))
385+
}
386+
}
360387

361388
return allErrs
362389
}
363390

391+
func checkValidEndpointTemplate(tmpl string) error {
392+
// Using an example input to ensure that the template expression is allowed
393+
data := utils.NewEndpointTemplateData(0, "1.2.3.4")
394+
_, err := utils.ComputeEndpoint(tmpl, data)
395+
return err
396+
}
397+
398+
//nolint:funlen,gocyclo // it's a sequence of checks
364399
func (r *Cluster) validatePandaproxyListeners() field.ErrorList {
365400
var allErrs field.ErrorList
366401
var proxyExternal *PandaproxyAPI
@@ -412,6 +447,25 @@ func (r *Cluster) validatePandaproxyListeners() field.ErrorList {
412447
r.Spec.Configuration.PandaproxyAPI[i],
413448
"sudomain of external pandaproxy must be the same as kafka's"))
414449
}
450+
//nolint:dupl // not identical
451+
if kafkaExternal != nil && proxyExternal.External.EndpointTemplate != "" {
452+
if proxyExternal.External.Subdomain == "" {
453+
allErrs = append(allErrs,
454+
field.Invalid(field.NewPath("spec").Child("configuration").Child("pandaproxyApi").Index(i).Child("external"),
455+
proxyExternal.External,
456+
"endpointTemplate can only be used in combination with subdomain"))
457+
}
458+
459+
err := checkValidEndpointTemplate(proxyExternal.External.EndpointTemplate)
460+
if err != nil {
461+
log.Error(err, "Invalid endpoint template received", "template", proxyExternal.External.EndpointTemplate)
462+
allErrs = append(allErrs,
463+
field.Invalid(field.NewPath("spec").Child("configuration").Child("pandaproxyApi").Index(i).
464+
Child("external").Child("endpointTemplate"),
465+
proxyExternal.External.EndpointTemplate,
466+
fmt.Sprintf("template is invalid: %v", err)))
467+
}
468+
}
415469
}
416470

417471
// for now only one listener can have TLS to be backward compatible with v1alpha1 API
@@ -510,6 +564,13 @@ func (r *Cluster) validateSchemaRegistryListener() field.ErrorList {
510564
r.Spec.Configuration.SchemaRegistry.External,
511565
"bootstrap loadbalancer not available for schema reigstry"))
512566
}
567+
if schemaRegistry.External.EndpointTemplate != "" {
568+
allErrs = append(allErrs,
569+
field.Invalid(field.NewPath("spec").Child("configuration").Child("schemaRegistry").Child("external").Child("endpointTemplate"),
570+
r.Spec.Configuration.SchemaRegistry.External.EndpointTemplate,
571+
"cannot provide an endpoint template for schema registry"))
572+
}
573+
513574
return allErrs
514575
}
515576

0 commit comments

Comments
 (0)