Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add sentry svc creation logic to connect with horcrux-proxy automatically #428

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions internal/fullnode/service_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ func BuildServices(crd *cosmosv1.CosmosFullNode) []diff.Resource[*corev1.Service
max = *v
}
maxExternal := lo.Clamp(max, 0, crd.Spec.Replicas)
p2ps := make([]diff.Resource[*corev1.Service], crd.Spec.Replicas)
svcs := make([]diff.Resource[*corev1.Service], func(nodeType cosmosv1.FullNodeType) int32 {
if nodeType == cosmosv1.Sentry {
return crd.Spec.Replicas * 2
}
return crd.Spec.Replicas
}(crd.Spec.Type))

for i := int32(0); i < crd.Spec.Replicas; i++ {
ordinal := i
Expand Down Expand Up @@ -65,12 +70,51 @@ func BuildServices(crd *cosmosv1.CosmosFullNode) []diff.Resource[*corev1.Service
svc.Spec.Type = corev1.ServiceTypeClusterIP
}

p2ps[i] = diff.Adapt(&svc, i)
svcs[i] = diff.Adapt(&svc, i)
}

rpc := rpcService(crd)

return append(p2ps, diff.Adapt(rpc, len(p2ps)))
if crd.Spec.Type == cosmosv1.Sentry {
for i := int32(0); i < crd.Spec.Replicas; i++ {
ordinal := i
var svc corev1.Service
svc.Name = sentryServiceName(crd, ordinal)
svc.Namespace = crd.Namespace
svc.Kind = "Service"
svc.APIVersion = "v1"

svc.Labels = defaultLabels(crd,
kube.InstanceLabel, instanceName(crd, ordinal),
kube.ComponentLabel, "cosmos-sentry",
)
svc.Annotations = map[string]string{}

svc.Spec.Ports = []corev1.ServicePort{
{
// https://github.com/strangelove-ventures/horcrux-proxy/blob/23a7d31806ce62481162a64adcca848fd879bc52/cmd/watcher.go#L162
Name: "sentry-privval",
Protocol: corev1.ProtocolTCP,
Port: privvalPort,
TargetPort: intstr.FromString("privval"),
},
}
svc.Spec.Selector = map[string]string{kube.InstanceLabel: instanceName(crd, ordinal)}

preserveMergeInto(svc.Labels, crd.Spec.Service.P2PTemplate.Metadata.Labels)
preserveMergeInto(svc.Annotations, crd.Spec.Service.P2PTemplate.Metadata.Annotations)
svc.Spec.Type = corev1.ServiceTypeClusterIP

// If you run pod as sentry mode, it'll be unhealthy cause of no API from app.
// But to run cosmos app, horcrux-proxy should connect to pod even pod's status is unhealthy.
// therefore, you should allow this option.
svc.Spec.PublishNotReadyAddresses = true

svcs[i] = diff.Adapt(&svc, i)
}
}

return append(svcs, diff.Adapt(rpc, len(svcs)))
}

func rpcService(crd *cosmosv1.CosmosFullNode) *corev1.Service {
Expand Down Expand Up @@ -139,6 +183,10 @@ func p2pServiceName(crd *cosmosv1.CosmosFullNode, ordinal int32) string {
return fmt.Sprintf("%s-p2p-%d", appName(crd), ordinal)
}

func sentryServiceName(crd *cosmosv1.CosmosFullNode, ordinal int32) string {
return fmt.Sprintf("%s-privval-%d", appName(crd), ordinal)
}

func rpcServiceName(crd *cosmosv1.CosmosFullNode) string {
return fmt.Sprintf("%s-rpc", appName(crd))
}