Skip to content

Commit 705115f

Browse files
authored
chore: Upgrading & Ingesting reconciler and controller runtime breaking changes (#100)
Signed-off-by: Rashed Kamal <[email protected]> Upgrading & Ingesting reconciler and controller runtime breaking changes - k8s 1.27 - controller-runtime 0.15 - reconciler-runtime 0.12
1 parent 3967255 commit 705115f

File tree

9 files changed

+1614
-1929
lines changed

9 files changed

+1614
-1929
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ jobs:
166166
- 1.24.12
167167
- 1.25.8
168168
- 1.26.3
169-
- 1.27.1
169+
- 1.27.2
170170
env:
171171
REGISTRY_NAME: registry.local
172172
BUNDLE: registry.local/source/bundle

apis/source/v1alpha1/mavenartifact_test.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"time"
2222

2323
"github.com/google/go-cmp/cmp"
24-
"github.com/vmware-labs/reconciler-runtime/validation"
2524
v1 "k8s.io/api/core/v1"
2625
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2726
"k8s.io/apimachinery/pkg/util/validation/field"
@@ -107,12 +106,12 @@ func TestMavenArtifactValidate(t *testing.T) {
107106
tests := []struct {
108107
name string
109108
seed *MavenArtifact
110-
expected validation.FieldErrors
109+
expected field.ErrorList
111110
}{
112111
{
113112
name: "empty is not valid",
114113
seed: &MavenArtifact{},
115-
expected: validation.FieldErrors{
114+
expected: field.ErrorList{
116115
field.Required(field.NewPath("spec", "artifact", "groupId"), ""),
117116
field.Required(field.NewPath("spec", "artifact", "artifactId"), ""),
118117
field.Required(field.NewPath("spec", "artifact", "version"), ""),
@@ -135,7 +134,7 @@ func TestMavenArtifactValidate(t *testing.T) {
135134
Interval: metav1.Duration{Duration: time.Minute},
136135
},
137136
},
138-
expected: validation.FieldErrors{},
137+
expected: field.ErrorList{},
139138
},
140139
{
141140
name: "fully valid",
@@ -158,7 +157,7 @@ func TestMavenArtifactValidate(t *testing.T) {
158157
Timeout: &metav1.Duration{Duration: time.Minute},
159158
},
160159
},
161-
expected: validation.FieldErrors{},
160+
expected: field.ErrorList{},
162161
},
163162
{
164163
name: "invalid version LATEST",
@@ -175,7 +174,7 @@ func TestMavenArtifactValidate(t *testing.T) {
175174
Interval: metav1.Duration{Duration: time.Minute},
176175
},
177176
},
178-
expected: validation.FieldErrors{},
177+
expected: field.ErrorList{},
179178
},
180179
{
181180
name: "valid version SNAPSHOT",
@@ -192,7 +191,7 @@ func TestMavenArtifactValidate(t *testing.T) {
192191
Interval: metav1.Duration{Duration: time.Minute},
193192
},
194193
},
195-
expected: validation.FieldErrors{},
194+
expected: field.ErrorList{},
196195
},
197196
{
198197
// TODO this is valid, but not presently supported
@@ -210,7 +209,7 @@ func TestMavenArtifactValidate(t *testing.T) {
210209
Interval: metav1.Duration{Duration: time.Minute},
211210
},
212211
},
213-
expected: validation.FieldErrors{
212+
expected: field.ErrorList{
214213
field.Invalid(field.NewPath("spec", "artifact", "version"), "[1.0,2.0)", ""),
215214
},
216215
},
@@ -229,7 +228,7 @@ func TestMavenArtifactValidate(t *testing.T) {
229228
Interval: metav1.Duration{Duration: time.Minute},
230229
},
231230
},
232-
expected: validation.FieldErrors{
231+
expected: field.ErrorList{
233232
field.Invalid(field.NewPath("spec", "repository", "url"), "https://repo1.maven.org:badport/maven2", ""),
234233
},
235234
},
@@ -248,7 +247,7 @@ func TestMavenArtifactValidate(t *testing.T) {
248247
Interval: metav1.Duration{Duration: time.Minute},
249248
},
250249
},
251-
expected: validation.FieldErrors{
250+
expected: field.ErrorList{
252251
field.Invalid(field.NewPath("spec", "repository", "url"), "http://repo1.maven.org/maven2", `Scheme "https" is required; scheme "http" is not allowed in repository URL "http://repo1.maven.org/maven2"`),
253252
},
254253
},
@@ -270,7 +269,7 @@ func TestMavenArtifactValidate(t *testing.T) {
270269
Interval: metav1.Duration{Duration: time.Minute},
271270
},
272271
},
273-
expected: validation.FieldErrors{
272+
expected: field.ErrorList{
274273
field.Invalid(field.NewPath("spec", "repository", "secretRef", "name"), "-", ""),
275274
},
276275
},
@@ -289,7 +288,7 @@ func TestMavenArtifactValidate(t *testing.T) {
289288
Interval: metav1.Duration{Duration: 0},
290289
},
291290
},
292-
expected: validation.FieldErrors{
291+
expected: field.ErrorList{
293292
field.Invalid(field.NewPath("spec", "interval"), metav1.Duration{}, ""),
294293
},
295294
},
@@ -309,26 +308,32 @@ func TestMavenArtifactValidate(t *testing.T) {
309308
Timeout: &metav1.Duration{Duration: 0},
310309
},
311310
},
312-
expected: validation.FieldErrors{
311+
expected: field.ErrorList{
313312
field.Invalid(field.NewPath("spec", "timeout"), &metav1.Duration{}, ""),
314313
},
315314
},
316315
}
317316

318317
for _, c := range tests {
319318
t.Run(c.name, func(t *testing.T) {
320-
if diff := cmp.Diff(c.expected, c.seed.Validate()); diff != "" {
319+
if diff := cmp.Diff(c.expected, c.seed.validate()); diff != "" {
321320
t.Errorf("validate (-expected, +actual): %s", diff)
322321
}
323322

324323
expectedErr := c.expected.ToAggregate()
325-
if diff := cmp.Diff(expectedErr, c.seed.ValidateCreate()); diff != "" {
324+
325+
_, actualCreateErr := c.seed.ValidateCreate()
326+
if diff := cmp.Diff(expectedErr, actualCreateErr); diff != "" {
326327
t.Errorf("ValidateCreate (-expected, +actual): %s", diff)
327328
}
328-
if diff := cmp.Diff(expectedErr, c.seed.ValidateUpdate(c.seed.DeepCopy())); diff != "" {
329+
330+
_, actualUpdateErr := c.seed.ValidateUpdate(c.seed.DeepCopy())
331+
if diff := cmp.Diff(expectedErr, actualUpdateErr); diff != "" {
329332
t.Errorf("ValidateCreate (-expected, +actual): %s", diff)
330333
}
331-
if diff := cmp.Diff(nil, c.seed.ValidateDelete()); diff != "" {
334+
335+
_, actualDeleteErr := c.seed.ValidateDelete()
336+
if diff := cmp.Diff(nil, actualDeleteErr); diff != "" {
332337
t.Errorf("ValidateDelete (-expected, +actual): %s", diff)
333338
}
334339
})

apis/source/v1alpha1/mavenartifact_validation.go

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,95 +21,95 @@ import (
2121
"net/url"
2222
"strings"
2323

24-
"github.com/vmware-labs/reconciler-runtime/validation"
25-
kvalidation "k8s.io/apimachinery/pkg/api/validation"
24+
"k8s.io/apimachinery/pkg/api/validation"
2625
runtime "k8s.io/apimachinery/pkg/runtime"
2726
"k8s.io/apimachinery/pkg/util/validation/field"
2827

2928
"sigs.k8s.io/controller-runtime/pkg/webhook"
29+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3030
)
3131

3232
// +kubebuilder:webhook:path=/validate-source-apps-tanzu-vmware-com-v1alpha1-mavenartifact,mutating=false,failurePolicy=fail,sideEffects=none,admissionReviewVersions=v1beta1,groups=source.apps.tanzu.vmware.com,resources=mavenartifacts,verbs=create;update,versions=v1alpha1,name=mavenartifacts.source.apps.tanzu.vmware.com
3333

34-
var (
35-
_ webhook.Validator = &MavenArtifact{}
36-
_ validation.FieldValidator = &MavenArtifact{}
37-
)
34+
var _ webhook.Validator = &MavenArtifact{}
3835

3936
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
40-
func (r *MavenArtifact) ValidateCreate() error {
41-
return r.Validate().ToAggregate()
37+
func (r *MavenArtifact) ValidateCreate() (admission.Warnings, error) {
38+
return nil, r.validate().ToAggregate()
4239
}
4340

4441
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
45-
func (c *MavenArtifact) ValidateUpdate(old runtime.Object) error {
42+
func (c *MavenArtifact) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
4643
// TODO check for immutable fields
47-
return c.Validate().ToAggregate()
44+
return nil, c.validate().ToAggregate()
4845
}
4946

5047
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
51-
func (c *MavenArtifact) ValidateDelete() error {
52-
return nil
48+
func (c *MavenArtifact) ValidateDelete() (admission.Warnings, error) {
49+
return nil, nil
5350
}
5451

55-
func (r *MavenArtifact) Validate() validation.FieldErrors {
56-
errs := validation.FieldErrors{}
57-
return errs.Also(r.Spec.Validate().ViaField("spec"))
52+
func (r *MavenArtifact) validate() field.ErrorList {
53+
errs := field.ErrorList{}
54+
55+
errs = append(errs, r.Spec.validate(field.NewPath("spec"))...)
56+
57+
return errs
5858
}
5959

60-
func (s *MavenArtifactSpec) Validate() validation.FieldErrors {
61-
errs := validation.FieldErrors{}
60+
func (s *MavenArtifactSpec) validate(fldPath *field.Path) field.ErrorList {
61+
errs := field.ErrorList{}
6262

63-
errs = errs.Also(s.Artifact.Validate().ViaField("artifact"))
64-
errs = errs.Also(s.Repository.Validate().ViaField("repository"))
63+
errs = append(errs, s.Artifact.validate(fldPath.Child("artifact"))...)
64+
errs = append(errs, s.Repository.validate(fldPath.Child("repository"))...)
6565

6666
if s.Interval.Duration <= 0 {
67-
errs = errs.Also(validation.ErrInvalidValue(s.Interval, "interval"))
67+
errs = append(errs, field.Invalid(fldPath.Child("interval"), s.Interval, ""))
6868
}
6969
if s.Timeout != nil && s.Timeout.Duration <= 0 {
70-
errs = errs.Also(validation.ErrInvalidValue(s.Timeout, "timeout"))
70+
errs = append(errs, field.Invalid(fldPath.Child("timeout"), s.Timeout, ""))
7171
}
7272

7373
return errs
7474
}
7575

76-
func (s *MavenArtifactType) Validate() validation.FieldErrors {
77-
errs := validation.FieldErrors{}
76+
func (s *MavenArtifactType) validate(fldPath *field.Path) field.ErrorList {
77+
errs := field.ErrorList{}
7878

7979
if s.GroupId == "" {
80-
errs = errs.Also(validation.ErrMissingField("groupId"))
80+
errs = append(errs, field.Required(fldPath.Child("groupId"), ""))
8181
}
8282
if s.ArtifactId == "" {
83-
errs = errs.Also(validation.ErrMissingField("artifactId"))
83+
errs = append(errs, field.Required(fldPath.Child("artifactId"), ""))
8484
}
8585

8686
if s.Version == "" {
87-
errs = errs.Also(validation.ErrMissingField("version"))
87+
errs = append(errs, field.Required(fldPath.Child("version"), ""))
8888
} else if strings.HasPrefix(s.Version, "[") ||
8989
strings.HasPrefix(s.Version, "(") {
9090
// TODO remove this validation rule when version range is resolvable
91-
errs = errs.Also(validation.ErrInvalidValue(s.Version, "version"))
91+
errs = append(errs, field.Invalid(fldPath.Child("version"), s.Version, ""))
9292
}
9393

9494
return errs
9595
}
9696

97-
func (s *Repository) Validate() validation.FieldErrors {
98-
errs := validation.FieldErrors{}
97+
func (s *Repository) validate(fldPath *field.Path) field.ErrorList {
98+
errs := field.ErrorList{}
9999

100100
if s.URL == "" {
101-
errs = errs.Also(validation.ErrMissingField("url"))
101+
errs = append(errs, field.Required(fldPath.Child("url"), ""))
102102
} else if u, err := url.Parse(s.URL); err != nil {
103-
errs = errs.Also(validation.ErrInvalidValue(s.URL, "url"))
103+
errs = append(errs, field.Invalid(fldPath.Child("url"), s.URL, ""))
104104
} else {
105105
if u.Scheme != "https" {
106-
errs = append(errs, field.Invalid(field.NewPath("url"), s.URL, fmt.Sprintf(`Scheme "https" is required; scheme %q is not allowed in repository URL %q`, u.Scheme, s.URL)))
106+
errs = append(errs, field.Invalid(fldPath.Child("url"), s.URL, fmt.Sprintf(`Scheme "https" is required; scheme %q is not allowed in repository URL %q`, u.Scheme, s.URL)))
107107
}
108108
}
109109

110110
if n := s.SecretRef.Name; n != "" {
111-
if out := kvalidation.NameIsDNSLabel(n, false); len(out) != 0 {
112-
errs = errs.Also(validation.ErrInvalidValue(s.SecretRef.Name, "secretRef.name"))
111+
if out := validation.NameIsDNSLabel(n, false); len(out) != 0 {
112+
errs = append(errs, field.Invalid(fldPath.Child("secretRef", "name"), s.SecretRef.Name, ""))
113113
}
114114
}
115115

controllers/imagerepository_controller.go

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"fmt"
2525
"io"
2626
"io/fs"
27-
"io/ioutil"
2827
"net/http"
2928
"os"
3029
"path"
@@ -46,8 +45,6 @@ import (
4645
"k8s.io/apimachinery/pkg/util/sets"
4746
controllerruntime "sigs.k8s.io/controller-runtime"
4847
"sigs.k8s.io/controller-runtime/pkg/builder"
49-
"sigs.k8s.io/controller-runtime/pkg/client"
50-
"sigs.k8s.io/controller-runtime/pkg/source"
5148

5249
sourcev1alpha1 "github.com/vmware-tanzu/tanzu-source-controller/apis/source/v1alpha1"
5350
)
@@ -58,12 +55,11 @@ import (
5855
//+kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch;create;update;patch;delete
5956

6057
// ImageRepositoryReconciler reconciles a ImageRepository object
61-
func ImageRepositoryReconciler(c reconcilers.Config, httpRootDir, httpHost string, now func() metav1.Time, certs []Cert) *reconcilers.ParentReconciler {
62-
return &reconcilers.ParentReconciler{
63-
Type: &sourcev1alpha1.ImageRepository{},
64-
Reconciler: &reconcilers.WithFinalizer{
58+
func ImageRepositoryReconciler(c reconcilers.Config, httpRootDir, httpHost string, now func() metav1.Time, certs []Cert) *reconcilers.ResourceReconciler[*sourcev1alpha1.ImageRepository] {
59+
return &reconcilers.ResourceReconciler[*sourcev1alpha1.ImageRepository]{
60+
Reconciler: &reconcilers.WithFinalizer[*sourcev1alpha1.ImageRepository]{
6561
Finalizer: sourcev1alpha1.Group + "/finalizer",
66-
Reconciler: reconcilers.Sequence{
62+
Reconciler: reconcilers.Sequence[*sourcev1alpha1.ImageRepository]{
6763
ImageRepositoryTransportSyncReconciler(certs),
6864
ImageRepositoryImagePullSecretsSyncReconciler(),
6965
ImageRepositoryImageDigestSyncReconciler(),
@@ -76,10 +72,10 @@ func ImageRepositoryReconciler(c reconcilers.Config, httpRootDir, httpHost strin
7672
}
7773
}
7874

79-
func ImageRepositoryTransportSyncReconciler(certs []Cert) reconcilers.SubReconciler {
80-
return &reconcilers.SyncReconciler{
75+
func ImageRepositoryTransportSyncReconciler(certs []Cert) reconcilers.SubReconciler[*sourcev1alpha1.ImageRepository] {
76+
return &reconcilers.SyncReconciler[*sourcev1alpha1.ImageRepository]{
8177
Name: "ImageRepositoryTransportSyncReconciler",
82-
Sync: func(ctx context.Context, _ client.Object) error {
78+
Sync: func(ctx context.Context, _ *sourcev1alpha1.ImageRepository) error {
8379
transport, err := newTransport(ctx, certs)
8480
if err != nil {
8581
return err
@@ -93,8 +89,8 @@ func ImageRepositoryTransportSyncReconciler(certs []Cert) reconcilers.SubReconci
9389
//+kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch
9490
//+kubebuilder:rbac:groups=core,resources=serviceaccounts,verbs=get;list;watch
9591

96-
func ImageRepositoryImagePullSecretsSyncReconciler() reconcilers.SubReconciler {
97-
return &reconcilers.SyncReconciler{
92+
func ImageRepositoryImagePullSecretsSyncReconciler() reconcilers.SubReconciler[*sourcev1alpha1.ImageRepository] {
93+
return &reconcilers.SyncReconciler[*sourcev1alpha1.ImageRepository]{
9894
Name: "ImageRepositoryImagePullSecretsSyncReconciler",
9995
Sync: func(ctx context.Context, parent *sourcev1alpha1.ImageRepository) error {
10096
c := reconcilers.RetrieveConfigOrDie(ctx)
@@ -144,17 +140,17 @@ func ImageRepositoryImagePullSecretsSyncReconciler() reconcilers.SubReconciler {
144140

145141
Setup: func(ctx context.Context, mgr controllerruntime.Manager, bldr *builder.Builder) error {
146142
// register an informer to watch Secret's metadata only. This reduces the cache size in memory.
147-
bldr.Watches(&source.Kind{Type: &corev1.Secret{}}, reconcilers.EnqueueTracked(ctx, &corev1.Secret{}), builder.OnlyMetadata)
143+
bldr.Watches(&corev1.Secret{}, reconcilers.EnqueueTracked(ctx), builder.OnlyMetadata)
148144
// register an informer to watch ServiceAccounts
149-
bldr.Watches(&source.Kind{Type: &corev1.ServiceAccount{}}, reconcilers.EnqueueTracked(ctx, &corev1.ServiceAccount{}))
145+
bldr.Watches(&corev1.ServiceAccount{}, reconcilers.EnqueueTracked(ctx))
150146

151147
return nil
152148
},
153149
}
154150
}
155151

156-
func ImageRepositoryImageDigestSyncReconciler() reconcilers.SubReconciler {
157-
return &reconcilers.SyncReconciler{
152+
func ImageRepositoryImageDigestSyncReconciler() reconcilers.SubReconciler[*sourcev1alpha1.ImageRepository] {
153+
return &reconcilers.SyncReconciler[*sourcev1alpha1.ImageRepository]{
158154
Name: "ImageRepositoryImageDigestSyncReconciler",
159155
Sync: func(ctx context.Context, parent *sourcev1alpha1.ImageRepository) error {
160156
log := logr.FromContextOrDiscard(ctx)
@@ -195,8 +191,8 @@ func ImageRepositoryImageDigestSyncReconciler() reconcilers.SubReconciler {
195191
}
196192
}
197193

198-
func ImageRepositoryPullImageSyncReconciler(httpRootDir, httpHost string, now func() metav1.Time) reconcilers.SubReconciler {
199-
return &reconcilers.SyncReconciler{
194+
func ImageRepositoryPullImageSyncReconciler(httpRootDir, httpHost string, now func() metav1.Time) reconcilers.SubReconciler[*sourcev1alpha1.ImageRepository] {
195+
return &reconcilers.SyncReconciler[*sourcev1alpha1.ImageRepository]{
200196
Name: "ImageRepositoryPullImageSyncReconciler",
201197
Finalize: func(ctx context.Context, parent *sourcev1alpha1.ImageRepository) error {
202198
log := logr.FromContextOrDiscard(ctx)
@@ -229,7 +225,7 @@ func ImageRepositoryPullImageSyncReconciler(httpRootDir, httpHost string, now fu
229225
}
230226

231227
// pull image
232-
dir, err := ioutil.TempDir(os.TempDir(), "image.*")
228+
dir, err := os.MkdirTemp(os.TempDir(), "image.*")
233229
if err != nil {
234230
return err
235231
}
@@ -302,10 +298,10 @@ func ImageRepositoryPullImageSyncReconciler(httpRootDir, httpHost string, now fu
302298
}
303299
}
304300

305-
func ImageRepositoryIntervalReconciler() reconcilers.SubReconciler {
306-
return &reconcilers.SyncReconciler{
301+
func ImageRepositoryIntervalReconciler() reconcilers.SubReconciler[*sourcev1alpha1.ImageRepository] {
302+
return &reconcilers.SyncReconciler[*sourcev1alpha1.ImageRepository]{
307303
Name: "ImageRepositoryIntervalReconciler",
308-
Sync: func(ctx context.Context, parent *sourcev1alpha1.ImageRepository) (controllerruntime.Result, error) {
304+
SyncWithResult: func(ctx context.Context, parent *sourcev1alpha1.ImageRepository) (controllerruntime.Result, error) {
309305
return controllerruntime.Result{RequeueAfter: parent.Spec.Interval.Duration}, nil
310306
},
311307
}

0 commit comments

Comments
 (0)