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.
- Go 1.16 or higher
- Cloudflare API credentials
- Argo CD API credentials
- Argo Rollouts API credentials
- ngrok
- nginx
- Smallstep
First clone the Tungsten repository:
git clone https://github.com/Montana/tungsten.git
cd tungstenMake sure everything is there, run:
ls -alWhen in the directory, if everything is there - we can move on.
First install it through your CLI:
go get -u github.com/Montana/tungstenThen import it as a dependency in your main.go:
import (
"github.com/Montana/tungsten"
)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.
Run the application:
go run main.goThe 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 }
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-managementThe 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.
This explains the flow of how ngrok interacts with Argo and routes traffic:
Let's move on to curl now.
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- 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.
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
}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.
Creator and Maintainer: Michael Allen Mendy. (c) 2024.

