INT 21h

Hi, I am Vladimir Smagin, SysAdmin, DevOps and barely good guy. Telegram Email / GIT / Микроблог / Thingiverse / RSS / GPG

Простой способ подготовки библиотеки на golang к тестированию

№ 11006 В разделе "Programming" от May 26th, 2020,

Чтобы без проблем тестировать программы, написанные с использованием вашей библиотеки ее необходимо подготовить для этого. Делаем интерфейс, который будет использоваться в тестах, где ваши реальные функции будут заменены функциями с тестовыми данными.

package main

import "fmt"

// library 

type FooAdapter interface {
	Read() string
}

type Foo struct {
	mvar    string
}

func NewFoo(v string) FooAdapter {
	return &Foo{mvar:v}
}

func (a *Foo) Read() string {
	return "orig: " + a.mvar
}

// test 

func NewFooStub(v string) FooAdapter {
	return &FooStub{mvar: v}
}

type FooStub struct {
	mvar string
}

func (s *FooStub) Read() string {
	return "stub: " + s.mvar
}

func main() {
	z := NewFoo("o")
	fmt.Println("Read", z.Read())
	m := NewFooStub("s")
	fmt.Println("Read", m.Read())
}

https://play.golang.org/p/9utaWeDjNjo

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

Hetzner DNS golang library

№ 10996 В разделе "Programming" от May 6th, 2020,

I made this library to interact with Hetzner DNS API in most easy way. Hopefully in future it will be used for Hetzner external-dns provider. Check out example directory and API_help.md.

Get your own token on Hetzner DNS and place it to token variable and run code

token := "jcB2UywP9XtZGhvhSHpH5m"
zone := "vhSHpH5mjcB2UywP9XtZGh"

log.Println("Create new instance")
hdns := hclouddns.New(token)

log.Println("Get zone", zone)

allRecords, err := hdns.GetRecords(zone)
if err != nil {
	log.Fatalln(err)
}

log.Println(allRecords.Records)
log.Println(allRecords.Error)

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

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: The weed of crime bears bitter fruit... but the leaves are good to smoke! -- The Shadow