This repository has been archived by the owner on Mar 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
cpod_adapter.go
346 lines (299 loc) · 10.1 KB
/
cpod_adapter.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
Copyright 2018 Oracle and/or its affiliates. All rights reserved.
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 cpod
import (
"errors"
"github.com/golang/glog"
cloudv1alpha1 "github.com/oracle/oci-manager/pkg/apis/cloud.k8s.io/v1alpha1"
ocicommon "github.com/oracle/oci-manager/pkg/apis/ocicommon.oracle.com/v1alpha1"
ocicorev1alpha1 "github.com/oracle/oci-manager/pkg/apis/ocicore.oracle.com/v1alpha1"
"github.com/oracle/oci-manager/pkg/client/clientset/versioned"
cloudcommon "github.com/oracle/oci-manager/pkg/controller/oci/cloud/common"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"reflect"
)
type CpodAdapter struct {
subscribtions []schema.GroupVersionResource
clientset versioned.Interface
lister cache.GenericLister
queue workqueue.RateLimitingInterface
}
const (
DefaultShape string = "VM.Standard1.1"
DefaultCpodInstanceOsType string = "Oracle-Linux"
DefaultCpodInstanceOSVersion string = "7.5-2018.05.09-1"
DefaultCpodHostPort string = "80"
)
var controllerKind = cloudv1alpha1.SchemeGroupVersion.WithKind(cloudv1alpha1.CpodKind)
// init to register cpod cloud type
func init() {
cloudcommon.RegisterCloudType(
cloudv1alpha1.CpodResourcePlural,
cloudv1alpha1.CpodKind,
cloudv1alpha1.GroupName,
nil,
NewCpodAdapter,
)
}
// factory method
func NewCpodAdapter(clientSet versioned.Interface, kubeclient kubernetes.Interface) cloudcommon.CloudTypeAdapter {
na := CpodAdapter{
clientset: clientSet,
}
na.subscribtions = subscribe()
return &na
}
// subscribe to subnet and instance events
func subscribe() []schema.GroupVersionResource {
subs := make([]schema.GroupVersionResource, 0)
subs = append(subs, ocicorev1alpha1.SchemeGroupVersion.WithResource(ocicorev1alpha1.InstanceResourcePlural))
return subs
}
// set lister
func (a *CpodAdapter) SetLister(lister cache.GenericLister) {
a.lister = lister
}
// set queue
func (a *CpodAdapter) SetQueue(q workqueue.RateLimitingInterface) {
a.queue = q
}
// kind
func (a *CpodAdapter) Kind() string {
return cloudv1alpha1.CpodKind
}
// resource
func (a *CpodAdapter) Resource() string {
return cloudv1alpha1.CpodResourcePlural
}
// group version with resource
func (a *CpodAdapter) GroupVersionWithResource() schema.GroupVersionResource {
return cloudv1alpha1.SchemeGroupVersion.WithResource(cloudv1alpha1.CpodResourcePlural)
}
// subscriptions
func (a *CpodAdapter) Subscriptions() []schema.GroupVersionResource {
return a.subscribtions
}
// object meta
func (a *CpodAdapter) ObjectMeta(obj runtime.Object) *metav1.ObjectMeta {
return &obj.(*cloudv1alpha1.Cpod).ObjectMeta
}
// equivalent
func (a *CpodAdapter) Equivalent(obj1, obj2 runtime.Object) bool {
return reflect.DeepEqual(obj1, obj2)
}
// delete cpod cloud event handler
func (a *CpodAdapter) Delete(obj runtime.Object) (runtime.Object, error) {
cpod := obj.(*cloudv1alpha1.Cpod)
wait := false
// Delete the cpod instance resource
cpodInstance, err := DeleteCpodInstance(a.clientset, cpod)
if err != nil {
cloudcommon.SetCondition(&cpod.Status.OperatorStatus, ocicorev1alpha1.InstanceKind, err.Error())
return cpod, err
}
if cpodInstance != nil {
err = errors.New("Cpod instance resource is not deleted yet")
cloudcommon.SetCondition(&cpod.Status.OperatorStatus, ocicorev1alpha1.InstanceKind, err.Error())
wait = true
}
// Remove finalizers
if !wait && len(cpod.Finalizers) > 0 {
cpod.SetFinalizers([]string{})
return cpod, nil
}
return cpod, nil
}
// update cpod cloud event handler
func (a *CpodAdapter) Update(obj runtime.Object) (runtime.Object, error) {
cpod := obj.(*cloudv1alpha1.Cpod)
resultObj, e := a.clientset.CloudV1alpha1().Cpods(cpod.Namespace).Update(cpod)
return resultObj, e
}
// reconcile - handles create and updates
func (a *CpodAdapter) Reconcile(obj runtime.Object) (runtime.Object, error) {
cpod := obj.(*cloudv1alpha1.Cpod)
controllerRef := cloudcommon.CreateControllerRef(cpod, controllerKind)
reconcileState := cloudv1alpha1.OperatorStateCreated
instance, _, err := CreateOrUpdateCpodInstance(a.clientset, cpod, controllerRef)
if err != nil {
cloudcommon.SetCondition(&cpod.Status.OperatorStatus, ocicorev1alpha1.InstanceKind, err.Error())
return cpod, err
}
if instance.Status.State != ocicommon.ResourceStateProcessed || !instance.IsResource() {
err = errors.New("instance resources are not processed yet")
cloudcommon.SetCondition(&cpod.Status.OperatorStatus, ocicorev1alpha1.InstanceKind, err.Error())
reconcileState = cloudv1alpha1.OperatorStatePending
}
/*
if reconcileState == cloudv1alpha1.OperatorStateCreated {
cpod.Status.State = cloudv1alpha1.OperatorStateCreated
cpod.Status.Conditions = []cloudv1alpha1.OperatorCondition{}
cpod.Status.PodStatus.Phase = corev1.PodRunning
cpod.Status.PodStatus.PodIP = *instance.Status.PrimaryVnic.PublicIp
if cpod.Status.PodStatus.HostIP == "" {
cpod.Status.PodStatus.HostIP = *instance.Status.PrimaryVnic.PublicIp
}
} else {
cpod.Status.State = cloudv1alpha1.OperatorStatePending
cpod.Status.PodStatus.Phase = corev1.PodPending
}
*/
// Everything is done. Update the State, reset the Conditions and return
podStatus := a.getPodState(reconcileState, instance)
if reconcileState == cloudv1alpha1.OperatorStateCreated {
if cpod.Status.PodStatus.HostIP == "" {
podStatus.HostIP = *instance.Status.PrimaryVnic.PublicIp
} else {
podStatus.HostIP = cpod.Status.PodStatus.HostIP
}
podStatus.PodIP = *instance.Status.PrimaryVnic.PublicIp
cpod.Status.Conditions = []cloudv1alpha1.OperatorCondition{}
}
cpod.Status.PodStatus = *podStatus
cpod.Status.State = reconcileState
return cpod, nil
}
func (a *CpodAdapter) getPodState(reconcileState cloudv1alpha1.OperatorState, inst *ocicorev1alpha1.Instance) *corev1.PodStatus {
podName := inst.Name
startedAt := inst.CreationTimestamp
var (
//podCondition corev1.PodCondition
containerState corev1.ContainerState
podPhase corev1.PodPhase
conditions []corev1.PodCondition
)
conditions = make([]corev1.PodCondition, 0)
switch reconcileState {
case cloudv1alpha1.OperatorStatePending:
podCondition := corev1.PodCondition{
Type: corev1.PodScheduled,
Status: corev1.ConditionFalse,
}
conditions = append(conditions, podCondition)
podPhase = corev1.PodPending
containerState = corev1.ContainerState{
Waiting: &corev1.ContainerStateWaiting{},
}
case cloudv1alpha1.OperatorStateCreated: // running
podConditionReady := corev1.PodCondition{
Type: corev1.PodReady,
Status: corev1.ConditionTrue,
}
conditions = append(conditions, podConditionReady)
podConditionScheduled := corev1.PodCondition{
Type: corev1.PodScheduled,
Status: corev1.ConditionTrue,
}
conditions = append(conditions, podConditionScheduled)
podConditionInited := corev1.PodCondition{
Type: corev1.PodInitialized,
Status: corev1.ConditionTrue,
}
conditions = append(conditions, podConditionInited)
podPhase = corev1.PodRunning
containerState = corev1.ContainerState{
Running: &corev1.ContainerStateRunning{
StartedAt: startedAt,
},
}
default: //unkown
podCondition := corev1.PodCondition{
Type: corev1.PodReasonUnschedulable,
Status: corev1.ConditionUnknown,
}
conditions = append(conditions, podCondition)
podPhase = corev1.PodUnknown
containerState = corev1.ContainerState{}
}
status := corev1.PodStatus{
Phase: podPhase,
Conditions: conditions,
Message: inst.Status.Message,
Reason: "",
ContainerStatuses: []corev1.ContainerStatus{
{
Name: podName,
RestartCount: int32(0),
Image: "fake",
ImageID: "fake",
ContainerID: "fake",
Ready: true,
State: containerState,
},
},
}
return &status
}
// callback for resource
func (a *CpodAdapter) CallbackForResource(resource schema.GroupVersionResource) cache.ResourceEventHandlerFuncs {
handlers := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
a.ignoreAddEvent(obj)
},
UpdateFunc: func(old, obj interface{}) {
a.processUpdateOrDeleteEvent(obj)
},
DeleteFunc: func(obj interface{}) {
a.processUpdateOrDeleteEvent(obj)
},
}
return handlers
}
// resolve controller reference
func (a *CpodAdapter) resolveControllerRef(namespace string, controllerRef *metav1.OwnerReference) *cloudv1alpha1.Cpod {
// We can't look up by UID, so look up by Name and then verify UID.
// Don't even try to look up by Name if it's the wrong Kind.
if controllerRef.Kind != controllerKind.Kind {
return nil
}
obj, err := a.lister.ByNamespace(namespace).Get(controllerRef.Name)
if err != nil {
return nil
}
cpod := obj.(*cloudv1alpha1.Cpod)
if cpod.UID != controllerRef.UID {
// The controller we found with this Name is not the same one that the
// ControllerRef points to.
return nil
}
return cpod
}
// ignore add event
func (a *CpodAdapter) ignoreAddEvent(obj interface{}) {
glog.V(5).Infof("Got add event: %v, ignore", obj)
}
// handle update or delete events
func (a *CpodAdapter) processUpdateOrDeleteEvent(obj interface{}) {
object := obj.(metav1.Object)
if controllerRef := cloudcommon.GetControllerOf(object); controllerRef != nil {
cpod := a.resolveControllerRef(object.GetNamespace(), controllerRef)
if cpod == nil {
return
}
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(cpod)
if err != nil {
glog.Errorf("Cpod deletion state error %v", err)
return
}
glog.V(4).Infof("Cpod %s received update event for %s %s\n", key, reflect.TypeOf(object).String(), object.GetName())
a.queue.Add(key)
//a.Reconcile(cpod)
return
}
}