Skip to content

Commit b53260d

Browse files
authored
Convert Agent back to an operator (#60)
* add controller manager * fix helm charts * fix imports * revert rbacs
1 parent 2dc9002 commit b53260d

File tree

5 files changed

+220
-146
lines changed

5 files changed

+220
-146
lines changed

charts/deployment-operator/templates/deployment.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,19 @@ spec:
3838
args:
3939
- --console-url
4040
- "{{ .Values.consoleUrl }}"
41+
- --leader-elect
42+
- "true"
4143
ports:
4244
- name: http
4345
containerPort: {{ .Values.service.port }}
4446
protocol: TCP
4547
livenessProbe:
4648
httpGet:
47-
path: /v1/health
49+
path: /healthz
4850
port: http
4951
readinessProbe:
5052
httpGet:
51-
path: /v1/health
53+
path: /healthz
5254
port: http
5355
resources:
5456
{{- toYaml .Values.resources | nindent 12 }}

cmd/main.go

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,87 @@
11
package main
22

33
import (
4-
"fmt"
5-
"net/http"
4+
"flag"
65
"os"
76
"time"
87

9-
"github.com/go-logr/logr"
108
"github.com/pluralsh/deployment-operator/pkg/agent"
11-
"github.com/pluralsh/deployment-operator/pkg/sync"
12-
"github.com/spf13/cobra"
13-
"k8s.io/client-go/tools/clientcmd"
14-
"k8s.io/klog/v2/klogr"
9+
"github.com/pluralsh/deployment-operator/pkg/log"
10+
"k8s.io/apimachinery/pkg/runtime"
11+
ctrl "sigs.k8s.io/controller-runtime"
12+
"sigs.k8s.io/controller-runtime/pkg/healthz"
13+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
1514
)
1615

17-
func main() {
18-
log := klogr.New() // Delegates to klog
19-
err := newCmd(log).Execute()
20-
checkError(err, log)
21-
}
22-
23-
func newCmd(log logr.Logger) *cobra.Command {
24-
var (
25-
clientConfig clientcmd.ClientConfig
26-
refreshInterval string
27-
resyncSeconds int
28-
port int
29-
consoleUrl string
30-
deployToken string
31-
)
32-
cmd := cobra.Command{
33-
Use: "deployment-agent",
34-
Run: func(cmd *cobra.Command, args []string) {
35-
http.HandleFunc("/v1/health", func(w http.ResponseWriter, request *http.Request) {
36-
log.Info("health check")
37-
w.WriteHeader(http.StatusOK)
38-
_, _ = w.Write([]byte("ping"))
39-
})
16+
var (
17+
scheme = runtime.NewScheme()
18+
setupLog = log.Logger
19+
)
4020

41-
go func() {
42-
checkError(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil), log)
43-
}()
21+
func main() {
22+
var enableLeaderElection bool
23+
var metricsAddr string
24+
var probeAddr string
25+
var refreshInterval string
26+
var resyncSeconds int
27+
var consoleUrl string
28+
var deployToken string
4429

45-
if deployToken == "" {
46-
deployToken = os.Getenv("DEPLOY_TOKEN")
47-
}
30+
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
31+
flag.StringVar(&probeAddr, "health-probe-bind-address", ":9001", "The address the probe endpoint binds to.")
32+
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
33+
"Enable leader election for controller manager. "+
34+
"Enabling this will ensure there is only one active controller manager.")
35+
flag.IntVar(&resyncSeconds, "resync-seconds", 300, "Resync duration in seconds.")
36+
flag.StringVar(&refreshInterval, "refresh-interval", "1m", "Refresh interval duration")
37+
flag.StringVar(&consoleUrl, "console-url", "", "the url of the console api to fetch services from")
38+
flag.StringVar(&deployToken, "deploy-token", "", "the deploy token to auth to console api with")
39+
opts := zap.Options{
40+
Development: true,
41+
}
42+
opts.BindFlags(flag.CommandLine)
43+
flag.Parse()
4844

49-
refresh, err := time.ParseDuration(refreshInterval)
50-
checkError(err, log)
45+
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
5146

52-
a, err := agent.New(clientConfig, refresh, consoleUrl, deployToken)
53-
checkError(err, log)
54-
a.Run()
55-
},
47+
if deployToken == "" {
48+
deployToken = os.Getenv("DEPLOY_TOKEN")
49+
}
50+
refresh, err := time.ParseDuration(refreshInterval)
51+
if err != nil {
52+
setupLog.Error(err, "unable to get refresh interval")
53+
os.Exit(1)
5654
}
57-
clientConfig = addKubectlFlagsToCmd(&cmd)
58-
cmd.Flags().IntVar(&resyncSeconds, "resync-seconds", 300, "Resync duration in seconds.")
59-
cmd.Flags().IntVar(&port, "port", 9001, "Port number.")
60-
cmd.Flags().StringVar(&refreshInterval, "refresh-interval", "1m", "Refresh interval duration")
61-
cmd.Flags().StringVar(&consoleUrl, "console-url", "", "the url of the console api to fetch services from")
62-
cmd.Flags().StringVar(&deployToken, "deploy-token", "", "the deploy token to auth to console api with")
63-
cmd.Flags().BoolVar(&sync.Local, "local", false, "whether you're running the agent locally (and should avoid recreating the deploy operator)")
64-
return &cmd
65-
}
6655

67-
// addKubectlFlagsToCmd adds kubectl like flags to a command and returns the ClientConfig interface
68-
// for retrieving the values.
69-
func addKubectlFlagsToCmd(cmd *cobra.Command) clientcmd.ClientConfig {
70-
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
71-
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
72-
overrides := clientcmd.ConfigOverrides{}
73-
kflags := clientcmd.RecommendedConfigOverrideFlags("")
74-
cmd.PersistentFlags().StringVar(&loadingRules.ExplicitPath, "kubeconfig", "", "Path to a kube config. Only required if out-of-cluster")
75-
clientcmd.BindOverrideFlags(&overrides, cmd.PersistentFlags(), kflags)
76-
return clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, &overrides, os.Stdin)
77-
}
56+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
57+
Scheme: scheme,
58+
LeaderElection: enableLeaderElection,
59+
LeaderElectionID: "dep12loy45.plural.sh",
60+
HealthProbeBindAddress: probeAddr,
61+
})
62+
if err != nil {
63+
setupLog.Error(err, "unable to create manager")
64+
os.Exit(1)
65+
}
66+
if err = mgr.AddHealthzCheck("ping", healthz.Ping); err != nil {
67+
setupLog.Error(err, "unable to create health check")
68+
os.Exit(1)
69+
}
7870

79-
// checkError is a convenience function to check if an error is non-nil and exit if it was
80-
func checkError(err error, log logr.Logger) {
71+
a, err := agent.New(mgr.GetConfig(), refresh, consoleUrl, deployToken)
8172
if err != nil {
82-
log.Error(err, "Fatal error")
73+
setupLog.Error(err, "unable to create agent")
74+
os.Exit(1)
75+
}
76+
if err := a.SetupWithManager(); err != nil {
77+
setupLog.Error(err, "unable to start agent")
78+
os.Exit(1)
79+
}
80+
81+
ctx := ctrl.SetupSignalHandler()
82+
setupLog.Info("starting manager")
83+
if err := mgr.Start(ctx); err != nil {
84+
setupLog.Error(err, "problem running manager")
8385
os.Exit(1)
8486
}
8587
}

go.mod

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ replace (
2020
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.24.2
2121
k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.24.2
2222
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.24.2
23+
k8s.io/kube-openapi => k8s.io/kube-openapi v0.0.0-20230525220651-2546d827e515
2324
k8s.io/kube-proxy => k8s.io/kube-proxy v0.24.2
2425
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.24.2
2526
k8s.io/kubectl => k8s.io/kubectl v0.24.2
@@ -29,66 +30,71 @@ replace (
2930
k8s.io/mount-utils => k8s.io/mount-utils v0.24.2
3031
k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.24.2
3132
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.24.2
33+
sigs.k8s.io/controller-runtime => sigs.k8s.io/controller-runtime v0.13.0
3234
)
3335

3436
require (
3537
github.com/Masterminds/sprig/v3 v3.2.3
3638
github.com/alitto/pond v1.8.3
3739
github.com/argoproj/gitops-engine v0.7.1-0.20230906152414-b0fffe419a0f
38-
github.com/go-logr/logr v1.2.4
3940
github.com/orcaman/concurrent-map/v2 v2.0.1
4041
github.com/osteele/liquid v1.3.1
4142
github.com/pluralsh/console-client-go v0.0.17
4243
github.com/pluralsh/polly v0.1.4
4344
github.com/samber/lo v1.38.1
44-
github.com/spf13/cobra v1.7.0
4545
github.com/spf13/pflag v1.0.5
4646
go.uber.org/zap v1.26.0
47-
k8s.io/apimachinery v0.24.2
48-
k8s.io/client-go v0.24.2
47+
k8s.io/apimachinery v0.28.1
48+
k8s.io/client-go v0.28.1
4949
k8s.io/klog/v2 v2.100.1
50+
sigs.k8s.io/controller-runtime v0.16.2
5051
)
5152

5253
require (
5354
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
5455
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd // indirect
5556
github.com/Masterminds/goutils v1.1.1 // indirect
5657
github.com/Masterminds/semver/v3 v3.2.0 // indirect
57-
github.com/PuerkitoBio/purell v1.1.1 // indirect
58-
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
5958
github.com/Yamashou/gqlgenc v0.14.0 // indirect
59+
github.com/beorn7/perks v1.0.1 // indirect
60+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
6061
github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5 // indirect
6162
github.com/davecgh/go-spew v1.1.1 // indirect
6263
github.com/docker/distribution v2.8.1+incompatible // indirect
63-
github.com/emicklei/go-restful v2.16.0+incompatible // indirect
64-
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
64+
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
65+
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
66+
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
6567
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
6668
github.com/fatih/camelcase v1.0.0 // indirect
6769
github.com/frankban/quicktest v1.14.4 // indirect
68-
github.com/fsnotify/fsnotify v1.5.4 // indirect
70+
github.com/fsnotify/fsnotify v1.6.0 // indirect
6971
github.com/fvbommel/sortorder v1.0.1 // indirect
7072
github.com/go-errors/errors v1.0.1 // indirect
71-
github.com/go-openapi/jsonpointer v0.19.5 // indirect
72-
github.com/go-openapi/jsonreference v0.19.5 // indirect
73-
github.com/go-openapi/swag v0.19.14 // indirect
73+
github.com/go-logr/logr v1.2.4 // indirect
74+
github.com/go-logr/zapr v1.2.4 // indirect
75+
github.com/go-openapi/jsonpointer v0.19.6 // indirect
76+
github.com/go-openapi/jsonreference v0.20.2 // indirect
77+
github.com/go-openapi/swag v0.22.3 // indirect
7478
github.com/gogo/protobuf v1.3.2 // indirect
79+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
7580
github.com/golang/protobuf v1.5.3 // indirect
7681
github.com/google/btree v1.0.1 // indirect
7782
github.com/google/gnostic v0.5.7-v3refs // indirect
7883
github.com/google/go-cmp v0.5.9 // indirect
79-
github.com/google/gofuzz v1.1.0 // indirect
84+
github.com/google/gofuzz v1.2.0 // indirect
8085
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
8186
github.com/google/uuid v1.3.0 // indirect
8287
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
8388
github.com/huandu/xstrings v1.3.3 // indirect
84-
github.com/imdario/mergo v0.3.11 // indirect
89+
github.com/imdario/mergo v0.3.12 // indirect
8590
github.com/inconshreveable/mousetrap v1.1.0 // indirect
8691
github.com/jonboulle/clockwork v0.2.2 // indirect
8792
github.com/josharian/intern v1.0.0 // indirect
8893
github.com/json-iterator/go v1.1.12 // indirect
8994
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
90-
github.com/mailru/easyjson v0.7.6 // indirect
95+
github.com/mailru/easyjson v0.7.7 // indirect
9196
github.com/mattn/go-runewidth v0.0.13 // indirect
97+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
9298
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
9399
github.com/mitchellh/copystructure v1.0.0 // indirect
94100
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
@@ -99,51 +105,55 @@ require (
99105
github.com/modern-go/reflect2 v1.0.2 // indirect
100106
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
101107
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
102-
github.com/onsi/gomega v1.27.10 // indirect
103108
github.com/opencontainers/go-digest v1.0.0 // indirect
104109
github.com/osteele/tuesday v1.0.3 // indirect
105110
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
106111
github.com/pkg/errors v0.9.1 // indirect
107112
github.com/pmezard/go-difflib v1.0.0 // indirect
113+
github.com/prometheus/client_golang v1.16.0 // indirect
114+
github.com/prometheus/client_model v0.4.0 // indirect
115+
github.com/prometheus/common v0.44.0 // indirect
116+
github.com/prometheus/procfs v0.10.1 // indirect
108117
github.com/rivo/uniseg v0.2.0 // indirect
109-
github.com/rogpeppe/go-internal v1.10.0 // indirect
110118
github.com/russross/blackfriday v1.5.2 // indirect
111119
github.com/schollz/progressbar/v3 v3.8.6 // indirect
112120
github.com/shopspring/decimal v1.2.0 // indirect
113121
github.com/spf13/cast v1.5.0 // indirect
122+
github.com/spf13/cobra v1.7.0 // indirect
114123
github.com/stretchr/testify v1.8.4 // indirect
115124
github.com/vektah/gqlparser/v2 v2.5.1 // indirect
116125
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
117126
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
118-
go.uber.org/multierr v1.10.0 // indirect
127+
go.uber.org/multierr v1.11.0 // indirect
119128
golang.org/x/crypto v0.12.0 // indirect
120129
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
121130
golang.org/x/net v0.14.0 // indirect
122-
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
131+
golang.org/x/oauth2 v0.8.0 // indirect
123132
golang.org/x/sync v0.3.0 // indirect
124133
golang.org/x/sys v0.11.0 // indirect
125134
golang.org/x/term v0.11.0 // indirect
126135
golang.org/x/text v0.12.0 // indirect
127-
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
136+
golang.org/x/time v0.3.0 // indirect
137+
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
128138
google.golang.org/appengine v1.6.7 // indirect
129-
google.golang.org/protobuf v1.28.0 // indirect
139+
google.golang.org/protobuf v1.30.0 // indirect
130140
gopkg.in/inf.v0 v0.9.1 // indirect
131141
gopkg.in/yaml.v2 v2.4.0 // indirect
132142
gopkg.in/yaml.v3 v3.0.1 // indirect
133-
k8s.io/api v0.24.2 // indirect
134-
k8s.io/apiextensions-apiserver v0.24.2 // indirect
135-
k8s.io/apiserver v0.24.2 // indirect
143+
k8s.io/api v0.28.1 // indirect
144+
k8s.io/apiextensions-apiserver v0.28.0 // indirect
145+
k8s.io/apiserver v0.28.1 // indirect
136146
k8s.io/cli-runtime v0.24.2 // indirect
137-
k8s.io/component-base v0.24.2 // indirect
147+
k8s.io/component-base v0.28.1 // indirect
138148
k8s.io/component-helpers v0.24.2 // indirect
139149
k8s.io/kube-aggregator v0.24.2 // indirect
140-
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
150+
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
141151
k8s.io/kubectl v0.24.2 // indirect
142152
k8s.io/kubernetes v1.24.2 // indirect
143-
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
144-
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
153+
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
154+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
145155
sigs.k8s.io/kustomize/api v0.11.4 // indirect
146156
sigs.k8s.io/kustomize/kyaml v0.13.6 // indirect
147-
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
157+
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
148158
sigs.k8s.io/yaml v1.3.0 // indirect
149159
)

0 commit comments

Comments
 (0)