This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
controller.go
154 lines (133 loc) · 3.39 KB
/
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright 2019 Red Hat Inc.
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 hook
import (
"context"
libcnd "github.com/konveyor/controller/pkg/condition"
"github.com/konveyor/controller/pkg/logging"
api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1"
"github.com/konveyor/forklift-controller/pkg/controller/base"
"github.com/konveyor/forklift-controller/pkg/settings"
k8serr "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apiserver/pkg/storage/names"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const (
// Name.
Name = "hook"
)
//
// Package logger.
var log = logging.WithName(Name)
//
// Application settings.
var Settings = &settings.Settings
//
// Creates a new Hook Controller and adds it to the Manager.
func Add(mgr manager.Manager) error {
reconciler := &Reconciler{
Reconciler: base.Reconciler{
EventRecorder: mgr.GetEventRecorderFor(Name),
Client: mgr.GetClient(),
Log: log,
},
}
cnt, err := controller.New(
Name,
mgr,
controller.Options{
Reconciler: reconciler,
})
if err != nil {
log.Trace(err)
return err
}
// Primary CR.
err = cnt.Watch(
&source.Kind{Type: &api.Hook{}},
&handler.EnqueueRequestForObject{},
&HookPredicate{})
if err != nil {
log.Trace(err)
return err
}
return nil
}
var _ reconcile.Reconciler = &Reconciler{}
//
// Reconciles a Hook object.
type Reconciler struct {
base.Reconciler
}
//
// Reconcile a Hook CR.
// Note: Must not a pointer receiver to ensure that the
// logger and other state is not shared.
func (r Reconciler) Reconcile(request reconcile.Request) (result reconcile.Result, err error) {
r.Log = logging.WithName(
names.SimpleNameGenerator.GenerateName(Name+"|"),
"hook",
request)
r.Started()
defer func() {
result.RequeueAfter = r.Ended(
result.RequeueAfter,
err)
err = nil
}()
// Fetch the CR.
hook := &api.Hook{}
err = r.Get(context.TODO(), request.NamespacedName, hook)
if err != nil {
if k8serr.IsNotFound(err) {
r.Log.Info("Hook deleted.")
err = nil
}
return
}
defer func() {
r.Log.V(2).Info("Conditions.", "all", hook.Status.Conditions)
}()
// Begin staging conditions.
hook.Status.BeginStagingConditions()
// Validations.
err = r.validate(hook)
if err != nil {
return
}
// Ready condition.
if !hook.Status.HasBlockerCondition() {
hook.Status.SetCondition(libcnd.Condition{
Type: libcnd.Ready,
Status: True,
Category: Required,
Message: "The hook is ready.",
})
}
// End staging conditions.
hook.Status.EndStagingConditions()
// Record events.
r.Record(hook, hook.Status.Conditions)
// Apply changes.
hook.Status.ObservedGeneration = hook.Generation
err = r.Status().Update(context.TODO(), hook)
if err != nil {
return
}
// Done
return
}