INT 21h

Hi, I am Vladimir Smagin, SysAdmin and Kaptain. Telegram Email / GIT / Thingiverse / RSS / GPG

Golang: marshal and unmarshal iota (yaml, json, toml)

№ 11025 В разделе "Programming" от June 16th, 2020,
В подшивках:

Tried to unmarshal enum value from app config file and failed?

DB:
  masterDB: "ips.sdb"
  clean: Full

Use strings instead of iota.

const (
	CleanRecreate  = "Recreate" // completely remove DB file and create again
	CleanFull      = "Full" // delete all and vacuum
	CleanLastDay   = "LastDay" // remove all before last day
	CleanLastWeek  = "LastWeek"
	CleanLastMonth = "LastMonth"
	CleanNever     = "Never" // do nothing
)

type CleanType string

type AppConfig struct {
	DB    struct {
		MasterDB string    `yaml:"masterDB"`        // master sqlite database
		Clean    CleanType `yaml:"clean,omitempty"` // cleanup at startup
	} `yaml:"DB"`
}

Now load config file:

func main() {
	flag.Parse()
	if *configFilename == "" {
		log.Fatalln("Set configuration filename")
	}

	// read settings from file
	log.Println("Loading config file", *configFilename)
	appConfig := AppConfig{}

	yamlFile, err := ioutil.ReadFile(*configFilename)
	if err != nil {
		log.Fatalf("Config read error: %v\n", err)
	}
	err = yaml.Unmarshal(yamlFile, &appConfig)
	if err != nil {
		log.Fatalf("Config format error: %v\n", err)
	}

	switch appConfig.DB.Clean {
	case CleanRecreate:
		log.Println("Recreate cleanup option set")
		os.Remove(appConfig.DB.MasterDB)
	case CleanFull:
		log.Println("Full cleanup option set")
	case CleanLastDay:
		log.Println("Save only last day cleanup option set")
	case CleanLastWeek:
		log.Println("Save only last week option set")
	case CleanLastMonth:
		log.Println("Save only last month option set")
	}
	dbHandler := dbLoadFile(appConfig.DB.MasterDB)
	defer dbHandler.Close()
}

Here is another solution https://gist.github.com/lummie/7f5c237a17853c031a57277371528e87#file-enum-go

Нет комментариев »

Warm Image operator for Kubernetes

№ 11010 В разделе "Sysadmin" от May 28th, 2020,
В подшивках: , , ,

For example, you have huge image with your software and running POD on node. When POD moving to another node your image downloads to new node minute or two. This operator forces nodes to download image before rescheduling, so POD starts faster.

It runs /bin/sh with infinite loop on specified image as DaemonSet with additional options like NodeSelector, Affinity or resource limits. You can specify custom command if your image not contains /bin/sh interpreter or you want to run own script.

Your first warmer:

apiVersion: blindage.org/v1alpha1
kind: WarmImage
metadata:
  name: mongo4
spec:
  image: mongo
  version: "4"
  nodeSelector:
    node-role.kubernetes.io/master: ""

Now you warmed mongo:4 on all master nodes.

Repository here https://git.blindage.org/21h/warm-image-operator

Нет комментариев »

CronJobs operator for Kubernetes

№ 10306 В разделах: Programming Sysadmin от September 17th, 2019,
В подшивках: , , ,

Helps to control multiple cronjobs with same image, but different commands.

Checkout code and documentation https://git.blindage.org/21h/cron-operator

Нет комментариев »

Vault secret retrieve and save to JSON file

№ 10280 В разделах: Programming Sysadmin от September 1st, 2019,
В подшивках: , ,

I wrote small program to retrieve secrets from Vault and provide them to my PHP and Python apps. ENV variables with connection credentials is useful with Docker containers and even Kubernetes, list of secrets to retrieve can be stored inside Docker image.

Secret stored in Vault

Result file on disk

Source code and binary release https://git.blindage.org/21h/vault-retriever

Нет комментариев »

Fast bootstrap of Operator SDK and your first project

№ 10268 В разделе "Programming" от August 30th, 2019,
В подшивках: ,

1) Install operator-sdk on your computer https://github.com/operator-framework/operator-sdk/blob/master/doc/user/install-operator-sdk.md. I prefer to install into $GOPATH/bin.
2) create script somewhere like /opt/bootstrap_operator.sh

#!/bin/bash
set -e

REPO="git.blindage.org/21h/redis-operator"
API_VERSION="blindage.org/v1alpha1"
KIND="Redis"
NAME="redis-operator"

# operator sdk
export GO111MODULE=on
echo "Bootstraping ${NAME}"
operator-sdk new ${NAME} --git-init --repo ${REPO} --verbose
cd ${NAME}
echo "Adding API ${API_VERSION}"
operator-sdk add api --api-version=${API_VERSION} --kind=${KIND} --verbose
echo "Creating controller"
operator-sdk add controller --api-version=${API_VERSION} --kind=${KIND} --verbose

3) change directory to your “~/dev” and run script bash /opt/bootstrap_operator.sh, it will create new directory “~/dev/redis-operator

4) build your new operator, pack to image operator-sdk build redis-operator

5) push new image to registry with docker push myregistry.ru/redis-operator

Нет комментариев »

Яндекс.Метрика

Fortune cookie: Floating idly one day through the air, A circus performer named Blair, Tied a sizeable rock, To the end of his cock, And shattered a balcony chair.