Skip to content

Commit 174c21d

Browse files
authored
Propagates sidecar's port to a service (#1078)
1 parent 4b3f0b2 commit 174c21d

File tree

4 files changed

+74
-34
lines changed

4 files changed

+74
-34
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- (Feature) Early connections support
66
- (Bugfix) Fix and document action timeouts
77

8+
- (Feature) Propagate sidecars' ports to a member's service
9+
810
## [1.2.16](https://github.com/arangodb/kube-arangodb/tree/1.2.16) (2022-09-14)
911
- (Feature) Add ArangoDeployment ServerGroupStatus
1012
- (Feature) (EE) Ordered Member IDs

pkg/deployment/resources/pod_leader.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import (
2424
"context"
2525
"sync"
2626

27+
core "k8s.io/api/core/v1"
2728
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
"k8s.io/apimachinery/pkg/types"
2930

3031
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
31-
"github.com/arangodb/kube-arangodb/pkg/apis/shared"
3232
"github.com/arangodb/kube-arangodb/pkg/deployment/features"
3333
"github.com/arangodb/kube-arangodb/pkg/deployment/patch"
3434
"github.com/arangodb/kube-arangodb/pkg/util/arangod"
@@ -118,9 +118,11 @@ func (r *Resources) EnsureLeader(ctx context.Context, cachedStatus inspectorInte
118118
leaderAgentSvcName := k8sutil.CreateAgentLeaderServiceName(r.context.GetAPIObject().GetName())
119119
deploymentName := r.context.GetAPIObject().GetName()
120120

121+
ports := []core.ServicePort{CreateServerServicePort(group)}
122+
121123
selector := k8sutil.LabelsForLeaderMember(deploymentName, group.AsRole(), leaderID)
122124
if s, ok := cachedStatus.Service().V1().GetSimple(leaderAgentSvcName); ok {
123-
if err, adjusted := r.adjustService(ctx, s, shared.ArangoPort, selector); err == nil {
125+
if err, adjusted := r.adjustService(ctx, s, ports, selector); err == nil {
124126
if !adjusted {
125127
// The service is not changed, so single server leader can be set.
126128
return r.ensureSingleServerLeader(ctx, cachedStatus)
@@ -132,7 +134,7 @@ func (r *Resources) EnsureLeader(ctx context.Context, cachedStatus inspectorInte
132134
}
133135
}
134136

135-
s := r.createService(leaderAgentSvcName, r.context.GetNamespace(), r.context.GetAPIObject().AsOwner(), shared.ArangoPort, selector)
137+
s := r.createService(leaderAgentSvcName, r.context.GetNamespace(), r.context.GetAPIObject().AsOwner(), ports, selector)
136138
err := globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
137139
_, err := cachedStatus.ServicesModInterface().V1().Create(ctxChild, s, meta.CreateOptions{})
138140
return err

pkg/deployment/resources/services.go

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/arangodb/kube-arangodb/pkg/util/globals"
4141
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
4242
inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector"
43+
v1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/pod/v1"
4344
servicev1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/service/v1"
4445
)
4546

@@ -49,7 +50,7 @@ var (
4950
)
5051

5152
// createService returns service's object.
52-
func (r *Resources) createService(name, namespace string, owner meta.OwnerReference, targetPort int32,
53+
func (r *Resources) createService(name, namespace string, owner meta.OwnerReference, ports []core.ServicePort,
5354
selector map[string]string) *core.Service {
5455

5556
return &core.Service{
@@ -61,15 +62,8 @@ func (r *Resources) createService(name, namespace string, owner meta.OwnerRefere
6162
},
6263
},
6364
Spec: core.ServiceSpec{
64-
Type: core.ServiceTypeClusterIP,
65-
Ports: []core.ServicePort{
66-
{
67-
Name: "server",
68-
Protocol: "TCP",
69-
Port: shared.ArangoPort,
70-
TargetPort: intstr.IntOrString{IntVal: targetPort},
71-
},
72-
},
65+
Type: core.ServiceTypeClusterIP,
66+
Ports: ports,
7367
PublishNotReadyAddresses: true,
7468
Selector: selector,
7569
},
@@ -78,19 +72,13 @@ func (r *Resources) createService(name, namespace string, owner meta.OwnerRefere
7872

7973
// adjustService checks whether service contains is valid and if not than it reconciles service.
8074
// Returns true if service is adjusted.
81-
func (r *Resources) adjustService(ctx context.Context, s *core.Service, targetPort int32, selector map[string]string) (error, bool) {
75+
func (r *Resources) adjustService(ctx context.Context, s *core.Service, ports []core.ServicePort,
76+
selector map[string]string) (error, bool) {
8277
services := r.context.ACS().CurrentClusterCache().ServicesModInterface().V1()
8378
spec := s.Spec.DeepCopy()
8479

8580
spec.Type = core.ServiceTypeClusterIP
86-
spec.Ports = []core.ServicePort{
87-
{
88-
Name: "server",
89-
Protocol: "TCP",
90-
Port: shared.ArangoPort,
91-
TargetPort: intstr.IntOrString{IntVal: targetPort},
92-
},
93-
}
81+
spec.Ports = ports
9482
spec.PublishNotReadyAddresses = true
9583
spec.Selector = selector
9684
if equality.Semantic.DeepDerivative(*spec, s.Spec) {
@@ -127,30 +115,23 @@ func (r *Resources) EnsureServices(ctx context.Context, cachedStatus inspectorIn
127115

128116
// Fetch existing services
129117
svcs := cachedStatus.ServicesModInterface().V1()
118+
podInspector := cachedStatus.Pod().V1()
130119

131120
reconcileRequired := k8sutil.NewReconcile(cachedStatus)
132121

133122
// Ensure member services
134123
for _, e := range status.Members.AsList() {
135-
var targetPort int32 = shared.ArangoPort
136-
137-
switch e.Group {
138-
case api.ServerGroupSyncMasters:
139-
targetPort = shared.ArangoSyncMasterPort
140-
case api.ServerGroupSyncWorkers:
141-
targetPort = shared.ArangoSyncWorkerPort
142-
}
143-
144124
memberName := e.Member.ArangoMemberName(r.context.GetAPIObject().GetName(), e.Group)
145125

146126
member, ok := cachedStatus.ArangoMember().V1().GetSimple(memberName)
147127
if !ok {
148128
return errors.Newf("Member %s not found", memberName)
149129
}
150130

131+
ports := CreateServerServicePortsWithSidecars(podInspector, e.Member.PodName, e.Group)
151132
selector := k8sutil.LabelsForMember(deploymentName, e.Group.AsRole(), e.Member.ID)
152133
if s, ok := cachedStatus.Service().V1().GetSimple(member.GetName()); !ok {
153-
s := r.createService(member.GetName(), member.GetNamespace(), member.AsOwner(), targetPort, selector)
134+
s := r.createService(member.GetName(), member.GetNamespace(), member.AsOwner(), ports, selector)
154135

155136
err := globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
156137
_, err := svcs.Create(ctxChild, s, meta.CreateOptions{})
@@ -165,7 +146,7 @@ func (r *Resources) EnsureServices(ctx context.Context, cachedStatus inspectorIn
165146
reconcileRequired.Required()
166147
continue
167148
} else {
168-
if err, adjusted := r.adjustService(ctx, s, targetPort, selector); err == nil {
149+
if err, adjusted := r.adjustService(ctx, s, ports, selector); err == nil {
169150
if adjusted {
170151
reconcileRequired.Required()
171152
}
@@ -456,3 +437,57 @@ func ensureManagedServiceSelector(ctx context.Context, selector map[string]strin
456437

457438
return false, nil
458439
}
440+
441+
// CreateServerServicePortsWithSidecars returns ports for the service.
442+
func CreateServerServicePortsWithSidecars(podInspector v1.Inspector, podName string, group api.ServerGroup) []core.ServicePort {
443+
// Create service port for the `server` container.
444+
ports := []core.ServicePort{CreateServerServicePort(group)}
445+
446+
if podInspector == nil {
447+
return ports
448+
}
449+
450+
if p, ok := podInspector.GetSimple(podName); ok {
451+
for _, c := range p.Spec.Containers {
452+
if c.Name == api.ServerGroupReservedContainerNameServer {
453+
// It is already added.
454+
continue
455+
}
456+
for _, port := range c.Ports {
457+
ports = append(ports, core.ServicePort{
458+
Name: port.Name,
459+
Protocol: core.ProtocolTCP,
460+
Port: port.ContainerPort,
461+
})
462+
}
463+
}
464+
}
465+
466+
return ports
467+
}
468+
469+
// CreateServerServicePort creates main server service port.
470+
func CreateServerServicePort(group api.ServerGroup) core.ServicePort {
471+
serverTargetPort := getTargetPort(group)
472+
return core.ServicePort{
473+
Name: api.ServerGroupReservedContainerNameServer,
474+
Protocol: core.ProtocolTCP,
475+
Port: shared.ArangoPort,
476+
TargetPort: intstr.IntOrString{
477+
IntVal: serverTargetPort,
478+
},
479+
}
480+
}
481+
482+
// getTargetPort returns target port for the given server group.
483+
func getTargetPort(group api.ServerGroup) int32 {
484+
if group == api.ServerGroupSyncMasters {
485+
return shared.ArangoSyncMasterPort
486+
}
487+
488+
if group == api.ServerGroupSyncWorkers {
489+
return shared.ArangoSyncWorkerPort
490+
}
491+
492+
return shared.ArangoPort
493+
}

pkg/util/k8sutil/services.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
core "k8s.io/api/core/v1"
3131
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
3232

33+
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
3334
"github.com/arangodb/kube-arangodb/pkg/apis/shared"
3435
"github.com/arangodb/kube-arangodb/pkg/util/errors"
3536
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/service"
@@ -145,7 +146,7 @@ func CreateDatabaseClientService(ctx context.Context, svcs servicev1.ModInterfac
145146
svcName := CreateDatabaseClientServiceName(deploymentName)
146147
ports := []core.ServicePort{
147148
{
148-
Name: "server",
149+
Name: api.ServerGroupReservedContainerNameServer,
149150
Protocol: core.ProtocolTCP,
150151
Port: shared.ArangoPort,
151152
},

0 commit comments

Comments
 (0)