gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/go-control-plane/internal/example/main/main.go (about)

     1  // Copyright 2020 Envoyproxy 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  package main
    15  
    16  import (
    17  	"context"
    18  	"flag"
    19  	"os"
    20  
    21  	"gitee.com/ks-custle/core-gm/go-control-plane/internal/example"
    22  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/cache/v3"
    23  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/server/v3"
    24  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/test/v3"
    25  )
    26  
    27  var (
    28  	l      example.Logger
    29  	port   uint
    30  	nodeID string
    31  )
    32  
    33  func init() {
    34  	l = example.Logger{}
    35  
    36  	flag.BoolVar(&l.Debug, "debug", false, "Enable xDS server debug logging")
    37  
    38  	// The port that this xDS server listens on
    39  	flag.UintVar(&port, "port", 18000, "xDS management server port")
    40  
    41  	// Tell Envoy to use this Node ID
    42  	flag.StringVar(&nodeID, "nodeID", "test-id", "Node ID")
    43  }
    44  
    45  func main() {
    46  	flag.Parse()
    47  
    48  	// Create a cache
    49  	cacheTmp := cache.NewSnapshotCache(false, cache.IDHash{}, l)
    50  
    51  	// Create the snapshot that we'll serve to Envoy
    52  	snapshot := example.GenerateSnapshot()
    53  	if err := snapshot.Consistent(); err != nil {
    54  		l.Errorf("snapshot inconsistency: %+v\n%+v", snapshot, err)
    55  		os.Exit(1)
    56  	}
    57  	l.Debugf("will serve snapshot %+v", snapshot)
    58  
    59  	// Add the snapshot to the cache
    60  	if err := cacheTmp.SetSnapshot(context.Background(), nodeID, snapshot); err != nil {
    61  		l.Errorf("snapshot error %q for %+v", err, snapshot)
    62  		os.Exit(1)
    63  	}
    64  
    65  	// Run the xDS server
    66  	ctx := context.Background()
    67  	cb := &test.Callbacks{Debug: l.Debug}
    68  	srv := server.NewServer(ctx, cacheTmp, cb)
    69  	example.RunServer(ctx, srv, port)
    70  }