Skip to content

Commit 2f9383b

Browse files
author
Shawn Hurley
authored
Merge pull request #634 from mhrivnak/move-startup-code
moves controller creation and startup out of ansible's main.go
2 parents 956ccda + ab3cccd commit 2f9383b

File tree

4 files changed

+21
-41
lines changed

4 files changed

+21
-41
lines changed

commands/operator-sdk/cmd/up/local.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,17 @@ func upLocalAnsible() {
132132
done := make(chan error)
133133

134134
// start the proxy
135-
proxy.RunProxy(done, proxy.Options{
135+
err = proxy.Run(done, proxy.Options{
136136
Address: "localhost",
137137
Port: 8888,
138138
KubeConfig: mgr.GetConfig(),
139139
})
140+
if err != nil {
141+
logrus.Fatalf("error starting proxy: %v", err)
142+
}
140143

141144
// start the operator
142-
go ansibleOperator.Run(done, mgr)
145+
go ansibleOperator.Run(done, mgr, "./watches.yaml")
143146

144147
// wait for either to finish
145148
err = <-done

pkg/ansible/operator/operator.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package operator
1616

1717
import (
18-
"log"
1918
"math/rand"
2019
"time"
2120

@@ -30,8 +29,8 @@ import (
3029
// Run - A blocking function which starts a controller-runtime manager
3130
// It starts an Operator by reading in the values in `./watches.yaml`, adds a controller
3231
// to the manager, and finally running the manager.
33-
func Run(done chan error, mgr manager.Manager) {
34-
watches, err := runner.NewFromWatches("./watches.yaml")
32+
func Run(done chan error, mgr manager.Manager, watchesPath string) {
33+
watches, err := runner.NewFromWatches(watchesPath)
3534
if err != nil {
3635
logrus.Error("Failed to get watches")
3736
done <- err
@@ -46,6 +45,5 @@ func Run(done chan error, mgr manager.Manager) {
4645
Runner: runner,
4746
})
4847
}
49-
log.Fatal(mgr.Start(c))
50-
done <- nil
48+
done <- mgr.Start(c)
5149
}

pkg/ansible/proxy/proxy.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ type Options struct {
108108
KubeConfig *rest.Config
109109
}
110110

111-
// RunProxy will start a proxy server in a go routine and return on the error
112-
// channel if something is not correct on startup.
113-
func RunProxy(done chan error, o Options) {
111+
// Run will start a proxy server in a go routine that returns on the error
112+
// channel if something is not correct on startup. Run will not return until
113+
// the network socket is listening.
114+
func Run(done chan error, o Options) error {
114115
server, err := newServer("/", o.KubeConfig)
115116
if err != nil {
116-
done <- err
117-
return
117+
return err
118118
}
119119
if o.Handler != nil {
120120
server.Handler = o.Handler(server.Handler)
@@ -125,11 +125,11 @@ func RunProxy(done chan error, o Options) {
125125
}
126126
l, err := server.Listen(o.Address, o.Port)
127127
if err != nil {
128-
done <- err
129-
return
128+
return err
130129
}
131130
go func() {
132131
logrus.Infof("Starting to serve on %s\n", l.Addr().String())
133132
done <- server.ServeOnListener(l)
134133
}()
134+
return nil
135135
}

test/ansible-operator/cmd/ansible-operator/main.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,15 @@ package main
1717
import (
1818
"flag"
1919
"log"
20-
"math/rand"
2120
"runtime"
22-
"time"
2321

24-
"github.com/operator-framework/operator-sdk/pkg/ansible/controller"
22+
"github.com/operator-framework/operator-sdk/pkg/ansible/operator"
2523
proxy "github.com/operator-framework/operator-sdk/pkg/ansible/proxy"
26-
"github.com/operator-framework/operator-sdk/pkg/ansible/runner"
2724
"github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
2825
sdkVersion "github.com/operator-framework/operator-sdk/version"
2926
"sigs.k8s.io/controller-runtime/pkg/client/config"
3027
"sigs.k8s.io/controller-runtime/pkg/manager"
3128
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
32-
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
3329

3430
"github.com/sirupsen/logrus"
3531
)
@@ -60,14 +56,17 @@ func main() {
6056
done := make(chan error)
6157

6258
// start the proxy
63-
proxy.RunProxy(done, proxy.Options{
59+
err = proxy.Run(done, proxy.Options{
6460
Address: "localhost",
6561
Port: 8888,
6662
KubeConfig: mgr.GetConfig(),
6763
})
64+
if err != nil {
65+
logrus.Fatalf("error starting proxy: %v", err)
66+
}
6867

6968
// start the operator
70-
go runSDK(done, mgr)
69+
go operator.Run(done, mgr, "/opt/ansible/watches.yaml")
7170

7271
// wait for either to finish
7372
err = <-done
@@ -77,23 +76,3 @@ func main() {
7776
logrus.Fatal(err.Error())
7877
}
7978
}
80-
81-
func runSDK(done chan error, mgr manager.Manager) {
82-
watches, err := runner.NewFromWatches("/opt/ansible/watches.yaml")
83-
if err != nil {
84-
logrus.Error("Failed to get watches")
85-
done <- err
86-
return
87-
}
88-
rand.Seed(time.Now().Unix())
89-
c := signals.SetupSignalHandler()
90-
91-
for gvk, runner := range watches {
92-
controller.Add(mgr, controller.Options{
93-
GVK: gvk,
94-
Runner: runner,
95-
})
96-
}
97-
log.Fatal(mgr.Start(c))
98-
done <- nil
99-
}

0 commit comments

Comments
 (0)