Skip to content

Commit 217c9b8

Browse files
authored
Merge pull request #50 from pusher/deamonset-support
Add DaemonSet support
2 parents 16e2a2a + 266789a commit 217c9b8

File tree

11 files changed

+918
-0
lines changed

11 files changed

+918
-0
lines changed

config/rbac/manager_role.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,44 @@ metadata:
44
creationTimestamp: null
55
name: manager-role
66
rules:
7+
- apiGroups:
8+
- apps
9+
resources:
10+
- daemonsets
11+
verbs:
12+
- get
13+
- list
14+
- watch
15+
- update
16+
- patch
17+
- apiGroups:
18+
- ""
19+
resources:
20+
- configmaps
21+
verbs:
22+
- get
23+
- list
24+
- watch
25+
- update
26+
- patch
27+
- apiGroups:
28+
- ""
29+
resources:
30+
- secrets
31+
verbs:
32+
- get
33+
- list
34+
- watch
35+
- update
36+
- patch
37+
- apiGroups:
38+
- ""
39+
resources:
40+
- events
41+
verbs:
42+
- create
43+
- update
44+
- patch
745
- apiGroups:
846
- apps
947
resources:

pkg/controller/add_daemonset.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Copyright 2018 Pusher Ltd.
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 controller
18+
19+
import (
20+
"github.com/pusher/wave/pkg/controller/daemonset"
21+
)
22+
23+
func init() {
24+
// AddToManagerFuncs is a list of functions to create controllers and add them to a manager.
25+
AddToManagerFuncs = append(AddToManagerFuncs, daemonset.Add)
26+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Copyright 2018 Pusher Ltd.
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 daemonset
18+
19+
import (
20+
"context"
21+
22+
"github.com/pusher/wave/pkg/core"
23+
appsv1 "k8s.io/api/apps/v1"
24+
corev1 "k8s.io/api/core/v1"
25+
"k8s.io/apimachinery/pkg/api/errors"
26+
"k8s.io/apimachinery/pkg/runtime"
27+
"sigs.k8s.io/controller-runtime/pkg/controller"
28+
"sigs.k8s.io/controller-runtime/pkg/handler"
29+
"sigs.k8s.io/controller-runtime/pkg/manager"
30+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
31+
"sigs.k8s.io/controller-runtime/pkg/source"
32+
)
33+
34+
// Add creates a new DaemonSet Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
35+
// and Start it when the Manager is Started.
36+
func Add(mgr manager.Manager) error {
37+
return add(mgr, newReconciler(mgr))
38+
}
39+
40+
// newReconciler returns a new reconcile.Reconciler
41+
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
42+
return &ReconcileDaemonSet{
43+
scheme: mgr.GetScheme(),
44+
handler: core.NewHandler(mgr.GetClient(), mgr.GetEventRecorderFor("wave")),
45+
}
46+
}
47+
48+
// add adds a new Controller to mgr with r as the reconcile.Reconciler
49+
func add(mgr manager.Manager, r reconcile.Reconciler) error {
50+
// Create a new controller
51+
c, err := controller.New("daemonset-controller", mgr, controller.Options{Reconciler: r})
52+
if err != nil {
53+
return err
54+
}
55+
56+
// Watch for changes to DaemonSet
57+
err = c.Watch(&source.Kind{Type: &appsv1.DaemonSet{}}, &handler.EnqueueRequestForObject{})
58+
if err != nil {
59+
return err
60+
}
61+
62+
// Watch ConfigMaps owned by a DaemonSet
63+
err = c.Watch(&source.Kind{Type: &corev1.ConfigMap{}}, &handler.EnqueueRequestForOwner{
64+
IsController: false,
65+
OwnerType: &appsv1.DaemonSet{},
66+
})
67+
if err != nil {
68+
return err
69+
}
70+
71+
// Watch Secrets owned by a DaemonSet
72+
err = c.Watch(&source.Kind{Type: &corev1.Secret{}}, &handler.EnqueueRequestForOwner{
73+
IsController: false,
74+
OwnerType: &appsv1.DaemonSet{},
75+
})
76+
if err != nil {
77+
return err
78+
}
79+
80+
return nil
81+
}
82+
83+
var _ reconcile.Reconciler = &ReconcileDaemonSet{}
84+
85+
// ReconcileDaemonSet reconciles a DaemonSet object
86+
type ReconcileDaemonSet struct {
87+
scheme *runtime.Scheme
88+
handler *core.Handler
89+
}
90+
91+
// Reconcile reads that state of the cluster for a DaemonSet object and
92+
// updates its PodSpec based on mounted configuration
93+
// +kubebuilder:rbac:groups=apps,resources=daemonsets,verbs=get;list;watch;update;patch
94+
// +kubebuilder:rbac:groups=,resources=configmaps,verbs=get;list;watch;update;patch
95+
// +kubebuilder:rbac:groups=,resources=secrets,verbs=get;list;watch;update;patch
96+
// +kubebuilder:rbac:groups=,resources=events,verbs=create;update;patch
97+
func (r *ReconcileDaemonSet) Reconcile(request reconcile.Request) (reconcile.Result, error) {
98+
// Fetch the DaemonSet instance
99+
instance := &appsv1.DaemonSet{}
100+
err := r.handler.Get(context.TODO(), request.NamespacedName, instance)
101+
if err != nil {
102+
if errors.IsNotFound(err) {
103+
// Object not found, return. Created objects are automatically garbage collected.
104+
return reconcile.Result{}, nil
105+
}
106+
// Error reading the object - requeue the request.
107+
return reconcile.Result{}, err
108+
}
109+
110+
return r.handler.HandleDaemonSet(instance)
111+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Copyright 2018 Pusher Ltd.
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 daemonset
18+
19+
import (
20+
"log"
21+
"path/filepath"
22+
"sync"
23+
"testing"
24+
25+
"github.com/pusher/wave/test/reporters"
26+
27+
"github.com/go-logr/glogr"
28+
. "github.com/onsi/ginkgo"
29+
. "github.com/onsi/gomega"
30+
"github.com/pusher/wave/pkg/apis"
31+
"k8s.io/client-go/kubernetes/scheme"
32+
"k8s.io/client-go/rest"
33+
"sigs.k8s.io/controller-runtime/pkg/envtest"
34+
"sigs.k8s.io/controller-runtime/pkg/manager"
35+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
36+
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
37+
)
38+
39+
var cfg *rest.Config
40+
41+
func TestMain(t *testing.T) {
42+
RegisterFailHandler(Fail)
43+
RunSpecsWithDefaultAndCustomReporters(t, "Wave Controller Suite", reporters.Reporters())
44+
}
45+
46+
var t *envtest.Environment
47+
48+
var _ = BeforeSuite(func() {
49+
t = &envtest.Environment{
50+
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crds")},
51+
}
52+
apis.AddToScheme(scheme.Scheme)
53+
54+
logf.SetLogger(glogr.New())
55+
56+
var err error
57+
if cfg, err = t.Start(); err != nil {
58+
log.Fatal(err)
59+
}
60+
})
61+
62+
var _ = AfterSuite(func() {
63+
t.Stop()
64+
})
65+
66+
// SetupTestReconcile returns a reconcile.Reconcile implementation that delegates to inner and
67+
// writes the request to requests after Reconcile is finished.
68+
func SetupTestReconcile(inner reconcile.Reconciler) (reconcile.Reconciler, chan reconcile.Request) {
69+
requests := make(chan reconcile.Request)
70+
fn := reconcile.Func(func(req reconcile.Request) (reconcile.Result, error) {
71+
result, err := inner.Reconcile(req)
72+
requests <- req
73+
return result, err
74+
})
75+
return fn, requests
76+
}
77+
78+
// StartTestManager adds recFn
79+
func StartTestManager(mgr manager.Manager) (chan struct{}, *sync.WaitGroup) {
80+
stop := make(chan struct{})
81+
wg := &sync.WaitGroup{}
82+
go func() {
83+
defer GinkgoRecover()
84+
wg.Add(1)
85+
Expect(mgr.Start(stop)).NotTo(HaveOccurred())
86+
wg.Done()
87+
}()
88+
return stop, wg
89+
}

0 commit comments

Comments
 (0)