mirror of
https://github.com/mulgadc/spinifex.git
synced 2026-10-15 17:11:30 +00:00
7bbd99c792
* feat(eks): multinode control-plane networking, IAM, and CoreDNS parity
Integrates the EKS multinode fix set for the managed-control-plane topology
so a 3-worker cluster brings up ingress/ALB and cross-node DNS the way AWS
EKS does. Squashed from feat/eks-workload-lifecycle, rebased onto dev; the
node-IAM piece (4f6ea8cf) is dropped as dev's lazy IAMProvider supersedes it.
EKS:
- Route apiserver egress (exec/logs/webhooks) through konnectivity
(apiserver-network-proxy): CP servers run konnectivity-server behind the
cluster NLB (:8132), workers run konnectivity-agent, so apiserver->pod
traffic rides the agent-initiated reverse tunnel instead of an unroutable
CP->VPC path. Server-count fan-out gives every apiserver a tunnel to every
replica (HA-correct).
- Pin k3s node-ip to the CP-VPC primary on overlay re-home; CP-overlay
flannel mesh SG rule authorized at cluster launch with ordered teardown.
- Pin VPC data NIC MTU to 1320 so flannel fits the OVN underlay.
- Pin the CoreDNS DaemonSet to workers (drop CriticalAddonsOnly + CP/master
tolerations, add anti-control-plane nodeAffinity) for AWS-EKS parity.
- Spread nodegroup workers across hosts; addon readiness falls back on
workload rollout when gvks are absent.
- elbv2: allow the proxy_protocol_v2.enabled target-group attribute.
EBS:
- Move volume attachment state to state.json so a live nbdkit VB cannot
clobber it; route mounted-volume state updates through the live VB with a
retry for in-use marks racing the mount responder; keep the boot volume
in-use across stop/crash unmount.
* fix(eks): gate orchestration on the resolved IAM ensurer, not raw deps.IAM
missingOrchestrationDeps gated on s.deps.IAM, but production wires only the
lazy deps.IAMProvider and leaves deps.IAM nil (set directly by tests only).
The gate therefore reported missing=[IAM] on every node forever, so
CreateCluster/CreateNodegroup were rejected with ServiceUnavailable even
though the IAM service was fully up via the provider.
Gate on s.iamEnsurer() instead — the same resolver (deps.IAM, else
deps.IAMProvider()) the nodegroup/role code already uses — so the protective
check still rejects a node with no IAM service but passes when the provider
supplies one. Add a regression test that sets only IAMProvider and asserts the
gate is satisfied; the existing nil-both test still rejects.
* fix(eks): let eks-token-webhook use IMDS creds when static keys absent
The CP VM launches on an instance profile, so buildK3sUserData omits
EKS_ACCESS_KEY/EKS_SECRET_KEY. loadConfig() rejected absent static keys
before eksgw.New could fall back to the IMDS SDK chain, so the webhook
aborted on boot, never wrote token-webhook.kubeconfig, and the apiserver
crashed on the missing --authentication-token-webhook-config-file file,
stalling the cluster at CREATING until it timed out to FAILED.
Drop the static-key precondition (matching the sibling eks-gateway-publish
and eks-gateway-fetch helpers); keep the genuinely-required gateway/account/
cluster gates.
* fix(eks): stop dhcpcd IPv4LL hijacking IMDS on multi-NIC control plane
The K3s control-plane system VM is dual-NIC (data ENI + mgmt NIC on br-mgmt,
which has no DHCP server). dhcpcd's no-lease IPv4LL fallback assigned a
169.254.x.x address and a 169.254.0.0/16 route on the mgmt NIC, capturing
169.254.169.254 and steering IMDS off the data NIC away from the per-tap
br-imds datapath, where the host RST it. eks-gateway-publish then failed to
sign requests and no bootstrap artifacts were ever published, so clusters sat
CREATING until they timed out to FAILED.
Bake noipv4ll into /etc/dhcpcd.conf so dhcpcd never assigns an IPv4LL address
or /16 route on a NIC that fails DHCP; real DHCP leases (workers, data ENI)
are unaffected and IMDS falls through the data NIC's default route to the
br-imds demux, matching the single-NIC path. Defense in depth: mulga-mgmt-net
deletes any default route on a NIC declared NIC<n>_DEFAULT=0.
* fix(eks): grant eks:WebhookTokenReview to the control-plane instance role
The eks-token-webhook relays `aws eks get-token` bearer tokens to the gateway
broker at POST /clusters/{name}/token-review, signing with the control-plane
VM's IMDS instance-role creds (spinifex-eks-server). That route authorizes
against eks:WebhookTokenReview, but the role's inline policy granted only
eks:PublishInternal and eks:ListInternalAddons, so the gateway 403'd every
relay and the apiserver returned 401 to all external kubectl.
Add eks:WebhookTokenReview to eksServerInlinePolicy. EnsureSystemInstanceProfile
re-asserts the inline policy on every control-plane instance-profile ensure, and
the gateway evaluates authorization per request, so existing clusters converge
without a rebuild once the shared role is updated.
154 lines
4.9 KiB
Go
154 lines
4.9 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"encoding/pem"
|
|
"math/big"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// writeTestCA mints a self-signed EC CA (cert + SEC1 key PEM) into dir, returning
|
|
// the two paths — a stand-in for K3s' /var/lib/rancher/k3s/server/tls/server-ca.*.
|
|
func writeTestCA(t *testing.T, dir string) (certPath, keyPath string) {
|
|
t.Helper()
|
|
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
t.Fatalf("gen ca key: %v", err)
|
|
}
|
|
tmpl := x509.Certificate{
|
|
SerialNumber: big.NewInt(1),
|
|
Subject: pkix.Name{CommonName: "k3s-server-ca"},
|
|
NotBefore: time.Now().Add(-time.Hour),
|
|
NotAfter: time.Now().AddDate(10, 0, 0),
|
|
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageDigitalSignature,
|
|
BasicConstraintsValid: true,
|
|
IsCA: true,
|
|
}
|
|
der, err := x509.CreateCertificate(rand.Reader, &tmpl, &tmpl, &priv.PublicKey, priv)
|
|
if err != nil {
|
|
t.Fatalf("create ca cert: %v", err)
|
|
}
|
|
keyDER, err := x509.MarshalECPrivateKey(priv)
|
|
if err != nil {
|
|
t.Fatalf("marshal ca key: %v", err)
|
|
}
|
|
certPath = filepath.Join(dir, "server-ca.crt")
|
|
keyPath = filepath.Join(dir, "server-ca.key")
|
|
if err := os.WriteFile(certPath, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}), 0o644); err != nil {
|
|
t.Fatalf("write ca cert: %v", err)
|
|
}
|
|
if err := os.WriteFile(keyPath, pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER}), 0o600); err != nil {
|
|
t.Fatalf("write ca key: %v", err)
|
|
}
|
|
return certPath, keyPath
|
|
}
|
|
|
|
func leafFrom(t *testing.T, certPath string) *x509.Certificate {
|
|
t.Helper()
|
|
b, err := os.ReadFile(certPath)
|
|
if err != nil {
|
|
t.Fatalf("read leaf: %v", err)
|
|
}
|
|
crt, err := x509.ParseCertificate(decodeFirst(b))
|
|
if err != nil {
|
|
t.Fatalf("parse leaf: %v", err)
|
|
}
|
|
return crt
|
|
}
|
|
|
|
func TestEnsureCertSignsLeafWithSANsThatChainsToCA(t *testing.T) {
|
|
dir := t.TempDir()
|
|
caCert, caKey := writeTestCA(t, dir)
|
|
|
|
dns, ips := splitSANs("10.32.100.4,203.0.113.9,eks-toc.example")
|
|
certPath, keyPath, err := ensureCert(dir, caCert, caKey, "konnectivity-server", dns, ips)
|
|
if err != nil {
|
|
t.Fatalf("ensureCert: %v", err)
|
|
}
|
|
if _, err := os.Stat(keyPath); err != nil {
|
|
t.Fatalf("key not written: %v", err)
|
|
}
|
|
|
|
leaf := leafFrom(t, certPath)
|
|
if leaf.IsCA {
|
|
t.Error("konnectivity serving cert must be a leaf, not a CA")
|
|
}
|
|
// The agent dials the NLB private-endpoint IP and verifies via the in-pod CA,
|
|
// so the IP must be a SAN or the tunnel TLS handshake fails.
|
|
if err := leaf.VerifyHostname("10.32.100.4"); err != nil {
|
|
t.Errorf("IP SAN 10.32.100.4 missing: %v", err)
|
|
}
|
|
|
|
// The leaf must chain to the K3s server CA — that CA is the in-pod ca.crt the
|
|
// agent trusts, so no separate CA distribution is needed.
|
|
caPEM, _ := os.ReadFile(caCert)
|
|
roots := x509.NewCertPool()
|
|
if !roots.AppendCertsFromPEM(caPEM) {
|
|
t.Fatal("append CA failed")
|
|
}
|
|
if _, err := leaf.Verify(x509.VerifyOptions{
|
|
Roots: roots,
|
|
DNSName: "eks-toc.example",
|
|
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
}); err != nil {
|
|
t.Errorf("leaf does not chain to the K3s server CA: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestEnsureCertReusesThenReissuesOnSANChange(t *testing.T) {
|
|
dir := t.TempDir()
|
|
caCert, caKey := writeTestCA(t, dir)
|
|
|
|
dns, ips := splitSANs("10.32.100.4,eks-toc.example")
|
|
c1, _, err := ensureCert(dir, caCert, caKey, "konnectivity-server", dns, ips)
|
|
if err != nil {
|
|
t.Fatalf("first ensureCert: %v", err)
|
|
}
|
|
first, _ := os.ReadFile(c1)
|
|
|
|
// Same SANs → cached pair reused byte-for-byte (a churning cert would bounce
|
|
// the konnectivity-server on every restart).
|
|
if _, _, err := ensureCert(dir, caCert, caKey, "konnectivity-server", dns, ips); err != nil {
|
|
t.Fatalf("second ensureCert: %v", err)
|
|
}
|
|
if second, _ := os.ReadFile(c1); string(first) != string(second) {
|
|
t.Error("ensureCert re-minted instead of reusing the SAN-current cached cert")
|
|
}
|
|
|
|
// A new endpoint in the SAN set must force a re-issue.
|
|
dns2, ips2 := splitSANs("10.32.100.4,eks-toc.example,10.32.100.99")
|
|
if _, _, err := ensureCert(dir, caCert, caKey, "konnectivity-server", dns2, ips2); err != nil {
|
|
t.Fatalf("third ensureCert: %v", err)
|
|
}
|
|
if third, _ := os.ReadFile(c1); string(first) == string(third) {
|
|
t.Error("ensureCert served a stale cert that does not cover the new SAN")
|
|
}
|
|
if err := leafFrom(t, c1).VerifyHostname("10.32.100.99"); err != nil {
|
|
t.Errorf("re-issued cert missing new SAN: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSplitSANsPartitionsIPsAndDNS(t *testing.T) {
|
|
dns, ips := splitSANs(" 10.0.0.1 , host.example , , 2001:db8::1 ")
|
|
if len(dns) != 1 || dns[0] != "host.example" {
|
|
t.Errorf("dns = %v, want [host.example]", dns)
|
|
}
|
|
wantIPs := []net.IP{net.ParseIP("10.0.0.1"), net.ParseIP("2001:db8::1")}
|
|
if len(ips) != len(wantIPs) {
|
|
t.Fatalf("ips = %v, want %v", ips, wantIPs)
|
|
}
|
|
for i, ip := range ips {
|
|
if !ip.Equal(wantIPs[i]) {
|
|
t.Errorf("ips[%d] = %v, want %v", i, ip, wantIPs[i])
|
|
}
|
|
}
|
|
}
|