-
Notifications
You must be signed in to change notification settings - Fork 20
/
podghost_controller.go
136 lines (115 loc) · 4.56 KB
/
podghost_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Copyright 2018 The Multicluster-Controller Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package podghost // import "admiralty.io/multicluster-controller/examples/podghost/pkg/controller/podghost"
import (
"context"
"fmt"
"reflect"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"admiralty.io/multicluster-controller/examples/podghost/pkg/apis"
"admiralty.io/multicluster-controller/examples/podghost/pkg/apis/multicluster/v1alpha1"
"admiralty.io/multicluster-controller/pkg/cluster"
"admiralty.io/multicluster-controller/pkg/controller"
"admiralty.io/multicluster-controller/pkg/reconcile"
"admiralty.io/multicluster-controller/pkg/reference"
)
func NewController(ctx context.Context, live *cluster.Cluster, ghost *cluster.Cluster, ghostNamespace string) (*controller.Controller, error) {
liveclient, err := live.GetDelegatingClient()
if err != nil {
return nil, fmt.Errorf("getting delegating client for live cluster: %v", err)
}
ghostclient, err := ghost.GetDelegatingClient()
if err != nil {
return nil, fmt.Errorf("getting delegating client for ghost cluster: %v", err)
}
co := controller.New(&reconciler{live: liveclient, ghost: ghostclient, ghostNamespace: ghostNamespace}, controller.Options{})
if err := co.WatchResourceReconcileObject(ctx, live, &v1.Pod{}, controller.WatchOptions{}); err != nil {
return nil, fmt.Errorf("setting up Pod watch in live cluster: %v", err)
}
if err := apis.AddToScheme(ghost.GetScheme()); err != nil {
return nil, fmt.Errorf("adding APIs to ghost cluster's scheme: %v", err)
}
// Note: At the moment, all clusters share the same scheme under the hood
// (k8s.io/client-go/kubernetes/scheme.Scheme), yet multicluster-controller gives each cluster a scheme pointer.
// Therefore, if we needed a custom resource in multiple clusters, we would redundantly
// add it to each cluster's scheme, which points to the same underlying scheme.
if err := co.WatchResourceReconcileController(ctx, ghost, &v1alpha1.PodGhost{}, controller.WatchOptions{}); err != nil {
return nil, fmt.Errorf("setting up PodGhost watch in ghost cluster: %v", err)
}
return co, nil
}
type reconciler struct {
live client.Client
ghost client.Client
ghostNamespace string
}
func (r *reconciler) Reconcile(req reconcile.Request) (reconcile.Result, error) {
p := &v1.Pod{}
if err := r.live.Get(context.TODO(), req.NamespacedName, p); err != nil {
if errors.IsNotFound(err) {
// ...TODO: multicluster garbage collector
// Until then...
err := r.deleteGhost(r.ghostNamespacedName(req.NamespacedName))
return reconcile.Result{}, err
}
return reconcile.Result{}, err
}
dg := r.makeGhost(p)
reference.SetMulticlusterControllerReference(dg, reference.NewMulticlusterOwnerReference(p, p.GroupVersionKind(), req.Context))
og := &v1alpha1.PodGhost{}
if err := r.ghost.Get(context.TODO(), r.ghostNamespacedName(req.NamespacedName), og); err != nil {
if errors.IsNotFound(err) {
err := r.ghost.Create(context.TODO(), dg)
return reconcile.Result{}, err
}
return reconcile.Result{}, err
}
if reflect.DeepEqual(dg.Spec, og.Spec) {
return reconcile.Result{}, nil
}
og.Spec = dg.Spec
err := r.ghost.Update(context.TODO(), og)
return reconcile.Result{}, err
}
func (r *reconciler) ghostNamespacedName(pod types.NamespacedName) types.NamespacedName {
return types.NamespacedName{
Namespace: r.ghostNamespace,
Name: fmt.Sprintf("%s-%s", pod.Namespace, pod.Name),
}
}
func (r *reconciler) deleteGhost(nsn types.NamespacedName) error {
g := &v1alpha1.PodGhost{}
if err := r.ghost.Get(context.TODO(), nsn, g); err != nil {
if errors.IsNotFound(err) {
// all good
return nil
}
return err
}
if err := r.ghost.Delete(context.TODO(), g); err != nil {
return err
}
return nil
}
func (r *reconciler) makeGhost(pod *v1.Pod) *v1alpha1.PodGhost {
return &v1alpha1.PodGhost{
ObjectMeta: metav1.ObjectMeta{
Namespace: r.ghostNamespace,
Name: fmt.Sprintf("%s-%s", pod.Namespace, pod.Name),
},
}
}