Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tungsten

Tungsten is a Cloudflare worker that can continuously handle tasks (HTTP requests) to manage traffic in Argo Rollouts using ngrok as a tunneling service for Travis CI.

Prerequisites

  • Go 1.16 or higher
  • Cloudflare API credentials
  • Argo CD API credentials
  • Argo Rollouts API credentials
  • ngrok
  • nginx
  • Smallstep

Installation

First clone the Tungsten repository:

git clone https://github.com/Montana/tungsten.git
cd tungsten

Make sure everything is there, run:

ls -al

When in the directory, if everything is there - we can move on.

Add tungsten into your Go project

First install it through your CLI:

go get -u github.com/Montana/tungsten

Then import it as a dependency in your main.go:

import (
    "github.com/Montana/tungsten"
)

Dynamic Proxy Configuration

Depending on the PROXY_OPTION (nginx, ngrok, Smallstep) environment variable, this will start an ngrok tunnel for easy, temporary public URLs. Start an nginx reverse proxy for more controlled and stable proxy management, even configure Smallstep for automated certificate management, ensuring secure communication.

Running tungsten

Run the application:

go run main.go

The server will start on port 8080, and an ngrok tunnel will be created. The tunnel URL will be printed in the console, important to remember to use the HTTP endpoint to manage Argo Rollouts traffic:

  • Endpoint: /manage-traffic

  • Method: POST

  • Request Body:

         {
             "name": "rollout-name",
             "namespace": "rollout-namespace",
             "weight": 50
         }

Running the Application with Docker

Build the Docker image:

 docker build -t tungsten .

Run the Docker container:

 docker run -d -p 8080:8080 \
 -e ARGOCD_URL=https://your-argocd-url \
 -e ARGOCD_TOKEN=your-argocd-token \
 -e ARGOROLLOUTS_URL=https://your-argorollouts-url \
 -e ARGOROLLOUTS_TOKEN=your-argorollouts-token \
 argo-traffic-management

The server will start on port 8080 inside the container, and an ngrok tunnel will be created. The tunnel URL will be printed in the container logs. Check the container logs to get the ngrok tunnel URL:

 docker logs <container_id>

Please look at the Dockerfile that's in this repository in the root directory.

Flowchart

This explains the flow of how ngrok interacts with Argo and routes traffic:

Tungsten

Let's move on to curl now.

Using curl

Example curl command:

 curl -X POST -H "Content-Type: application/json" -d '{"name": "rollout-name", "namespace": "rollout-namespace", "weight": 50}' http://localhost:8080/manage-traffic

Main Components

  • ArgoApplication: Struct to hold Argo CD application metadata.
  • TrafficRouting: Struct to define traffic routing for Argo Rollouts.
  • getArgoCDApplications: Function to fetch Argo CD applications.
  • manageArgoRolloutsTraffic: Function to manage Argo Rollouts traffic routing.
  • handleRequest: HTTP handler for managing traffic.
  • startServer: Function to start the HTTP server and ngrok tunnel.

Screenshot 2024-06-17 at 11 43 53 PM

Modify your existing functions

Example modification in getArgoCDApplications function:

func getArgoCDApplications() ([]ArgoApplication, error) {
    argoCDURL := os.Getenv("ARGOCD_URL")
    argoCDToken := os.Getenv("ARGOCD_TOKEN")

    if argoCDURL == "" || argoCDToken == "" {
        return nil, fmt.Errorf("ARGOCD_URL and ARGOCD_TOKEN must be set")
    }

    client := tungsten.NewClient() 
    req, err := tungsten.NewRequest("GET", fmt.Sprintf("%s/api/v1/applications", argoCDURL), nil) // Using tungsten to create a new request
    if err != nil {
        return nil, err
    }

    req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", argoCDToken))
    resp, err := client.Do(req)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()

    if resp.StatusCode != http.StatusOK {
        return nil, fmt.Errorf("failed to get Argo CD applications, status code: %d", resp.StatusCode)
    }

    var applications struct {
        Items []ArgoApplication `json:"items"`
    }
    err = json.NewDecoder(resp.Body).Decode(&applications)
    if err != nil {
        return nil, err
    }

    return applications.Items, nil
}

Configuration

Configure the application by setting the following environment variables. You can set them in a Bash script or directly in your shell.

  • CLOUDFLARE_API_KEY: Your Cloudflare API key.
  • CLOUDFLARE_EMAIL: Your Cloudflare account email.
  • CLOUDFLARE_ZONE_NAME: The name of the Cloudflare zone.
  • ARGOCD_URL: The URL of your Argo CD instance.
  • ARGOCD_TOKEN: The API token for your Argo CD instance.
  • ARGOROLLOUTS_URL: The URL of your Argo Rollouts instance.
  • ARGOROLLOUTS_TOKEN: The API token for your Argo Rollouts instance.

Copyright

Creator and Maintainer: Michael Allen Mendy. (c) 2024.

About

Tungsten provides a web service that can update traffic routing for Argo Rollouts applications. It uses ngrok and or nginx (your choice) to make the local server accessible from the internet, allowing remote interaction with the service. To add, you can configure Smallstep for automated certificate management, ensuring secure communication.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages