Skip to content

Commit 3eebcf1

Browse files
authored
Merge pull request #12 from bpineau/move_observer
Move observer to his own dedicated package
2 parents ef3ab0b + 6ac820e commit 3eebcf1

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ lint:
1616
--enable=structcheck \
1717
--enable=deadcode \
1818
--enable=ineffassign \
19+
--enable=dupl \
1920
--enable=gotype \
2021
--enable=varcheck \
2122
--enable=interfacer \

pkg/controller/controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ type Controller struct {
5252
informer cache.SharedIndexInformer
5353
}
5454

55-
// NewController return an untyped, generic Kubernetes controller
56-
func NewController(lw cache.ListerWatcher, evchan chan Event, name string, config *config.KfConfig) *Controller {
55+
// New return an untyped, generic Kubernetes controller
56+
func New(lw cache.ListerWatcher, evchan chan Event, name string, config *config.KfConfig) *Controller {
5757
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
5858

5959
informer := cache.NewSharedIndexInformer(

pkg/controller/observer.go renamed to pkg/observer/observer.go

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
package controller
2-
3-
// An observer polls the Kubernetes api-server to discover all supported
1+
// Package observer polls the Kubernetes api-server to discover all supported
42
// API groups/object kinds, and launch a new controller for each of them.
53
// Due to CRD/TPR, new API groups / object kinds may appear at any time,
64
// that's why we keep polling the API server.
5+
package observer
76

87
import (
98
"fmt"
109
"strings"
1110
"time"
1211

1312
"github.com/bpineau/katafygio/config"
13+
"github.com/bpineau/katafygio/pkg/controller"
1414

1515
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1616
"k8s.io/apimachinery/pkg/runtime"
@@ -27,10 +27,10 @@ const discoveryInterval = 60 * time.Second
2727
type Observer struct {
2828
stop chan struct{}
2929
done chan struct{}
30-
evch chan Event
30+
evch chan controller.Event
3131
disc *discovery.DiscoveryClient
3232
cpool dynamic.ClientPool
33-
ctrls map[string]*Controller
33+
ctrls map[string]*controller.Controller
3434
config *config.KfConfig
3535
}
3636

@@ -44,15 +44,15 @@ type gvk struct {
4444

4545
type resources map[string]*gvk
4646

47-
// NewObserver returns a new observer, that will watch for api resource kinds
47+
// New returns a new observer, that will watch for api resource kinds
4848
// and create new controllers for each one.
49-
func NewObserver(config *config.KfConfig, evch chan Event) *Observer {
49+
func New(config *config.KfConfig, evch chan controller.Event) *Observer {
5050
return &Observer{
5151
config: config,
5252
evch: evch,
5353
disc: discovery.NewDiscoveryClientForConfigOrDie(config.Client),
5454
cpool: dynamic.NewDynamicClientPool(config.Client),
55-
ctrls: make(map[string]*Controller),
55+
ctrls: make(map[string]*controller.Controller),
5656
}
5757
}
5858

@@ -126,7 +126,7 @@ func (c *Observer) refresh() error {
126126
},
127127
}
128128

129-
c.ctrls[name] = NewController(lw, c.evch, strings.ToLower(res.ar.Kind), c.config)
129+
c.ctrls[name] = controller.New(lw, c.evch, strings.ToLower(res.ar.Kind), c.config)
130130
go c.ctrls[name].Start()
131131
}
132132

@@ -146,9 +146,13 @@ func (c *Observer) expandAndFilterAPIResources(groups []*metav1.APIResourceList)
146146
resources := make(map[string]*gvk)
147147

148148
for _, group := range groups {
149-
gv, _ := schema.ParseGroupVersion(group.GroupVersion)
150-
for _, ar := range group.APIResources {
149+
gv, err := schema.ParseGroupVersion(group.GroupVersion)
150+
if err != nil {
151+
c.config.Logger.Errorf("api-server sent an unparsable group version: %v", err)
152+
continue
153+
}
151154

155+
for _, ar := range group.APIResources {
152156
// remove subresources (like job/status or deployments/scale)
153157
if strings.ContainsRune(ar.Name, '/') {
154158
continue

pkg/run/run.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/bpineau/katafygio/config"
1111
"github.com/bpineau/katafygio/pkg/controller"
1212
"github.com/bpineau/katafygio/pkg/health"
13+
"github.com/bpineau/katafygio/pkg/observer"
1314
"github.com/bpineau/katafygio/pkg/recorder"
1415
"github.com/bpineau/katafygio/pkg/store/git"
1516
)
@@ -24,7 +25,7 @@ func Run(config *config.KfConfig) {
2425
evchan := make(chan controller.Event)
2526

2627
reco := recorder.New(config, evchan).Start()
27-
ctrl := controller.NewObserver(config, evchan).Start()
28+
ctrl := observer.New(config, evchan).Start()
2829

2930
http, err := health.New(config).Start()
3031
if err != nil {

0 commit comments

Comments
 (0)