From f2d27185f7d8904d7ce31ca0eeca1d7d8182f399 Mon Sep 17 00:00:00 2001 From: Francesco Romani Date: Fri, 24 Mar 2023 15:10:36 +0100 Subject: [PATCH] rework ocihooks asset Signed-off-by: Francesco Romani --- pkg/assets/rte/assets.go | 22 +++++-- .../hookconfigrtenotifier.json.template | 0 pkg/assets/rte/{ => ocihooks}/rte-notifier.sh | 0 pkg/manifests/manifests.go | 66 +++++++++++-------- pkg/manifests/manifests_test.go | 5 +- pkg/manifests/rte/rte.go | 5 +- 6 files changed, 64 insertions(+), 34 deletions(-) rename pkg/assets/rte/{ => ocihooks}/hookconfigrtenotifier.json.template (100%) rename pkg/assets/rte/{ => ocihooks}/rte-notifier.sh (100%) diff --git a/pkg/assets/rte/assets.go b/pkg/assets/rte/assets.go index ff4467e7..bb0349f7 100644 --- a/pkg/assets/rte/assets.go +++ b/pkg/assets/rte/assets.go @@ -7,6 +7,10 @@ import ( "github.com/k8stopologyawareschedwg/deployer/pkg/deployer/platform" ) +const ( + NotifierName = "rte-notifier.sh" +) + const ( // OCPVersion4.11 is DEPRECATED and will be removed in the next versions OCPVersion411 = "v4.11" @@ -14,6 +18,7 @@ const ( const ( selinuxPolicyDir = "selinuxpolicy" + ocihooksDir = "ocihooks" ocpVersion410 = "v4.10" // TODO: demote public constant here once we can remove from the public API @@ -21,18 +26,23 @@ const ( ocpVersion413 = "v4.13" ) -//go:embed selinuxinstall.service.template -var SELinuxInstallSystemdServiceTemplate []byte +//go:embed ocihooks +var ocihooks embed.FS -//go:embed hookconfigrtenotifier.json.template -var HookConfigRTENotifier []byte +func GetOCIHookNotifierConfig() ([]byte, error) { + return ocihooks.ReadFile(filepath.Join(ocihooksDir, "hookconfigrtenotifier.json.template")) +} -//go:embed rte-notifier.sh -var NotifierScript []byte +func GetOCIHookNotifier() ([]byte, error) { + return ocihooks.ReadFile(filepath.Join(ocihooksDir, NotifierName)) +} //go:embed selinuxpolicy var selinuxpolicy embed.FS +//go:embed selinuxinstall.service.template +var SELinuxInstallSystemdServiceTemplate []byte + func GetSELinuxPolicy(ver platform.Version) ([]byte, error) { // keep it ordered from most recent supported to the oldest supported for _, cand := range []string{ocpVersion413, ocpVersion412, OCPVersion411, ocpVersion410} { diff --git a/pkg/assets/rte/hookconfigrtenotifier.json.template b/pkg/assets/rte/ocihooks/hookconfigrtenotifier.json.template similarity index 100% rename from pkg/assets/rte/hookconfigrtenotifier.json.template rename to pkg/assets/rte/ocihooks/hookconfigrtenotifier.json.template diff --git a/pkg/assets/rte/rte-notifier.sh b/pkg/assets/rte/ocihooks/rte-notifier.sh similarity index 100% rename from pkg/assets/rte/rte-notifier.sh rename to pkg/assets/rte/ocihooks/rte-notifier.sh diff --git a/pkg/manifests/manifests.go b/pkg/manifests/manifests.go index 5a88c059..9c8dc19d 100644 --- a/pkg/manifests/manifests.go +++ b/pkg/manifests/manifests.go @@ -428,7 +428,11 @@ func DaemonSet(component, subComponent string, plat platform.Platform, namespace return ds, nil } -func MachineConfig(component string, ver platform.Version) (*machineconfigv1.MachineConfig, error) { +type MachineConfigOptions struct { + EnableNotifier bool +} + +func MachineConfig(component string, ver platform.Version, opts MachineConfigOptions) (*machineconfigv1.MachineConfig, error) { if component != ComponentResourceTopologyExporter { return nil, fmt.Errorf("component %q is not an %q component", component, ComponentResourceTopologyExporter) } @@ -443,7 +447,7 @@ func MachineConfig(component string, ver platform.Version) (*machineconfigv1.Mac return nil, fmt.Errorf("unexpected type, got %t", obj) } - ignitionConfig, err := getIgnitionConfig(ver) + ignitionConfig, err := getIgnitionConfig(ver, opts) if err != nil { return nil, err } @@ -452,7 +456,7 @@ func MachineConfig(component string, ver platform.Version) (*machineconfigv1.Mac return mc, nil } -func getIgnitionConfig(ver platform.Version) ([]byte, error) { +func getIgnitionConfig(ver platform.Version, opts MachineConfigOptions) ([]byte, error) { var files []igntypes.File // get SELinux policy @@ -464,29 +468,6 @@ func getIgnitionConfig(ver platform.Version) ([]byte, error) { // load SELinux policy files = addFileToIgnitionConfig(files, selinuxPolicy, 0644, seLinuxRTEPolicyDst) - // load RTE notifier OCI hook config - notifierHookConfigContent, err := getTemplateContent(rteassets.HookConfigRTENotifier, map[string]string{ - templateNotifierBinaryDst: filepath.Join(defaultScriptsDir, "rte-notifier.sh"), - templateNotifierFilePath: filepath.Join(hostNotifierDir, rteNotifierFileName), - }) - if err != nil { - return nil, err - } - files = addFileToIgnitionConfig( - files, - notifierHookConfigContent, - 0644, - filepath.Join(defaultOCIHooksDir, "rte-notifier.json"), - ) - - // load RTE notifier script - files = addFileToIgnitionConfig( - files, - rteassets.NotifierScript, - 0755, - filepath.Join(defaultScriptsDir, "rte-notifier.sh"), - ) - // load systemd service to install SELinux policy systemdServiceContent, err := getTemplateContent( rteassets.SELinuxInstallSystemdServiceTemplate, @@ -498,6 +479,39 @@ func getIgnitionConfig(ver platform.Version) ([]byte, error) { return nil, err } + if opts.EnableNotifier { + // load RTE notifier OCI hook config + hooknotifierConfig, err := rteassets.GetOCIHookNotifierConfig() + if err != nil { + return nil, err + } + notifierHookConfigContent, err := getTemplateContent(hooknotifierConfig, map[string]string{ + templateNotifierBinaryDst: filepath.Join(defaultScriptsDir, rteassets.NotifierName), + templateNotifierFilePath: filepath.Join(hostNotifierDir, rteNotifierFileName), + }) + if err != nil { + return nil, err + } + files = addFileToIgnitionConfig( + files, + notifierHookConfigContent, + 0644, + filepath.Join(defaultOCIHooksDir, "rte-notifier.json"), + ) + + notifierScript, err := rteassets.GetOCIHookNotifier() + if err != nil { + return nil, err + } + // load RTE notifier script + files = addFileToIgnitionConfig( + files, + notifierScript, + 0755, + filepath.Join(defaultScriptsDir, rteassets.NotifierName), + ) + } + ignitionConfig := &igntypes.Config{ Ignition: igntypes.Ignition{ Version: defaultIgnitionVersion, diff --git a/pkg/manifests/manifests_test.go b/pkg/manifests/manifests_test.go index 58e750bb..0705342b 100644 --- a/pkg/manifests/manifests_test.go +++ b/pkg/manifests/manifests_test.go @@ -591,7 +591,10 @@ func TestMachineConfig(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - mc, err := MachineConfig(ComponentResourceTopologyExporter, platform.Version(tc.platformVersion)) + mcOpts := MachineConfigOptions{ + EnableNotifier: true, + } + mc, err := MachineConfig(ComponentResourceTopologyExporter, platform.Version(tc.platformVersion), mcOpts) if err != nil { t.Fatalf("unexpected error: %v", err) } diff --git a/pkg/manifests/rte/rte.go b/pkg/manifests/rte/rte.go index 30ca176c..1eedc4bb 100644 --- a/pkg/manifests/rte/rte.go +++ b/pkg/manifests/rte/rte.go @@ -271,7 +271,10 @@ func GetManifests(plat platform.Platform, version platform.Version, namespace st mf := New(plat) if plat == platform.OpenShift { - mf.MachineConfig, err = manifests.MachineConfig(manifests.ComponentResourceTopologyExporter, version) + mcOpts := manifests.MachineConfigOptions{ + EnableNotifier: true, + } + mf.MachineConfig, err = manifests.MachineConfig(manifests.ComponentResourceTopologyExporter, version, mcOpts) if err != nil { return mf, err }