|
| 1 | +package actions |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/netip" |
| 7 | + "slices" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/shipkeepdev/shipkeep/agent/internal/caddy" |
| 12 | + "github.com/shipkeepdev/shipkeep/agent/internal/docker" |
| 13 | + "github.com/shipkeepdev/shipkeep/agent/internal/journal" |
| 14 | +) |
| 15 | + |
| 16 | +// ApplicationLabel is the label the control plane gives every container of an application: the |
| 17 | +// application's id, which is also the id of the application's route. |
| 18 | +const ApplicationLabel = "shipkeep.application" |
| 19 | + |
| 20 | +// Why a route was not adopted. Codes only: never an address, a name or an error text. |
| 21 | +const ( |
| 22 | + // AdoptNoContainer: no running managed container has an address the route proxies to. |
| 23 | + AdoptNoContainer = "no_running_container" |
| 24 | + // AdoptOtherApplication: the container at an address the route proxies to belongs to another |
| 25 | + // application, or to none. The route already sends its traffic elsewhere. |
| 26 | + AdoptOtherApplication = "other_application" |
| 27 | + // AdoptUnexpected: the route is not one an agent writes (an upstream that is not a private |
| 28 | + // address and port, the same address twice, or weights out of range). |
| 29 | + AdoptUnexpected = "unexpected_route" |
| 30 | +) |
| 31 | + |
| 32 | +// RouteLister lists the routes of the Shipkeep server; the Caddy client implements it. |
| 33 | +type RouteLister interface { |
| 34 | + Routes(ctx context.Context) ([]caddy.Route, error) |
| 35 | +} |
| 36 | + |
| 37 | +// Inventory lists and inspects the managed containers; the Docker engine implements it. |
| 38 | +type Inventory interface { |
| 39 | + Containers |
| 40 | + ManagedContainers(ctx context.Context) ([]docker.ContainerSummary, error) |
| 41 | +} |
| 42 | + |
| 43 | +// Adoption is what AdoptRoutes found: the routes already followed, the routes it recorded, and the |
| 44 | +// routes it could not tie to their containers, by Shipkeep route id, with why. |
| 45 | +type Adoption struct { |
| 46 | + Followed, Adopted []string |
| 47 | + Refused map[string]string |
| 48 | +} |
| 49 | + |
| 50 | +// AdoptRoutes records the routes that agents up to 0.1.0-alpha.3 wrote without a record (ADR |
| 51 | +// 0017), so that from now on they follow their containers when Docker gives them other addresses. |
| 52 | +// It runs once, from the installer's migration, while the agent is stopped and before Docker |
| 53 | +// restarts: until then each container still has the address the route was given. |
| 54 | +// |
| 55 | +// A route is adopted only when every address it proxies to is the address of a running managed |
| 56 | +// container of the route's own application, by its label, so that a route whose traffic already |
| 57 | +// goes elsewhere is never recorded as right. Such a route, and any other it cannot read, is left |
| 58 | +// as it is and named in Refused: its application is deployed again to give it a record. A route |
| 59 | +// whose record matches what Caddy holds is already followed. Nothing is written to Caddy. |
| 60 | +func AdoptRoutes(ctx context.Context, routes RouteLister, containers Inventory, book *RouteBook, now time.Time) (Adoption, error) { |
| 61 | + listed, err := routes.Routes(ctx) |
| 62 | + if err != nil { |
| 63 | + return Adoption{}, fmt.Errorf("list routes: %w", err) |
| 64 | + } |
| 65 | + owners, err := addressOwners(ctx, containers) |
| 66 | + if err != nil { |
| 67 | + return Adoption{}, err |
| 68 | + } |
| 69 | + defer book.lock()() |
| 70 | + adoption := Adoption{Refused: map[string]string{}} |
| 71 | + for _, route := range listed { |
| 72 | + application, ok := caddy.RouteName(route.ID) |
| 73 | + if !ok || !routeID.MatchString(application) || len(route.Handle) != 1 || route.Handle[0].Handler != "reverse_proxy" { |
| 74 | + // Not a Shipkeep route, or one with nothing to follow: the agent's 503 answer, the |
| 75 | + // maintenance page. |
| 76 | + continue |
| 77 | + } |
| 78 | + record, found, err := book.Store.RouteRecord(ctx, route.ID) |
| 79 | + if err != nil { |
| 80 | + return adoption, fmt.Errorf("read route record: %w", err) |
| 81 | + } |
| 82 | + if _, ours := written(route, record); found && ours { |
| 83 | + adoption.Followed = append(adoption.Followed, application) |
| 84 | + continue |
| 85 | + } |
| 86 | + upstreams, reason := adopted(route.Handle[0], application, owners) |
| 87 | + if reason != "" { |
| 88 | + adoption.Refused[application] = reason |
| 89 | + continue |
| 90 | + } |
| 91 | + next := journal.RouteRecord{Upstreams: upstreams, Written: [][]string{dials(route)}} |
| 92 | + if err := book.Store.SetRouteRecord(ctx, route.ID, next, now); err != nil { |
| 93 | + return adoption, fmt.Errorf("record route: %w", err) |
| 94 | + } |
| 95 | + adoption.Adopted = append(adoption.Adopted, application) |
| 96 | + } |
| 97 | + return adoption, nil |
| 98 | +} |
| 99 | + |
| 100 | +// addressOwner is the running managed container at an address. |
| 101 | +type addressOwner struct { |
| 102 | + name, application string |
| 103 | + // shared marks an address two containers report: neither is taken for it. |
| 104 | + shared bool |
| 105 | +} |
| 106 | + |
| 107 | +// addressOwners maps each running managed container's service address to the container. |
| 108 | +func addressOwners(ctx context.Context, containers Inventory) (map[netip.Addr]addressOwner, error) { |
| 109 | + summaries, err := containers.ManagedContainers(ctx) |
| 110 | + if err != nil { |
| 111 | + return nil, fmt.Errorf("list containers: %w", err) |
| 112 | + } |
| 113 | + owners := map[netip.Addr]addressOwner{} |
| 114 | + for _, summary := range summaries { |
| 115 | + if len(summary.Names) == 0 { |
| 116 | + continue |
| 117 | + } |
| 118 | + name := strings.TrimPrefix(summary.Names[0], "/") |
| 119 | + if !resourceName.MatchString(name) { |
| 120 | + continue |
| 121 | + } |
| 122 | + container, err := containers.Container(ctx, summary.ID) |
| 123 | + if err != nil { |
| 124 | + // Docker did not answer, or the container went away: nothing is recorded on a guess. |
| 125 | + return nil, fmt.Errorf("inspect container: %w", err) |
| 126 | + } |
| 127 | + address, ok := serviceAddress(container) |
| 128 | + if !ok { |
| 129 | + continue |
| 130 | + } |
| 131 | + if _, taken := owners[address]; taken { |
| 132 | + owners[address] = addressOwner{shared: true} |
| 133 | + continue |
| 134 | + } |
| 135 | + owners[address] = addressOwner{name: name, application: container.Config.Labels[ApplicationLabel]} |
| 136 | + } |
| 137 | + return owners, nil |
| 138 | +} |
| 139 | + |
| 140 | +// adopted lists the upstreams of a route's handler as a record keeps them, or says why it cannot. |
| 141 | +func adopted(handler caddy.Handler, application string, owners map[netip.Addr]addressOwner) ([]journal.RouteUpstream, string) { |
| 142 | + if len(handler.Upstreams) == 0 || len(handler.Upstreams) > maxUpstreams { |
| 143 | + return nil, AdoptUnexpected |
| 144 | + } |
| 145 | + weights := installedWeights(handler) |
| 146 | + if handler.LoadBalancing != nil && len(weights) != len(handler.Upstreams) { |
| 147 | + return nil, AdoptUnexpected |
| 148 | + } |
| 149 | + upstreams := make([]journal.RouteUpstream, 0, len(handler.Upstreams)) |
| 150 | + for index, upstream := range handler.Upstreams { |
| 151 | + dial, err := netip.ParseAddrPort(upstream.Dial) |
| 152 | + if err != nil || dial.Port() == 0 || !dial.Addr().IsPrivate() || dial.String() != upstream.Dial { |
| 153 | + return nil, AdoptUnexpected |
| 154 | + } |
| 155 | + weight := 1 |
| 156 | + if weights != nil { |
| 157 | + weight = weights[index] |
| 158 | + } |
| 159 | + if weight < 1 || weight > maxWeight { |
| 160 | + return nil, AdoptUnexpected |
| 161 | + } |
| 162 | + owner, ok := owners[dial.Addr()] |
| 163 | + switch { |
| 164 | + case !ok: |
| 165 | + return nil, AdoptNoContainer |
| 166 | + case owner.shared || owner.application != application: |
| 167 | + return nil, AdoptOtherApplication |
| 168 | + } |
| 169 | + if slices.ContainsFunc(upstreams, func(u journal.RouteUpstream) bool { return u.Container == owner.name && u.Port == dial.Port() }) { |
| 170 | + return nil, AdoptUnexpected |
| 171 | + } |
| 172 | + upstreams = append(upstreams, journal.RouteUpstream{Container: owner.name, Port: dial.Port(), Weight: weight}) |
| 173 | + } |
| 174 | + return upstreams, "" |
| 175 | +} |
0 commit comments