Skip to content

Instantly share code, notes, and snippets.

View kaugm's full-sized avatar
💭
Learning GCP & Kubernetes

Karl Martin kaugm

💭
Learning GCP & Kubernetes
View GitHub Profile
@kaugm
kaugm / README.md
Created September 20, 2023 18:52 — forked from stevenfeltner/README.md
Spot.io Log Query Service

spot-log query service

Install this next to your spot-controller to print logs from the Spot.io Ocean. This service simply spits out logs to stdout in JSON format. Then your log aggregation service can use the container logs and publish them to your own logging platform.

Install

kubectl apply -f spot-logs.yaml --namespace kube-system
@kaugm
kaugm / cognito.yaml
Last active July 13, 2023 01:19 — forked from singledigit/cognito.yaml
Create a Cognito Authentication Backend via CloudFormation
AWSTemplateFormatVersion: '2010-09-09'
Description: Cognito Stack
Parameters:
AuthName:
Type: String
Description: Unique Auth Name for Cognito Resources
Resources:
# Creates a user pool in cognito for your app to auth against
# This example requires MFA and validates the phone number to use as MFA
@kaugm
kaugm / ocean_refresh_nodes.py
Created May 22, 2023 17:11
Refresh Ocean nodes based on uptime by detaching them.
#!/opt/homebrew/bin/python3
"""
Proof of Concept: Refresh nodes in Ocean Cluster once they reach a certain uptime. Script created per customer request for this functionality.
Purpose: Currently, Ocean does not support the ability to automatically detach "old" nodes once they reach a certain uptime and spin up fresh nodes to replace them.
How it works:
1. Pull the current node count of the Ocean cluster via the API
2. Loop through the list of nodes
3. For each node, if its uptime is greater than a predefined limit, detach it
@kaugm
kaugm / ocean_vng_od_count.py
Last active May 22, 2023 17:10
Approximate an On-Demand count per Ocean VNG
#!/opt/homebrew/bin/python3
"""
Proof of Concept: On-Demand Count per Ocean VNG (AWS K8s). Need to test how Ocean rounds to nearest whole instance based on Spot Percentage.
Purpose: Currently, Ocean does not support the configuration of 'On-Demand Count', only Elastigroup does. By running this script with AWS Lambda + EventBridge CRON scheduling, on say an hourly basis, this functionality can be approximated.
How it works:
1. Pull the current node count of the Ocean cluster via the API
2. Breakdown node count into separate VNGs
3. For each VNG (or desired VNG), calculate what Risk (Spot Percentage) would equate to having 1x On-Demand instance ( using math.ceil() )
@kaugm
kaugm / elastigroup_cfn.yaml
Last active April 3, 2023 20:57
Create an Elastigroup with CloudFormation (Example)
AWSTemplateFormatVersion: "2010-09-09"
Description: This template creates an Elastigroup. This is not meant to be run as a standalone CloudFormation template, but rather to have the Parameters and Resource added to existing ASG CloudFormation templates to create and manage both simultaneously. All API parameters in Spot documentation are valid for the Elastigroup resource https://docs.spot.io/api/#tag/Elastigroup-AWS/operation/elastigroupAwsCreate
Parameters:
ServiceToken:
Type: String
Description: ARN of Lambda function in Spot's AWS account that will handle creation of Elastigroup. DO NOT CHANGE THIS.
Default: arn:aws:lambda:us-west-2:178579023202:function:spotinst-cloudformation
accessToken:
@kaugm
kaugm / spot_api.sh
Created March 23, 2023 21:26
Call the Spot.io API with a shell script
#!/usr/bin/env bash
# This script shows how to call the Spot.io API via a shell script and contains 4 examples for HTTP methods: GET, POST, PUT, and DELETE
# Requires jq
# Written by Karl Martin
# [Optional] Set Spot token and account manually. Currently set as environment variables
# SPOTINST_TOKEN=''
# SPOTINST_ACCOUNT_AWS=''
@kaugm
kaugm / export_rightsizing_recommendations.py
Created March 2, 2023 20:44
Export Spot.io Ocean Rightsizing Recommendations to CSV
#!/opt/homebrew/bin/python3
try:
import requests
import os
import json
import re
import csv
from datetime import datetime
except ModuleNotFoundError:
@kaugm
kaugm / spotinst_api.py
Last active May 4, 2023 18:34
Spot.io API Calls via Python
#!/opt/homebrew/bin/python3
"""
Perform Spot.io API calls with Python
Ensure body JSON is correct when performing API calls, otherwise the response will be 'bad request'.
"""
try:
import requests
import os
import json
@kaugm
kaugm / ocean_costs.py
Last active January 10, 2023 21:25
Spot.io Ocean - Get Cluster Costs - Python SDK Example
#!/opt/homebrew/bin/python3
import os
from datetime import datetime
try:
from spotinst_sdk2 import SpotinstSession
from spotinst_sdk2.models.ocean.aws import *
except ModuleNotFoundError:
print('Spotinst SDK Library not installed. Exiting..')
@kaugm
kaugm / mysql_connection.py
Last active April 11, 2023 14:32
MySQL Connection Class Object
class MySQL_Connection:
"""Connection to MySQL database
Arguments:
HOST: <class 'str'> Full hostname to connect to
DATABASE: <class 'str'> Database within host
"""
# Get Database Credentials From Environment Variables
DB_USERNAME = os.environ.get('SPOT_DB_USERNAME')