func serverCommand()

in cmd/proxy/main.go [79:192]


func serverCommand(ctx context.Context, args *ServerCmd) (err error) {
	l := zerolog.Ctx(ctx)

	store.PrefetchWorkers = args.PrefetchWorkers

	_, httpsPort, err := net.SplitHostPort(args.HttpsAddr)
	if err != nil {
		return err
	}

	clientset, err := k8s.NewKubernetesInterface(pcontext.KubeConfigPath, pcontext.NodeName)
	if err != nil {
		return err
	}

	ctx, err = events.WithContext(ctx, clientset)
	if err != nil {
		return err
	}
	eventsRecorder := events.FromContext(ctx)
	defer func() {
		if err != nil {
			eventsRecorder.Failed()
		}
	}()
	eventsRecorder.Initializing()

	r, err := routing.NewRouter(ctx, clientset, args.RouterAddr, httpsPort)
	if err != nil {
		return err
	}

	err = addMirrorConfiguration(ctx, args)
	if err != nil {
		return err
	}

	containerdStore, err := containerd.NewDefaultStore(args.Hosts)
	if err != nil {
		return err
	}
	err = containerdStore.Verify(ctx)
	if err != nil {
		return err
	}

	filesStore, err := store.NewFilesStore(ctx, r, store.DefaultFileCachePath)
	if err != nil {
		return err
	}

	g, ctx := errgroup.WithContext(ctx)

	g.Go(func() error {
		provider.Provide(ctx, r, containerdStore, filesStore.Subscribe())
		return nil
	})

	handler, err := handlers.Handler(ctx, r, containerdStore, filesStore)
	if err != nil {
		return err
	}

	httpsSrv := &http.Server{
		Addr:      args.HttpsAddr,
		Handler:   handler,
		TLSConfig: r.Net().DefaultTLSConfig(),
	}
	g.Go(func() error {
		if err := httpsSrv.ListenAndServeTLS("", ""); err != nil && !errors.Is(err, http.ErrServerClosed) {
			return err
		}
		return nil
	})
	g.Go(func() error {
		<-ctx.Done()
		shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
		defer cancel()
		return httpsSrv.Shutdown(shutdownCtx)
	})

	httpSrv := &http.Server{
		Addr:    args.HttpAddr,
		Handler: handler,
	}
	g.Go(func() error {
		if err := httpSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
			return err
		}
		return nil
	})
	g.Go(func() error {
		<-ctx.Done()
		shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
		defer cancel()
		return httpSrv.Shutdown(shutdownCtx)
	})

	g.Go(func() error {
		http.Handle("/metrics/prometheus", promhttp.Handler())
		if err = http.ListenAndServe(args.PromAddr, nil); err != nil && !errors.Is(err, http.ErrServerClosed) {
			return err
		}
		return nil
	})

	l.Info().Str("https", args.HttpsAddr).Str("http", args.HttpAddr).Str("router", args.RouterAddr).Str("prom", args.PromAddr).Msg("server start")
	err = g.Wait()
	if err != nil {
		return err
	}

	return nil
}