github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/runsc/metricserver/metricserver_lifecycle.go (about)

     1  // Copyright 2023 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build go1.1
    16  // +build go1.1
    17  
    18  package metricserver
    19  
    20  import (
    21  	"context"
    22  	"os"
    23  	"time"
    24  
    25  	"github.com/ttpreport/gvisor-ligolo/pkg/log"
    26  	"github.com/ttpreport/gvisor-ligolo/runsc/container"
    27  )
    28  
    29  // verifyLoopInterval is the interval at which we check whether there are any sandboxes we need
    30  // to serve metrics for.
    31  const verifyLoopInterval = 20 * time.Second
    32  
    33  // sandboxData contains additional per-sandbox data.
    34  type sandboxData struct{}
    35  
    36  // load loads additional per-sandbox data.
    37  func (s *sandboxData) load(*servedSandbox) error {
    38  	return nil
    39  }
    40  
    41  // serverData contains additional server-wide data.
    42  type serverData struct{}
    43  
    44  // verify is one iteration of verifyLoop.
    45  // It runs in a loop in the background which checks all sandboxes for liveness, tries to load
    46  // their metadata if that hasn't been loaded yet, and tries to pick up new sandboxes that
    47  // failed to register for whatever reason.
    48  func (m *metricServer) verify(ctx context.Context) {
    49  	_, err := container.ListSandboxes(m.rootDir)
    50  	m.mu.Lock()
    51  	defer m.mu.Unlock()
    52  	if err != nil {
    53  		if !m.allowUnknownRoot {
    54  			log.Warningf("Cannot list sandboxes in root directory %s, it has likely gone away: %v. Server shutting down.", m.rootDir, err)
    55  			m.shutdownLocked(ctx)
    56  		}
    57  		return
    58  	}
    59  	m.refreshSandboxesLocked()
    60  }
    61  
    62  // startVerifyLoop runs in the background and periodically calls verify.
    63  func (m *metricServer) startVerifyLoop(ctx context.Context) error {
    64  	go func() {
    65  		ticker := time.NewTicker(verifyLoopInterval)
    66  		defer ticker.Stop()
    67  		for ctx.Err() == nil {
    68  			select {
    69  			case <-ctx.Done():
    70  				return
    71  			case <-m.shutdownCh:
    72  				log.Infof("Received interrupt signal, shutting down server.")
    73  				m.mu.Lock()
    74  				m.shutdownLocked(ctx)
    75  				m.mu.Unlock()
    76  				return
    77  			case <-ticker.C:
    78  				m.verify(ctx)
    79  			}
    80  		}
    81  	}()
    82  	return nil
    83  }
    84  
    85  // shutdownLocked shuts down the server. It assumes mu is held.
    86  func (m *metricServer) shutdownLocked(ctx context.Context) {
    87  	log.Infof("Server shutting down.")
    88  	m.shuttingDown = true
    89  	if m.udsPath != "" {
    90  		if err := os.Remove(m.udsPath); err != nil {
    91  			log.Warningf("Cannot remove UDS at %s: %v", m.udsPath, err)
    92  		} else {
    93  			m.udsPath = ""
    94  		}
    95  	}
    96  	if m.pidFile != "" {
    97  		if err := os.Remove(m.pidFile); err != nil {
    98  			log.Warningf("Cannot remove PID file at %s: %v", m.pidFile, err)
    99  		} else {
   100  			m.pidFile = ""
   101  		}
   102  	}
   103  	m.srv.Shutdown(ctx)
   104  }