Newer
Older
/*
Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.gnu.org/licenses/agpl-3.0.html
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 reconciler
import (
"context"
"time"
"github.com/presslabs/controller-util/syncer"
batchv1 "k8s.io/api/batch/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/record"
interfaces "k8s.libre.sh/controller-utils/interfaces"
"k8s.libre.sh/controller-utils/status"
kstatus "sigs.k8s.io/cli-utils/pkg/kstatus/status"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
const (
RetryInterval = 50 * time.Millisecond
RetryTimeout = 2 * time.Second
// ReadinessTimeout = 30 * time.Second
ReadinessTimeout = 5 * time.Minute
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
GCTimeout = 5 * time.Minute
)
func CleanUpResources(old, new *status.Resources, c client.Client, ns string) error {
diff := status.Difference(old, new)
if len(diff.Objects) > 0 {
for _, o := range diff.Objects {
unstruct := &unstructured.Unstructured{}
unstruct.SetGroupVersionKind(o.GVK())
unstruct.SetName(o.Name)
unstruct.SetNamespace(ns)
deleteBackground := metav1.DeletePropagationBackground
opts := client.DeleteOptions{
PropagationPolicy: &deleteBackground,
}
if err := c.Delete(context.TODO(), unstruct, &opts); err != nil {
return err
}
}
}
return nil
}
func Record(owner runtime.Object, recorder record.EventRecorder, result syncer.SyncResult) error {
if recorder != nil && owner != nil && result.EventType != "" && result.EventReason != "" && result.EventMessage != "" {
if result.Operation != controllerutil.OperationResultNone {
recorder.Eventf(owner, result.EventType, result.EventReason, result.EventMessage)
}
}
return nil
}
func WaitUntilCompletion(r interfaces.Reconcile, obj runtime.Object) error {
return wait.Poll(RetryInterval, ReadinessTimeout, func() (bool, error) {
objectKey, _ := client.ObjectKeyFromObject(obj)
if err := r.GetClient().Get(context.Background(), objectKey, obj); err != nil {
return false, err
}
unstruct := &unstructured.Unstructured{}
unstructuredObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return false, err
}
unstruct.Object = unstructuredObj
s, err := kstatus.Compute(unstruct)
// Check if job then check if completed
obj, ok := obj.(*batchv1.Job)
if ok {
if obj.Status.Succeeded > 0 { // || job.Status.Failed > types.Int32(job.Spec.BackoffLimit) {
return true, nil
}
if obj.Status.Failed > *obj.Spec.BackoffLimit {
return true, errors.New("failed")
}
return false, nil
}
if err != nil {
return false, err
}
if s.Status == kstatus.FailedStatus {
return true, errors.New("failed")
}
if s.Status == kstatus.UnknownStatus {
return true, errors.New("unkown")
}
if s.Status == kstatus.InProgressStatus {
return false, nil
}
return true, nil
})
}