Skip to content

Commit

Permalink
feat: add support for disabling search domains
Browse files Browse the repository at this point in the history
This adds the option to toggle the automatic creation of search domains.

Signed-off-by: Bᴇʀɴᴅ Sᴄʜᴏʀɢᴇʀs <[email protected]>
Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
bjw-s authored and smira committed May 23, 2022
1 parent a1b6f21 commit f03002e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 5 deletions.
11 changes: 8 additions & 3 deletions internal/app/machined/pkg/controllers/network/etcfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (ctrl *EtcFileController) Run(ctx context.Context, r controller.Runtime, lo
if resolverStatus != nil {
if err = r.Modify(ctx, files.NewEtcFileSpec(files.NamespaceName, "resolv.conf"),
func(r resource.Resource) error {
r.(*files.EtcFileSpec).TypedSpec().Contents = ctrl.renderResolvConf(resolverStatus, hostnameStatus)
r.(*files.EtcFileSpec).TypedSpec().Contents = ctrl.renderResolvConf(resolverStatus, hostnameStatus, cfgProvider)
r.(*files.EtcFileSpec).TypedSpec().Mode = 0o644

return nil
Expand All @@ -152,7 +152,7 @@ func (ctrl *EtcFileController) Run(ctx context.Context, r controller.Runtime, lo
}
}

func (ctrl *EtcFileController) renderResolvConf(resolverStatus *network.ResolverStatusSpec, hostnameStatus *network.HostnameStatusSpec) []byte {
func (ctrl *EtcFileController) renderResolvConf(resolverStatus *network.ResolverStatusSpec, hostnameStatus *network.HostnameStatusSpec, cfgProvider talosconfig.Provider) []byte {
var buf bytes.Buffer

for i, resolver := range resolverStatus.DNSServers {
Expand All @@ -164,7 +164,12 @@ func (ctrl *EtcFileController) renderResolvConf(resolverStatus *network.Resolver
fmt.Fprintf(&buf, "nameserver %s\n", resolver)
}

if hostnameStatus != nil && hostnameStatus.Domainname != "" {
var disableSearchDomain bool
if cfgProvider != nil {
disableSearchDomain = cfgProvider.Machine().Network().DisableSearchDomain()
}

if !disableSearchDomain && hostnameStatus != nil && hostnameStatus.Domainname != "" {
fmt.Fprintf(&buf, "\nsearch %s\n", hostnameStatus.Domainname)
}

Expand Down
18 changes: 18 additions & 0 deletions internal/app/machined/pkg/controllers/network/etcfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,24 @@ func (suite *EtcFileConfigSuite) TestNoExtraHosts() {
)
}

func (suite *EtcFileConfigSuite) TestNoSearchDomain() {
cfg := config.NewMachineConfig(
&v1alpha1.Config{
ConfigVersion: "v1alpha1",
MachineConfig: &v1alpha1.MachineConfig{
MachineNetwork: &v1alpha1.NetworkConfig{
NetworkDisableSearchDomain: true,
},
},
},
)
suite.testFiles(
[]resource.Resource{cfg, suite.defaultAddress, suite.hostnameStatus, suite.resolverStatus},
"nameserver 1.1.1.1\nnameserver 2.2.2.2\nnameserver 3.3.3.3\n",
"127.0.0.1 localhost\n33.11.22.44 foo.example.com foo\n::1 localhost ip6-localhost ip6-loopback\nff02::1 ip6-allnodes\nff02::2 ip6-allrouters", //nolint:lll
)
}

func (suite *EtcFileConfigSuite) TestNoDomainname() {
suite.hostnameStatus.TypedSpec().Domainname = ""

Expand Down
1 change: 1 addition & 0 deletions pkg/machinery/config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ type MachineNetwork interface {
Devices() []Device
ExtraHosts() []ExtraHost
KubeSpan() KubeSpan
DisableSearchDomain() bool
}

// ExtraHost represents a host entry in /etc/hosts.
Expand Down
5 changes: 5 additions & 0 deletions pkg/machinery/config/types/v1alpha1/v1alpha1_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,11 @@ func (n *NetworkConfig) Hostname() string {
return n.NetworkHostname
}

// DisableSearchDomain implements the config.Provider interface.
func (n *NetworkConfig) DisableSearchDomain() bool {
return n.NetworkDisableSearchDomain
}

// Devices implements the config.Provider interface.
func (n *NetworkConfig) Devices() []config.Device {
interfaces := make([]config.Device, len(n.NetworkInterfaces))
Expand Down
13 changes: 12 additions & 1 deletion pkg/machinery/config/types/v1alpha1/v1alpha1_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ var (
kubeletImageExample = (&KubeletConfig{}).Image()

machineNetworkConfigExample = &NetworkConfig{
NetworkHostname: "worker-1",
NetworkHostname: "worker-1",
NetworkDisableSearchDomain: false,
NetworkInterfaces: []*Device{
{
DeviceInterface: "eth0",
Expand Down Expand Up @@ -1061,6 +1062,16 @@ type NetworkConfig struct {
// examples:
// - value: networkKubeSpanExample
NetworkKubeSpan NetworkKubeSpan `yaml:"kubespan,omitempty"`
// description: |
// Disable generating a default search domain in /etc/resolv.conf
// based on the machine hostname.
// Defaults to `false`.
// values:
// - true
// - yes
// - false
// - no
NetworkDisableSearchDomain bool `yaml:"disableSearchDomain,omitempty"`
}

// InstallConfig represents the installation options for preparing a node.
Expand Down
13 changes: 12 additions & 1 deletion pkg/machinery/config/types/v1alpha1/v1alpha1_types_doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions website/content/v1.1/reference/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,7 @@ extraHostEntries:
kubespan:
enabled: true # Enable the KubeSpan feature.
{{< /highlight >}}</details> | |
|`disableSearchDomain` |bool |<details><summary>Disable generating a default search domain in /etc/resolv.conf</summary>based on the machine hostname.<br />Defaults to `false`.</details> |`true`<br />`yes`<br />`false`<br />`no`<br /> |



Expand Down

0 comments on commit f03002e

Please sign in to comment.