/
reconcile_pod.go
71 lines (59 loc) · 2.31 KB
/
reconcile_pod.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
/*
Copyright 2020,2021 Avi Zimmerman
This file is part of kvdi.
kvdi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
kvdi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with kvdi. If not, see <https://www.gnu.org/licenses/>.
*/
package reconcile
import (
"context"
"github.com/kvdi/kvdi/pkg/util/errors"
"github.com/kvdi/kvdi/pkg/util/k8sutil"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// Pod will reconcile a given pod definition with the cluster. If a pod
// with the same name exists but has a different configuration, the pod will be
// deleted and requeued. If the found pod has a deletion timestamp (e.g. it is still terminating)
// then the request will also be requued.
func Pod(ctx context.Context, reqLogger logr.Logger, c client.Client, pod *corev1.Pod) (bool, error) {
if err := k8sutil.SetCreationSpecAnnotation(&pod.ObjectMeta, pod); err != nil {
return false, err
}
found := &corev1.Pod{}
if err := c.Get(ctx, types.NamespacedName{Name: pod.Name, Namespace: pod.Namespace}, found); err != nil {
// Return API error
if client.IgnoreNotFound(err) != nil {
return false, err
}
// Create the Pod
reqLogger.Info("Creating new Pod", "Pod.Name", pod.Name, "Pod.Namespace", pod.Namespace)
if err := c.Create(ctx, pod); err != nil {
return false, err
}
return true, nil
}
// Check if the found pod is in the middle of terminating
if found.GetDeletionTimestamp() != nil {
return false, errors.NewRequeueError("Existing pod is still being terminated, requeuing", 3)
}
// Check the found pod spec
if !k8sutil.CreationSpecsEqual(pod.ObjectMeta, found.ObjectMeta) {
// We need to delete the pod and return a requeue
if err := c.Delete(ctx, found); err != nil {
return false, err
}
return false, errors.NewRequeueError("Pod spec has changed, recreating", 3)
}
return false, nil
}