envoy-proxy-go

command module
v0.0.0-...-e2192bd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

  1. Clone the repository: git clone https://github.com/yourusername/envoy-go-control-plane.git cd envoy-go-control-plane

  2. Install dependencies: go mod tidy

  3. Run the control plane server: go run main.go

  4. Configure Envoy to use the control plane (use the go-control-plane.yaml configuration file).

  5. Start Envoy: envoy -c go-control-plane.yaml

Project Structure

  • main.go: The main Go file containing the control plane server implementation.
  • go-control-plane.yaml: Envoy configuration file that points to the control plane.
  • README.md: This file, containing project documentation.

How It Works

The control plane server sets up an xDS server using go-control-plane. It creates a snapshot cache and serves configuration to Envoy proxies. Envoy is configured to connect to this control plane and receive dynamic updates.

Code Examples

Control Plane Server (main.go)

package main

import (
 "context"
 "log"
 "net"
 "time"

 "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
 "github.com/envoyproxy/go-control-plane/pkg/server/v3"
 "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
 "github.com/envoyproxy/go-control-plane/pkg/test/v3"
 "google.golang.org/grpc"
)

func main() {
 // Create a snapshot cache
 snapshotCache := cache.NewSnapshotCache(true, cache.IDHash{}, nil)

 // Create a new gRPC server
 grpcServer := grpc.NewServer()

 // Create a new xDS server
 xdsServer := server.NewServer(context.Background(), snapshotCache, nil)

 // Register the xDS server with the gRPC server
 resource.RegisterServer(grpcServer, xdsServer)

 // Listen on a port
 lis, err := net.Listen("tcp", ":18000")
 if err != nil {
     log.Fatalf("Failed to listen: %v", err)
 }

 // Start the gRPC server
 go func() {
     if err := grpcServer.Serve(lis); err != nil {
         log.Fatalf("Failed to serve: %v", err)
     }
 }()

 // Create a snapshot with a simple configuration
 nodeID := "test-node"
 snapshot := cache.NewSnapshot(
     "1", // version
     nil, // endpoints
     nil, // clusters
     nil, // routes
     nil, // listeners
     nil, // runtimes
     nil, // secrets
 )

 // Set the snapshot for the node
 if err := snapshotCache.SetSnapshot(context.Background(), nodeID, snapshot); err != nil {
     log.Fatalf("Failed to set snapshot: %v", err)
 }

 log.Println("xDS server is running...")

 // Keep the server running
 select {}
}
Envoy Configuration (go-control-plane.yaml)
yaml
Copy Code
static_resources:
  listeners:
  - name: listener_0
    address:
      socket_address:
        address: 0.0.0.0
        port_value: 10000
    filter_chains:
    - filters:
      - name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          stat_prefix: ingress_http
          route_config:
            name: local_route
            virtual_hosts:
            - name: local_service
              domains: ["*"]
              routes:
              - match:
                  prefix: "/"
                route:
                  cluster: service_0
          http_filters:
          - name: envoy.filters.http.router
  clusters:
  - name: service_0
    connect_timeout: 0.25s
    type: LOGICAL_DNS
    lb_policy: ROUND_ROBIN
    load_assignment:
      cluster_name: service_0
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: service_0
                port_value: 80

dynamic_resources:
  ads_config:
    api_type: GRPC
    grpc_services:
    - envoy_grpc:
        cluster_name: xds_cluster
  cds_config:
    resource_api_version: V3
  lds_config:
    resource_api_version: V3

static_resources:
  clusters:
  - name: xds_cluster
    connect_timeout: 0.25s
    type: STRICT_DNS
    lb_policy: ROUND_ROBIN
    http2_protocol_options: {}
    load_assignment:
      cluster_name: xds_cluster
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: 127.0.0.1
                port_value: 18000
Contributing
We welcome contributions to improve this project! Here's how you can contribute:

Fork the Repository: Click the 'Fork' button at the top right of this page and clone your fork.

Create a Branch: Create a new branch for your feature or bug fix.

git checkout -b feature/your-feature-name
Make Changes: Implement your changes, adhering to the existing code style.

Test Your Changes: Ensure your changes don't break existing functionality.

Commit Your Changes: Use clear and concise commit messages.

git commit -m "Add feature: your feature description"
Push to Your Fork:

git push origin feature/your-feature-name
Create a Pull Request: Go to the original repository and click 'New Pull Request'. Select your fork and the branch you created.

Describe Your Changes: In the PR description, explain your changes and their purpose.

Code Review: Wait for the maintainers to review your PR. Make any requested changes.

Contribution Guidelines
Follow Go best practices and coding standards.
Write clear, commented code.
Update documentation for any new features or changes.
Add tests for new functionality.
Ensure all tests pass before submitting a PR.
License
This project is licensed under the MIT License - see the LICENSE file for details.



Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL