From f5262ae033aded885dac48f4190856a97c882cef Mon Sep 17 00:00:00 2001 From: Dmitrii Gadeev Date: Mon, 22 Apr 2024 16:39:41 +0200 Subject: [PATCH 1/7] allow to customize readiness probe path --- pkg/components/httpproxy.go | 13 ++++++++++ pkg/components/rpcproxy.go | 7 ++++++ pkg/components/server.go | 35 +++++++++++++++++++-------- pkg/components/serveroptions.go | 42 +++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 pkg/components/serveroptions.go diff --git a/pkg/components/httpproxy.go b/pkg/components/httpproxy.go index 76a9219e..53415e20 100644 --- a/pkg/components/httpproxy.go +++ b/pkg/components/httpproxy.go @@ -56,6 +56,19 @@ func NewHTTPProxy( func() ([]byte, error) { return cfgen.GetHTTPProxyConfig(spec) }, + WithComponentContainerPorts([]corev1.ContainerPort{ + { + Name: "http-proxy-http", + ContainerPort: consts.HTTPProxyHTTPPort, + Protocol: corev1.ProtocolTCP, + }, + { + Name: "http-proxy-https", + ContainerPort: consts.HTTPProxyHTTPSPort, + Protocol: corev1.ProtocolTCP, + }, + }), + WithReadinessProbeHTTPPath("/ping"), ) var httpsSecret *resources.TLSSecret diff --git a/pkg/components/rpcproxy.go b/pkg/components/rpcproxy.go index 3bc223fc..a62368a2 100644 --- a/pkg/components/rpcproxy.go +++ b/pkg/components/rpcproxy.go @@ -53,6 +53,13 @@ func NewRPCProxy( func() ([]byte, error) { return cfgen.GetRPCProxyConfig(spec) }, + WithComponentContainerPorts([]corev1.ContainerPort{ + { + Name: "rpc-proxy", + ContainerPort: consts.RPCProxyRPCPort, + Protocol: corev1.ProtocolTCP, + }, + }), ) var balancingService *resources.RPCService = nil diff --git a/pkg/components/server.go b/pkg/components/server.go index d17f2d46..af72f7c9 100644 --- a/pkg/components/server.go +++ b/pkg/components/server.go @@ -55,6 +55,9 @@ type serverImpl struct { configHelper *ConfigHelper builtStatefulSet *appsv1.StatefulSet + + readinessProbeHTTPPath string + componentContainerPorts []corev1.ContainerPort } func newServer( @@ -63,6 +66,7 @@ func newServer( instanceSpec *ytv1.InstanceSpec, binaryPath, configFileName, statefulSetName, serviceName string, generator ytconfig.YsonGeneratorFunc, + options ...Option, ) server { proxy := ytsaurus.APIProxy() commonSpec := ytsaurus.GetCommonSpec() @@ -73,6 +77,7 @@ func newServer( instanceSpec, binaryPath, configFileName, statefulSetName, serviceName, generator, + options..., ) } @@ -83,6 +88,7 @@ func newServerConfigured( instanceSpec *ytv1.InstanceSpec, binaryPath, configFileName, statefulSetName, serviceName string, generator ytconfig.YsonGeneratorFunc, + options ...Option, ) server { image := commonSpec.CoreImage if instanceSpec.Image != nil { @@ -107,7 +113,7 @@ func newServerConfigured( consts.BusSecretMountPoint) } - return &serverImpl{ + srv := &serverImpl{ labeller: l, image: image, proxy: proxy, @@ -143,7 +149,22 @@ func newServerConfigured( Fmt: ytconfig.ConfigFormatYson, }, }), + + readinessProbeHTTPPath: readinessProbeHTTPPath, + componentContainerPorts: []corev1.ContainerPort{ + { + Name: serverAPIPortName, + ContainerPort: *instanceSpec.MonitoringPort, + Protocol: corev1.ProtocolTCP, + }, + }, } + + for _, opt := range options { + opt.apply(srv) + } + + return srv } func (s *serverImpl) Fetch(ctx context.Context) error { @@ -272,19 +293,13 @@ func (s *serverImpl) rebuildStatefulSet() *appsv1.StatefulSet { Name: consts.YTServerContainerName, Command: command, VolumeMounts: volumeMounts, - Ports: []corev1.ContainerPort{ - { - Name: serverAPIPortName, - ContainerPort: *s.instanceSpec.MonitoringPort, - Protocol: corev1.ProtocolTCP, - }, - }, - Resources: s.instanceSpec.Resources, + Ports: s.componentContainerPorts, + Resources: s.instanceSpec.Resources, ReadinessProbe: &corev1.Probe{ ProbeHandler: corev1.ProbeHandler{ HTTPGet: &corev1.HTTPGetAction{ Port: intstr.FromString(serverAPIPortName), - Path: readinessProbeHTTPPath, + Path: s.readinessProbeHTTPPath, }, }, InitialDelaySeconds: readinessProbeParams.InitialDelaySeconds, diff --git a/pkg/components/serveroptions.go b/pkg/components/serveroptions.go new file mode 100644 index 00000000..92cf5dee --- /dev/null +++ b/pkg/components/serveroptions.go @@ -0,0 +1,42 @@ +package components + +import corev1 "k8s.io/api/core/v1" + +type Option interface { + apply(srv *serverImpl) +} + +var ( + _ Option = &ReadinessProbeHTTPPath{} + _ Option = &ComponentContainerPorts{} +) + +type ReadinessProbeHTTPPath struct { + path string +} + +func (r ReadinessProbeHTTPPath) apply(srv *serverImpl) { + srv.readinessProbeHTTPPath = r.path +} + +func WithReadinessProbeHTTPPath(path string) Option { + return ReadinessProbeHTTPPath{ + path: path, + } +} + +type ComponentContainerPorts struct { + ports []corev1.ContainerPort +} + +func (c ComponentContainerPorts) apply(srv *serverImpl) { + for _, port := range c.ports { + srv.componentContainerPorts = append(srv.componentContainerPorts, port) + } +} + +func WithComponentContainerPorts(ports []corev1.ContainerPort) Option { + return ComponentContainerPorts{ + ports: ports, + } +} From 5835578cabf31525bad6e803472ffb408b5967d4 Mon Sep 17 00:00:00 2001 From: Dmitrii Gadeev Date: Mon, 22 Apr 2024 16:44:17 +0200 Subject: [PATCH 2/7] linting --- pkg/components/serveroptions.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/components/serveroptions.go b/pkg/components/serveroptions.go index 92cf5dee..b5ddd620 100644 --- a/pkg/components/serveroptions.go +++ b/pkg/components/serveroptions.go @@ -30,9 +30,7 @@ type ComponentContainerPorts struct { } func (c ComponentContainerPorts) apply(srv *serverImpl) { - for _, port := range c.ports { - srv.componentContainerPorts = append(srv.componentContainerPorts, port) - } + srv.componentContainerPorts = append(srv.componentContainerPorts, c.ports...) } func WithComponentContainerPorts(ports []corev1.ContainerPort) Option { From f0fbc7aabfbbd1916e3e0b7a21f5bdb15a254550 Mon Sep 17 00:00:00 2001 From: Dmitrii Gadeev Date: Tue, 23 Apr 2024 08:57:28 +0200 Subject: [PATCH 3/7] fix --- pkg/components/httpproxy.go | 7 ++++--- pkg/components/rpcproxy.go | 2 +- pkg/components/server.go | 13 ++++++++----- pkg/components/serveroptions.go | 27 +++++++++++++++++++-------- pkg/consts/address.go | 7 +++++-- pkg/resources/monitoring_service.go | 2 +- 6 files changed, 38 insertions(+), 20 deletions(-) diff --git a/pkg/components/httpproxy.go b/pkg/components/httpproxy.go index 53415e20..6a3d3082 100644 --- a/pkg/components/httpproxy.go +++ b/pkg/components/httpproxy.go @@ -4,6 +4,7 @@ import ( "context" "go.ytsaurus.tech/library/go/ptr" + "k8s.io/apimachinery/pkg/util/intstr" corev1 "k8s.io/api/core/v1" @@ -58,17 +59,17 @@ func NewHTTPProxy( }, WithComponentContainerPorts([]corev1.ContainerPort{ { - Name: "http-proxy-http", + Name: "http", ContainerPort: consts.HTTPProxyHTTPPort, Protocol: corev1.ProtocolTCP, }, { - Name: "http-proxy-https", + Name: "https", ContainerPort: consts.HTTPProxyHTTPSPort, Protocol: corev1.ProtocolTCP, }, }), - WithReadinessProbeHTTPPath("/ping"), + WithCustomReadinessProbeEndpoint(ptr.T(intstr.FromInt32(consts.HTTPProxyHTTPPort)), ptr.T("/ping")), ) var httpsSecret *resources.TLSSecret diff --git a/pkg/components/rpcproxy.go b/pkg/components/rpcproxy.go index a62368a2..fb8292b8 100644 --- a/pkg/components/rpcproxy.go +++ b/pkg/components/rpcproxy.go @@ -55,7 +55,7 @@ func NewRPCProxy( }, WithComponentContainerPorts([]corev1.ContainerPort{ { - Name: "rpc-proxy", + Name: "rpc", ContainerPort: consts.RPCProxyRPCPort, Protocol: corev1.ProtocolTCP, }, diff --git a/pkg/components/server.go b/pkg/components/server.go index af72f7c9..21267054 100644 --- a/pkg/components/server.go +++ b/pkg/components/server.go @@ -20,7 +20,6 @@ import ( ) const ( - serverAPIPortName = "http" readinessProbeHTTPPath = "/orchid/service" ) @@ -56,8 +55,10 @@ type serverImpl struct { builtStatefulSet *appsv1.StatefulSet - readinessProbeHTTPPath string componentContainerPorts []corev1.ContainerPort + + readinessProbePort intstr.IntOrString + readinessProbeHTTPPath string } func newServer( @@ -150,14 +151,16 @@ func newServerConfigured( }, }), - readinessProbeHTTPPath: readinessProbeHTTPPath, componentContainerPorts: []corev1.ContainerPort{ { - Name: serverAPIPortName, + Name: consts.YTMonitoringContainerPortName, ContainerPort: *instanceSpec.MonitoringPort, Protocol: corev1.ProtocolTCP, }, }, + + readinessProbePort: intstr.FromString(consts.YTMonitoringContainerPortName), + readinessProbeHTTPPath: readinessProbeHTTPPath, } for _, opt := range options { @@ -298,7 +301,7 @@ func (s *serverImpl) rebuildStatefulSet() *appsv1.StatefulSet { ReadinessProbe: &corev1.Probe{ ProbeHandler: corev1.ProbeHandler{ HTTPGet: &corev1.HTTPGetAction{ - Port: intstr.FromString(serverAPIPortName), + Port: s.readinessProbePort, Path: s.readinessProbeHTTPPath, }, }, diff --git a/pkg/components/serveroptions.go b/pkg/components/serveroptions.go index b5ddd620..8e4ee536 100644 --- a/pkg/components/serveroptions.go +++ b/pkg/components/serveroptions.go @@ -1,26 +1,37 @@ package components -import corev1 "k8s.io/api/core/v1" +import ( + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/util/intstr" +) type Option interface { apply(srv *serverImpl) } var ( - _ Option = &ReadinessProbeHTTPPath{} + _ Option = &CustomReadinessProbeEndpoint{} _ Option = &ComponentContainerPorts{} ) -type ReadinessProbeHTTPPath struct { - path string +type CustomReadinessProbeEndpoint struct { + port *intstr.IntOrString + path *string } -func (r ReadinessProbeHTTPPath) apply(srv *serverImpl) { - srv.readinessProbeHTTPPath = r.path +func (c CustomReadinessProbeEndpoint) apply(srv *serverImpl) { + if c.port != nil { + srv.readinessProbePort = *c.port + } + + if c.path != nil { + srv.readinessProbeHTTPPath = *c.path + } } -func WithReadinessProbeHTTPPath(path string) Option { - return ReadinessProbeHTTPPath{ +func WithCustomReadinessProbeEndpoint(port *intstr.IntOrString, path *string) Option { + return CustomReadinessProbeEndpoint{ + port: port, path: path, } } diff --git a/pkg/consts/address.go b/pkg/consts/address.go index 2f960873..ec2aafeb 100644 --- a/pkg/consts/address.go +++ b/pkg/consts/address.go @@ -1,7 +1,10 @@ package consts -const YTMonitoringPortName = "ytsaurus-metrics" -const YTMonitoringPort = 10000 +const ( + YTMonitoringContainerPortName = "metrics" + YTMonitoringServicePortName = "ytsaurus-metrics" + YTMonitoringPort = 10000 +) const ( DiscoveryRPCPort = 9020 diff --git a/pkg/resources/monitoring_service.go b/pkg/resources/monitoring_service.go index bfd54e5c..68a589e6 100644 --- a/pkg/resources/monitoring_service.go +++ b/pkg/resources/monitoring_service.go @@ -63,7 +63,7 @@ func (s *MonitoringService) Build() *corev1.Service { Selector: s.labeller.GetSelectorLabelMap(), Ports: []corev1.ServicePort{ { - Name: consts.YTMonitoringPortName, + Name: consts.YTMonitoringServicePortName, Port: consts.YTMonitoringPort, TargetPort: intstr.IntOrString{IntVal: s.monitoringTargetPort}, }, From c081a145b8b9e83d00722f75b9b456c392a1e0fc Mon Sep 17 00:00:00 2001 From: Dmitrii Gadeev Date: Thu, 25 Apr 2024 16:49:03 +0200 Subject: [PATCH 4/7] stage --- pkg/components/controller_agent.go | 6 ++++++ pkg/components/data_node.go | 6 ++++++ pkg/components/discovery.go | 6 ++++++ pkg/components/exec_node.go | 6 ++++++ pkg/components/exec_node_remote.go | 6 ++++++ pkg/components/httpproxy.go | 13 +++++++++---- pkg/components/master.go | 5 +++++ pkg/components/master_caches.go | 6 ++++++ pkg/components/query_tracker.go | 5 +++++ pkg/components/queue_agent.go | 5 +++++ pkg/components/rpcproxy.go | 10 ++++------ pkg/components/scheduler.go | 5 +++++ pkg/components/serveroptions.go | 2 +- pkg/components/tablet_node.go | 6 ++++++ pkg/components/yql_agent.go | 5 +++++ pkg/consts/address.go | 2 ++ 16 files changed, 83 insertions(+), 11 deletions(-) diff --git a/pkg/components/controller_agent.go b/pkg/components/controller_agent.go index b9389ceb..6232406a 100644 --- a/pkg/components/controller_agent.go +++ b/pkg/components/controller_agent.go @@ -4,6 +4,7 @@ import ( "context" "go.ytsaurus.tech/library/go/ptr" + corev1 "k8s.io/api/core/v1" ytv1 "github.com/ytsaurus/yt-k8s-operator/api/v1" "github.com/ytsaurus/yt-k8s-operator/pkg/apiproxy" @@ -41,6 +42,11 @@ func NewControllerAgent(cfgen *ytconfig.Generator, ytsaurus *apiproxy.Ytsaurus, "ca", "controller-agents", func() ([]byte, error) { return cfgen.GetControllerAgentConfig(resource.Spec.ControllerAgents) }, + WithContainerPorts(corev1.ContainerPort{ + Name: consts.YTRPCPortName, + ContainerPort: consts.ControllerAgentRPCPort, + Protocol: corev1.ProtocolTCP, + }), ) return &ControllerAgent{ diff --git a/pkg/components/data_node.go b/pkg/components/data_node.go index 7cb22794..3d5f4cb7 100644 --- a/pkg/components/data_node.go +++ b/pkg/components/data_node.go @@ -4,6 +4,7 @@ import ( "context" "go.ytsaurus.tech/library/go/ptr" + corev1 "k8s.io/api/core/v1" ytv1 "github.com/ytsaurus/yt-k8s-operator/api/v1" "github.com/ytsaurus/yt-k8s-operator/pkg/apiproxy" @@ -48,6 +49,11 @@ func NewDataNode( func() ([]byte, error) { return cfgen.GetDataNodeConfig(spec) }, + WithContainerPorts(corev1.ContainerPort{ + Name: consts.YTRPCPortName, + ContainerPort: consts.DataNodeRPCPort, + Protocol: corev1.ProtocolTCP, + }), ) return &DataNode{ diff --git a/pkg/components/discovery.go b/pkg/components/discovery.go index 87c2f8be..cc580ee9 100644 --- a/pkg/components/discovery.go +++ b/pkg/components/discovery.go @@ -4,6 +4,7 @@ import ( "context" "go.ytsaurus.tech/library/go/ptr" + corev1 "k8s.io/api/core/v1" ytv1 "github.com/ytsaurus/yt-k8s-operator/api/v1" "github.com/ytsaurus/yt-k8s-operator/pkg/apiproxy" @@ -42,6 +43,11 @@ func NewDiscovery(cfgen *ytconfig.Generator, ytsaurus *apiproxy.Ytsaurus) *Disco func() ([]byte, error) { return cfgen.GetDiscoveryConfig(&resource.Spec.Discovery) }, + WithContainerPorts(corev1.ContainerPort{ + Name: consts.YTRPCPortName, + ContainerPort: consts.DiscoveryRPCPort, + Protocol: corev1.ProtocolTCP, + }), ) return &Discovery{ diff --git a/pkg/components/exec_node.go b/pkg/components/exec_node.go index ea0605e0..d607909d 100644 --- a/pkg/components/exec_node.go +++ b/pkg/components/exec_node.go @@ -4,6 +4,7 @@ import ( "context" "go.ytsaurus.tech/library/go/ptr" + corev1 "k8s.io/api/core/v1" ytv1 "github.com/ytsaurus/yt-k8s-operator/api/v1" "github.com/ytsaurus/yt-k8s-operator/pkg/apiproxy" @@ -47,6 +48,11 @@ func NewExecNode( func() ([]byte, error) { return cfgen.GetExecNodeConfig(spec) }, + WithContainerPorts(corev1.ContainerPort{ + Name: consts.YTRPCPortName, + ContainerPort: consts.ExecNodeRPCPort, + Protocol: corev1.ProtocolTCP, + }), ) var sidecarConfig *ConfigHelper diff --git a/pkg/components/exec_node_remote.go b/pkg/components/exec_node_remote.go index 7c51e145..24b70e67 100644 --- a/pkg/components/exec_node_remote.go +++ b/pkg/components/exec_node_remote.go @@ -4,6 +4,7 @@ import ( "context" "go.ytsaurus.tech/library/go/ptr" + corev1 "k8s.io/api/core/v1" ytv1 "github.com/ytsaurus/yt-k8s-operator/api/v1" "github.com/ytsaurus/yt-k8s-operator/pkg/apiproxy" @@ -47,6 +48,11 @@ func NewRemoteExecNodes( func() ([]byte, error) { return cfgen.GetExecNodeConfig(spec) }, + WithContainerPorts(corev1.ContainerPort{ + Name: consts.YTRPCPortName, + ContainerPort: consts.ExecNodeRPCPort, + Protocol: corev1.ProtocolTCP, + }), ) return &RemoteExecNode{ baseComponent: baseComponent{labeller: &l}, diff --git a/pkg/components/httpproxy.go b/pkg/components/httpproxy.go index 6a3d3082..ced14e00 100644 --- a/pkg/components/httpproxy.go +++ b/pkg/components/httpproxy.go @@ -57,18 +57,23 @@ func NewHTTPProxy( func() ([]byte, error) { return cfgen.GetHTTPProxyConfig(spec) }, - WithComponentContainerPorts([]corev1.ContainerPort{ - { + WithContainerPorts( + corev1.ContainerPort{ + Name: consts.YTRPCPortName, + ContainerPort: consts.HTTPProxyRPCPort, + Protocol: corev1.ProtocolTCP, + }, + corev1.ContainerPort{ Name: "http", ContainerPort: consts.HTTPProxyHTTPPort, Protocol: corev1.ProtocolTCP, }, - { + corev1.ContainerPort{ Name: "https", ContainerPort: consts.HTTPProxyHTTPSPort, Protocol: corev1.ProtocolTCP, }, - }), + ), WithCustomReadinessProbeEndpoint(ptr.T(intstr.FromInt32(consts.HTTPProxyHTTPPort)), ptr.T("/ping")), ) diff --git a/pkg/components/master.go b/pkg/components/master.go index 47ba8cda..57f71144 100644 --- a/pkg/components/master.go +++ b/pkg/components/master.go @@ -55,6 +55,11 @@ func NewMaster(cfgen *ytconfig.Generator, ytsaurus *apiproxy.Ytsaurus) *Master { cfgen.GetMastersStatefulSetName(), cfgen.GetMastersServiceName(), func() ([]byte, error) { return cfgen.GetMasterConfig(&resource.Spec.PrimaryMasters) }, + WithContainerPorts(corev1.ContainerPort{ + Name: consts.YTRPCPortName, + ContainerPort: consts.MasterRPCPort, + Protocol: corev1.ProtocolTCP, + }), ) initJob := NewInitJob( diff --git a/pkg/components/master_caches.go b/pkg/components/master_caches.go index c1588a3c..2d6b9fa5 100644 --- a/pkg/components/master_caches.go +++ b/pkg/components/master_caches.go @@ -4,6 +4,7 @@ import ( "context" "go.ytsaurus.tech/library/go/ptr" + corev1 "k8s.io/api/core/v1" ytv1 "github.com/ytsaurus/yt-k8s-operator/api/v1" "github.com/ytsaurus/yt-k8s-operator/pkg/apiproxy" @@ -41,6 +42,11 @@ func NewMasterCache(cfgen *ytconfig.Generator, ytsaurus *apiproxy.Ytsaurus) *Mas cfgen.GetMasterCachesStatefulSetName(), cfgen.GetMasterCachesServiceName(), func() ([]byte, error) { return cfgen.GetMasterCachesConfig(resource.Spec.MasterCaches) }, + WithContainerPorts(corev1.ContainerPort{ + Name: consts.YTRPCPortName, + ContainerPort: consts.MasterCachesRPCPort, + Protocol: corev1.ProtocolTCP, + }), ) return &MasterCache{ diff --git a/pkg/components/query_tracker.go b/pkg/components/query_tracker.go index ad1a7f79..60007a55 100644 --- a/pkg/components/query_tracker.go +++ b/pkg/components/query_tracker.go @@ -59,6 +59,11 @@ func NewQueryTracker( cfgen.GetQueryTrackerStatefulSetName(), cfgen.GetQueryTrackerServiceName(), func() ([]byte, error) { return cfgen.GetQueryTrackerConfig(resource.Spec.QueryTrackers) }, + WithContainerPorts(corev1.ContainerPort{ + Name: consts.YTRPCPortName, + ContainerPort: consts.QueryTrackerRPCPort, + Protocol: corev1.ProtocolTCP, + }), ) image := ytsaurus.GetResource().Spec.CoreImage diff --git a/pkg/components/queue_agent.go b/pkg/components/queue_agent.go index b0481b32..4017106c 100644 --- a/pkg/components/queue_agent.go +++ b/pkg/components/queue_agent.go @@ -61,6 +61,11 @@ func NewQueueAgent( cfgen.GetQueueAgentStatefulSetName(), cfgen.GetQueueAgentServiceName(), func() ([]byte, error) { return cfgen.GetQueueAgentConfig(resource.Spec.QueueAgents) }, + WithContainerPorts(corev1.ContainerPort{ + Name: consts.YTRPCPortName, + ContainerPort: consts.QueueAgentRPCPort, + Protocol: corev1.ProtocolTCP, + }), ) image := ytsaurus.GetResource().Spec.CoreImage diff --git a/pkg/components/rpcproxy.go b/pkg/components/rpcproxy.go index fb8292b8..c25ae8c5 100644 --- a/pkg/components/rpcproxy.go +++ b/pkg/components/rpcproxy.go @@ -53,12 +53,10 @@ func NewRPCProxy( func() ([]byte, error) { return cfgen.GetRPCProxyConfig(spec) }, - WithComponentContainerPorts([]corev1.ContainerPort{ - { - Name: "rpc", - ContainerPort: consts.RPCProxyRPCPort, - Protocol: corev1.ProtocolTCP, - }, + WithContainerPorts(corev1.ContainerPort{ + Name: consts.YTRPCPortName, + ContainerPort: consts.RPCProxyRPCPort, + Protocol: corev1.ProtocolTCP, }), ) diff --git a/pkg/components/scheduler.go b/pkg/components/scheduler.go index 629806e0..cd715424 100644 --- a/pkg/components/scheduler.go +++ b/pkg/components/scheduler.go @@ -59,6 +59,11 @@ func NewScheduler( func() ([]byte, error) { return cfgen.GetSchedulerConfig(resource.Spec.Schedulers) }, + WithContainerPorts(corev1.ContainerPort{ + Name: consts.YTRPCPortName, + ContainerPort: consts.SchedulerRPCPort, + Protocol: corev1.ProtocolTCP, + }), ) return &Scheduler{ diff --git a/pkg/components/serveroptions.go b/pkg/components/serveroptions.go index 8e4ee536..8dfdf10a 100644 --- a/pkg/components/serveroptions.go +++ b/pkg/components/serveroptions.go @@ -44,7 +44,7 @@ func (c ComponentContainerPorts) apply(srv *serverImpl) { srv.componentContainerPorts = append(srv.componentContainerPorts, c.ports...) } -func WithComponentContainerPorts(ports []corev1.ContainerPort) Option { +func WithContainerPorts(ports ...corev1.ContainerPort) Option { return ComponentContainerPorts{ ports: ports, } diff --git a/pkg/components/tablet_node.go b/pkg/components/tablet_node.go index 763a7cfc..351cc6f3 100644 --- a/pkg/components/tablet_node.go +++ b/pkg/components/tablet_node.go @@ -7,6 +7,7 @@ import ( "go.ytsaurus.tech/library/go/ptr" "go.ytsaurus.tech/yt/go/ypath" "go.ytsaurus.tech/yt/go/yt" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/log" @@ -62,6 +63,11 @@ func NewTabletNode( func() ([]byte, error) { return cfgen.GetTabletNodeConfig(spec) }, + WithContainerPorts(corev1.ContainerPort{ + Name: consts.YTRPCPortName, + ContainerPort: consts.TabletNodeRPCPort, + Protocol: corev1.ProtocolTCP, + }), ) return &TabletNode{ diff --git a/pkg/components/yql_agent.go b/pkg/components/yql_agent.go index 34296d2a..dd68cf45 100644 --- a/pkg/components/yql_agent.go +++ b/pkg/components/yql_agent.go @@ -51,6 +51,11 @@ func NewYQLAgent(cfgen *ytconfig.Generator, ytsaurus *apiproxy.Ytsaurus, master func() ([]byte, error) { return cfgen.GetYQLAgentConfig(resource.Spec.YQLAgents) }, + WithContainerPorts(corev1.ContainerPort{ + Name: consts.YTRPCPortName, + ContainerPort: consts.YQLAgentRPCPort, + Protocol: corev1.ProtocolTCP, + }), ) return &YqlAgent{ diff --git a/pkg/consts/address.go b/pkg/consts/address.go index ec2aafeb..adc825f8 100644 --- a/pkg/consts/address.go +++ b/pkg/consts/address.go @@ -1,6 +1,8 @@ package consts const ( + YTRPCPortName = "rpc" + YTMonitoringContainerPortName = "metrics" YTMonitoringServicePortName = "ytsaurus-metrics" YTMonitoringPort = 10000 From f43db1da9a5dee5d0dde458fcdc7609f2bcf8c9b Mon Sep 17 00:00:00 2001 From: Dmitrii Gadeev Date: Thu, 25 Apr 2024 17:11:42 +0200 Subject: [PATCH 5/7] stage --- pkg/components/httpproxy.go | 4 +-- pkg/components/server.go | 38 ++++++++++++++------------ pkg/components/serveroptions.go | 48 +++++++++++---------------------- 3 files changed, 37 insertions(+), 53 deletions(-) diff --git a/pkg/components/httpproxy.go b/pkg/components/httpproxy.go index ced14e00..3afac033 100644 --- a/pkg/components/httpproxy.go +++ b/pkg/components/httpproxy.go @@ -4,8 +4,6 @@ import ( "context" "go.ytsaurus.tech/library/go/ptr" - "k8s.io/apimachinery/pkg/util/intstr" - corev1 "k8s.io/api/core/v1" ytv1 "github.com/ytsaurus/yt-k8s-operator/api/v1" @@ -74,7 +72,7 @@ func NewHTTPProxy( Protocol: corev1.ProtocolTCP, }, ), - WithCustomReadinessProbeEndpoint(ptr.T(intstr.FromInt32(consts.HTTPProxyHTTPPort)), ptr.T("/ping")), + WithCustomReadinessProbeEndpoint(ptr.T(int32(consts.HTTPProxyHTTPPort)), ptr.T("/ping")), ) var httpsSecret *resources.TLSSecret diff --git a/pkg/components/server.go b/pkg/components/server.go index 21267054..facd53ec 100644 --- a/pkg/components/server.go +++ b/pkg/components/server.go @@ -89,7 +89,7 @@ func newServerConfigured( instanceSpec *ytv1.InstanceSpec, binaryPath, configFileName, statefulSetName, serviceName string, generator ytconfig.YsonGeneratorFunc, - options ...Option, + optFuncs ...Option, ) server { image := commonSpec.CoreImage if instanceSpec.Image != nil { @@ -114,7 +114,23 @@ func newServerConfigured( consts.BusSecretMountPoint) } - srv := &serverImpl{ + opts := &options{ + containerPorts: []corev1.ContainerPort{ + { + Name: consts.YTMonitoringContainerPortName, + ContainerPort: *instanceSpec.MonitoringPort, + Protocol: corev1.ProtocolTCP, + }, + }, + readinessProbeEndpointPort: intstr.FromString(consts.YTMonitoringContainerPortName), + readinessProbeEndpointPath: readinessProbeHTTPPath, + } + + for _, fn := range optFuncs { + fn(opts) + } + + return &serverImpl{ labeller: l, image: image, proxy: proxy, @@ -151,23 +167,11 @@ func newServerConfigured( }, }), - componentContainerPorts: []corev1.ContainerPort{ - { - Name: consts.YTMonitoringContainerPortName, - ContainerPort: *instanceSpec.MonitoringPort, - Protocol: corev1.ProtocolTCP, - }, - }, - - readinessProbePort: intstr.FromString(consts.YTMonitoringContainerPortName), - readinessProbeHTTPPath: readinessProbeHTTPPath, - } + componentContainerPorts: opts.containerPorts, - for _, opt := range options { - opt.apply(srv) + readinessProbePort: opts.readinessProbeEndpointPort, + readinessProbeHTTPPath: opts.readinessProbeEndpointPath, } - - return srv } func (s *serverImpl) Fetch(ctx context.Context) error { diff --git a/pkg/components/serveroptions.go b/pkg/components/serveroptions.go index 8dfdf10a..70bd14bd 100644 --- a/pkg/components/serveroptions.go +++ b/pkg/components/serveroptions.go @@ -5,47 +5,29 @@ import ( "k8s.io/apimachinery/pkg/util/intstr" ) -type Option interface { - apply(srv *serverImpl) -} - -var ( - _ Option = &CustomReadinessProbeEndpoint{} - _ Option = &ComponentContainerPorts{} -) +type options struct { + containerPorts []corev1.ContainerPort -type CustomReadinessProbeEndpoint struct { - port *intstr.IntOrString - path *string + readinessProbeEndpointPort intstr.IntOrString + readinessProbeEndpointPath string } -func (c CustomReadinessProbeEndpoint) apply(srv *serverImpl) { - if c.port != nil { - srv.readinessProbePort = *c.port - } +type Option func(opts *options) - if c.path != nil { - srv.readinessProbeHTTPPath = *c.path - } -} +func WithCustomReadinessProbeEndpoint(port *int32, path *string) Option { + return func(opts *options) { + if port != nil { + opts.readinessProbeEndpointPort = intstr.FromInt32(*port) + } -func WithCustomReadinessProbeEndpoint(port *intstr.IntOrString, path *string) Option { - return CustomReadinessProbeEndpoint{ - port: port, - path: path, + if path != nil { + opts.readinessProbeEndpointPath = *path + } } } -type ComponentContainerPorts struct { - ports []corev1.ContainerPort -} - -func (c ComponentContainerPorts) apply(srv *serverImpl) { - srv.componentContainerPorts = append(srv.componentContainerPorts, c.ports...) -} - func WithContainerPorts(ports ...corev1.ContainerPort) Option { - return ComponentContainerPorts{ - ports: ports, + return func(opts *options) { + opts.containerPorts = append(opts.containerPorts, ports...) } } From 4374a3e23d2c7dfc31aa4da8d2b9af799fe88360 Mon Sep 17 00:00:00 2001 From: Dmitrii Gadeev Date: Thu, 25 Apr 2024 18:51:14 +0200 Subject: [PATCH 6/7] refactor --- pkg/components/httpproxy.go | 3 ++- pkg/components/serveroptions.go | 14 +++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/pkg/components/httpproxy.go b/pkg/components/httpproxy.go index 3afac033..46931fdc 100644 --- a/pkg/components/httpproxy.go +++ b/pkg/components/httpproxy.go @@ -72,7 +72,8 @@ func NewHTTPProxy( Protocol: corev1.ProtocolTCP, }, ), - WithCustomReadinessProbeEndpoint(ptr.T(int32(consts.HTTPProxyHTTPPort)), ptr.T("/ping")), + WithCustomReadinessProbeEndpointPort(int32(consts.HTTPProxyHTTPPort)), + WithCustomReadinessProbeEndpointPath("/ping"), ) var httpsSecret *resources.TLSSecret diff --git a/pkg/components/serveroptions.go b/pkg/components/serveroptions.go index 70bd14bd..67448142 100644 --- a/pkg/components/serveroptions.go +++ b/pkg/components/serveroptions.go @@ -14,15 +14,15 @@ type options struct { type Option func(opts *options) -func WithCustomReadinessProbeEndpoint(port *int32, path *string) Option { +func WithCustomReadinessProbeEndpointPort(port int32) Option { return func(opts *options) { - if port != nil { - opts.readinessProbeEndpointPort = intstr.FromInt32(*port) - } + opts.readinessProbeEndpointPort = intstr.FromInt32(port) + } +} - if path != nil { - opts.readinessProbeEndpointPath = *path - } +func WithCustomReadinessProbeEndpointPath(path string) Option { + return func(opts *options) { + opts.readinessProbeEndpointPath = path } } From 29852d76943c1172960ace91b8506d1e2176bbd9 Mon Sep 17 00:00:00 2001 From: Dmitrii Gadeev Date: Thu, 25 Apr 2024 18:52:37 +0200 Subject: [PATCH 7/7] simplify --- pkg/components/httpproxy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/components/httpproxy.go b/pkg/components/httpproxy.go index 46931fdc..fc7671fa 100644 --- a/pkg/components/httpproxy.go +++ b/pkg/components/httpproxy.go @@ -72,7 +72,7 @@ func NewHTTPProxy( Protocol: corev1.ProtocolTCP, }, ), - WithCustomReadinessProbeEndpointPort(int32(consts.HTTPProxyHTTPPort)), + WithCustomReadinessProbeEndpointPort(consts.HTTPProxyHTTPPort), WithCustomReadinessProbeEndpointPath("/ping"), )