-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy of the Go-based visitors operator
- Loading branch information
Jay Dobies
committed
Oct 10, 2019
1 parent
a56b5dc
commit 0a85e6b
Showing
26 changed files
with
1,409 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Temporary Build Files | ||
build/_output | ||
build/_test | ||
# Created by https://www.gitignore.io/api/go,vim,emacs,visualstudiocode | ||
### Emacs ### | ||
# -*- mode: gitignore; -*- | ||
*~ | ||
\#*\# | ||
/.emacs.desktop | ||
/.emacs.desktop.lock | ||
*.elc | ||
auto-save-list | ||
tramp | ||
.\#* | ||
# Org-mode | ||
.org-id-locations | ||
*_archive | ||
# flymake-mode | ||
*_flymake.* | ||
# eshell files | ||
/eshell/history | ||
/eshell/lastdir | ||
# elpa packages | ||
/elpa/ | ||
# reftex files | ||
*.rel | ||
# AUCTeX auto folder | ||
/auto/ | ||
# cask packages | ||
.cask/ | ||
dist/ | ||
# Flycheck | ||
flycheck_*.el | ||
# server auth directory | ||
/server/ | ||
# projectiles files | ||
.projectile | ||
projectile-bookmarks.eld | ||
# directory configuration | ||
.dir-locals.el | ||
# saveplace | ||
places | ||
# url cache | ||
url/cache/ | ||
# cedet | ||
ede-projects.el | ||
# smex | ||
smex-items | ||
# company-statistics | ||
company-statistics-cache.el | ||
# anaconda-mode | ||
anaconda-mode/ | ||
### Go ### | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
# Test binary, build with 'go test -c' | ||
*.test | ||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
### Vim ### | ||
# swap | ||
.sw[a-p] | ||
.*.sw[a-p] | ||
# session | ||
Session.vim | ||
# temporary | ||
.netrwhist | ||
# auto-generated tag files | ||
tags | ||
### VisualStudioCode ### | ||
.vscode/* | ||
.history | ||
# End of https://www.gitignore.io/api/go,vim,emacs,visualstudiocode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
FROM registry.access.redhat.com/ubi7/ubi-minimal:latest | ||
|
||
ENV OPERATOR=/usr/local/bin/visitors-operator \ | ||
USER_UID=1001 \ | ||
USER_NAME=visitors-operator | ||
|
||
# install operator binary | ||
COPY build/_output/bin/visitors-operator ${OPERATOR} | ||
|
||
COPY build/bin /usr/local/bin | ||
RUN /usr/local/bin/user_setup | ||
|
||
ENTRYPOINT ["/usr/local/bin/entrypoint"] | ||
|
||
USER ${USER_UID} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/sh -e | ||
|
||
# This is documented here: | ||
# https://docs.openshift.com/container-platform/3.11/creating_images/guidelines.html#openshift-specific-guidelines | ||
|
||
if ! whoami &>/dev/null; then | ||
if [ -w /etc/passwd ]; then | ||
echo "${USER_NAME:-visitors-operator}:x:$(id -u):$(id -g):${USER_NAME:-visitors-operator} user:${HOME}:/sbin/nologin" >> /etc/passwd | ||
fi | ||
fi | ||
|
||
exec ${OPERATOR} $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/sh | ||
set -x | ||
|
||
# ensure $HOME exists and is accessible by group 0 (we don't know what the runtime UID will be) | ||
mkdir -p ${HOME} | ||
chown ${USER_UID}:0 ${HOME} | ||
chmod ug+rwx ${HOME} | ||
|
||
# runtime user will need to be able to self-insert in /etc/passwd | ||
chmod g+rw /etc/passwd | ||
|
||
# no need for this script to remain in the image after running | ||
rm $0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"runtime" | ||
|
||
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) | ||
_ "k8s.io/client-go/plugin/pkg/client/auth" | ||
|
||
"github.com/jdob/visitors-operator/pkg/apis" | ||
"github.com/jdob/visitors-operator/pkg/controller" | ||
|
||
"github.com/operator-framework/operator-sdk/pkg/k8sutil" | ||
"github.com/operator-framework/operator-sdk/pkg/leader" | ||
"github.com/operator-framework/operator-sdk/pkg/log/zap" | ||
"github.com/operator-framework/operator-sdk/pkg/metrics" | ||
"github.com/operator-framework/operator-sdk/pkg/restmapper" | ||
sdkVersion "github.com/operator-framework/operator-sdk/version" | ||
"github.com/spf13/pflag" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" | ||
"sigs.k8s.io/controller-runtime/pkg/runtime/signals" | ||
) | ||
|
||
// Change below variables to serve metrics on different host or port. | ||
var ( | ||
metricsHost = "0.0.0.0" | ||
metricsPort int32 = 8383 | ||
) | ||
var log = logf.Log.WithName("cmd") | ||
|
||
func printVersion() { | ||
log.Info(fmt.Sprintf("Go Version: %s", runtime.Version())) | ||
log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)) | ||
log.Info(fmt.Sprintf("Version of operator-sdk: %v", sdkVersion.Version)) | ||
} | ||
|
||
func main() { | ||
// Add the zap logger flag set to the CLI. The flag set must | ||
// be added before calling pflag.Parse(). | ||
pflag.CommandLine.AddFlagSet(zap.FlagSet()) | ||
|
||
// Add flags registered by imported packages (e.g. glog and | ||
// controller-runtime) | ||
pflag.CommandLine.AddGoFlagSet(flag.CommandLine) | ||
|
||
pflag.Parse() | ||
|
||
// Use a zap logr.Logger implementation. If none of the zap | ||
// flags are configured (or if the zap flag set is not being | ||
// used), this defaults to a production zap logger. | ||
// | ||
// The logger instantiated here can be changed to any logger | ||
// implementing the logr.Logger interface. This logger will | ||
// be propagated through the whole operator, generating | ||
// uniform and structured logs. | ||
logf.SetLogger(zap.Logger()) | ||
|
||
printVersion() | ||
|
||
namespace, err := k8sutil.GetWatchNamespace() | ||
if err != nil { | ||
log.Error(err, "Failed to get watch namespace") | ||
os.Exit(1) | ||
} | ||
|
||
// Get a config to talk to the apiserver | ||
cfg, err := config.GetConfig() | ||
if err != nil { | ||
log.Error(err, "") | ||
os.Exit(1) | ||
} | ||
|
||
ctx := context.TODO() | ||
|
||
// Become the leader before proceeding | ||
err = leader.Become(ctx, "visitors-operator-lock") | ||
if err != nil { | ||
log.Error(err, "") | ||
os.Exit(1) | ||
} | ||
|
||
// Create a new Cmd to provide shared dependencies and start components | ||
mgr, err := manager.New(cfg, manager.Options{ | ||
Namespace: namespace, | ||
MapperProvider: restmapper.NewDynamicRESTMapper, | ||
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort), | ||
}) | ||
if err != nil { | ||
log.Error(err, "") | ||
os.Exit(1) | ||
} | ||
|
||
log.Info("Registering Components.") | ||
|
||
// Setup Scheme for all resources | ||
if err := apis.AddToScheme(mgr.GetScheme()); err != nil { | ||
log.Error(err, "") | ||
os.Exit(1) | ||
} | ||
|
||
// Setup all Controllers | ||
if err := controller.AddToManager(mgr); err != nil { | ||
log.Error(err, "") | ||
os.Exit(1) | ||
} | ||
|
||
// Create Service object to expose the metrics port. | ||
_, err = metrics.ExposeMetricsPort(ctx, metricsPort) | ||
if err != nil { | ||
log.Info(err.Error()) | ||
} | ||
|
||
log.Info("Starting the Cmd.") | ||
|
||
// Start the Cmd | ||
if err := mgr.Start(signals.SetupSignalHandler()); err != nil { | ||
log.Error(err, "Manager exited non-zero") | ||
os.Exit(1) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
ch07/visitors-operator/deploy/crds/example_v1_visitorsapp_cr.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
apiVersion: example.com/v1 | ||
kind: VisitorsApp | ||
metadata: | ||
name: ex | ||
spec: | ||
size: 1 | ||
title: "Custom Dashboard Title" |
50 changes: 50 additions & 0 deletions
50
ch07/visitors-operator/deploy/crds/example_v1_visitorsapp_crd.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
apiVersion: apiextensions.k8s.io/v1beta1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
name: visitorsapps.example.com | ||
spec: | ||
group: example.com | ||
names: | ||
kind: VisitorsApp | ||
listKind: VisitorsAppList | ||
plural: visitorsapps | ||
singular: visitorsapp | ||
scope: Namespaced | ||
subresources: | ||
status: {} | ||
validation: | ||
openAPIV3Schema: | ||
properties: | ||
apiVersion: | ||
description: 'APIVersion defines the versioned schema of this representation | ||
of an object. Servers should convert recognized schemas to the latest | ||
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources' | ||
type: string | ||
kind: | ||
description: 'Kind is a string value representing the REST resource this | ||
object represents. Servers may infer this from the endpoint the client | ||
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds' | ||
type: string | ||
metadata: | ||
type: object | ||
spec: | ||
type: object | ||
properties: | ||
size: | ||
type: integer | ||
title: | ||
type: string | ||
required: | ||
- size | ||
status: | ||
type: object | ||
properties: | ||
backendImage: | ||
type: string | ||
frontendImage: | ||
type: string | ||
version: v1 | ||
versions: | ||
- name: v1 | ||
served: true | ||
storage: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: visitors-operator | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
name: visitors-operator | ||
template: | ||
metadata: | ||
labels: | ||
name: visitors-operator | ||
spec: | ||
serviceAccountName: visitors-operator | ||
containers: | ||
- name: visitors-operator | ||
# Replace this with the built image name | ||
image: REPLACE_IMAGE | ||
command: | ||
- visitors-operator | ||
imagePullPolicy: Always | ||
env: | ||
- name: WATCH_NAMESPACE | ||
valueFrom: | ||
fieldRef: | ||
fieldPath: metadata.namespace | ||
- name: POD_NAME | ||
valueFrom: | ||
fieldRef: | ||
fieldPath: metadata.name | ||
- name: OPERATOR_NAME | ||
value: "visitors-operator" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: Role | ||
metadata: | ||
creationTimestamp: null | ||
name: visitors-operator | ||
rules: | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- pods | ||
- services | ||
- endpoints | ||
- persistentvolumeclaims | ||
- events | ||
- configmaps | ||
- secrets | ||
verbs: | ||
- '*' | ||
- apiGroups: | ||
- apps | ||
resources: | ||
- deployments | ||
- daemonsets | ||
- replicasets | ||
- statefulsets | ||
verbs: | ||
- '*' | ||
- apiGroups: | ||
- monitoring.coreos.com | ||
resources: | ||
- servicemonitors | ||
verbs: | ||
- get | ||
- create | ||
- apiGroups: | ||
- apps | ||
resourceNames: | ||
- visitors-operator | ||
resources: | ||
- deployments/finalizers | ||
verbs: | ||
- update | ||
- apiGroups: | ||
- example.com | ||
resources: | ||
- '*' | ||
verbs: | ||
- '*' |
Oops, something went wrong.