|
2 | 2 | // License, v. 2.0. If a copy of the MPL was not distributed with this
|
3 | 3 | // file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
4 | 4 |
|
5 |
| -// Package network provides controllers which manage network resources. |
6 |
| -// |
7 |
| -//nolint:dupl |
8 | 5 | package network
|
9 | 6 |
|
10 | 7 | import (
|
11 |
| - "context" |
12 |
| - "fmt" |
13 |
| - |
14 | 8 | "github.com/cosi-project/runtime/pkg/controller"
|
15 | 9 | "github.com/cosi-project/runtime/pkg/resource"
|
16 | 10 | "github.com/cosi-project/runtime/pkg/safe"
|
17 |
| - "github.com/cosi-project/runtime/pkg/state" |
18 | 11 | "go.uber.org/zap"
|
19 | 12 |
|
20 | 13 | "github.com/siderolabs/talos/pkg/machinery/resources/network"
|
21 | 14 | )
|
22 | 15 |
|
23 |
| -// AddressMergeController merges network.AddressSpec in network.ConfigNamespace and produces final network.AddressSpec in network.Namespace. |
24 |
| -type AddressMergeController struct{} |
25 |
| - |
26 |
| -// Name implements controller.Controller interface. |
27 |
| -func (ctrl *AddressMergeController) Name() string { |
28 |
| - return "network.AddressMergeController" |
29 |
| -} |
30 |
| - |
31 |
| -// Inputs implements controller.Controller interface. |
32 |
| -func (ctrl *AddressMergeController) Inputs() []controller.Input { |
33 |
| - return []controller.Input{ |
34 |
| - { |
35 |
| - Namespace: network.ConfigNamespaceName, |
36 |
| - Type: network.AddressSpecType, |
37 |
| - Kind: controller.InputWeak, |
38 |
| - }, |
39 |
| - { |
40 |
| - Namespace: network.NamespaceName, |
41 |
| - Type: network.AddressSpecType, |
42 |
| - Kind: controller.InputDestroyReady, |
43 |
| - }, |
44 |
| - } |
45 |
| -} |
46 |
| - |
47 |
| -// Outputs implements controller.Controller interface. |
48 |
| -func (ctrl *AddressMergeController) Outputs() []controller.Output { |
49 |
| - return []controller.Output{ |
50 |
| - { |
51 |
| - Type: network.AddressSpecType, |
52 |
| - Kind: controller.OutputShared, |
53 |
| - }, |
54 |
| - } |
55 |
| -} |
56 |
| - |
57 |
| -// Run implements controller.Controller interface. |
| 16 | +// NewAddressMergeController initializes a AddressMergeController. |
58 | 17 | //
|
59 |
| -//nolint:gocyclo |
60 |
| -func (ctrl *AddressMergeController) Run(ctx context.Context, r controller.Runtime, _ *zap.Logger) error { |
61 |
| - for { |
62 |
| - select { |
63 |
| - case <-ctx.Done(): |
64 |
| - return nil |
65 |
| - case <-r.EventCh(): |
66 |
| - } |
67 |
| - |
68 |
| - // list source network configuration resources |
69 |
| - list, err := r.List(ctx, resource.NewMetadata(network.ConfigNamespaceName, network.AddressSpecType, "", resource.VersionUndefined)) |
70 |
| - if err != nil { |
71 |
| - return fmt.Errorf("error listing source network addresses: %w", err) |
72 |
| - } |
73 |
| - |
74 |
| - // address is allowed as long as it's not duplicate, for duplicate higher layer takes precedence |
75 |
| - addresses := map[string]*network.AddressSpec{} |
76 |
| - |
77 |
| - for _, res := range list.Items { |
78 |
| - address := res.(*network.AddressSpec) //nolint:forcetypeassert |
79 |
| - id := network.AddressID(address.TypedSpec().LinkName, address.TypedSpec().Address) |
80 |
| - |
81 |
| - existing, ok := addresses[id] |
82 |
| - if ok && existing.TypedSpec().ConfigLayer > address.TypedSpec().ConfigLayer { |
83 |
| - // skip this address, as existing one is higher layer |
84 |
| - continue |
85 |
| - } |
86 |
| - |
87 |
| - addresses[id] = address |
88 |
| - } |
89 |
| - |
90 |
| - conflictsDetected := 0 |
91 |
| - |
92 |
| - for id, address := range addresses { |
93 |
| - if err = safe.WriterModify(ctx, r, network.NewAddressSpec(network.NamespaceName, id), func(addr *network.AddressSpec) error { |
94 |
| - *addr.TypedSpec() = *address.TypedSpec() |
95 |
| - |
96 |
| - return nil |
97 |
| - }); err != nil { |
98 |
| - if state.IsPhaseConflictError(err) { |
99 |
| - // phase conflict, resource is being torn down, skip updating it and trigger reconcile |
100 |
| - // later by failing the |
101 |
| - conflictsDetected++ |
102 |
| - |
103 |
| - delete(addresses, id) |
104 |
| - } else { |
105 |
| - return fmt.Errorf("error updating resource: %w", err) |
106 |
| - } |
107 |
| - } |
108 |
| - } |
109 |
| - |
110 |
| - // list addresses for cleanup |
111 |
| - list, err = r.List(ctx, resource.NewMetadata(network.NamespaceName, network.AddressSpecType, "", resource.VersionUndefined)) |
112 |
| - if err != nil { |
113 |
| - return fmt.Errorf("error listing resources: %w", err) |
114 |
| - } |
115 |
| - |
116 |
| - for _, res := range list.Items { |
117 |
| - if _, ok := addresses[res.Metadata().ID()]; !ok { |
118 |
| - var okToDestroy bool |
119 |
| - |
120 |
| - okToDestroy, err = r.Teardown(ctx, res.Metadata()) |
121 |
| - if err != nil { |
122 |
| - return fmt.Errorf("error cleaning up addresses: %w", err) |
| 18 | +// AddressMergeController merges network.AddressSpec in network.ConfigNamespace and produces final network.AddressSpec in network.Namespace. |
| 19 | +func NewAddressMergeController() controller.Controller { |
| 20 | + return GenericMergeController( |
| 21 | + network.ConfigNamespaceName, |
| 22 | + network.NamespaceName, |
| 23 | + func(logger *zap.Logger, list safe.List[*network.AddressSpec]) map[resource.ID]*network.AddressSpecSpec { |
| 24 | + // address is allowed as long as it's not duplicate, for duplicate higher layer takes precedence |
| 25 | + addresses := map[resource.ID]*network.AddressSpecSpec{} |
| 26 | + |
| 27 | + for address := range list.All() { |
| 28 | + id := network.AddressID(address.TypedSpec().LinkName, address.TypedSpec().Address) |
| 29 | + |
| 30 | + existing, ok := addresses[id] |
| 31 | + if ok && existing.ConfigLayer > address.TypedSpec().ConfigLayer { |
| 32 | + // skip this address, as existing one is higher layer |
| 33 | + continue |
123 | 34 | }
|
124 | 35 |
|
125 |
| - if okToDestroy { |
126 |
| - if err = r.Destroy(ctx, res.Metadata()); err != nil { |
127 |
| - return fmt.Errorf("error cleaning up addresses: %w", err) |
128 |
| - } |
129 |
| - } |
| 36 | + addresses[id] = address.TypedSpec() |
130 | 37 | }
|
131 |
| - } |
132 | 38 |
|
133 |
| - if conflictsDetected > 0 { |
134 |
| - return fmt.Errorf("%d conflict(s) detected", conflictsDetected) |
135 |
| - } |
136 |
| - |
137 |
| - r.ResetRestartBackoff() |
138 |
| - } |
| 39 | + return addresses |
| 40 | + }, |
| 41 | + ) |
139 | 42 | }
|
0 commit comments