Skip to content

Commit e71b11e

Browse files
author
jdob
committed
Merge branch 'master' of github.com:kubernetes-operators-book/chapters
2 parents 7f8c031 + 0a85e6b commit e71b11e

29 files changed

+1435
-2
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: mysql-auth
5+
type: Opaque
6+
stringData:
7+
username: visitors-user
8+
password: visitors-pass

ch06/visitors-helm/templates/backend-deployment.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,13 @@ spec:
2626
value: visitors
2727
- name: MYSQL_SERVICE_HOST
2828
value: mysql-service
29+
- name: MYSQL_USERNAME
30+
valueFrom:
31+
secretKeyRef:
32+
name: mysql-auth
33+
key: username
34+
- name: MYSQL_PASSWORD
35+
valueFrom:
36+
secretKeyRef:
37+
name: mysql-auth
38+
key: password

ch06/visitors-helm/templates/mysql-deployment.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ spec:
2828
- name: MYSQL_DATABASE
2929
value: visitors
3030
- name: MYSQL_USER
31-
value: visitors
31+
valueFrom:
32+
secretKeyRef:
33+
name: mysql-auth
34+
key: username
3235
- name: MYSQL_PASSWORD
33-
value: visitors
36+
valueFrom:
37+
secretKeyRef:
38+
name: mysql-auth
39+
key: password

ch07/visitors-operator/.gitignore

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Temporary Build Files
2+
build/_output
3+
build/_test
4+
# Created by https://www.gitignore.io/api/go,vim,emacs,visualstudiocode
5+
### Emacs ###
6+
# -*- mode: gitignore; -*-
7+
*~
8+
\#*\#
9+
/.emacs.desktop
10+
/.emacs.desktop.lock
11+
*.elc
12+
auto-save-list
13+
tramp
14+
.\#*
15+
# Org-mode
16+
.org-id-locations
17+
*_archive
18+
# flymake-mode
19+
*_flymake.*
20+
# eshell files
21+
/eshell/history
22+
/eshell/lastdir
23+
# elpa packages
24+
/elpa/
25+
# reftex files
26+
*.rel
27+
# AUCTeX auto folder
28+
/auto/
29+
# cask packages
30+
.cask/
31+
dist/
32+
# Flycheck
33+
flycheck_*.el
34+
# server auth directory
35+
/server/
36+
# projectiles files
37+
.projectile
38+
projectile-bookmarks.eld
39+
# directory configuration
40+
.dir-locals.el
41+
# saveplace
42+
places
43+
# url cache
44+
url/cache/
45+
# cedet
46+
ede-projects.el
47+
# smex
48+
smex-items
49+
# company-statistics
50+
company-statistics-cache.el
51+
# anaconda-mode
52+
anaconda-mode/
53+
### Go ###
54+
# Binaries for programs and plugins
55+
*.exe
56+
*.exe~
57+
*.dll
58+
*.so
59+
*.dylib
60+
# Test binary, build with 'go test -c'
61+
*.test
62+
# Output of the go coverage tool, specifically when used with LiteIDE
63+
*.out
64+
### Vim ###
65+
# swap
66+
.sw[a-p]
67+
.*.sw[a-p]
68+
# session
69+
Session.vim
70+
# temporary
71+
.netrwhist
72+
# auto-generated tag files
73+
tags
74+
### VisualStudioCode ###
75+
.vscode/*
76+
.history
77+
# End of https://www.gitignore.io/api/go,vim,emacs,visualstudiocode
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM registry.access.redhat.com/ubi7/ubi-minimal:latest
2+
3+
ENV OPERATOR=/usr/local/bin/visitors-operator \
4+
USER_UID=1001 \
5+
USER_NAME=visitors-operator
6+
7+
# install operator binary
8+
COPY build/_output/bin/visitors-operator ${OPERATOR}
9+
10+
COPY build/bin /usr/local/bin
11+
RUN /usr/local/bin/user_setup
12+
13+
ENTRYPOINT ["/usr/local/bin/entrypoint"]
14+
15+
USER ${USER_UID}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh -e
2+
3+
# This is documented here:
4+
# https://docs.openshift.com/container-platform/3.11/creating_images/guidelines.html#openshift-specific-guidelines
5+
6+
if ! whoami &>/dev/null; then
7+
if [ -w /etc/passwd ]; then
8+
echo "${USER_NAME:-visitors-operator}:x:$(id -u):$(id -g):${USER_NAME:-visitors-operator} user:${HOME}:/sbin/nologin" >> /etc/passwd
9+
fi
10+
fi
11+
12+
exec ${OPERATOR} $@
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
set -x
3+
4+
# ensure $HOME exists and is accessible by group 0 (we don't know what the runtime UID will be)
5+
mkdir -p ${HOME}
6+
chown ${USER_UID}:0 ${HOME}
7+
chmod ug+rwx ${HOME}
8+
9+
# runtime user will need to be able to self-insert in /etc/passwd
10+
chmod g+rw /etc/passwd
11+
12+
# no need for this script to remain in the image after running
13+
rm $0
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"fmt"
7+
"os"
8+
"runtime"
9+
10+
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
11+
_ "k8s.io/client-go/plugin/pkg/client/auth"
12+
13+
"github.com/jdob/visitors-operator/pkg/apis"
14+
"github.com/jdob/visitors-operator/pkg/controller"
15+
16+
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
17+
"github.com/operator-framework/operator-sdk/pkg/leader"
18+
"github.com/operator-framework/operator-sdk/pkg/log/zap"
19+
"github.com/operator-framework/operator-sdk/pkg/metrics"
20+
"github.com/operator-framework/operator-sdk/pkg/restmapper"
21+
sdkVersion "github.com/operator-framework/operator-sdk/version"
22+
"github.com/spf13/pflag"
23+
"sigs.k8s.io/controller-runtime/pkg/client/config"
24+
"sigs.k8s.io/controller-runtime/pkg/manager"
25+
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
26+
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
27+
)
28+
29+
// Change below variables to serve metrics on different host or port.
30+
var (
31+
metricsHost = "0.0.0.0"
32+
metricsPort int32 = 8383
33+
)
34+
var log = logf.Log.WithName("cmd")
35+
36+
func printVersion() {
37+
log.Info(fmt.Sprintf("Go Version: %s", runtime.Version()))
38+
log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH))
39+
log.Info(fmt.Sprintf("Version of operator-sdk: %v", sdkVersion.Version))
40+
}
41+
42+
func main() {
43+
// Add the zap logger flag set to the CLI. The flag set must
44+
// be added before calling pflag.Parse().
45+
pflag.CommandLine.AddFlagSet(zap.FlagSet())
46+
47+
// Add flags registered by imported packages (e.g. glog and
48+
// controller-runtime)
49+
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
50+
51+
pflag.Parse()
52+
53+
// Use a zap logr.Logger implementation. If none of the zap
54+
// flags are configured (or if the zap flag set is not being
55+
// used), this defaults to a production zap logger.
56+
//
57+
// The logger instantiated here can be changed to any logger
58+
// implementing the logr.Logger interface. This logger will
59+
// be propagated through the whole operator, generating
60+
// uniform and structured logs.
61+
logf.SetLogger(zap.Logger())
62+
63+
printVersion()
64+
65+
namespace, err := k8sutil.GetWatchNamespace()
66+
if err != nil {
67+
log.Error(err, "Failed to get watch namespace")
68+
os.Exit(1)
69+
}
70+
71+
// Get a config to talk to the apiserver
72+
cfg, err := config.GetConfig()
73+
if err != nil {
74+
log.Error(err, "")
75+
os.Exit(1)
76+
}
77+
78+
ctx := context.TODO()
79+
80+
// Become the leader before proceeding
81+
err = leader.Become(ctx, "visitors-operator-lock")
82+
if err != nil {
83+
log.Error(err, "")
84+
os.Exit(1)
85+
}
86+
87+
// Create a new Cmd to provide shared dependencies and start components
88+
mgr, err := manager.New(cfg, manager.Options{
89+
Namespace: namespace,
90+
MapperProvider: restmapper.NewDynamicRESTMapper,
91+
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
92+
})
93+
if err != nil {
94+
log.Error(err, "")
95+
os.Exit(1)
96+
}
97+
98+
log.Info("Registering Components.")
99+
100+
// Setup Scheme for all resources
101+
if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
102+
log.Error(err, "")
103+
os.Exit(1)
104+
}
105+
106+
// Setup all Controllers
107+
if err := controller.AddToManager(mgr); err != nil {
108+
log.Error(err, "")
109+
os.Exit(1)
110+
}
111+
112+
// Create Service object to expose the metrics port.
113+
_, err = metrics.ExposeMetricsPort(ctx, metricsPort)
114+
if err != nil {
115+
log.Info(err.Error())
116+
}
117+
118+
log.Info("Starting the Cmd.")
119+
120+
// Start the Cmd
121+
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
122+
log.Error(err, "Manager exited non-zero")
123+
os.Exit(1)
124+
}
125+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: example.com/v1
2+
kind: VisitorsApp
3+
metadata:
4+
name: ex
5+
spec:
6+
size: 1
7+
title: "Custom Dashboard Title"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: visitorsapps.example.com
5+
spec:
6+
group: example.com
7+
names:
8+
kind: VisitorsApp
9+
listKind: VisitorsAppList
10+
plural: visitorsapps
11+
singular: visitorsapp
12+
scope: Namespaced
13+
subresources:
14+
status: {}
15+
validation:
16+
openAPIV3Schema:
17+
properties:
18+
apiVersion:
19+
description: 'APIVersion defines the versioned schema of this representation
20+
of an object. Servers should convert recognized schemas to the latest
21+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
22+
type: string
23+
kind:
24+
description: 'Kind is a string value representing the REST resource this
25+
object represents. Servers may infer this from the endpoint the client
26+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
27+
type: string
28+
metadata:
29+
type: object
30+
spec:
31+
type: object
32+
properties:
33+
size:
34+
type: integer
35+
title:
36+
type: string
37+
required:
38+
- size
39+
status:
40+
type: object
41+
properties:
42+
backendImage:
43+
type: string
44+
frontendImage:
45+
type: string
46+
version: v1
47+
versions:
48+
- name: v1
49+
served: true
50+
storage: true
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: visitors-operator
5+
spec:
6+
replicas: 1
7+
selector:
8+
matchLabels:
9+
name: visitors-operator
10+
template:
11+
metadata:
12+
labels:
13+
name: visitors-operator
14+
spec:
15+
serviceAccountName: visitors-operator
16+
containers:
17+
- name: visitors-operator
18+
# Replace this with the built image name
19+
image: REPLACE_IMAGE
20+
command:
21+
- visitors-operator
22+
imagePullPolicy: Always
23+
env:
24+
- name: WATCH_NAMESPACE
25+
valueFrom:
26+
fieldRef:
27+
fieldPath: metadata.namespace
28+
- name: POD_NAME
29+
valueFrom:
30+
fieldRef:
31+
fieldPath: metadata.name
32+
- name: OPERATOR_NAME
33+
value: "visitors-operator"

0 commit comments

Comments
 (0)