-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathgitrepo.go
More file actions
208 lines (177 loc) · 7.1 KB
/
gitrepo.go
File metadata and controls
208 lines (177 loc) · 7.1 KB
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
/*
Copyright (c) 2020 - 2026 SUSE LLC
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.
*/
// Code generated by main. DO NOT EDIT.
package v1alpha1
import (
"context"
"sync"
"time"
v1alpha1 "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/wrangler/v3/pkg/apply"
"github.com/rancher/wrangler/v3/pkg/condition"
"github.com/rancher/wrangler/v3/pkg/generic"
"github.com/rancher/wrangler/v3/pkg/kv"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// GitRepoController interface for managing GitRepo resources.
type GitRepoController interface {
generic.ControllerInterface[*v1alpha1.GitRepo, *v1alpha1.GitRepoList]
}
// GitRepoClient interface for managing GitRepo resources in Kubernetes.
type GitRepoClient interface {
generic.ClientInterface[*v1alpha1.GitRepo, *v1alpha1.GitRepoList]
}
// GitRepoCache interface for retrieving GitRepo resources in memory.
type GitRepoCache interface {
generic.CacheInterface[*v1alpha1.GitRepo]
}
// GitRepoStatusHandler is executed for every added or modified GitRepo. Should return the new status to be updated
type GitRepoStatusHandler func(obj *v1alpha1.GitRepo, status v1alpha1.GitRepoStatus) (v1alpha1.GitRepoStatus, error)
// GitRepoGeneratingHandler is the top-level handler that is executed for every GitRepo event. It extends GitRepoStatusHandler by a returning a slice of child objects to be passed to apply.Apply
type GitRepoGeneratingHandler func(obj *v1alpha1.GitRepo, status v1alpha1.GitRepoStatus) ([]runtime.Object, v1alpha1.GitRepoStatus, error)
// RegisterGitRepoStatusHandler configures a GitRepoController to execute a GitRepoStatusHandler for every events observed.
// If a non-empty condition is provided, it will be updated in the status conditions for every handler execution
func RegisterGitRepoStatusHandler(ctx context.Context, controller GitRepoController, condition condition.Cond, name string, handler GitRepoStatusHandler) {
statusHandler := &gitRepoStatusHandler{
client: controller,
condition: condition,
handler: handler,
}
controller.AddGenericHandler(ctx, name, generic.FromObjectHandlerToHandler(statusHandler.sync))
}
// RegisterGitRepoGeneratingHandler configures a GitRepoController to execute a GitRepoGeneratingHandler for every events observed, passing the returned objects to the provided apply.Apply.
// If a non-empty condition is provided, it will be updated in the status conditions for every handler execution
func RegisterGitRepoGeneratingHandler(ctx context.Context, controller GitRepoController, apply apply.Apply,
condition condition.Cond, name string, handler GitRepoGeneratingHandler, opts *generic.GeneratingHandlerOptions) {
statusHandler := &gitRepoGeneratingHandler{
GitRepoGeneratingHandler: handler,
apply: apply,
name: name,
gvk: controller.GroupVersionKind(),
}
if opts != nil {
statusHandler.opts = *opts
}
controller.OnChange(ctx, name, statusHandler.Remove)
RegisterGitRepoStatusHandler(ctx, controller, condition, name, statusHandler.Handle)
}
type gitRepoStatusHandler struct {
client GitRepoClient
condition condition.Cond
handler GitRepoStatusHandler
}
// sync is executed on every resource addition or modification. Executes the configured handlers and sends the updated status to the Kubernetes API
func (a *gitRepoStatusHandler) sync(key string, obj *v1alpha1.GitRepo) (*v1alpha1.GitRepo, error) {
if obj == nil {
return obj, nil
}
origStatus := obj.Status.DeepCopy()
obj = obj.DeepCopy()
newStatus, err := a.handler(obj, obj.Status)
if err != nil {
// Revert to old status on error
newStatus = *origStatus.DeepCopy()
}
if a.condition != "" {
if errors.IsConflict(err) {
a.condition.SetError(&newStatus, "", nil)
} else {
a.condition.SetError(&newStatus, "", err)
}
}
if !equality.Semantic.DeepEqual(origStatus, &newStatus) {
if a.condition != "" {
// Since status has changed, update the lastUpdatedTime
a.condition.LastUpdated(&newStatus, time.Now().UTC().Format(time.RFC3339))
}
var newErr error
obj.Status = newStatus
newObj, newErr := a.client.UpdateStatus(obj)
if err == nil {
err = newErr
}
if newErr == nil {
obj = newObj
}
}
return obj, err
}
type gitRepoGeneratingHandler struct {
GitRepoGeneratingHandler
apply apply.Apply
opts generic.GeneratingHandlerOptions
gvk schema.GroupVersionKind
name string
seen sync.Map
}
// Remove handles the observed deletion of a resource, cascade deleting every associated resource previously applied
func (a *gitRepoGeneratingHandler) Remove(key string, obj *v1alpha1.GitRepo) (*v1alpha1.GitRepo, error) {
if obj != nil {
return obj, nil
}
obj = &v1alpha1.GitRepo{}
obj.Namespace, obj.Name = kv.RSplit(key, "/")
obj.SetGroupVersionKind(a.gvk)
if a.opts.UniqueApplyForResourceVersion {
a.seen.Delete(key)
}
return nil, generic.ConfigureApplyForObject(a.apply, obj, &a.opts).
WithOwner(obj).
WithSetID(a.name).
ApplyObjects()
}
// Handle executes the configured GitRepoGeneratingHandler and pass the resulting objects to apply.Apply, finally returning the new status of the resource
func (a *gitRepoGeneratingHandler) Handle(obj *v1alpha1.GitRepo, status v1alpha1.GitRepoStatus) (v1alpha1.GitRepoStatus, error) {
if !obj.DeletionTimestamp.IsZero() {
return status, nil
}
objs, newStatus, err := a.GitRepoGeneratingHandler(obj, status)
if err != nil {
return newStatus, err
}
if !a.isNewResourceVersion(obj) {
return newStatus, nil
}
err = generic.ConfigureApplyForObject(a.apply, obj, &a.opts).
WithOwner(obj).
WithSetID(a.name).
ApplyObjects(objs...)
if err != nil {
return newStatus, err
}
a.storeResourceVersion(obj)
return newStatus, nil
}
// isNewResourceVersion detects if a specific resource version was already successfully processed.
// Only used if UniqueApplyForResourceVersion is set in generic.GeneratingHandlerOptions
func (a *gitRepoGeneratingHandler) isNewResourceVersion(obj *v1alpha1.GitRepo) bool {
if !a.opts.UniqueApplyForResourceVersion {
return true
}
// Apply once per resource version
key := obj.Namespace + "/" + obj.Name
previous, ok := a.seen.Load(key)
return !ok || previous != obj.ResourceVersion
}
// storeResourceVersion keeps track of the latest resource version of an object for which Apply was executed
// Only used if UniqueApplyForResourceVersion is set in generic.GeneratingHandlerOptions
func (a *gitRepoGeneratingHandler) storeResourceVersion(obj *v1alpha1.GitRepo) {
if !a.opts.UniqueApplyForResourceVersion {
return
}
key := obj.Namespace + "/" + obj.Name
a.seen.Store(key, obj.ResourceVersion)
}