Code Days, February 2021, talk by Mario-Leander Reimer (@LeanderReimer, Chief Software Architect at QAware)
== Please download slides if blurred! ==
Abstract: Use the right tool (and language) for the job! That is the general motto of this hands-on session. Go has established itself as a simple, reliable and efficient programming language, especially in the Ops and cloud-native area: Docker, Kubernetes and many other well-known tools and infrastructure components are all implemented in Go. This session is packed with many practical use cases and examples to illustrate and explore the power of the Go language universe.
3. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
3
Code & Demos
https://github.com/qaware/go-for-operations
4. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
How do you organise and enable
DevOps teams for
fast
fl
ow and high productivity?
4
5. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
Too much cognitive load will become a bottleneck
for fast
fl
ow and high productivity.
• Instrinsic Cognitive Load - relates to fundamental aspects
and knowledge in the problem space (e.g. used languages,
APIs, frameworks)
• Extraneous Cognitive Load - relates to the environment
(e.g. deployment, con
fi
guration, console commands)
• Germane Cognitive Load - relates to speci
fi
c aspects of the
business domain (aka. „value added“ thinking)
5
https://teamtopologies.com
https://web.devopstopologies.com
6. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
Eliminate
extraneous cognitive load
Minimize
intrinsic cognitive load
6
7. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
7
8. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
Use the right tools for the job!
8
Getty Images Liliboas
Ansible Shell Scripts
Golang
Ruby Python
9. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
Why Go?
• Go is Open Source and maintained by Google
• Go is an e
ffi
cient distributed, parallel language for systems
programming at Google to solve problems of C++ code
• Single, self contained binary. Runs almost on any platform and OS.
• Vivid community. Good documentation. Good Tooling.
• Go is the major language behind the Cloud Native Stack, many
important components are written in Go
9
10. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
The outline for today
1. Getting to Know Go: Basics and Tooling
2. Building CLI applications with Cobra
3. Implementing custom kubectl plugins
4. Building a Sidecar container
5. Building a Kubernetes Operator in Go
10
11. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
11
https:/
/gobyexample.com
https:/
/learnxinyminutes.com/docs/go/
12. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
The renaissance of the plain old Make
fi
le
12
VERSION = v1.0.0
.PHONY: build
build:
# omit the symbol table, debug information and the DWARF table
@go build -o go-example -ldflags="-s -w -X main.version=$(VERSION)"
clean:
@go clean
test: build
@go test -v
all: clean build test
release: all
@goreleaser --snapshot --skip-publish --rm-dist
13. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
Use GoReleaser to publish multi OS binaries
• Cross-compile your Go project
• Release to GitHub, GitLab and Gitea
• Create Docker images and manifests
• Create Linux packages and Homebrew
taps
• Upload to Bintray, Artifactory to Public
Cloud Blob Stores
• ... and much more!
13
project_name: go-example
before:
hooks:
- go mod download
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
goarch:
- 386
- amd64
ldflags: -s -w -X main.version={{.Version}}
archives:
- name_template: '{{ .ProjectName }}-{{ .Version }}-{{ .Os }}-{{
format_overrides:
- goos: windows
format: zip
replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
14. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
The Swiss Army Knife of Operations.
14
CLIs - The Swiss Army Knife of Operations
15. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
The basics of 12-factor CLI apps
• Great help is essential. What version am I on?
• Prefer
fl
ags to positional arguments.
• Mind the streams. stdout is for output, stderr is for messaging.
• Handle things going wrong: error code, title, how to
fi
x, URL, …
• Be fancy: use colours, have shell completion.
• Prompt if you can.
• Be speedy. CLIs need to start fast.
• Be clear about subcommands.
15
For complete list and info, read https://medium.com/@jdxcode/12-factor-cli-apps-dd3c227a0e46
16. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
Introducing
• https://github.com/spf13/cobra
• Cobra is a library providing a simple interface to create powerful
modern CLI interfaces similar to git & go tools.
• Cobra is also an application that will generate your application
scaffolding to rapidly develop a Cobra-based application.
• Cobra is used in many Go projects such as Kubernetes, Docker,
Skaffold, Helm and Istio just to name a few.
16
18. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
Container Orchestration Patterns
18
Sidecar Container
Extended Container Behaviour
• Log Extraction / Reformatting
(
fl
uentd,
fi
le beat)
• Scheduling (cron, quartz)
Ambassador Container
Proxy Communication
• TLS Tunnel (ghostunnel, Istio)
• Circuit Breaking (linked, Istio)
• Request Monitoring (linked, Istio)
Adapter Container
Standardized Ops Interfaces
• Monitoring (Prometheus)
• Con
fi
guration (Con
fi
gMaps, Secrets, …)
19. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
Use a multi-stage Docker
fi
le to build Linux binary
19
FROM golang:1.15.2 as builder
WORKDIR /build
COPY . /build
RUN go build -o logship-sidecar -ldflags="-s -w"
FROM gcr.io/distroless/static-debian10
COPY --from=builder /build/logship-sidecar /
ENV LOG_DIRECTORY=/logs
ENV LOG_FILE_PATTERN=.+.gz
ENV LOG_SCAN_INTERVAL=10
ENTRYPOINT ["/logship-sidecar"]
CMD [""]
Stage 1: Building
Stage 2: Running
20. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
20
Operator.
- Do stuff to my Kubernetes.
21. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
What are operators?
• Operators are codi
fi
ed Ops procedures!
• Operators are the path towards Zero-Ops. They enable auto-updating,
self-monitoring and self-healing infrastructure and applications.
• The concept was coined in the Kubernetes world. It’s now been
adopted and used widespread in the cloud native world.
• Examples: OKD, Sealed Secrets, Kube Monkey, Weave Flux
21
22. // Code Days 2021 Digital —> Go for Operations // @LeanderReimer #cloudnativenerd #qaware #CodeDays #OOPmuc
Kubernetes API Extensions via Custom Resources
• User de
fi
ned extensions of the Kubernetes APIs
• Allow the abstraction of complex application constructs and concepts
• De
fi
nition solely via CustomResourceDefinitions
• Structure de
fi
nition via OpenAPI v3.0 Validation Schema
• Default Support for several API Features: CRUD, Watch, Discovery,
json-patch, merge-patch, Admission Webhooks, Metadata, RBAC, …
• Versioning und Conversion supported via Webhooks
22