Skip to content

Commit f32f7fc

Browse files
committed
feature:service creation logic + conflict resolutions
1 parent 9f7d772 commit f32f7fc

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

internal/fullnode/service_builder.go

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ func BuildServices(crd *cosmosv1.CosmosFullNode) []diff.Resource[*corev1.Service
3030
max = *v
3131
}
3232
maxExternal := lo.Clamp(max, 0, crd.Spec.Replicas)
33-
p2ps := make([]diff.Resource[*corev1.Service], crd.Spec.Replicas)
34-
for i := int32(0); i < crd.Spec.Replicas; i++ {
33+
svcs := make([]diff.Resource[*corev1.Service], func(nodeType cosmosv1.FullNodeType) int32 {
34+
if nodeType == cosmosv1.Sentry {
35+
return crd.Spec.Replicas * 2
36+
}
37+
return crd.Spec.Replicas
38+
}(crd.Spec.Type))
39+
startOrdinal := crd.Spec.Ordinals.Start
40+
for i := startOrdinal; i < startOrdinal+crd.Spec.Replicas; i++ {
3541
ordinal := crd.Spec.Ordinals.Start + i
3642
var svc corev1.Service
3743
svc.Name = p2pServiceName(crd, ordinal)
@@ -61,10 +67,48 @@ func BuildServices(crd *cosmosv1.CosmosFullNode) []diff.Resource[*corev1.Service
6167
svc.Spec.Type = corev1.ServiceTypeClusterIP
6268
svc.Spec.ClusterIP = *valOrDefault(crd.Spec.Service.P2PTemplate.ClusterIP, ptr(""))
6369
}
64-
p2ps[i] = diff.Adapt(&svc, int(i))
70+
svcs[i] = diff.Adapt(&svc, int(i))
6571
}
6672
rpc := rpcService(crd)
67-
return append(p2ps, diff.Adapt(rpc, len(p2ps)))
73+
if crd.Spec.Type == cosmosv1.Sentry {
74+
for i := int32(0); i < crd.Spec.Replicas; i++ {
75+
ordinal := i
76+
var svc corev1.Service
77+
svc.Name = sentryServiceName(crd, ordinal)
78+
svc.Namespace = crd.Namespace
79+
svc.Kind = "Service"
80+
svc.APIVersion = "v1"
81+
82+
svc.Labels = defaultLabels(crd,
83+
kube.InstanceLabel, instanceName(crd, ordinal),
84+
kube.ComponentLabel, "cosmos-sentry",
85+
)
86+
svc.Annotations = map[string]string{}
87+
88+
svc.Spec.Ports = []corev1.ServicePort{
89+
{
90+
// https://github.com/strangelove-ventures/horcrux-proxy/blob/23a7d31806ce62481162a64adcca848fd879bc52/cmd/watcher.go#L162
91+
Name: "sentry-privval",
92+
Protocol: corev1.ProtocolTCP,
93+
Port: privvalPort,
94+
TargetPort: intstr.FromString("privval"),
95+
},
96+
}
97+
svc.Spec.Selector = map[string]string{kube.InstanceLabel: instanceName(crd, ordinal)}
98+
99+
preserveMergeInto(svc.Labels, crd.Spec.Service.P2PTemplate.Metadata.Labels)
100+
preserveMergeInto(svc.Annotations, crd.Spec.Service.P2PTemplate.Metadata.Annotations)
101+
svc.Spec.Type = corev1.ServiceTypeClusterIP
102+
103+
// If you run pod as sentry mode, it'll be unhealthy cause of no API from app.
104+
// But to run cosmos app, horcrux-proxy should connect to pod even pod's status is unhealthy.
105+
// therefore, you should allow this option.
106+
svc.Spec.PublishNotReadyAddresses = true
107+
108+
svcs[i] = diff.Adapt(&svc, i)
109+
}
110+
}
111+
return append(svcs, diff.Adapt(rpc, len(svcs)))
68112
}
69113

70114
func rpcService(crd *cosmosv1.CosmosFullNode) *corev1.Service {
@@ -131,6 +175,10 @@ func p2pServiceName(crd *cosmosv1.CosmosFullNode, ordinal int32) string {
131175
return fmt.Sprintf("%s-p2p-%d", appName(crd), ordinal)
132176
}
133177

178+
func sentryServiceName(crd *cosmosv1.CosmosFullNode, ordinal int32) string {
179+
return fmt.Sprintf("%s-privval-%d", appName(crd), ordinal)
180+
}
181+
134182
func rpcServiceName(crd *cosmosv1.CosmosFullNode) string {
135183
return fmt.Sprintf("%s-rpc", appName(crd))
136184
}

0 commit comments

Comments
 (0)