github.com/cilium/cilium@v1.16.2/clustermesh-apiserver/kvstoremesh/api.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package kvstoremesh
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/cilium/hive/cell"
    10  	"github.com/go-openapi/loads"
    11  	"github.com/go-openapi/swag"
    12  	"github.com/spf13/pflag"
    13  
    14  	"github.com/cilium/cilium/api/v1/kvstoremesh/server"
    15  )
    16  
    17  // DefaultAPIServeAddr is the default address the KVStoreMesh API is served on.
    18  const DefaultAPIServeAddr = "localhost:9889"
    19  
    20  type apiServerConfig struct {
    21  	APIServeAddr string
    22  }
    23  
    24  func (def apiServerConfig) Flags(flags *pflag.FlagSet) {
    25  	flags.String("api-serve-addr", def.APIServeAddr, "Address to serve the KVStoreMesh API")
    26  }
    27  
    28  var APIServerCell = cell.Module(
    29  	"kvstoremesh-api-server",
    30  	"KVStoreMesh API Server",
    31  
    32  	server.Cell,
    33  
    34  	cell.Config(apiServerConfig{APIServeAddr: DefaultAPIServeAddr}),
    35  	cell.Provide(apiServerSpec),
    36  	cell.Invoke(configureAPIServer),
    37  )
    38  
    39  // Reduced version of server.Spec, which doesn't allow to administratively disable
    40  // APIs, as overkill in this context, and registering an unnecessary flag.
    41  func apiServerSpec() (*server.Spec, error) {
    42  	swaggerSpec, err := loads.Analyzed(server.SwaggerJSON, "")
    43  	if err != nil {
    44  		return nil, fmt.Errorf("failed to load swagger spec: %w", err)
    45  	}
    46  	return &server.Spec{Document: swaggerSpec}, nil
    47  }
    48  
    49  func configureAPIServer(s *server.Server, cfg apiServerConfig) error {
    50  	host, port, err := swag.SplitHostPort(cfg.APIServeAddr)
    51  	if err != nil {
    52  		return fmt.Errorf("failed to configure API Server: %w", err)
    53  	}
    54  
    55  	s.EnabledListeners = []string{"http"}
    56  	s.Host = host
    57  	s.Port = port
    58  	return nil
    59  }