func main()

in agent/agent.go [551:627]


func main() {

	// Update capabilities of Agent process before starting Envoy
	er := setAgentCapabilities()
	if er != nil {
		// Failed to set Agent's capabilities but continuing bootstrap as capabilities may not be needed by Envoy's dynamic config
		log.Errorf("Error while modifying Agent's capabilities: %v", er)
	}

	er = setupOpenfilesLimit()
	if er != nil {
		log.Errorf("Failed to setup open files limit: %v", er)
	}

	var agentStartTime = time.Now()
	var messageSources messagesources.MessageSources
	var agentConfig config.AgentConfig
	var healthStatus healthcheck.HealthStatus
	var snapshotter stats.Snapshotter

	agentConfig.ParseFlags(os.Args)
	agentConfig.SetDefaults()

	// TODO: Move this logic to envoy_bootstrap.go so we can write unit test for it.
	if agentConfig.EnableRelayModeForXds {
		err := bootstrap.CreateRelayBootstrapYamlFile(agentConfig)
		if err != nil {
			log.Errorf("Failed to create relay bootstrap configuration yaml file:[%s] %v", agentConfig.EnvoyConfigPath, err)
			os.Exit(1)
		}
	} else {
		err := bootstrap.CreateBootstrapYamlFile(agentConfig)
		if err != nil {
			log.Errorf("Failed to create bootstrap configuration yaml file:[%s] %v", agentConfig.EnvoyConfigPath, err)
			os.Exit(1)
		}
	}

	// Setup channels for various agent operations
	messageSources.SetupChannels()

	logging.SetupLogger(&agentConfig)

	if agentConfig.AgentPollEnvoyReadiness {
		if err := pollEnvoyReadiness(agentConfig); err != nil {
			log.Errorf("Polling envoy readiness failed with error: %v\n", err)
			os.Exit(1)
		}

		os.Exit(0)
	}

	setupSignalHandling(agentConfig, &messageSources)
	setupUdsForEnvoyAdmin(agentConfig, &messageSources)

	// Start the configured binary and keep it alive
	go keepCommandAlive(agentConfig, &messageSources)
	defer stopProcesses(agentConfig.StopProcessWaitTime, &messageSources)

	go healthStatus.StartHealthCheck(agentStartTime, agentConfig, &messageSources)
	if agentConfig.EnableStatsSnapshot {
		log.Debug("Enabling stats snapshot...")
		go snapshotter.StartSnapshot(agentConfig)
	}

	// Start the agent http server only if APPNET_AGENT_ADMIN_MODE is set
	if _, exists := os.LookupEnv("APPNET_AGENT_ADMIN_MODE"); exists {
		// TODO: Refactor this - we can have an http server struct that contains the healthStatus and Snapshotter
		// and other resources for the server. Otherwise, this arg list is going to always increase when we add more
		// handlers that needs extra process running alongside the server.
		setupHttpServer(agentConfig, &healthStatus, &snapshotter, &messageSources)
	}

	// Block until we are told its ok to exit
	messageSources.GetAgentExit()
	os.Exit(0)
}