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]>
  • Loading branch information
ffromani committed Mar 24, 2023
1 parent f2d2718 commit 7a512cf
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 19 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 @@ -96,6 +96,8 @@ func NewRemoveCommand(commonOpts *CommonOptions) *cobra.Command {
PullIfNotPresent: commonOpts.PullIfNotPresent,
PFPEnable: commonOpts.UpdaterPFPEnable,
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 @@ -222,6 +224,8 @@ func NewDeployTopologyUpdaterCommand(commonOpts *CommonOptions, opts *DeployOpti
PullIfNotPresent: commonOpts.PullIfNotPresent,
PFPEnable: commonOpts.UpdaterPFPEnable,
RTEConfigData: commonOpts.RTEConfigData,
OCIHookNotifier: commonOpts.OCIHookNotifier,
OCIHookListing: commonOpts.OCIHookListing,
})
},
Args: cobra.NoArgs,
Expand Down Expand Up @@ -332,6 +336,8 @@ func NewRemoveTopologyUpdaterCommand(commonOpts *CommonOptions, opts *DeployOpti
PullIfNotPresent: commonOpts.PullIfNotPresent,
PFPEnable: commonOpts.UpdaterPFPEnable,
RTEConfigData: commonOpts.RTEConfigData,
OCIHookNotifier: commonOpts.OCIHookNotifier,
OCIHookListing: commonOpts.OCIHookListing,
})
},
Args: cobra.NoArgs,
Expand Down Expand Up @@ -371,6 +377,8 @@ func deployOnCluster(commonOpts *CommonOptions, opts *DeployOptions) error {
PullIfNotPresent: commonOpts.PullIfNotPresent,
PFPEnable: commonOpts.UpdaterPFPEnable,
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 @@ -140,7 +140,11 @@ func makeUpdaterObjects(commonOpts *CommonOptions) ([]client.Object, string, err
RTEConfigData: commonOpts.RTEConfigData,
PFPEnable: commonOpts.UpdaterPFPEnable,
}
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 @@ -52,6 +52,8 @@ type CommonOptions struct {
PullIfNotPresent bool
UpdaterType string
UpdaterPFPEnable bool
OCIHookNotifier bool
OCIHookListing bool
rteConfigFile string
plat string
platVer string
Expand Down Expand Up @@ -112,6 +114,8 @@ func InitFlags(flags *pflag.FlagSet, commonOpts *CommonOptions) {
flags.BoolVar(&commonOpts.UpdaterPFPEnable, "updater-pfp-enable", true, "toggle PFP support on the updater side.")
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 @@ -22,14 +22,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 @@ -53,9 +54,9 @@ func GetObjects(opts Options, updaterType, namespace string) ([]client.Object, e
return nil, fmt.Errorf("unsupported updater: %q", updaterType)
}

func getCreatableObjects(env *deployer.Environment, opts Options, updaterType, namespace string) ([]deployer.WaitableObject, error) {
func getCreatableObjects(env *deployer.Environment, opts Options, 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 @@ -79,9 +80,9 @@ func getCreatableObjects(env *deployer.Environment, opts Options, updaterType, n
return nil, fmt.Errorf("unsupported updater: %q", updaterType)
}

func getDeletableObjects(env *deployer.Environment, opts Options, updaterType, namespace string) ([]deployer.WaitableObject, error) {
func getDeletableObjects(env *deployer.Environment, opts Options, 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 @@ -40,6 +40,8 @@ type Options struct {
PullIfNotPresent bool
RTEConfigData string
PFPEnable bool
OCIHookNotifier bool
OCIHookListing bool
}

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

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

objs, err := getDeletableObjects(env, opts, updaterType, namespace)
mcOpts := manifests.MachineConfigOptions{
EnableNotifier: opts.OCIHookNotifier,
EnableListing: opts.OCIHookListing,
}
objs, err := getDeletableObjects(env, opts, 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 @@ -76,6 +76,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 @@ -266,14 +266,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/negative.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ var _ = ginkgo.Describe("[NegativeFlow] Deployer execution with PFP disabled", f
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
Loading

0 comments on commit 7a512cf

Please sign in to comment.