-
Notifications
You must be signed in to change notification settings - Fork 601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: udevd rules controller #7164
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
package cri_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -84,29 +85,6 @@ func (suite *CRISeccompProfileSuite) TestReconcileSeccompProfile() { | |
}) | ||
} | ||
|
||
suite.AssertWithin(1*time.Second, 100*time.Millisecond, func() error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was already handled in the test case above |
||
seccompProfile, err := ctest.Get[*criseccompresource.SeccompProfile]( | ||
suite, | ||
criseccompresource.NewSeccompProfile("audit.json").Metadata(), | ||
) | ||
if err != nil { | ||
if state.IsNotFoundError(err) { | ||
return retry.ExpectedError(err) | ||
} | ||
|
||
return err | ||
} | ||
|
||
spec := seccompProfile.TypedSpec() | ||
|
||
suite.Assert().Equal("audit.json", spec.Name) | ||
suite.Assert().Equal(map[string]interface{}{ | ||
"defaultAction": "SCMP_ACT_LOG", | ||
}, spec.Value) | ||
|
||
return nil | ||
}) | ||
|
||
// test deletion | ||
cfg = config.NewMachineConfig(&v1alpha1.Config{ | ||
MachineConfig: &v1alpha1.MachineConfig{ | ||
|
@@ -123,24 +101,23 @@ func (suite *CRISeccompProfileSuite) TestReconcileSeccompProfile() { | |
}, | ||
}) | ||
|
||
ctest.UpdateWithConflicts(suite, cfg, func(mc *config.MachineConfig) error { | ||
return nil | ||
}) | ||
cfg.Metadata().SetVersion(cfg.Metadata().Version().Next()) | ||
suite.Require().NoError(suite.State().Update(suite.Ctx(), cfg)) | ||
|
||
suite.AssertWithin(1*time.Second, 100*time.Millisecond, func() error { | ||
_, err := ctest.Get[*criseccompresource.SeccompProfile]( | ||
suite, | ||
criseccompresource.NewSeccompProfile("deny.json").Metadata(), | ||
) | ||
if err != nil { | ||
if !state.IsNotFoundError(err) { | ||
return err | ||
if state.IsNotFoundError(err) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this test was always broken 😅 |
||
return nil | ||
} | ||
|
||
return err | ||
} | ||
|
||
return nil | ||
return retry.ExpectedError(fmt.Errorf("seccomp profile with id deny.json should not exist")) | ||
}) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// 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 files | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/cosi-project/runtime/pkg/controller" | ||
"github.com/cosi-project/runtime/pkg/resource" | ||
"github.com/cosi-project/runtime/pkg/safe" | ||
"github.com/cosi-project/runtime/pkg/state" | ||
"github.com/martinlindhe/base36" | ||
"github.com/siderolabs/go-pointer" | ||
"go.uber.org/zap" | ||
|
||
"github.com/siderolabs/talos/pkg/machinery/resources/config" | ||
"github.com/siderolabs/talos/pkg/machinery/resources/files" | ||
) | ||
|
||
// UdevRuleController is a controller that generates udev rules. | ||
type UdevRuleController struct{} | ||
|
||
// Name implements controller.Controller interface. | ||
func (ctrl *UdevRuleController) Name() string { | ||
return "files.UdevRuleController" | ||
} | ||
|
||
// Inputs implements controller.Controller interface. | ||
func (ctrl *UdevRuleController) Inputs() []controller.Input { | ||
return []controller.Input{ | ||
{ | ||
Namespace: config.NamespaceName, | ||
Type: config.MachineConfigType, | ||
ID: pointer.To(config.V1Alpha1ID), | ||
Kind: controller.InputWeak, | ||
}, | ||
} | ||
} | ||
|
||
// Outputs implements controller.Controller interface. | ||
func (ctrl *UdevRuleController) Outputs() []controller.Output { | ||
return []controller.Output{ | ||
{ | ||
Type: files.UdevRuleType, | ||
Kind: controller.OutputExclusive, | ||
}, | ||
} | ||
} | ||
|
||
// Run implements controller.Controller interface. | ||
// | ||
// nolint:gocyclo | ||
func (ctrl *UdevRuleController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-r.EventCh(): | ||
} | ||
|
||
cfg, err := safe.ReaderGet[*config.MachineConfig](ctx, r, resource.NewMetadata(config.NamespaceName, config.MachineConfigType, config.V1Alpha1ID, resource.VersionUndefined)) | ||
if err != nil { | ||
if state.IsNotFoundError(err) { | ||
continue | ||
} | ||
|
||
return fmt.Errorf("error getting config: %w", err) | ||
} | ||
|
||
touchedIDs := make(map[string]struct{}, len(cfg.Config().Machine().Udev().Rules())) | ||
|
||
for _, rule := range cfg.Config().Machine().Udev().Rules() { | ||
ruleID := ctrl.generateRuleHash(rule) | ||
|
||
if err = safe.WriterModify(ctx, r, files.NewUdevRule(ruleID), func(udevRule *files.UdevRule) error { | ||
udevRule.TypedSpec().Rule = rule | ||
|
||
return nil | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
touchedIDs[ruleID] = struct{}{} | ||
} | ||
|
||
// list keys for cleanup | ||
list, err := safe.ReaderList[*files.UdevRule](ctx, r, resource.NewMetadata(files.NamespaceName, files.UdevRuleType, "", resource.VersionUndefined)) | ||
if err != nil { | ||
return fmt.Errorf("error listing udev rules: %w", err) | ||
} | ||
|
||
for iter := safe.IteratorFromList(list); iter.Next(); { | ||
rule := iter.Value() | ||
|
||
if _, ok := touchedIDs[rule.Metadata().ID()]; !ok { | ||
if err := r.Destroy(ctx, rule.Metadata()); err != nil { | ||
return fmt.Errorf("error deleting udev rule %s: %w", rule.Metadata().ID(), err) | ||
} | ||
} | ||
} | ||
|
||
r.ResetRestartBackoff() | ||
} | ||
} | ||
|
||
func (ctrl *UdevRuleController) generateRuleHash(rule string) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is done since we don't take in rule name from machine config |
||
h := sha256.New() | ||
h.Write([]byte(rule)) | ||
|
||
hashBytes := h.Sum(nil) | ||
|
||
b36 := strings.ToLower(base36.EncodeBytes(hashBytes)) | ||
|
||
return b36[:8] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
golangcilint seems really weird now, maybe it;s my cache