@@ -67,15 +67,7 @@ func (r *Storage) GetGRPCServiceEndpoint() string {
6767}
6868
6969func (r * Storage ) GetHostFromConfigEndpoint () string {
70- var rawYamlConfiguration string
71- // skip handle error because we already checked in webhook
72- success , dynConfig , _ := ParseDynConfig (r .Spec .Configuration )
73- if success {
74- config , _ := yaml .Marshal (dynConfig .Config )
75- rawYamlConfiguration = string (config )
76- } else {
77- rawYamlConfiguration = r .Spec .Configuration
78- }
70+ rawYamlConfiguration := r .getRawYamlConfiguration ()
7971
8072 configuration , _ := ParseConfiguration (rawYamlConfiguration )
8173 randNum := rand .Intn (len (configuration .Hosts )) // #nosec G404
@@ -443,6 +435,115 @@ func (r *Storage) ValidateUpdate(old runtime.Object) error {
443435 return crdCheckError
444436 }
445437
438+ if err := r .validateGrpcPorts (); err != nil {
439+ return err
440+ }
441+
442+ return nil
443+ }
444+
445+ func (r * Storage ) getRawYamlConfiguration () string {
446+ var rawYamlConfiguration string
447+ // skip handle error because we already checked in webhook
448+ success , dynConfig , _ := ParseDynConfig (r .Spec .Configuration )
449+ if success {
450+ config , _ := yaml .Marshal (dynConfig .Config )
451+ rawYamlConfiguration = string (config )
452+ } else {
453+ rawYamlConfiguration = r .Spec .Configuration
454+ }
455+
456+ return rawYamlConfiguration
457+ }
458+
459+ func (r * Storage ) validateGrpcPorts () error {
460+ // There are three possible ways to configure grpc ports:
461+
462+ // service:
463+ // grpc: == this means one insecure port, tls is disabled
464+ // port: 2135
465+
466+ // service:
467+ // grpc:
468+ // port: 2136 == this means one secure port, tls is enabled
469+ // tls:
470+ // enabled: true
471+
472+ // service:
473+ // grpc:
474+ // insecurePort: 2135 == this means two ports, one secure \ one insecure
475+ // port: 2136
476+ // tls:
477+ // enabled: true
478+
479+ rawYamlConfiguration := r .getRawYamlConfiguration ()
480+ configuration , err := ParseConfiguration (rawYamlConfiguration )
481+ if err != nil {
482+ return fmt .Errorf ("failed to parse configuration immediately after building it, should not happen, %w" , err )
483+ }
484+ configurationPort := int32 (GRPCPort )
485+ if configuration .GrpcConfig .Port != 0 {
486+ configurationPort = configuration .GrpcConfig .Port
487+ }
488+ configurationSslPort := int32 (0 )
489+ if configuration .GrpcConfig .SslPort != 0 {
490+ configurationSslPort = configuration .GrpcConfig .SslPort
491+ }
492+
493+ if ! r .Spec .Service .GRPC .TLSConfiguration .Enabled {
494+ // there should be only 1 port, both in service and in config, insecure
495+ servicePort := int32 (GRPCPort )
496+ if r .Spec .Service .GRPC .Port != 0 {
497+ servicePort = r .Spec .Service .GRPC .Port
498+ }
499+ if configurationPort != servicePort {
500+ return fmt .Errorf (
501+ "inconsistent grpc ports: spec.service.grpc.port (%v) != configuration.grpc_config.port (%v)" ,
502+ servicePort ,
503+ configurationPort ,
504+ )
505+ }
506+
507+ if r .Spec .Service .GRPC .InsecurePort != 0 {
508+ return fmt .Errorf (
509+ "spec.service.grpc.tls.enabled is false, use `port` instead of `insecurePort` field to assign non-tls grpc port" ,
510+ )
511+ }
512+ return nil
513+ }
514+
515+ // otherwise, there might be 1 (secure only) port...
516+ servicePort := int32 (GRPCPort )
517+ if r .Spec .Service .GRPC .Port != 0 {
518+ servicePort = r .Spec .Service .GRPC .Port
519+ }
520+ if configurationSslPort == 0 {
521+ return fmt .Errorf (
522+ "configuration.grpc_config.ssl_port is absent in cluster configuration, but spec.service.grpc has tls enabled and port %v" ,
523+ servicePort ,
524+ )
525+ }
526+ if configurationSslPort != servicePort {
527+ return fmt .Errorf (
528+ "inconsistent grpc ports: spec.service.grpc.port (%v) != configuration.grpc_config.ssl_port (%v)" ,
529+ servicePort ,
530+ configurationSslPort ,
531+ )
532+ }
533+
534+ // or, optionally, one more: insecure port
535+ if r .Spec .Service .GRPC .InsecurePort != 0 {
536+ serviceInsecurePort := r .Spec .Service .GRPC .InsecurePort
537+
538+ if configurationPort != serviceInsecurePort {
539+ return fmt .Errorf (
540+ "inconsistent grpc insecure ports: spec.service.grpc.insecure_port (%v) != configuration.grpc_config.port (%v)" ,
541+ serviceInsecurePort ,
542+ configurationPort ,
543+ )
544+ }
545+ }
546+
446547 return nil
447548}
448549
0 commit comments