-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #4694 User services run alongside with Talos system services. Every user service container root filesystem should be already present in the Talos root filesystem. Signed-off-by: Andrey Smirnov <[email protected]>
- Loading branch information
Showing
38 changed files
with
1,156 additions
and
45 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
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
131 changes: 131 additions & 0 deletions
131
internal/app/machined/pkg/controllers/runtime/extension_service.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,131 @@ | ||
// 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 runtime | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/cosi-project/runtime/pkg/controller" | ||
"go.uber.org/zap" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/talos-systems/talos/internal/app/machined/pkg/system" | ||
"github.com/talos-systems/talos/internal/app/machined/pkg/system/services" | ||
extservices "github.com/talos-systems/talos/pkg/machinery/extensions/services" | ||
) | ||
|
||
// ServiceManager is the interface to the v1alpha1 services subsystems. | ||
type ServiceManager interface { | ||
Load(services ...system.Service) []string | ||
Start(serviceIDs ...string) error | ||
} | ||
|
||
// ExtensionServiceController creates extension services based on the extension service configuration found in the rootfs. | ||
type ExtensionServiceController struct { | ||
V1Alpha1Services ServiceManager | ||
ConfigPath string | ||
} | ||
|
||
// Name implements controller.Controller interface. | ||
func (ctrl *ExtensionServiceController) Name() string { | ||
return "runtime.ExtensionServiceController" | ||
} | ||
|
||
// Inputs implements controller.Controller interface. | ||
func (ctrl *ExtensionServiceController) Inputs() []controller.Input { | ||
return nil | ||
} | ||
|
||
// Outputs implements controller.Controller interface. | ||
func (ctrl *ExtensionServiceController) Outputs() []controller.Output { | ||
return nil | ||
} | ||
|
||
// Run implements controller.Controller interface. | ||
// | ||
//nolint:gocyclo | ||
func (ctrl *ExtensionServiceController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-r.EventCh(): | ||
} | ||
|
||
// controller runs only once, as services are static | ||
serviceFiles, err := os.ReadDir(ctrl.ConfigPath) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
// directory not present, skip completely | ||
logger.Debug("extension service directory is not found") | ||
|
||
return nil | ||
} | ||
|
||
return err | ||
} | ||
|
||
extServices := map[string]struct{}{} | ||
|
||
for _, serviceFile := range serviceFiles { | ||
if filepath.Ext(serviceFile.Name()) != ".yaml" { | ||
logger.Debug("skipping config file", zap.String("filename", serviceFile.Name())) | ||
|
||
continue | ||
} | ||
|
||
spec, err := ctrl.loadSpec(filepath.Join(ctrl.ConfigPath, serviceFile.Name())) | ||
if err != nil { | ||
logger.Error("error loading extension service spec", zap.String("filename", serviceFile.Name()), zap.Error(err)) | ||
|
||
continue | ||
} | ||
|
||
if err = spec.Validate(); err != nil { | ||
logger.Error("error validating extension service spec", zap.String("filename", serviceFile.Name()), zap.Error(err)) | ||
|
||
continue | ||
} | ||
|
||
if _, exists := extServices[spec.Name]; exists { | ||
logger.Error("duplicate service spec", zap.String("filename", serviceFile.Name()), zap.String("name", spec.Name)) | ||
|
||
continue | ||
} | ||
|
||
extServices[spec.Name] = struct{}{} | ||
|
||
svc := &services.Extension{ | ||
Spec: spec, | ||
} | ||
|
||
ctrl.V1Alpha1Services.Load(svc) | ||
|
||
if err = ctrl.V1Alpha1Services.Start(svc.ID(nil)); err != nil { | ||
return fmt.Errorf("error starting %q service: %w", spec.Name, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (ctrl *ExtensionServiceController) loadSpec(path string) (*extservices.Spec, error) { | ||
var spec extservices.Spec | ||
|
||
f, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer f.Close() //nolint:errcheck | ||
|
||
if err = yaml.NewDecoder(f).Decode(&spec); err != nil { | ||
return nil, fmt.Errorf("error unmarshalling extension service config: %w", err) | ||
} | ||
|
||
return &spec, nil | ||
} |
103 changes: 103 additions & 0 deletions
103
internal/app/machined/pkg/controllers/runtime/extension_service_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,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 runtime_test | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"sort" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/suite" | ||
"github.com/talos-systems/go-retry/retry" | ||
|
||
runtimecontrollers "github.com/talos-systems/talos/internal/app/machined/pkg/controllers/runtime" | ||
"github.com/talos-systems/talos/internal/app/machined/pkg/system" | ||
"github.com/talos-systems/talos/internal/app/machined/pkg/system/services" | ||
) | ||
|
||
type ExtensionServiceSuite struct { | ||
RuntimeSuite | ||
} | ||
|
||
type serviceMock struct { | ||
mu sync.Mutex | ||
services map[string]system.Service | ||
} | ||
|
||
func (mock *serviceMock) Load(services ...system.Service) []string { | ||
mock.mu.Lock() | ||
defer mock.mu.Unlock() | ||
|
||
ids := []string{} | ||
|
||
for _, svc := range services { | ||
mock.services[svc.ID(nil)] = svc | ||
ids = append(ids, svc.ID(nil)) | ||
} | ||
|
||
return ids | ||
} | ||
|
||
func (mock *serviceMock) Start(serviceIDs ...string) error { | ||
return nil | ||
} | ||
|
||
func (mock *serviceMock) getIDs() []string { | ||
mock.mu.Lock() | ||
defer mock.mu.Unlock() | ||
|
||
ids := []string{} | ||
|
||
for id := range mock.services { | ||
ids = append(ids, id) | ||
} | ||
|
||
sort.Strings(ids) | ||
|
||
return ids | ||
} | ||
|
||
func (mock *serviceMock) get(id string) system.Service { | ||
mock.mu.Lock() | ||
defer mock.mu.Unlock() | ||
|
||
return mock.services[id] | ||
} | ||
|
||
func (suite *ExtensionServiceSuite) TestReconcile() { | ||
svcMock := &serviceMock{ | ||
services: map[string]system.Service{}, | ||
} | ||
|
||
suite.Require().NoError(suite.runtime.RegisterController(&runtimecontrollers.ExtensionServiceController{ | ||
V1Alpha1Services: svcMock, | ||
ConfigPath: "testdata/extservices/", | ||
})) | ||
|
||
suite.startRuntime() | ||
|
||
suite.Assert().NoError(retry.Constant(10*time.Second, retry.WithUnits(100*time.Millisecond)).Retry( | ||
func() error { | ||
ids := svcMock.getIDs() | ||
|
||
if !reflect.DeepEqual(ids, []string{"ext-hello-world"}) { | ||
return retry.ExpectedError(fmt.Errorf("services registered: %q", ids)) | ||
} | ||
|
||
return nil | ||
}, | ||
)) | ||
|
||
helloSvc := svcMock.get("ext-hello-world") | ||
suite.Require().IsType(&services.Extension{}, helloSvc) | ||
|
||
suite.Assert().Equal("./hello-world", helloSvc.(*services.Extension).Spec.Container.Entrypoint) | ||
} | ||
|
||
func TestExtensionServiceSuite(t *testing.T) { | ||
suite.Run(t, new(ExtensionServiceSuite)) | ||
} |
Empty file.
10 changes: 10 additions & 0 deletions
10
internal/app/machined/pkg/controllers/runtime/testdata/extservices/hello.yaml
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,10 @@ | ||
name: hello-world | ||
container: | ||
entrypoint: ./hello-world | ||
args: | ||
- --msg | ||
- Talos Linux Extension Service | ||
depends: | ||
- network: | ||
- addresses | ||
restart: always |
9 changes: 9 additions & 0 deletions
9
internal/app/machined/pkg/controllers/runtime/testdata/extservices/invalid.yaml
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,9 @@ | ||
name: invalid | ||
container: | ||
entrypoint: ./hello-world | ||
args: | ||
- --msg | ||
- Talos Linux Extension Service | ||
depends: | ||
- nothing: true | ||
restart: random |
9 changes: 9 additions & 0 deletions
9
internal/app/machined/pkg/controllers/runtime/testdata/extservices/zduplicate.yaml
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,9 @@ | ||
name: hello-world | ||
container: | ||
entrypoint: ./duplicate | ||
args: | ||
- should not get registered | ||
depends: | ||
- network: | ||
- addresses | ||
restart: always |
Oops, something went wrong.