-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement KubeSpan identity controller
Fixes #4138 When KubeSpan is enabled, Talos automatically generates or loads KubeSpan identity which consists of Wireguard key pair. ULA address is calculated based on ClusterID and first NIC MAC address. Some code was borrowed from #3577. Example: ``` $ talosctl -n 172.20.0.2 get ksi NODE NAMESPACE TYPE ID VERSION ADDRESS PUBLICKEY 172.20.0.2 kubespan KubeSpanIdentity local 1 fd71:6e1d:86be:6302:e871:1bff:feb2:ccee/128 Oak2fBEWngBhwslBxDVgnRNHXs88OAp4kjroSX0uqUE= ``` Additional changes: * `--with-kubespan` flag for `talosctl cluster create` for quick testing * validate that cluster discovery (and KubeSpan) requires ClusterID and ClusterSecret. Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com> Signed-off-by: Seán C McCord <ulexus@gmail.com> Co-authored-by: Seán C McCord <ulexus@gmail.com>
- Loading branch information
Showing
30 changed files
with
1,134 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
internal/app/machined/pkg/controllers/kubespan/config.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// 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 kubespan | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/AlekSi/pointer" | ||
"github.com/cosi-project/runtime/pkg/controller" | ||
"github.com/cosi-project/runtime/pkg/resource" | ||
"github.com/cosi-project/runtime/pkg/state" | ||
"go.uber.org/zap" | ||
|
||
"github.com/talos-systems/talos/pkg/resources/config" | ||
"github.com/talos-systems/talos/pkg/resources/kubespan" | ||
) | ||
|
||
// ConfigController watches v1alpha1.Config, updates KubeSpan config. | ||
type ConfigController struct{} | ||
|
||
// Name implements controller.Controller interface. | ||
func (ctrl *ConfigController) Name() string { | ||
return "kubespan.ConfigController" | ||
} | ||
|
||
// Inputs implements controller.Controller interface. | ||
func (ctrl *ConfigController) Inputs() []controller.Input { | ||
return []controller.Input{ | ||
{ | ||
Namespace: config.NamespaceName, | ||
Type: config.MachineConfigType, | ||
ID: pointer.ToString(config.V1Alpha1ID), | ||
Kind: controller.InputWeak, | ||
}, | ||
} | ||
} | ||
|
||
// Outputs implements controller.Controller interface. | ||
func (ctrl *ConfigController) Outputs() []controller.Output { | ||
return []controller.Output{ | ||
{ | ||
Type: kubespan.ConfigType, | ||
Kind: controller.OutputExclusive, | ||
}, | ||
} | ||
} | ||
|
||
// Run implements controller.Controller interface. | ||
// | ||
//nolint:gocyclo | ||
func (ctrl *ConfigController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-r.EventCh(): | ||
cfg, err := r.Get(ctx, resource.NewMetadata(config.NamespaceName, config.MachineConfigType, config.V1Alpha1ID, resource.VersionUndefined)) | ||
if err != nil { | ||
if !state.IsNotFoundError(err) { | ||
return fmt.Errorf("error getting config: %w", err) | ||
} | ||
} | ||
|
||
touchedIDs := make(map[resource.ID]struct{}) | ||
|
||
if cfg != nil { | ||
c := cfg.(*config.MachineConfig).Config() | ||
|
||
if err = r.Modify(ctx, kubespan.NewConfig(config.NamespaceName, kubespan.ConfigID), func(res resource.Resource) error { | ||
res.(*kubespan.Config).TypedSpec().Enabled = c.Machine().Network().KubeSpan().Enabled() | ||
res.(*kubespan.Config).TypedSpec().ClusterID = c.Cluster().ID() | ||
|
||
return nil | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
touchedIDs[kubespan.ConfigID] = struct{}{} | ||
} | ||
|
||
// list keys for cleanup | ||
list, err := r.List(ctx, resource.NewMetadata(config.NamespaceName, kubespan.ConfigType, "", resource.VersionUndefined)) | ||
if err != nil { | ||
return fmt.Errorf("error listing resources: %w", err) | ||
} | ||
|
||
for _, res := range list.Items { | ||
if res.Metadata().Owner() != ctrl.Name() { | ||
continue | ||
} | ||
|
||
if _, ok := touchedIDs[res.Metadata().ID()]; !ok { | ||
if err = r.Destroy(ctx, res.Metadata()); err != nil { | ||
return fmt.Errorf("error cleaning up specs: %w", err) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
internal/app/machined/pkg/controllers/kubespan/config_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 kubespan_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/cosi-project/runtime/pkg/resource" | ||
"github.com/stretchr/testify/suite" | ||
"github.com/talos-systems/go-retry/retry" | ||
|
||
kubespanctrl "github.com/talos-systems/talos/internal/app/machined/pkg/controllers/kubespan" | ||
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1" | ||
"github.com/talos-systems/talos/pkg/resources/config" | ||
"github.com/talos-systems/talos/pkg/resources/kubespan" | ||
) | ||
|
||
type ConfigSuite struct { | ||
KubeSpanSuite | ||
} | ||
|
||
func (suite *ConfigSuite) TestReconcileConfig() { | ||
suite.Require().NoError(suite.runtime.RegisterController(&kubespanctrl.ConfigController{})) | ||
|
||
suite.startRuntime() | ||
|
||
cfg := config.NewMachineConfig(&v1alpha1.Config{ | ||
ConfigVersion: "v1alpha1", | ||
MachineConfig: &v1alpha1.MachineConfig{ | ||
MachineNetwork: &v1alpha1.NetworkConfig{ | ||
NetworkKubeSpan: v1alpha1.NetworkKubeSpan{ | ||
KubeSpanEnabled: true, | ||
}, | ||
}, | ||
}, | ||
ClusterConfig: &v1alpha1.ClusterConfig{ | ||
ClusterID: "8XuV9TZHW08DOk3bVxQjH9ih_TBKjnh-j44tsCLSBzo=", | ||
}, | ||
}) | ||
|
||
suite.Require().NoError(suite.state.Create(suite.ctx, cfg)) | ||
|
||
specMD := resource.NewMetadata(config.NamespaceName, kubespan.ConfigType, kubespan.ConfigID, resource.VersionUndefined) | ||
|
||
suite.Assert().NoError(retry.Constant(3*time.Second, retry.WithUnits(100*time.Millisecond)).Retry( | ||
suite.assertResource( | ||
specMD, | ||
func(res resource.Resource) error { | ||
spec := res.(*kubespan.Config).TypedSpec() | ||
|
||
suite.Assert().True(spec.Enabled) | ||
suite.Assert().Equal("8XuV9TZHW08DOk3bVxQjH9ih_TBKjnh-j44tsCLSBzo=", spec.ClusterID) | ||
|
||
return nil | ||
}, | ||
), | ||
)) | ||
} | ||
|
||
func (suite *ConfigSuite) TestReconcileDisabled() { | ||
suite.Require().NoError(suite.runtime.RegisterController(&kubespanctrl.ConfigController{})) | ||
|
||
suite.startRuntime() | ||
|
||
cfg := config.NewMachineConfig(&v1alpha1.Config{ | ||
ConfigVersion: "v1alpha1", | ||
MachineConfig: &v1alpha1.MachineConfig{}, | ||
ClusterConfig: &v1alpha1.ClusterConfig{}, | ||
}) | ||
|
||
suite.Require().NoError(suite.state.Create(suite.ctx, cfg)) | ||
|
||
specMD := resource.NewMetadata(config.NamespaceName, kubespan.ConfigType, kubespan.ConfigID, resource.VersionUndefined) | ||
|
||
suite.Assert().NoError(retry.Constant(3*time.Second, retry.WithUnits(100*time.Millisecond)).Retry( | ||
suite.assertResource( | ||
specMD, | ||
func(res resource.Resource) error { | ||
spec := res.(*kubespan.Config).TypedSpec() | ||
|
||
suite.Assert().False(spec.Enabled) | ||
|
||
return nil | ||
}, | ||
), | ||
)) | ||
} | ||
|
||
func TestConfigSuite(t *testing.T) { | ||
suite.Run(t, new(ConfigSuite)) | ||
} |
Oops, something went wrong.