Skip to content

Commit c5b5aeb

Browse files
kvapsclaude
andcommitted
feat(dispatcher): controller-side reconciler dispatches Apply to satellites
pkg/dispatcher.Dispatcher takes a Resource CRD + same-RD peers + Node list, resolves each Node's SatelliteEndpoint prop, builds a DesiredResource (port/minor/node-id derived deterministically by SHA256 of the RD name; peers list + per-peer drbd_options for the mesh), dials the target satellite over insecure gRPC, and calls ApplyResources. ResourceReconciler picks up Resource CRD changes and calls Apply. cmd/main.go wires a real Dialer; envtest scaffolding stays a no-op when Dispatcher is nil. Closes the controller→satellite half of the dispatch path: a Resource CRD now actually drives Reconciler.Apply on the right satellite pod. Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
1 parent 0d8bab8 commit c5b5aeb

4 files changed

Lines changed: 490 additions & 15 deletions

File tree

cmd/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737

3838
blockstoriov1alpha1 "github.com/cozystack/blockstor/api/v1alpha1"
3939
"github.com/cozystack/blockstor/internal/controller"
40+
"github.com/cozystack/blockstor/pkg/dispatcher"
4041
"github.com/cozystack/blockstor/pkg/rest"
4142
"github.com/cozystack/blockstor/pkg/satellitecontroller"
4243
"github.com/cozystack/blockstor/pkg/store"
@@ -222,8 +223,9 @@ func main() {
222223
os.Exit(1)
223224
}
224225
if err := (&controller.ResourceReconciler{
225-
Client: mgr.GetClient(),
226-
Scheme: mgr.GetScheme(),
226+
Client: mgr.GetClient(),
227+
Scheme: mgr.GetScheme(),
228+
Dispatcher: dispatcher.New(dispatcher.NewDialer()),
227229
}).SetupWithManager(mgr); err != nil {
228230
setupLog.Error(err, "Failed to create controller", "controller", "resource")
229231
os.Exit(1)

internal/controller/resource_controller.go

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,95 @@ package controller
1818

1919
import (
2020
"context"
21+
"time"
2122

23+
"k8s.io/apimachinery/pkg/api/errors"
2224
"k8s.io/apimachinery/pkg/runtime"
2325
ctrl "sigs.k8s.io/controller-runtime"
2426
"sigs.k8s.io/controller-runtime/pkg/client"
2527
logf "sigs.k8s.io/controller-runtime/pkg/log"
2628

2729
blockstoriov1alpha1 "github.com/cozystack/blockstor/api/v1alpha1"
30+
"github.com/cozystack/blockstor/pkg/dispatcher"
2831
)
2932

30-
// ResourceReconciler reconciles a Resource object
33+
// ResourceReconciler dispatches Resource CRD changes to the right
34+
// satellite via the Dispatcher. It collects same-RD peers and the
35+
// full Node list (for endpoint resolution) on every reconcile —
36+
// fine for the stand smoke; once Resource counts grow we'll switch
37+
// to a cached lister or label-selector watch.
3138
type ResourceReconciler struct {
3239
client.Client
33-
Scheme *runtime.Scheme
40+
Scheme *runtime.Scheme
41+
Dispatcher *dispatcher.Dispatcher
3442
}
3543

3644
// +kubebuilder:rbac:groups=blockstor.io.blockstor.io,resources=resources,verbs=get;list;watch;create;update;patch;delete
3745
// +kubebuilder:rbac:groups=blockstor.io.blockstor.io,resources=resources/status,verbs=get;update;patch
3846
// +kubebuilder:rbac:groups=blockstor.io.blockstor.io,resources=resources/finalizers,verbs=update
47+
// +kubebuilder:rbac:groups=blockstor.io.blockstor.io,resources=nodes,verbs=get;list;watch
48+
// +kubebuilder:rbac:groups=blockstor.io.blockstor.io,resources=resourcedefinitions,verbs=get;list;watch
3949

40-
// Reconcile is part of the main kubernetes reconciliation loop which aims to
41-
// move the current state of the cluster closer to the desired state.
42-
// TODO(user): Modify the Reconcile function to compare the state specified by
43-
// the Resource object against the actual cluster state, and then
44-
// perform operations to make the cluster state reflect the state specified by
45-
// the user.
46-
//
47-
// For more details, check Reconcile and its Result here:
48-
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.23.3/pkg/reconcile
50+
// Reconcile reads a Resource and pushes the matching DesiredResource
51+
// to the satellite that hosts it. Per-replica errors land in the
52+
// log; transport faults trigger a 10s requeue.
4953
func (r *ResourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
50-
_ = logf.FromContext(ctx)
54+
log := logf.FromContext(ctx)
5155

52-
// TODO(user): your logic here
56+
if r.Dispatcher == nil {
57+
// envtest scaffolding (suite_test.go) constructs the reconciler
58+
// without a Dispatcher — keep the original no-op behaviour for
59+
// it so the boilerplate test stays green.
60+
return ctrl.Result{}, nil
61+
}
62+
63+
var target blockstoriov1alpha1.Resource
64+
65+
err := r.Get(ctx, req.NamespacedName, &target)
66+
if err != nil {
67+
if errors.IsNotFound(err) {
68+
return ctrl.Result{}, nil
69+
}
70+
71+
return ctrl.Result{}, err
72+
}
73+
74+
var resList blockstoriov1alpha1.ResourceList
75+
76+
err = r.List(ctx, &resList)
77+
if err != nil {
78+
return ctrl.Result{}, err
79+
}
80+
81+
peers := make([]blockstoriov1alpha1.Resource, 0, len(resList.Items))
82+
83+
for i := range resList.Items {
84+
if resList.Items[i].Spec.ResourceDefinitionName == target.Spec.ResourceDefinitionName {
85+
peers = append(peers, resList.Items[i])
86+
}
87+
}
88+
89+
var nodeList blockstoriov1alpha1.NodeList
90+
91+
err = r.List(ctx, &nodeList)
92+
if err != nil {
93+
return ctrl.Result{}, err
94+
}
95+
96+
result, err := r.Dispatcher.Apply(ctx, &target, peers, nodeList.Items)
97+
if err != nil {
98+
log.Error(err, "Apply RPC failed", "resource", target.Name, "node", target.Spec.NodeName)
99+
100+
return ctrl.Result{RequeueAfter: 10 * time.Second}, nil
101+
}
102+
103+
if !result.GetOk() {
104+
log.Info("satellite rejected apply", "msg", result.GetMessage(),
105+
"resource", target.Name, "node", target.Spec.NodeName)
106+
} else {
107+
log.Info("satellite accepted apply",
108+
"resource", target.Name, "node", target.Spec.NodeName)
109+
}
53110

54111
return ctrl.Result{}, nil
55112
}

pkg/dispatcher/dispatcher.go

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
/*
2+
Copyright 2026 Cozystack contributors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package dispatcher is the controller-side glue that turns Resource
18+
// CRDs into satellite-side ApplyResources calls. The kubebuilder
19+
// reconciler delegates here so the wire format and the gRPC dial /
20+
// retry logic live in one testable place.
21+
package dispatcher
22+
23+
import (
24+
"context"
25+
"crypto/sha256"
26+
"encoding/binary"
27+
"sort"
28+
"strconv"
29+
30+
"github.com/cockroachdb/errors"
31+
"google.golang.org/grpc"
32+
"google.golang.org/grpc/credentials/insecure"
33+
34+
blockstoriov1alpha1 "github.com/cozystack/blockstor/api/v1alpha1"
35+
satellitepb "github.com/cozystack/blockstor/pkg/satellite/proto"
36+
)
37+
38+
// drbdAddrAny is the placeholder address we put into the .res file at
39+
// dispatch time. The satellite rewrites it to its actual pod IP when
40+
// it renders the file (drbd doesn't accept literal 0.0.0.0).
41+
const drbdAddrAny = "0.0.0.0"
42+
43+
// Dialer abstracts how we open a gRPC connection. Production wires
44+
// the actual `grpc.NewClient`; tests inject a stub that returns a
45+
// canned client.
46+
type Dialer interface {
47+
Dial(ctx context.Context, endpoint string) (satellitepb.SatelliteClient, func() error, error)
48+
}
49+
50+
// realDialer wraps grpc.NewClient with our standard insecure (cluster-
51+
// internal) transport.
52+
type realDialer struct{}
53+
54+
// NewDialer returns a production Dialer. We export it so the
55+
// reconciler in main.go can wire it without leaking grpc imports
56+
// across packages.
57+
func NewDialer() Dialer {
58+
return realDialer{}
59+
}
60+
61+
// Dial opens a connection to endpoint and returns the satellite
62+
// client, a close func, or an error.
63+
func (realDialer) Dial(_ context.Context, endpoint string) (satellitepb.SatelliteClient, func() error, error) {
64+
conn, err := grpc.NewClient(endpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
65+
if err != nil {
66+
return nil, nil, errors.Wrapf(err, "dial %s", endpoint)
67+
}
68+
69+
return satellitepb.NewSatelliteClient(conn), conn.Close, nil
70+
}
71+
72+
// Dispatcher pushes a Resource's desired state to the satellite that
73+
// hosts it.
74+
type Dispatcher struct {
75+
dialer Dialer
76+
}
77+
78+
// New constructs a Dispatcher with the given Dialer.
79+
func New(dialer Dialer) *Dispatcher {
80+
return &Dispatcher{dialer: dialer}
81+
}
82+
83+
// Apply builds the DesiredResource for this Resource (looking up its
84+
// peers from the full RD-wide list) and sends it to the target
85+
// satellite. Returns the per-resource result the satellite reported.
86+
//
87+
// nodes is the full Node CRD list — Apply uses it to resolve each
88+
// peer's SatelliteEndpoint property.
89+
func (d *Dispatcher) Apply(ctx context.Context, target *blockstoriov1alpha1.Resource, peers []blockstoriov1alpha1.Resource, nodes []blockstoriov1alpha1.Node) (*satellitepb.ResourceApplyResult, error) {
90+
endpoint := lookupEndpoint(target.Spec.NodeName, nodes)
91+
if endpoint == "" {
92+
return nil, errors.Errorf("no SatelliteEndpoint for node %q", target.Spec.NodeName)
93+
}
94+
95+
desired := buildDesired(target, peers)
96+
97+
client, closer, err := d.dialer.Dial(ctx, endpoint)
98+
if err != nil {
99+
return nil, errors.Wrapf(err, "dial %s", endpoint)
100+
}
101+
defer func() { _ = closer() }()
102+
103+
resp, err := client.ApplyResources(ctx, &satellitepb.ApplyResourcesRequest{
104+
Resources: []*satellitepb.DesiredResource{desired},
105+
})
106+
if err != nil {
107+
return nil, errors.Wrap(err, "ApplyResources RPC")
108+
}
109+
110+
if len(resp.GetResults()) == 0 {
111+
return nil, errors.New("empty ApplyResources response")
112+
}
113+
114+
return resp.GetResults()[0], nil
115+
}
116+
117+
// lookupEndpoint reads SatelliteEndpoint from the Node prop bag.
118+
func lookupEndpoint(nodeName string, nodes []blockstoriov1alpha1.Node) string {
119+
for i := range nodes {
120+
if nodes[i].Name == nodeName {
121+
return nodes[i].Spec.Props["SatelliteEndpoint"]
122+
}
123+
}
124+
125+
return ""
126+
}
127+
128+
// buildDesired translates a Resource + its same-RD peers into the
129+
// satellite-facing DesiredResource. Port/minor/node-id assignment is
130+
// deterministic from the RD name + sorted peer list — good enough for
131+
// the first stand smoke; the autoplacer will replace it with a real
132+
// allocator once we wire the IPAM hookup.
133+
func buildDesired(target *blockstoriov1alpha1.Resource, peers []blockstoriov1alpha1.Resource) *satellitepb.DesiredResource {
134+
rdName := target.Spec.ResourceDefinitionName
135+
port := derivePort(rdName)
136+
minor := deriveMinor(rdName)
137+
138+
// Collect every node hosting this RD, sorted, with self at front.
139+
nodeNames := append([]string{}, target.Spec.NodeName)
140+
seen := map[string]bool{target.Spec.NodeName: true}
141+
142+
for i := range peers {
143+
name := peers[i].Spec.NodeName
144+
if seen[name] {
145+
continue
146+
}
147+
148+
seen[name] = true
149+
nodeNames = append(nodeNames, name)
150+
}
151+
152+
// Sort all (incl. self) so node-id assignment is stable across
153+
// reconciles.
154+
sortedAll := append([]string{}, nodeNames...)
155+
sort.Strings(sortedAll)
156+
157+
idOf := map[string]int{}
158+
for i, name := range sortedAll {
159+
idOf[name] = i
160+
}
161+
162+
dropped := nodeNames[1:]
163+
164+
drbdOpts := map[string]string{
165+
"port": strconv.Itoa(port),
166+
"node-id": strconv.Itoa(idOf[target.Spec.NodeName]),
167+
"address": drbdAddrAny, // satellite picks pod IP at .res render time
168+
"minor": strconv.Itoa(minor),
169+
}
170+
171+
// Per-peer entries — used by ConfFileBuilder on the satellite to
172+
// compose the connection mesh.
173+
for _, peer := range dropped {
174+
drbdOpts["peer."+peer+".port"] = strconv.Itoa(port)
175+
drbdOpts["peer."+peer+".node-id"] = strconv.Itoa(idOf[peer])
176+
drbdOpts["peer."+peer+".address"] = drbdAddrAny
177+
}
178+
179+
return &satellitepb.DesiredResource{
180+
Name: rdName,
181+
NodeName: target.Spec.NodeName,
182+
Flags: target.Spec.Flags,
183+
Props: target.Spec.Props,
184+
Peers: dropped,
185+
DrbdOptions: drbdOpts,
186+
}
187+
}
188+
189+
// derivePort hashes the RD name into the drbd-9 reserved range
190+
// 7000–7999. Matches what upstream LINSTOR's TcpPortPool does for
191+
// fresh deployments — collisions on a real cluster are handled by
192+
// the autoplacer, but for the smoke we live with the hash.
193+
func derivePort(rd string) int {
194+
const (
195+
portBase = 7000
196+
portRange = 1000
197+
)
198+
199+
digest := sha256.Sum256([]byte(rd))
200+
201+
return portBase + int(binary.BigEndian.Uint16(digest[:2])%portRange)
202+
}
203+
204+
// deriveMinor likewise hashes into 1000–9999.
205+
func deriveMinor(rd string) int {
206+
const (
207+
minorBase = 1000
208+
minorRange = 9000
209+
)
210+
211+
digest := sha256.Sum256([]byte(rd))
212+
213+
return minorBase + int(binary.BigEndian.Uint16(digest[2:4])%minorRange)
214+
}

0 commit comments

Comments
 (0)