Keep llmgw's internal ports to the cluster's networks with an nftables table of its own

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-04 11:03:46 +02:00
1 parent 7e57863edd
commit 7b6276901a
17 files changed
+800 -10

No files matched your search

+44 -1
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net"
"net/netip"
"os"
"runtime"
"strings"
@@ -13,7 +14,9 @@ import (
"github.com/spf13/cobra"
"gitea.mynodes.xyz/ilker/llmgw/internal/firewall"
"gitea.mynodes.xyz/ilker/llmgw/internal/mirror"
"gitea.mynodes.xyz/ilker/llmgw/internal/pgha"
"gitea.mynodes.xyz/ilker/llmgw/internal/tlsconf"
)
@@ -26,6 +29,7 @@ func newMirrorInitCmd(dir *string) *cobra.Command {
var (
controlPlanes []string
base string
noFirewall bool
)
cmd := &cobra.Command{
Use: "init",
@@ -56,11 +60,19 @@ Safe to run again: it changes only what differs.`,
if _, err := parseAllow(controlPlanes); err != nil {
return err
}
return mirrorInit(cmd.Context(), cmd.OutOrStdout(), *dir, base, controlPlanes)
if err := mirrorInit(cmd.Context(), cmd.OutOrStdout(), *dir, base, controlPlanes); err != nil {
return err
}
if !noFirewall {
mirrorFirewall(cmd.Context(), cmd.OutOrStdout(), controlPlanes)
}
return nil
},
}
cmd.Flags().StringArrayVar(&controlPlanes, "control-plane", nil,
"address of the control plane that manages this mirror; repeatable")
cmd.Flags().BoolVar(&noFirewall, "no-firewall", false,
"leave the firewall alone, instead of keeping the mirror's ports to the cluster's networks")
cmd.Flags().StringVar(&base, "base", "",
"the address nodes fetch from, http://host:8081 (default: this machine's first address)")
return cmd
@@ -123,3 +135,34 @@ func mirrorInit(ctx context.Context, out io.Writer, dir, base string, controlPla
fmt.Fprintf(out, "\nThe mirror is up. On the control plane:\n\n sudo llmgw server init --mirror %s\n\n", api)
return nil
}
// mirrorFirewall keeps the mirror's ports to the networks it reaches the
// control planes on: its files are for the cluster's nodes, its API for the
// control planes, which the API checks again by address.
func mirrorFirewall(ctx context.Context, out io.Writer, controlPlanes []string) {
allowed, _ := parseAllow(controlPlanes)
var toward []string
var nets []netip.Prefix
for _, p := range allowed {
if p.IsSingleIP() {
toward = append(toward, p.Addr().String())
} else {
nets = append(nets, p)
}
}
found, err := firewall.Networks(ctx, pgha.Local{}, toward)
if err == nil {
var changed bool
changed, err = firewall.Install(ctx, pgha.Local{}, firewall.Spec{
Ports: []firewall.Port{firewall.MirrorFiles, firewall.MirrorAPI, firewall.NodeExporter},
Allow: append(found, nets...),
})
if err == nil {
if changed {
fmt.Fprintln(out, " ok firewall: the mirror's ports answer the cluster's networks only")
}
return
}
}
fmt.Fprintf(out, " !! firewall: %v; the mirror's ports answer every network\n", err)
}