-
Notifications
You must be signed in to change notification settings - Fork 453
/
maintenance_cert_sans.go
107 lines (87 loc) · 2.93 KB
/
maintenance_cert_sans.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package secrets
import (
"context"
"fmt"
"net/netip"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/cosi-project/runtime/pkg/state"
"github.com/siderolabs/gen/optional"
"go.uber.org/zap"
"github.com/siderolabs/talos/pkg/machinery/constants"
"github.com/siderolabs/talos/pkg/machinery/resources/network"
"github.com/siderolabs/talos/pkg/machinery/resources/secrets"
)
// MaintenanceCertSANsController manages secrets.APICertSANs based on configuration.
type MaintenanceCertSANsController struct{}
// Name implements controller.Controller interface.
func (ctrl *MaintenanceCertSANsController) Name() string {
return "secrets.MaintenanceCertSANsController"
}
// Inputs implements controller.Controller interface.
func (ctrl *MaintenanceCertSANsController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: network.NamespaceName,
Type: network.HostnameStatusType,
ID: optional.Some(network.HostnameID),
Kind: controller.InputWeak,
},
{
Namespace: network.NamespaceName,
Type: network.NodeAddressType,
ID: optional.Some(network.NodeAddressAccumulativeID),
Kind: controller.InputWeak,
},
}
}
// Outputs implements controller.Controller interface.
func (ctrl *MaintenanceCertSANsController) Outputs() []controller.Output {
return []controller.Output{
{
Type: secrets.CertSANType,
Kind: controller.OutputShared,
},
}
}
// Run implements controller.Controller interface.
func (ctrl *MaintenanceCertSANsController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
for {
select {
case <-ctx.Done():
return nil
case <-r.EventCh():
}
hostnameStatus, err := safe.ReaderGetByID[*network.HostnameStatus](ctx, r, network.HostnameID)
if err != nil && !state.IsNotFoundError(err) {
return fmt.Errorf("failed to get hostname status: %w", err)
}
nodeAddresses, err := safe.ReaderGetByID[*network.NodeAddress](ctx, r, network.NodeAddressAccumulativeID)
if err != nil {
if state.IsNotFoundError(err) {
continue
}
return err
}
if err = r.Modify(ctx, secrets.NewCertSAN(secrets.NamespaceName, secrets.CertSANMaintenanceID), func(r resource.Resource) error {
spec := r.(*secrets.CertSAN).TypedSpec()
spec.Reset()
spec.AppendIPs(nodeAddresses.TypedSpec().IPs()...)
spec.AppendIPs(netip.MustParseAddr("127.0.0.1"))
spec.AppendIPs(netip.MustParseAddr("::1"))
if hostnameStatus != nil {
spec.AppendDNSNames(hostnameStatus.TypedSpec().DNSNames()...)
}
spec.FQDN = constants.MaintenanceServiceCommonName
spec.Sort()
return nil
}); err != nil {
return err
}
r.ResetRestartBackoff()
}
}