From 98917e7547d1cc263a21135c1029798add7864a0 Mon Sep 17 00:00:00 2001 From: Scott Gress Date: Tue, 26 Nov 2024 16:57:32 -0600 Subject: [PATCH] Split up apple and windows profile management cron jobs (#23975) for #22824 This PR splits up the `newMDMProfileManager` function into two functions `newAppleMDMProfileManager` and `newWindowsMDMProfileManager`, the latter containing the code to start the windows-specific profile management job. This allows scheduling and scaling the Apple and Windows profile management jobs separately. Tested locally by checking that the jobs were scheduled at startup, and then triggering the `mdm_apple_profile_manager` and `mdm_windows_profile_manager` schedules via the `/trigger` API; nothing blew up. --- cmd/fleet/cron.go | 25 ++++++++++++++++++++++++- cmd/fleet/cron_test.go | 14 ++++++++++++-- cmd/fleet/serve.go | 13 ++++++++++++- server/fleet/cron_schedules.go | 1 + 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/cmd/fleet/cron.go b/cmd/fleet/cron.go index 90dc3fffa9..75d6f3ba83 100644 --- a/cmd/fleet/cron.go +++ b/cmd/fleet/cron.go @@ -1182,7 +1182,7 @@ func appleMDMDEPSyncerJob( } } -func newMDMProfileManager( +func newAppleMDMProfileManagerSchedule( ctx context.Context, instanceID string, ds fleet.Datastore, @@ -1207,6 +1207,29 @@ func newMDMProfileManager( schedule.WithJob("manage_apple_declarations", func(ctx context.Context) error { return service.ReconcileAppleDeclarations(ctx, ds, commander, logger) }), + ) + + return s, nil +} + +func newWindowsMDMProfileManagerSchedule( + ctx context.Context, + instanceID string, + ds fleet.Datastore, + logger kitlog.Logger, +) (*schedule.Schedule, error) { + const ( + name = string(fleet.CronMDMWindowsProfileManager) + // Note: per a request from #g-product we are running this cron + // every 30 seconds, we should re-evaluate how we handle the + // cron interval as we scale to more hosts. + defaultInterval = 30 * time.Second + ) + + logger = kitlog.With(logger, "cron", name) + s := schedule.New( + ctx, name, instanceID, defaultInterval, ds, ds, + schedule.WithLogger(logger), schedule.WithJob("manage_windows_profiles", func(ctx context.Context) error { return service.ReconcileWindowsProfiles(ctx, ds, logger) }), diff --git a/cmd/fleet/cron_test.go b/cmd/fleet/cron_test.go index 789b38c405..2f051d9f17 100644 --- a/cmd/fleet/cron_test.go +++ b/cmd/fleet/cron_test.go @@ -23,14 +23,24 @@ import ( kitlog "github.com/go-kit/log" ) -func TestNewMDMProfileManagerWithoutConfig(t *testing.T) { +func TestNewAppleMDMProfileManagerWithoutConfig(t *testing.T) { ctx := context.Background() mdmStorage := &mdmmock.MDMAppleStore{} ds := new(mock.Store) cmdr := apple_mdm.NewMDMAppleCommander(mdmStorage, nil) logger := kitlog.NewNopLogger() - sch, err := newMDMProfileManager(ctx, "foo", ds, cmdr, logger) + sch, err := newAppleMDMProfileManagerSchedule(ctx, "foo", ds, cmdr, logger) + require.NotNil(t, sch) + require.NoError(t, err) +} + +func TestNewWindowsMDMProfileManagerWithoutConfig(t *testing.T) { + ctx := context.Background() + ds := new(mock.Store) + logger := kitlog.NewNopLogger() + + sch, err := newWindowsMDMProfileManagerSchedule(ctx, "foo", ds, logger) require.NotNil(t, sch) require.NoError(t, err) } diff --git a/cmd/fleet/serve.go b/cmd/fleet/serve.go index a523a91bac..0b770ca43a 100644 --- a/cmd/fleet/serve.go +++ b/cmd/fleet/serve.go @@ -924,7 +924,7 @@ the way that the Fleet server works. } if err := cronSchedules.StartCronSchedule(func() (fleet.CronSchedule, error) { - return newMDMProfileManager( + return newAppleMDMProfileManagerSchedule( ctx, instanceID, ds, @@ -935,6 +935,17 @@ the way that the Fleet server works. initFatal(err, "failed to register mdm_apple_profile_manager schedule") } + if err := cronSchedules.StartCronSchedule(func() (fleet.CronSchedule, error) { + return newWindowsMDMProfileManagerSchedule( + ctx, + instanceID, + ds, + logger, + ) + }); err != nil { + initFatal(err, "failed to register mdm_windows_profile_manager schedule") + } + if err := cronSchedules.StartCronSchedule(func() (fleet.CronSchedule, error) { return newMDMAPNsPusher( ctx, diff --git a/server/fleet/cron_schedules.go b/server/fleet/cron_schedules.go index 42541a96d1..12fa1ef7ad 100644 --- a/server/fleet/cron_schedules.go +++ b/server/fleet/cron_schedules.go @@ -21,6 +21,7 @@ const ( CronWorkerIntegrations CronScheduleName = "integrations" CronActivitiesStreaming CronScheduleName = "activities_streaming" CronMDMAppleProfileManager CronScheduleName = "mdm_apple_profile_manager" + CronMDMWindowsProfileManager CronScheduleName = "mdm_windows_profile_manager" CronAppleMDMIPhoneIPadRefetcher CronScheduleName = "apple_mdm_iphone_ipad_refetcher" CronAppleMDMAPNsPusher CronScheduleName = "apple_mdm_apns_pusher" CronCalendar CronScheduleName = "calendar"