-
Notifications
You must be signed in to change notification settings - Fork 458
/
config.go
93 lines (75 loc) · 2.96 KB
/
config.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
// 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 cluster
import (
"context"
"encoding/base64"
"net"
"net/url"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/controller/generic/transform"
"github.com/siderolabs/gen/optional"
"go.uber.org/zap"
"github.com/siderolabs/talos/pkg/machinery/resources/cluster"
"github.com/siderolabs/talos/pkg/machinery/resources/config"
)
// ConfigController watches v1alpha1.Config, updates discovery config.
type ConfigController = transform.Controller[*config.MachineConfig, *cluster.Config]
// NewConfigController instanciates the config controller.
func NewConfigController() *ConfigController {
return transform.NewController(
transform.Settings[*config.MachineConfig, *cluster.Config]{
Name: "cluster.ConfigController",
MapMetadataOptionalFunc: func(cfg *config.MachineConfig) optional.Optional[*cluster.Config] {
if cfg.Metadata().ID() != config.V1Alpha1ID {
return optional.None[*cluster.Config]()
}
if cfg.Config().Cluster() == nil {
return optional.None[*cluster.Config]()
}
return optional.Some(cluster.NewConfig(config.NamespaceName, cluster.ConfigID))
},
TransformFunc: func(ctx context.Context, r controller.Reader, logger *zap.Logger, cfg *config.MachineConfig, res *cluster.Config) error {
c := cfg.Config()
res.TypedSpec().DiscoveryEnabled = c.Cluster().Discovery().Enabled()
if c.Cluster().Discovery().Enabled() {
res.TypedSpec().RegistryKubernetesEnabled = c.Cluster().Discovery().Registries().Kubernetes().Enabled()
res.TypedSpec().RegistryServiceEnabled = c.Cluster().Discovery().Registries().Service().Enabled()
if c.Cluster().Discovery().Registries().Service().Enabled() {
var u *url.URL
u, err := url.ParseRequestURI(c.Cluster().Discovery().Registries().Service().Endpoint())
if err != nil {
return err
}
host := u.Hostname()
port := u.Port()
if port == "" {
if u.Scheme == "http" {
port = "80"
} else {
port = "443" // use default https port for everything else
}
}
res.TypedSpec().ServiceEndpoint = net.JoinHostPort(host, port)
res.TypedSpec().ServiceEndpointInsecure = u.Scheme == "http"
res.TypedSpec().ServiceEncryptionKey, err = base64.StdEncoding.DecodeString(c.Cluster().Secret())
if err != nil {
return err
}
res.TypedSpec().ServiceClusterID = c.Cluster().ID()
} else {
res.TypedSpec().ServiceEndpoint = ""
res.TypedSpec().ServiceEndpointInsecure = false
res.TypedSpec().ServiceEncryptionKey = nil
res.TypedSpec().ServiceClusterID = ""
}
} else {
res.TypedSpec().RegistryKubernetesEnabled = false
res.TypedSpec().RegistryServiceEnabled = false
}
return nil
},
},
)
}