-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement inline manifests in the machine configuration
Inline manifests work exactly same way as extra manifests, but the manifest itself can be stored in the config body. Example config patch: ``` --config-patch '[{"op": "replace", "path": "/cluster/inlineManifests", "value": [{"name": "foo", "contents": "apiVersion: v1\nkind: Namespace\nmetadata:\n name: ci\n"}]}]' ``` Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
- Loading branch information
Showing
15 changed files
with
508 additions
and
19 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
153 changes: 153 additions & 0 deletions
153
internal/app/machined/pkg/controllers/k8s/extra_manifest_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,153 @@ | ||
// 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 k8s_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"reflect" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/suite" | ||
"github.com/talos-systems/go-retry/retry" | ||
"github.com/talos-systems/os-runtime/pkg/controller/runtime" | ||
"github.com/talos-systems/os-runtime/pkg/resource" | ||
"github.com/talos-systems/os-runtime/pkg/state" | ||
"github.com/talos-systems/os-runtime/pkg/state/impl/inmem" | ||
"github.com/talos-systems/os-runtime/pkg/state/impl/namespaced" | ||
|
||
k8sctrl "github.com/talos-systems/talos/internal/app/machined/pkg/controllers/k8s" | ||
"github.com/talos-systems/talos/pkg/resources/config" | ||
"github.com/talos-systems/talos/pkg/resources/k8s" | ||
"github.com/talos-systems/talos/pkg/resources/v1alpha1" | ||
) | ||
|
||
type ExtraManifestSuite struct { | ||
suite.Suite | ||
|
||
state state.State | ||
|
||
runtime *runtime.Runtime | ||
wg sync.WaitGroup | ||
|
||
ctx context.Context | ||
ctxCancel context.CancelFunc | ||
} | ||
|
||
func (suite *ExtraManifestSuite) SetupTest() { | ||
suite.ctx, suite.ctxCancel = context.WithTimeout(context.Background(), 3*time.Minute) | ||
|
||
suite.state = state.WrapCore(namespaced.NewState(inmem.Build)) | ||
|
||
var err error | ||
|
||
logger := log.New(log.Writer(), "controller-runtime: ", log.Flags()) | ||
|
||
suite.runtime, err = runtime.NewRuntime(suite.state, logger) | ||
suite.Require().NoError(err) | ||
|
||
suite.Require().NoError(suite.runtime.RegisterController(&k8sctrl.ExtraManifestController{})) | ||
|
||
suite.startRuntime() | ||
} | ||
|
||
func (suite *ExtraManifestSuite) startRuntime() { | ||
suite.wg.Add(1) | ||
|
||
go func() { | ||
defer suite.wg.Done() | ||
|
||
suite.Assert().NoError(suite.runtime.Run(suite.ctx)) | ||
}() | ||
} | ||
|
||
//nolint:dupl | ||
func (suite *ExtraManifestSuite) assertExtraManifests(manifests []string) error { | ||
resources, err := suite.state.List(suite.ctx, resource.NewMetadata(k8s.ControlPlaneNamespaceName, k8s.ManifestType, "", resource.VersionUndefined)) | ||
if err != nil { | ||
return retry.UnexpectedError(err) | ||
} | ||
|
||
ids := make([]string, 0, len(resources.Items)) | ||
|
||
for _, res := range resources.Items { | ||
ids = append(ids, res.Metadata().ID()) | ||
} | ||
|
||
if !reflect.DeepEqual(manifests, ids) { | ||
return retry.ExpectedError(fmt.Errorf("expected %q, got %q", manifests, ids)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (suite *ExtraManifestSuite) TestReconcileInlineManifests() { | ||
configExtraManifests := config.NewK8sExtraManifests() | ||
configExtraManifests.SetExtraManifests(config.K8sExtraManifestsSpec{ | ||
ExtraManifests: []config.ExtraManifest{ | ||
{ | ||
Name: "namespaces", | ||
Priority: "99", | ||
InlineManifest: strings.TrimSpace(` | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: ci | ||
--- | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: build | ||
`), | ||
}, | ||
}, | ||
}) | ||
|
||
serviceNetworkd := v1alpha1.NewService("networkd") | ||
serviceNetworkd.SetRunning(true) | ||
serviceNetworkd.SetHealthy(true) | ||
|
||
suite.Require().NoError(suite.state.Create(suite.ctx, configExtraManifests)) | ||
suite.Require().NoError(suite.state.Create(suite.ctx, serviceNetworkd)) | ||
|
||
suite.Assert().NoError(retry.Constant(10*time.Second, retry.WithUnits(100*time.Millisecond)).Retry( | ||
func() error { | ||
return suite.assertExtraManifests( | ||
[]string{ | ||
"99-namespaces", | ||
}, | ||
) | ||
}, | ||
)) | ||
|
||
r, err := suite.state.Get(suite.ctx, resource.NewMetadata(k8s.ControlPlaneNamespaceName, k8s.ManifestType, "99-namespaces", resource.VersionUndefined)) | ||
suite.Require().NoError(err) | ||
|
||
manifest := r.(*k8s.Manifest) //nolint:errcheck,forcetypeassert | ||
|
||
suite.Assert().Len(manifest.Objects(), 2) | ||
suite.Assert().Equal("ci", manifest.Objects()[0].GetName()) | ||
suite.Assert().Equal("build", manifest.Objects()[1].GetName()) | ||
} | ||
|
||
func (suite *ExtraManifestSuite) TearDownTest() { | ||
suite.T().Log("tear down") | ||
|
||
suite.ctxCancel() | ||
|
||
suite.wg.Wait() | ||
|
||
// trigger updates in resources to stop watch loops | ||
suite.Assert().NoError(suite.state.Create(context.Background(), v1alpha1.NewService("foo"))) | ||
suite.Assert().NoError(suite.state.Create(context.Background(), config.NewK8sManifests())) | ||
} | ||
|
||
func TestExtraManifestSuite(t *testing.T) { | ||
suite.Run(t, new(ExtraManifestSuite)) | ||
} |
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
15 changes: 15 additions & 0 deletions
15
pkg/machinery/config/types/v1alpha1/v1alpha1_inlinemanifest.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,15 @@ | ||
// 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 | ||
|
||
// Name implements the config.InlineManifest interface. | ||
func (m ClusterInlineManifest) Name() string { | ||
return m.InlineManifestName | ||
} | ||
|
||
// Contents implements the config.InlineManifest interface. | ||
func (m ClusterInlineManifest) Contents() string { | ||
return m.InlineManifestContents | ||
} |
Oops, something went wrong.