Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 1 | // Package clusternet implements a Cluster Networking mesh service running on all |
| 2 | // Metropolis nodes. |
| 3 | // |
| 4 | // The mesh is based on wireguard and a centralized configuration store in the |
| 5 | // cluster Curator (in etcd). |
| 6 | // |
| 7 | // While the implementation is nearly generic, it currently makes an assumption |
| 8 | // that it is used only for Kubernetes pod networking. That has a few |
| 9 | // implications: |
| 10 | // |
| 11 | // First, we only have a single real route on the host into the wireguard |
| 12 | // networking mesh / interface, and that is configured ahead of time in the |
| 13 | // Service as ClusterNet. All destination addresses that should be carried by the |
| 14 | // mesh must thus be part of this single route. Otherwise, traffic will be able |
| 15 | // to flow into the node from other nodes, but will exit through another |
| 16 | // interface. This is used in practice to allow other host nodes (whose external |
| 17 | // addresses are outside the cluster network) to access the cluster network. |
| 18 | // |
Serge Bazanski | b565cc6 | 2023-03-30 18:43:51 +0200 | [diff] [blame] | 19 | // Second, we have two hardcoded/purpose-specific sources of prefixes: |
| 20 | // 1. Pod networking node prefixes from the kubelet |
| 21 | // 2. The host's external IP address (as a /32) from the network service. |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 22 | package clusternet |
| 23 | |
| 24 | import ( |
| 25 | "context" |
| 26 | "fmt" |
| 27 | "net" |
Serge Bazanski | b565cc6 | 2023-03-30 18:43:51 +0200 | [diff] [blame] | 28 | "net/netip" |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 29 | |
| 30 | "github.com/cenkalti/backoff/v4" |
| 31 | |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 32 | "source.monogon.dev/metropolis/node/core/localstorage" |
Serge Bazanski | b565cc6 | 2023-03-30 18:43:51 +0200 | [diff] [blame] | 33 | "source.monogon.dev/metropolis/node/core/network" |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 34 | "source.monogon.dev/metropolis/pkg/event" |
| 35 | "source.monogon.dev/metropolis/pkg/supervisor" |
Serge Bazanski | b565cc6 | 2023-03-30 18:43:51 +0200 | [diff] [blame] | 36 | |
| 37 | apb "source.monogon.dev/metropolis/node/core/curator/proto/api" |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 38 | cpb "source.monogon.dev/metropolis/proto/common" |
| 39 | ) |
| 40 | |
| 41 | // Service implements the Cluster Networking Mesh. See package-level docs for |
| 42 | // more details. |
| 43 | type Service struct { |
| 44 | // Curator is the gRPC client that the service will use to reach the cluster's |
| 45 | // Curator, for pushing locally announced prefixes and pulling information about |
| 46 | // other nodes. |
| 47 | Curator apb.CuratorClient |
| 48 | // ClusterNet is the prefix that will be programmed to exit through the wireguard |
| 49 | // mesh. |
| 50 | ClusterNet net.IPNet |
| 51 | // DataDirectory is where the WireGuard key of this node will be stored. |
| 52 | DataDirectory *localstorage.DataKubernetesClusterNetworkingDirectory |
| 53 | // LocalKubernetesPodNetwork is an event.Value watched for prefixes that should |
| 54 | // be announced into the mesh. This is to be Set by the Kubernetes service once |
| 55 | // it knows about the local node's IPAM address assignment. |
| 56 | LocalKubernetesPodNetwork event.Value[*Prefixes] |
Serge Bazanski | b565cc6 | 2023-03-30 18:43:51 +0200 | [diff] [blame] | 57 | // Network service used to get the local node's IP address to submit it as a /32. |
| 58 | Network event.Value[*network.Status] |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 59 | |
| 60 | // wg is the interface to all the low-level interactions with WireGuard (and |
| 61 | // kernel routing). If not set, this defaults to a production implementation. |
| 62 | // This can be overridden by test to a test implementation instead. |
| 63 | wg wireguard |
| 64 | } |
| 65 | |
| 66 | // Run the Service. This must be used in a supervisor Runnable. |
| 67 | func (s *Service) Run(ctx context.Context) error { |
| 68 | if s.wg == nil { |
| 69 | s.wg = &localWireguard{} |
| 70 | } |
| 71 | if err := s.wg.ensureOnDiskKey(s.DataDirectory); err != nil { |
| 72 | return fmt.Errorf("could not ensure wireguard key: %w", err) |
| 73 | } |
| 74 | if err := s.wg.setup(&s.ClusterNet); err != nil { |
| 75 | return fmt.Errorf("could not setup wireguard: %w", err) |
| 76 | } |
| 77 | |
| 78 | supervisor.Logger(ctx).Infof("Wireguard setup complete, starting updaters...") |
| 79 | |
Serge Bazanski | b565cc6 | 2023-03-30 18:43:51 +0200 | [diff] [blame] | 80 | kubeC := make(chan *Prefixes) |
| 81 | netC := make(chan *network.Status) |
| 82 | if err := supervisor.RunGroup(ctx, map[string]supervisor.Runnable{ |
| 83 | "source-kubernetes": event.Pipe(s.LocalKubernetesPodNetwork, kubeC), |
| 84 | "source-network": event.Pipe(s.Network, netC), |
| 85 | "push": func(ctx context.Context) error { |
| 86 | return s.push(ctx, kubeC, netC) |
| 87 | }, |
| 88 | }); err != nil { |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 89 | return err |
| 90 | } |
Serge Bazanski | b565cc6 | 2023-03-30 18:43:51 +0200 | [diff] [blame] | 91 | |
| 92 | if err := supervisor.Run(ctx, "pull", s.pull); err != nil { |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 93 | return err |
| 94 | } |
| 95 | supervisor.Signal(ctx, supervisor.SignalHealthy) |
| 96 | <-ctx.Done() |
| 97 | return ctx.Err() |
| 98 | } |
| 99 | |
| 100 | // push is the sub-runnable responsible for letting the Curator know about what |
| 101 | // prefixes that are originated by this node. |
Serge Bazanski | b565cc6 | 2023-03-30 18:43:51 +0200 | [diff] [blame] | 102 | func (s *Service) push(ctx context.Context, kubeC chan *Prefixes, netC chan *network.Status) error { |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 103 | supervisor.Signal(ctx, supervisor.SignalHealthy) |
| 104 | |
Serge Bazanski | b565cc6 | 2023-03-30 18:43:51 +0200 | [diff] [blame] | 105 | var kubePrefixes *Prefixes |
Serge Bazanski | 521a835 | 2023-06-29 12:38:17 +0200 | [diff] [blame] | 106 | var prevKubePrefixes *Prefixes |
| 107 | |
Serge Bazanski | b565cc6 | 2023-03-30 18:43:51 +0200 | [diff] [blame] | 108 | var localAddr net.IP |
Serge Bazanski | 521a835 | 2023-06-29 12:38:17 +0200 | [diff] [blame] | 109 | var prevLocalAddr net.IP |
| 110 | |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 111 | for { |
Serge Bazanski | 521a835 | 2023-06-29 12:38:17 +0200 | [diff] [blame] | 112 | kubeChanged := false |
| 113 | localChanged := false |
| 114 | |
Serge Bazanski | b565cc6 | 2023-03-30 18:43:51 +0200 | [diff] [blame] | 115 | select { |
| 116 | case <-ctx.Done(): |
| 117 | return ctx.Err() |
| 118 | case kubePrefixes = <-kubeC: |
Serge Bazanski | 521a835 | 2023-06-29 12:38:17 +0200 | [diff] [blame] | 119 | if !kubePrefixes.Equal(prevKubePrefixes) { |
| 120 | kubeChanged = true |
| 121 | } |
Serge Bazanski | b565cc6 | 2023-03-30 18:43:51 +0200 | [diff] [blame] | 122 | case n := <-netC: |
| 123 | localAddr = n.ExternalAddress |
Serge Bazanski | 521a835 | 2023-06-29 12:38:17 +0200 | [diff] [blame] | 124 | if !localAddr.Equal(prevLocalAddr) { |
| 125 | localChanged = true |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Ignore spurious updates. |
| 130 | if !localChanged && !kubeChanged { |
| 131 | continue |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Serge Bazanski | b565cc6 | 2023-03-30 18:43:51 +0200 | [diff] [blame] | 134 | // Prepare prefixes to submit to cluster. |
| 135 | var prefixes Prefixes |
| 136 | |
| 137 | // Do we have a local node address? Add it to the prefixes. |
| 138 | if len(localAddr) > 0 { |
| 139 | addr, ok := netip.AddrFromSlice(localAddr) |
| 140 | if ok { |
| 141 | prefixes = append(prefixes, netip.PrefixFrom(addr, 32)) |
| 142 | } |
| 143 | } |
| 144 | // Do we have any kubelet prefixes? Add them, too. |
| 145 | if kubePrefixes != nil { |
| 146 | prefixes.Update(kubePrefixes) |
| 147 | } |
| 148 | |
Serge Bazanski | 521a835 | 2023-06-29 12:38:17 +0200 | [diff] [blame] | 149 | supervisor.Logger(ctx).Infof("Submitting prefixes: %s (kube update: %v, local update: %v)", prefixes, kubeChanged, localChanged) |
Serge Bazanski | b565cc6 | 2023-03-30 18:43:51 +0200 | [diff] [blame] | 150 | |
| 151 | err := backoff.Retry(func() error { |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 152 | _, err := s.Curator.UpdateNodeClusterNetworking(ctx, &apb.UpdateNodeClusterNetworkingRequest{ |
| 153 | Clusternet: &cpb.NodeClusterNetworking{ |
| 154 | WireguardPubkey: s.wg.key().PublicKey().String(), |
Serge Bazanski | b565cc6 | 2023-03-30 18:43:51 +0200 | [diff] [blame] | 155 | Prefixes: prefixes.proto(), |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 156 | }, |
| 157 | }) |
| 158 | if err != nil { |
| 159 | supervisor.Logger(ctx).Warningf("Could not submit cluster networking update: %v", err) |
| 160 | } |
| 161 | return err |
| 162 | }, backoff.WithContext(backoff.NewExponentialBackOff(), ctx)) |
| 163 | if err != nil { |
| 164 | return fmt.Errorf("couldn't update curator: %w", err) |
| 165 | } |
Serge Bazanski | 521a835 | 2023-06-29 12:38:17 +0200 | [diff] [blame] | 166 | |
| 167 | prevKubePrefixes = kubePrefixes |
| 168 | prevLocalAddr = localAddr |
| 169 | |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | // pull is the sub-runnable responsible for fetching information about the |
| 174 | // cluster networking setup/status of other nodes, and programming it as |
| 175 | // WireGuard peers. |
| 176 | func (s *Service) pull(ctx context.Context) error { |
| 177 | supervisor.Signal(ctx, supervisor.SignalHealthy) |
| 178 | |
| 179 | srv, err := s.Curator.Watch(ctx, &apb.WatchRequest{ |
| 180 | Kind: &apb.WatchRequest_NodesInCluster_{ |
| 181 | NodesInCluster: &apb.WatchRequest_NodesInCluster{}, |
| 182 | }, |
| 183 | }) |
| 184 | if err != nil { |
| 185 | return fmt.Errorf("curator watch failed: %w", err) |
| 186 | } |
| 187 | defer srv.CloseSend() |
| 188 | |
| 189 | nodes := newNodemap() |
| 190 | for { |
| 191 | ev, err := srv.Recv() |
| 192 | if err != nil { |
| 193 | return fmt.Errorf("curator watch recv failed: %w", err) |
| 194 | } |
| 195 | |
| 196 | updated, removed := nodes.update(ctx, ev) |
| 197 | |
| 198 | for _, n := range removed { |
| 199 | supervisor.Logger(ctx).Infof("Node %s removed, unconfiguring", n.id) |
Serge Bazanski | ea6353f | 2023-06-20 13:08:55 +0200 | [diff] [blame] | 200 | if err := s.wg.unconfigurePeer(n.copy()); err != nil { |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 201 | // Do nothing and hope whatever caused this will go away at some point. |
| 202 | supervisor.Logger(ctx).Errorf("Node %s couldn't be unconfigured: %v", n.id, err) |
| 203 | } |
| 204 | } |
| 205 | var newNodes []*node |
| 206 | for _, n := range updated { |
Serge Bazanski | ea6353f | 2023-06-20 13:08:55 +0200 | [diff] [blame] | 207 | newNodes = append(newNodes, n.copy()) |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 208 | supervisor.Logger(ctx).Infof("Node %s updated: pk %s, address %s, prefixes %v", n.id, n.pubkey, n.address, n.prefixes) |
| 209 | } |
| 210 | succeeded := 0 |
| 211 | if err := s.wg.configurePeers(newNodes); err != nil { |
| 212 | // If configuring all nodes at once failed, go node-by-node to make sure we've |
| 213 | // done as much as possible. |
| 214 | supervisor.Logger(ctx).Warningf("Bulk node update call failed, trying node-by-node..: %v", err) |
| 215 | for _, n := range newNodes { |
| 216 | if err := s.wg.configurePeers([]*node{n}); err != nil { |
| 217 | supervisor.Logger(ctx).Errorf("Node %s failed: %v", n.id, err) |
| 218 | } else { |
| 219 | succeeded += 1 |
| 220 | } |
| 221 | } |
| 222 | } else { |
| 223 | succeeded = len(newNodes) |
| 224 | } |
Serge Bazanski | 521a835 | 2023-06-29 12:38:17 +0200 | [diff] [blame] | 225 | |
| 226 | if len(newNodes) != 0 { |
| 227 | supervisor.Logger(ctx).Infof("Successfully updated %d out of %d nodes", succeeded, len(newNodes)) |
| 228 | |
| 229 | numNodes, numPrefixes := nodes.stats() |
| 230 | supervisor.Logger(ctx).Infof("Total: %d nodes, %d prefixes.", numNodes, numPrefixes) |
| 231 | } |
Serge Bazanski | 93d593b | 2023-03-28 16:43:47 +0200 | [diff] [blame] | 232 | } |
| 233 | } |