github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/k8s/ip.go (about)

     1  package k8s
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  
     7  	"github.com/tilt-dev/clusterid"
     8  	"github.com/tilt-dev/tilt/pkg/logger"
     9  )
    10  
    11  // Some K8s environments expose a single IP for the whole cluster.
    12  type NodeIP string
    13  
    14  type nodeIPAsync struct {
    15  	mkClient MinikubeClient
    16  	env      clusterid.Product
    17  	once     sync.Once
    18  	nodeIP   NodeIP
    19  }
    20  
    21  func newNodeIPAsync(env clusterid.Product, mkClient MinikubeClient) *nodeIPAsync {
    22  	return &nodeIPAsync{
    23  		env:      env,
    24  		mkClient: mkClient,
    25  	}
    26  }
    27  
    28  func (a *nodeIPAsync) detectNodeIP(ctx context.Context) NodeIP {
    29  	if a.env != clusterid.ProductMinikube {
    30  		return ""
    31  	}
    32  	nodeIP, err := a.mkClient.NodeIP(ctx)
    33  	if err != nil {
    34  		logger.Get(ctx).Warnf("%s", err.Error())
    35  	}
    36  	return nodeIP
    37  }
    38  
    39  func (a *nodeIPAsync) NodeIP(ctx context.Context) NodeIP {
    40  	a.once.Do(func() {
    41  		a.nodeIP = a.detectNodeIP(ctx)
    42  	})
    43  	return a.nodeIP
    44  }
    45  
    46  func (c K8sClient) NodeIP(ctx context.Context) NodeIP {
    47  	return c.nodeIPAsync.NodeIP(ctx)
    48  }