Skip to content

Commit

Permalink
rte ocihook: add listing support
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Romani <[email protected]>
(cherry picked from commit 7a512cf)
  • Loading branch information
ffromani committed Mar 24, 2023
1 parent 10a9ea6 commit e0200ff
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 18 deletions.
19 changes: 18 additions & 1 deletion pkg/assets/rte/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
)

const (
NotifierName = "rte-notifier.sh"
NotifierName = "rte-notifier.sh"
ListCreateName = "rte-create.sh"
ListDeleteName = "rte-delete.sh"
)

const (
Expand All @@ -33,9 +35,24 @@ func GetOCIHookNotifierConfig() ([]byte, error) {
return ocihooks.ReadFile(filepath.Join(ocihooksDir, "hookconfigrtenotifier.json.template"))
}

func GetOCIHookListCreateConfig() ([]byte, error) {
return ocihooks.ReadFile(filepath.Join(ocihooksDir, "hookconfigrtecreate.json.template"))
}

func GetOCIHookListDeleteConfig() ([]byte, error) {
return ocihooks.ReadFile(filepath.Join(ocihooksDir, "hookconfigrtedelete.json.template"))
}

func GetOCIHookNotifier() ([]byte, error) {
return ocihooks.ReadFile(filepath.Join(ocihooksDir, NotifierName))
}
func GetOCIHookListCreate() ([]byte, error) {
return ocihooks.ReadFile(filepath.Join(ocihooksDir, ListCreateName))
}

func GetOCIHookListDelete() ([]byte, error) {
return ocihooks.ReadFile(filepath.Join(ocihooksDir, ListDeleteName))
}

//go:embed selinuxpolicy
var selinuxpolicy embed.FS
Expand Down
11 changes: 11 additions & 0 deletions pkg/assets/rte/ocihooks/hookconfigrtecreate.json.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "1.0.0",
"when": {
"always": true
},
"hook": {
"path": "{{.rteCreateScriptPath}}",
"args": ["rte-create.sh", "{{.rteListingDirPath}}"]
},
"stages": ["poststart"]
}
11 changes: 11 additions & 0 deletions pkg/assets/rte/ocihooks/hookconfigrtedelete.json.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "1.0.0",
"when": {
"always": true
},
"hook": {
"path": "{{.rteDeleteScriptPath}}",
"args": ["rte-delete.sh", "{{.rteListingDirPath}}"]
},
"stages": ["poststop"]
}
9 changes: 9 additions & 0 deletions pkg/assets/rte/ocihooks/rte-create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

JQ="/usr/bin/jq"
LISTING_DIR="${1}"

bundle=$( ${JQ} -r '.bundle' /dev/stdin 2>&1 )
pod_ident=$( ${JQ} '"___" + .annotations["io.kubernetes.pod.namespace"] + "___" + .annotations["io.kubernetes.pod.name"]' < ${bundle}/config.json )

touch "${LISTING_DIR}/${pod_ident}" || :
9 changes: 9 additions & 0 deletions pkg/assets/rte/ocihooks/rte-delete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

JQ="/usr/bin/jq"
LISTING_DIR="${1}"

bundle=$( ${JQ} -r '.bundle' /dev/stdin 2>&1 )
pod_ident=$( ${JQ} '"___" + .annotations["io.kubernetes.pod.namespace"] + "___" + .annotations["io.kubernetes.pod.name"]' < ${bundle}/config.json )

rm "${LISTING_DIR}/${pod_ident}" || :
8 changes: 8 additions & 0 deletions pkg/commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func NewRemoveCommand(commonOpts *CommonOptions) *cobra.Command {
WaitCompletion: opts.waitCompletion,
PullIfNotPresent: commonOpts.PullIfNotPresent,
RTEConfigData: commonOpts.RTEConfigData,
OCIHookNotifier: commonOpts.OCIHookNotifier,
OCIHookListing: commonOpts.OCIHookListing,
})
if err != nil {
// intentionally keep going to remove as much as possible
Expand Down Expand Up @@ -209,6 +211,8 @@ func NewDeployTopologyUpdaterCommand(commonOpts *CommonOptions, opts *DeployOpti
WaitCompletion: opts.waitCompletion,
PullIfNotPresent: commonOpts.PullIfNotPresent,
RTEConfigData: commonOpts.RTEConfigData,
OCIHookNotifier: commonOpts.OCIHookNotifier,
OCIHookListing: commonOpts.OCIHookListing,
})
},
Args: cobra.NoArgs,
Expand Down Expand Up @@ -311,6 +315,8 @@ func NewRemoveTopologyUpdaterCommand(commonOpts *CommonOptions, opts *DeployOpti
WaitCompletion: opts.waitCompletion,
PullIfNotPresent: commonOpts.PullIfNotPresent,
RTEConfigData: commonOpts.RTEConfigData,
OCIHookNotifier: commonOpts.OCIHookNotifier,
OCIHookListing: commonOpts.OCIHookListing,
})
},
Args: cobra.NoArgs,
Expand Down Expand Up @@ -347,6 +353,8 @@ func deployOnCluster(commonOpts *CommonOptions, opts *DeployOptions) error {
WaitCompletion: opts.waitCompletion,
PullIfNotPresent: commonOpts.PullIfNotPresent,
RTEConfigData: commonOpts.RTEConfigData,
OCIHookNotifier: commonOpts.OCIHookNotifier,
OCIHookListing: commonOpts.OCIHookListing,
}); err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/commands/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ func makeUpdaterObjects(commonOpts *CommonOptions) ([]client.Object, string, err
PullIfNotPresent: commonOpts.PullIfNotPresent,
RTEConfigData: commonOpts.RTEConfigData,
}
objs, err := updaters.GetObjects(opts, commonOpts.UpdaterType, namespace)
mcOpts := manifests.MachineConfigOptions{
EnableNotifier: commonOpts.OCIHookNotifier,
EnableListing: commonOpts.OCIHookListing,
}
objs, err := updaters.GetObjects(opts, commonOpts.UpdaterType, namespace, mcOpts)
if err != nil {
return nil, namespace, err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type CommonOptions struct {
RTEConfigData string
PullIfNotPresent bool
UpdaterType string
OCIHookNotifier bool
OCIHookListing bool
rteConfigFile string
plat string
platVer string
Expand Down Expand Up @@ -110,6 +112,8 @@ func InitFlags(flags *pflag.FlagSet, commonOpts *CommonOptions) {
flags.StringVar(&commonOpts.UpdaterType, "updater-type", "RTE", "type of updater to deploy - RTE or NFD")
flags.StringVar(&commonOpts.schedProfileName, "sched-profile-name", DefaultSchedulerProfileName, "inject scheduler profile name.")
flags.DurationVar(&commonOpts.schedResyncPeriod, "sched-resync-period", DefaultSchedulerResyncPeriod, "inject scheduler resync period.")
flags.BoolVar(&commonOpts.OCIHookNotifier, "oci-hook-notifier", true, "toggle support for the notifier OCI hook.")
flags.BoolVar(&commonOpts.OCIHookListing, "oci-hook-listing", false, "toggle support for the listing OCI hook.")
}

func PostSetupOptions(commonOpts *CommonOptions) error {
Expand Down
13 changes: 7 additions & 6 deletions pkg/deployer/updaters/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/k8stopologyawareschedwg/deployer/pkg/deployer"
"github.com/k8stopologyawareschedwg/deployer/pkg/manifests"
nfdmanifests "github.com/k8stopologyawareschedwg/deployer/pkg/manifests/nfd"
rtemanifests "github.com/k8stopologyawareschedwg/deployer/pkg/manifests/rte"
)

func GetObjects(opts Options, updaterType, namespace string) ([]client.Object, error) {
func GetObjects(opts Options, updaterType, namespace string, mcOpts manifests.MachineConfigOptions) ([]client.Object, error) {

if updaterType == RTE {
mf, err := rtemanifests.GetManifests(opts.Platform, opts.PlatformVersion, namespace)
mf, err := rtemanifests.GetManifests(opts.Platform, opts.PlatformVersion, namespace, mcOpts)
if err != nil {
return nil, err
}
Expand All @@ -55,9 +56,9 @@ func GetObjects(opts Options, updaterType, namespace string) ([]client.Object, e
return nil, fmt.Errorf("unsupported updater: %q", updaterType)
}

func getCreatableObjects(opts Options, cli client.Client, log logr.Logger, updaterType, namespace string) ([]deployer.WaitableObject, error) {
func getCreatableObjects(opts Options, cli client.Client, log logr.Logger, updaterType, namespace string, mcOpts manifests.MachineConfigOptions) ([]deployer.WaitableObject, error) {
if updaterType == RTE {
mf, err := rtemanifests.GetManifests(opts.Platform, opts.PlatformVersion, namespace)
mf, err := rtemanifests.GetManifests(opts.Platform, opts.PlatformVersion, namespace, mcOpts)
if err != nil {
return nil, err
}
Expand All @@ -81,9 +82,9 @@ func getCreatableObjects(opts Options, cli client.Client, log logr.Logger, updat
return nil, fmt.Errorf("unsupported updater: %q", updaterType)
}

func getDeletableObjects(opts Options, cli client.Client, log logr.Logger, updaterType, namespace string) ([]deployer.WaitableObject, error) {
func getDeletableObjects(opts Options, cli client.Client, log logr.Logger, updaterType, namespace string, mcOpts manifests.MachineConfigOptions) ([]deployer.WaitableObject, error) {
if updaterType == RTE {
mf, err := rtemanifests.GetManifests(opts.Platform, opts.PlatformVersion, namespace)
mf, err := rtemanifests.GetManifests(opts.Platform, opts.PlatformVersion, namespace, mcOpts)
if err != nil {
return nil, err
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/deployer/updaters/updaters.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type Options struct {
WaitCompletion bool
PullIfNotPresent bool
RTEConfigData string
OCIHookNotifier bool
OCIHookListing bool
}

func Deploy(env *deployer.Environment, updaterType string, opts Options) error {
Expand All @@ -49,7 +51,11 @@ func Deploy(env *deployer.Environment, updaterType string, opts Options) error {
return err
}

objs, err := getCreatableObjects(opts, env.Cli, env.Log, updaterType, namespace)
mcOpts := manifests.MachineConfigOptions{
EnableNotifier: opts.OCIHookNotifier,
EnableListing: opts.OCIHookListing,
}
objs, err := getCreatableObjects(opts, env.Cli, env.Log, updaterType, namespace, mcOpts)
if err != nil {
return err
}
Expand Down Expand Up @@ -85,7 +91,11 @@ func Remove(env *deployer.Environment, updaterType string, opts Options) error {
}
namespace := ns.Name

objs, err := getDeletableObjects(opts, env.Cli, env.Log, updaterType, namespace)
mcOpts := manifests.MachineConfigOptions{
EnableNotifier: opts.OCIHookNotifier,
EnableListing: opts.OCIHookListing,
}
objs, err := getDeletableObjects(opts, env.Cli, env.Log, updaterType, namespace, mcOpts)
if err != nil {
return err
}
Expand Down
65 changes: 65 additions & 0 deletions pkg/manifests/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ const (
templateSELinuxPolicyDst = "selinuxPolicyDst"
templateNotifierBinaryDst = "notifierScriptPath"
templateNotifierFilePath = "notifierFilePath"
templateCreateBinaryDst = "rteCreateScriptPath"
templateDeleteBinaryDst = "rteCreateScriptPath"
templateListingDirPath = "rteListingDirPath"
)

const (
Expand Down Expand Up @@ -430,6 +433,7 @@ func DaemonSet(component, subComponent string, plat platform.Platform, namespace

type MachineConfigOptions struct {
EnableNotifier bool
EnableListing bool
}

func MachineConfig(component string, ver platform.Version, opts MachineConfigOptions) (*machineconfigv1.MachineConfig, error) {
Expand Down Expand Up @@ -512,6 +516,67 @@ func getIgnitionConfig(ver platform.Version, opts MachineConfigOptions) ([]byte,
)
}

if opts.EnableListing {
hookcreateConfig, err := rteassets.GetOCIHookListCreateConfig()
if err != nil {
return nil, err
}
hookcreateConfigContent, err := getTemplateContent(hookcreateConfig, map[string]string{
templateCreateBinaryDst: filepath.Join(defaultScriptsDir, rteassets.ListCreateName),
templateListingDirPath: hostNotifierDir,
})
if err != nil {
return nil, err
}
files = addFileToIgnitionConfig(
files,
hookcreateConfigContent,
0644,
filepath.Join(defaultOCIHooksDir, "rte-create.json"),
)

hookcreateScript, err := rteassets.GetOCIHookListCreate()
if err != nil {
return nil, err
}
files = addFileToIgnitionConfig(
files,
hookcreateScript,
0755,
filepath.Join(defaultScriptsDir, rteassets.ListCreateName),
)

hookdeleteConfig, err := rteassets.GetOCIHookListDeleteConfig()
if err != nil {
return nil, err
}
hookdeleteConfigContent, err := getTemplateContent(hookdeleteConfig, map[string]string{
templateDeleteBinaryDst: filepath.Join(defaultScriptsDir, rteassets.ListDeleteName),
templateListingDirPath: hostNotifierDir,
})
if err != nil {
return nil, err
}
files = addFileToIgnitionConfig(
files,
hookdeleteConfigContent,
0644,
filepath.Join(defaultOCIHooksDir, "rte-delete.json"),
)

hookdeleteScript, err := rteassets.GetOCIHookListDelete()
if err != nil {
return nil, err
}
// load RTE notifier script
files = addFileToIgnitionConfig(
files,
hookdeleteScript,
0755,
filepath.Join(defaultScriptsDir, rteassets.ListDeleteName),
)
}

ignitionConfig := &igntypes.Config{
Ignition: igntypes.Ignition{
Version: defaultIgnitionVersion,
Expand Down
1 change: 1 addition & 0 deletions pkg/manifests/manifests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ func TestMachineConfig(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
mcOpts := MachineConfigOptions{
EnableNotifier: true,
EnableListing: false, // TODO: add tests
}
mc, err := MachineConfig(ComponentResourceTopologyExporter, platform.Version(tc.platformVersion), mcOpts)
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions pkg/manifests/rte/rte.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,11 @@ func New(plat platform.Platform) Manifests {
return mf
}

func GetManifests(plat platform.Platform, version platform.Version, namespace string) (Manifests, error) {
func GetManifests(plat platform.Platform, version platform.Version, namespace string, mcOpts manifests.MachineConfigOptions) (Manifests, error) {
var err error
mf := New(plat)

if plat == platform.OpenShift {
mcOpts := manifests.MachineConfigOptions{
EnableNotifier: true,
}
mf.MachineConfig, err = manifests.MachineConfig(manifests.ComponentResourceTopologyExporter, version, mcOpts)
if err != nil {
return mf, err
Expand Down
19 changes: 16 additions & 3 deletions pkg/manifests/rte/rte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/k8stopologyawareschedwg/deployer/pkg/deployer/platform"
"github.com/k8stopologyawareschedwg/deployer/pkg/manifests"
)

func TestClone(t *testing.T) {
Expand Down Expand Up @@ -54,8 +55,12 @@ func TestClone(t *testing.T) {
},
}

mcOpts := manifests.MachineConfigOptions{
EnableNotifier: true,
EnableListing: true,
}
for _, tc := range testCases {
tc.mf, _ = GetManifests(tc.plat, tc.platVersion, "")
tc.mf, _ = GetManifests(tc.plat, tc.platVersion, "", mcOpts)
cMf := tc.mf.Clone()

if &cMf == &tc.mf {
Expand Down Expand Up @@ -95,8 +100,12 @@ func TestRender(t *testing.T) {
},
}

mcOpts := manifests.MachineConfigOptions{
EnableNotifier: true,
EnableListing: true,
}
for _, tc := range testCases {
tc.mf, _ = GetManifests(tc.plat, tc.platVersion, "")
tc.mf, _ = GetManifests(tc.plat, tc.platVersion, "", mcOpts)
mfBeforeRender := tc.mf.Clone()
uMf, err := tc.mf.Render(RenderOptions{})
if err != nil {
Expand Down Expand Up @@ -132,8 +141,12 @@ func TestGetManifestsOpenShift(t *testing.T) {
platVersion: platform.Version("v4.11"),
},
}
mcOpts := manifests.MachineConfigOptions{
EnableNotifier: true,
EnableListing: true,
}
for _, tc := range testCases {
mf, err := GetManifests(tc.plat, tc.platVersion, "test")
mf, err := GetManifests(tc.plat, tc.platVersion, "test", mcOpts)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
6 changes: 5 additions & 1 deletion test/e2e/positive.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ var _ = ginkgo.Describe("[PositiveFlow] Deployer execution", func() {
ns, err := manifests.Namespace(manifests.ComponentResourceTopologyExporter)
gomega.Expect(err).ToNot(gomega.HaveOccurred())

mf, err := rte.GetManifests(platform.Kubernetes, platform.Version("1.23"), ns.Name)
mcOpts := manifests.MachineConfigOptions{
EnableNotifier: true,
EnableListing: true,
}
mf, err := rte.GetManifests(platform.Kubernetes, platform.Version("1.23"), ns.Name, mcOpts)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
mf, err = mf.Render(rte.RenderOptions{
Namespace: ns.Name,
Expand Down

0 comments on commit e0200ff

Please sign in to comment.