func Run()

in cmd/google_guest_agent/setup/setup.go [187:244]


func Run(ctx context.Context, c Config) error {
	conf, err := fetchRuntimeConfig(ctx, metadata.New())
	if err != nil {
		return fmt.Errorf("failed to get instance ID: %w", err)
	}

	galog.Infof("Running Guest Agent setup with config: %+v, runtime config: %+v", c, conf)

	// Registers the acs event watcher and initializes the acs handler if
	// on-demand plugins are enabled in the configuration file.
	// This is done as early as possible to ensure that the handler is ready
	// to handle to respond to non-plugin configuration requests as they serve as
	// heartbeat for the agent.
	if c.EnableACSWatcher && conf.svcActPresent {
		if err := events.FetchManager().AddWatcher(ctx, watcher.New()); err != nil {
			galog.Fatalf("Failed to add ACS watcher: %v", err)
		}
		handler.Init(c.Version)
		galog.Infof("Registered ACS watcher and handler")
	} else {
		galog.Infof("ACS watcher config enabled: %t, service account is present: %t, skipping ACS watcher and handler initialization. On Demand plugins will not be available.", c.EnableACSWatcher, conf.svcActPresent)

	}

	pm, err := manager.InitPluginManager(ctx, conf.id)
	if err != nil {
		return fmt.Errorf("plugin manager initialization: %w", err)
	}
	galog.Infof("Plugin manager initialized")

	go func() {
		if err := command.Setup(ctx, command.ListenerGuestAgent); err != nil {
			galog.Errorf("Failed to setup command monitor for Guest Agent: %v", err)
		}
	}()

	// If core plugin initialization is skipped just assume instance is ready
	// and run as if core-plugin has already sent ready event.
	if c.SkipCorePlugin {
		galog.Debug("Skipping core plugin initialization")
		coreReady(ctx, c)
		return nil
	}

	if err := install(ctx, pm, c); err != nil {
		return fmt.Errorf("core plugin installation: %w", err)
	}

	events.FetchManager().Subscribe(manager.EventID, events.EventSubscriber{Name: "GuestAgent", Data: c, Callback: handlePluginEvent})

	// Ignore returned [watcher] as it takes care of deregistering itself.
	_, err = manager.InitWatcher(ctx, corePluginName, successStatusCode, pluginStatusRequest)
	if err != nil {
		return fmt.Errorf("init %s watcher: %w", corePluginName, err)
	}

	return nil
}