Skip to content

Commit

Permalink
fix: add ApplyDynamicConfig call in the apply-config --immediate mode
Browse files Browse the repository at this point in the history
This should align all `apply-config` modes to use the same flows.
Also added unit-tests for `ApplyDynamicConfig`.

Signed-off-by: Artem Chernyshev <artem.0xD2@gmail.com>
  • Loading branch information
Unix4ever authored and talos-bot committed Mar 1, 2021
1 parent 376fdcf commit c2f7a4b
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 15 deletions.
37 changes: 22 additions & 15 deletions internal/app/machined/internal/server/v1alpha1/v1alpha1_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,33 @@ func (s *Server) Register(obj *grpc.Server) {
func (s *Server) ApplyConfiguration(ctx context.Context, in *machine.ApplyConfigurationRequest) (*machine.ApplyConfigurationResponse, error) {
log.Printf("apply config request: immediate %v, on reboot %v", in.Immediate, in.OnReboot)

applyDynamicConfig := func() ([]byte, error) {
cfg, err := s.Controller.Runtime().ValidateConfig(in.GetData())
if err != nil {
return nil, err
}

err = cfg.ApplyDynamicConfig(ctx, s.Controller.Runtime().State().Platform())
if err != nil {
return nil, err
}

return cfg.Bytes()
}

switch {
// --immediate
case in.Immediate:
if err := s.Controller.Runtime().CanApplyImmediate(in.GetData()); err != nil {
return nil, err
}

if err := s.Controller.Runtime().SetConfig(in.GetData()); err != nil {
cfg, err := applyDynamicConfig()
if err != nil {
return nil, err
}

if err := s.Controller.Runtime().SetConfig(cfg); err != nil {
return nil, err
}

Expand All @@ -142,24 +161,12 @@ func (s *Server) ApplyConfiguration(ctx context.Context, in *machine.ApplyConfig
}()
// --no-reboot
case in.OnReboot:
cfg, err := s.Controller.Runtime().ValidateConfig(in.GetData())
if err != nil {
return nil, err
}

err = cfg.ApplyDynamicConfig(ctx, s.Controller.Runtime().State().Platform())
if err != nil {
return nil, err
}

var b []byte

b, err = cfg.Bytes()
cfg, err := applyDynamicConfig()
if err != nil {
return nil, err
}

if err = ioutil.WriteFile(constants.ConfigPath, b, 0o600); err != nil {
if err = ioutil.WriteFile(constants.ConfigPath, cfg, 0o600); err != nil {
return nil, err
}
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/machinery/config/types/v1alpha1/bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func NewConfigBundle(opts ...Option) (*v1alpha1.ConfigBundle, error) {
fmt.Println("generating PKI and tokens")
}

if options.InputOptions == nil {
return nil, fmt.Errorf("no WithInputOptions is defined")
}

secrets, err := generate.NewSecretsBundle(generate.NewClock(), options.InputOptions.GenOptions...)
if err != nil {
return bundle, err
Expand Down
94 changes: 94 additions & 0 deletions pkg/machinery/config/types/v1alpha1/v1alpha1_provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 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 v1alpha1_test

import (
"context"
"net"
"testing"

"github.com/stretchr/testify/require"

"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/bundle"
"github.com/talos-systems/talos/pkg/machinery/constants"
)

type dynamicConfigProvider struct {
externalIPs []net.IP
}

// Hostname implements DynamicConfigProvider.
func (cp *dynamicConfigProvider) Hostname(ctx context.Context) ([]byte, error) {
return []byte("talos-default-master-1"), nil
}

// ExternalIPs implements DynamicConfigProvider.
func (cp *dynamicConfigProvider) ExternalIPs(ctx context.Context) ([]net.IP, error) {
return cp.externalIPs, nil
}

// TestApplyDynamicConfig check ApplyDynamicConfig works and is idempotent.
func TestApplyDynamicConfig(t *testing.T) {
b, err := bundle.NewConfigBundle(
bundle.WithInputOptions(
&bundle.InputOptions{
ClusterName: "talos-default",
Endpoint: "10.5.0.1",
KubeVersion: constants.DefaultKubernetesVersion,
},
),
)
require.NoError(t, err)

config := b.ControlPlane()

ctx := context.Background()

provider := &dynamicConfigProvider{
externalIPs: []net.IP{
net.ParseIP("10.2.0.3"),
net.ParseIP("10.10.1.2"),
},
}

err = config.ApplyDynamicConfig(ctx, provider)
require.NoError(t, err)

c, ok := config.(*v1alpha1.Config)

require.True(t, ok)

require.Equal(t, "talos-default-master-1", c.Machine().Network().Hostname())
require.Equal(t, []string{"10.2.0.3", "10.10.1.2"}, c.MachineConfig.CertSANs())

provider.externalIPs = []net.IP{
net.ParseIP("10.2.0.3"),
net.ParseIP("10.10.1.2"),
}

provider = &dynamicConfigProvider{
externalIPs: []net.IP{
net.ParseIP("10.2.0.3"),
net.ParseIP("10.10.1.2"),
net.ParseIP("10.10.1.3"),
},
}

err = config.ApplyDynamicConfig(ctx, provider)
require.NoError(t, err)
require.Equal(t, []string{"10.2.0.3", "10.10.1.2", "10.10.1.3"}, c.MachineConfig.CertSANs())
require.Equal(t, []string{"10.2.0.3", "10.10.1.2", "10.10.1.3"}, c.ClusterConfig.CertSANs())

c.MachineConfig.MachineNetwork.NetworkInterfaces = append(c.MachineConfig.MachineNetwork.NetworkInterfaces, &v1alpha1.Device{
DeviceVIPConfig: &v1alpha1.DeviceVIPConfig{
SharedIP: "192.168.88.77",
},
})

err = config.ApplyDynamicConfig(ctx, provider)
require.NoError(t, err)
require.Equal(t, []string{"10.2.0.3", "10.10.1.2", "10.10.1.3", "192.168.88.77"}, c.MachineConfig.CertSANs())
}

0 comments on commit c2f7a4b

Please sign in to comment.