Skip to content

Commit d40d48e

Browse files
authored
updates based on SDK's helm-operator 1.0.0 (#59)
1 parent 12b55ea commit d40d48e

File tree

14 files changed

+250
-208
lines changed

14 files changed

+250
-208
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ RUN go mod download
1212
# Copy the go source
1313
COPY main.go main.go
1414
COPY pkg/ pkg/
15-
COPY version/ version/
15+
COPY internal/ internal/
1616

1717
# Build
1818
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o helm-operator main.go

Makefile

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,30 @@ else
1010
GOBIN=$(shell go env GOBIN)
1111
endif
1212

13+
# GO_BUILD_ARGS should be set when running 'go build' or 'go install'.
14+
REPO = $(shell go list -m)
15+
VERSION = $(shell git describe --dirty --tags --always)
16+
GIT_COMMIT = $(shell git rev-parse HEAD)
17+
GO_BUILD_ARGS = \
18+
-gcflags "all=-trimpath=$(shell dirname $(shell pwd))" \
19+
-asmflags "all=-trimpath=$(shell dirname $(shell pwd))" \
20+
-ldflags " \
21+
-X '$(REPO)/internal/version.Version=$(VERSION)' \
22+
-X '$(REPO)/internal/version.GitCommit=$(GIT_COMMIT)' \
23+
" \
24+
1325
all: manager
1426

1527
# Run tests
16-
test: fmt vet testbin
17-
go test -race -covermode atomic -coverprofile cover.out ./...
28+
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
29+
test: fmt vet
30+
mkdir -p ${ENVTEST_ASSETS_DIR}
31+
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh
32+
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test -race -covermode atomic -coverprofile cover.out ./...
1833

1934
# Build manager binary
2035
build: fmt vet
21-
go build -o bin/helm-operator main.go
36+
go build $(GO_BUILD_ARGS) -o bin/helm-operator main.go
2237

2338
# Run go fmt against code
2439
fmt:
@@ -32,7 +47,7 @@ lint: golangci-lint
3247
$(GOLANGCI_LINT) run
3348

3449
# Build the docker image
35-
docker-build: test
50+
docker-build:
3651
docker build . -t ${IMG}
3752

3853
# Push the docker image
@@ -51,14 +66,3 @@ GOLANGCI_LINT=$(shell go env GOPATH)/bin/golangci-lint
5166
else
5267
GOLANGCI_LINT=$(shell which golangci-lint)
5368
endif
54-
55-
K8S_VER ?= v1.18.2
56-
ETCD_VER ?= v3.4.3
57-
OS=$(shell uname -s | tr '[:upper:]' '[:lower:]')
58-
ARCH=$(shell uname -m | sed 's/x86_64/amd64/')
59-
60-
.PHONY: testbin
61-
testbin:
62-
mkdir -p testbin
63-
[[ -x testbin/etcd ]] || curl -L https://storage.googleapis.com/etcd/${ETCD_VER}/etcd-${ETCD_VER}-${OS}-${ARCH}.tar.gz | tar zx -C testbin --strip-components=1 etcd-${ETCD_VER}-${OS}-${ARCH}/etcd
64-
[[ -x testbin/kube-apiserver && -x testbin/kubectl ]] || curl -L https://dl.k8s.io/${K8S_VER}/kubernetes-server-${OS}-${ARCH}.tar.gz | tar zx -C testbin --strip-components=3 kubernetes/server/bin/kube-apiserver kubernetes/server/bin/kubectl

example/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ endif
1313

1414
# Run against the configured Kubernetes cluster in ~/.kube/config
1515
run:
16-
go run ../main.go
16+
go run ../main.go run
1717

1818
# Install CRDs into a cluster
1919
install: kustomize

example/config/samples/helm_v1_nginx.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ kind: Nginx
33
metadata:
44
name: nginx-sample
55
annotations:
6-
helm.operator-sdk/upgrade-force: "true"
7-
helm.operator-sdk/install-disable-hooks: "true"
8-
helm.operator-sdk/upgrade-disable-hooks: "true"
9-
helm.operator-sdk/uninstall-disable-hooks: "true"
6+
helm.sdk.operatorframework.io/upgrade-force: "true"
7+
helm.sdk.operatorframework.io/install-disable-hooks: "true"
8+
helm.sdk.operatorframework.io/upgrade-disable-hooks: "true"
9+
helm.sdk.operatorframework.io/uninstall-disable-hooks: "true"
1010
spec:
1111
replicaCount: 1
1212
image:

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/onsi/ginkgo v1.12.1
88
github.com/onsi/gomega v1.10.1
99
github.com/operator-framework/operator-lib v0.1.0
10+
github.com/spf13/cobra v1.0.0
1011
github.com/spf13/pflag v1.0.5
1112
github.com/stretchr/testify v1.5.1
1213
go.uber.org/zap v1.13.0

internal/cmd/run/cmd.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
Copyright 2020 The Operator-SDK Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package run
18+
19+
import (
20+
"flag"
21+
"os"
22+
"runtime"
23+
"time"
24+
25+
"github.com/spf13/cobra"
26+
"github.com/spf13/pflag"
27+
ctrl "sigs.k8s.io/controller-runtime"
28+
logf "sigs.k8s.io/controller-runtime/pkg/log"
29+
zapl "sigs.k8s.io/controller-runtime/pkg/log/zap"
30+
31+
"github.com/joelanford/helm-operator/internal/version"
32+
"github.com/joelanford/helm-operator/pkg/annotation"
33+
"github.com/joelanford/helm-operator/pkg/manager"
34+
"github.com/joelanford/helm-operator/pkg/reconciler"
35+
"github.com/joelanford/helm-operator/pkg/watches"
36+
)
37+
38+
func NewCmd() *cobra.Command {
39+
r := run{}
40+
zapfs := flag.NewFlagSet("zap", flag.ExitOnError)
41+
opts := &zapl.Options{}
42+
opts.BindFlags(zapfs)
43+
44+
cmd := &cobra.Command{
45+
Use: "run",
46+
Short: "Run the operator",
47+
Run: func(cmd *cobra.Command, _ []string) {
48+
logf.SetLogger(zapl.New(zapl.UseFlagOptions(opts)))
49+
r.run(cmd)
50+
},
51+
}
52+
r.bindFlags(cmd.Flags())
53+
cmd.Flags().AddGoFlagSet(zapfs)
54+
return cmd
55+
}
56+
57+
type run struct {
58+
metricsAddr string
59+
enableLeaderElection bool
60+
leaderElectionID string
61+
leaderElectionNamespace string
62+
63+
watchesFile string
64+
defaultMaxConcurrentReconciles int
65+
defaultReconcilePeriod time.Duration
66+
}
67+
68+
func (r *run) bindFlags(fs *pflag.FlagSet) {
69+
fs.StringVar(&r.metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
70+
fs.BoolVar(&r.enableLeaderElection, "enable-leader-election", false,
71+
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
72+
fs.StringVar(&r.leaderElectionID, "leader-election-id", "",
73+
"Name of the configmap that is used for holding the leader lock.")
74+
fs.StringVar(&r.leaderElectionNamespace, "leader-election-namespace", "",
75+
"Namespace in which to create the leader election configmap for holding the leader lock (required if running locally with leader election enabled).")
76+
77+
fs.StringVar(&r.watchesFile, "watches-file", "./watches.yaml", "Path to watches.yaml file.")
78+
fs.DurationVar(&r.defaultReconcilePeriod, "reconcile-period", time.Minute, "Default reconcile period for controllers (use 0 to disable periodic reconciliation)")
79+
fs.IntVar(&r.defaultMaxConcurrentReconciles, "max-concurrent-reconciles", runtime.NumCPU(), "Default maximum number of concurrent reconciles for controllers.")
80+
}
81+
82+
var log = logf.Log.WithName("cmd")
83+
84+
func printVersion() {
85+
log.Info("Version",
86+
"Go Version", runtime.Version(),
87+
"GOOS", runtime.GOOS,
88+
"GOARCH", runtime.GOARCH,
89+
"helm-operator", version.Version)
90+
}
91+
92+
func (r *run) run(cmd *cobra.Command) {
93+
printVersion()
94+
95+
// Deprecated: OPERATOR_NAME environment variable is an artifact of the legacy operator-sdk project scaffolding.
96+
// Flag `--leader-election-id` should be used instead.
97+
if operatorName, found := os.LookupEnv("OPERATOR_NAME"); found {
98+
log.Info("environment variable OPERATOR_NAME has been deprecated, use --leader-election-id instead.")
99+
if cmd.Flags().Lookup("leader-election-id").Changed {
100+
log.Info("ignoring OPERATOR_NAME environment variable since --leader-election-id is set")
101+
} else {
102+
r.leaderElectionID = operatorName
103+
}
104+
}
105+
106+
options := ctrl.Options{
107+
MetricsBindAddress: r.metricsAddr,
108+
LeaderElection: r.enableLeaderElection,
109+
LeaderElectionID: r.leaderElectionID,
110+
LeaderElectionNamespace: r.leaderElectionNamespace,
111+
NewClient: manager.NewDelegatingClientFunc(),
112+
}
113+
manager.ConfigureWatchNamespaces(&options, log)
114+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
115+
if err != nil {
116+
log.Error(err, "unable to start manager")
117+
os.Exit(1)
118+
}
119+
120+
ws, err := watches.Load(r.watchesFile)
121+
if err != nil {
122+
log.Error(err, "unable to load watches.yaml", "path", r.watchesFile)
123+
os.Exit(1)
124+
}
125+
126+
for _, w := range ws {
127+
reconcilePeriod := r.defaultReconcilePeriod
128+
if w.ReconcilePeriod != nil {
129+
reconcilePeriod = w.ReconcilePeriod.Duration
130+
}
131+
132+
maxConcurrentReconciles := r.defaultMaxConcurrentReconciles
133+
if w.MaxConcurrentReconciles != nil {
134+
maxConcurrentReconciles = *w.MaxConcurrentReconciles
135+
}
136+
137+
r, err := reconciler.New(
138+
reconciler.WithChart(*w.Chart),
139+
reconciler.WithGroupVersionKind(w.GroupVersionKind),
140+
reconciler.WithOverrideValues(w.OverrideValues),
141+
reconciler.SkipDependentWatches(w.WatchDependentResources != nil && !*w.WatchDependentResources),
142+
reconciler.WithMaxConcurrentReconciles(maxConcurrentReconciles),
143+
reconciler.WithReconcilePeriod(reconcilePeriod),
144+
reconciler.WithInstallAnnotations(annotation.DefaultInstallAnnotations...),
145+
reconciler.WithUpgradeAnnotations(annotation.DefaultUpgradeAnnotations...),
146+
reconciler.WithUninstallAnnotations(annotation.DefaultUninstallAnnotations...),
147+
)
148+
if err != nil {
149+
log.Error(err, "unable to create helm reconciler", "controller", "Helm")
150+
os.Exit(1)
151+
}
152+
153+
if err := r.SetupWithManager(mgr); err != nil {
154+
log.Error(err, "unable to create controller", "controller", "Helm")
155+
os.Exit(1)
156+
}
157+
log.Info("configured watch", "gvk", w.GroupVersionKind, "chartPath", w.ChartPath, "maxConcurrentReconciles", maxConcurrentReconciles, "reconcilePeriod", reconcilePeriod)
158+
}
159+
160+
log.Info("starting manager")
161+
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
162+
log.Error(err, "problem running manager")
163+
os.Exit(1)
164+
}
165+
}

internal/cmd/version/cmd.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright 2020 The Operator-SDK Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package version
18+
19+
import (
20+
"fmt"
21+
"runtime"
22+
23+
"github.com/spf13/cobra"
24+
25+
"github.com/joelanford/helm-operator/internal/version"
26+
)
27+
28+
func NewCmd() *cobra.Command {
29+
versionCmd := &cobra.Command{
30+
Use: "version",
31+
Short: "Prints the version of helm-operator",
32+
Run: func(cmd *cobra.Command, args []string) {
33+
run()
34+
},
35+
}
36+
return versionCmd
37+
}
38+
39+
func run() {
40+
fmt.Printf("helm-operator version: %q, commit: %q, go version: %q, GOOS: %q, GOARCH: %q\n",
41+
version.Version, version.GitCommit, runtime.Version(), runtime.GOOS, runtime.GOARCH)
42+
}

version/version.go renamed to internal/version/version.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
package version
1818

19-
const (
20-
Version = "0.0.0+git"
19+
var (
20+
Version = "unknown"
21+
GitCommit = "unknown"
2122
)

0 commit comments

Comments
 (0)