Skip to content

Commit 73a0668

Browse files
authored
🐛 Revert alpha2 crd (#735)
Removed v1alpha2. Adds 0.5 fields to v1alpha1 as optional. Update the hub to use v1alpah1. Updates CR status to support conditions. The addon controller will perform migration to ensure the addon.Container is populated. The task manager will wait until resources have been reconciled. --- Moved api/reflect/field.go Fields() to new _reflect_ package at the root. Added new functions: - HasField() - Select() - Omit My main concern was the the task manager is using Select() to restrict Save() specific fields. The "Error" fields as incorrect (should be "Errors") and silently didn't update the field. --- This won't pass CI without operator changes. --------- Signed-off-by: Jeff Ortel <[email protected]>
1 parent ca8c391 commit 73a0668

29 files changed

+581
-2129
lines changed

api/addon.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/http"
77

88
"github.com/gin-gonic/gin"
9-
crd "github.com/konveyor/tackle2-hub/k8s/api/tackle/v1alpha2"
9+
crd "github.com/konveyor/tackle2-hub/k8s/api/tackle/v1alpha1"
1010
core "k8s.io/api/core/v1"
1111
"k8s.io/apimachinery/pkg/api/errors"
1212
k8s "sigs.k8s.io/controller-runtime/pkg/client"

api/base.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
"github.com/gin-gonic/gin/binding"
1515
liberr "github.com/jortel/go-utils/error"
1616
"github.com/jortel/go-utils/logr"
17-
"github.com/konveyor/tackle2-hub/api/reflect"
1817
"github.com/konveyor/tackle2-hub/api/sort"
1918
"github.com/konveyor/tackle2-hub/auth"
2019
"github.com/konveyor/tackle2-hub/model"
20+
"github.com/konveyor/tackle2-hub/reflect"
2121
"gopkg.in/yaml.v2"
2222
"gorm.io/gorm"
2323
"sigs.k8s.io/controller-runtime/pkg/client"

api/sort/sort.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"strings"
55

66
"github.com/gin-gonic/gin"
7-
"github.com/konveyor/tackle2-hub/api/reflect"
7+
"github.com/konveyor/tackle2-hub/reflect"
88
"gorm.io/gorm"
99
)
1010

api/taskgroup.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"net/http"
66

77
"github.com/gin-gonic/gin"
8-
crd "github.com/konveyor/tackle2-hub/k8s/api/tackle/v1alpha2"
8+
crd "github.com/konveyor/tackle2-hub/k8s/api/tackle/v1alpha1"
99
"github.com/konveyor/tackle2-hub/model"
1010
tasking "github.com/konveyor/tackle2-hub/task"
1111
"gorm.io/gorm/clause"

controller/addon.go

+61-47
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package controller
22

33
import (
44
"context"
5+
"strings"
56

67
"github.com/go-logr/logr"
78
logr2 "github.com/jortel/go-utils/logr"
8-
api "github.com/konveyor/tackle2-hub/k8s/api/tackle/v1alpha2"
9+
api "github.com/konveyor/tackle2-hub/k8s/api/tackle/v1alpha1"
910
"github.com/konveyor/tackle2-hub/settings"
1011
"gorm.io/gorm"
1112
k8serr "k8s.io/apimachinery/pkg/api/errors"
13+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1214
"k8s.io/apiserver/pkg/storage/names"
1315
"k8s.io/client-go/tools/record"
1416
k8s "sigs.k8s.io/controller-runtime/pkg/client"
@@ -32,9 +34,10 @@ var Settings = &settings.Settings
3234
// Add the controller.
3335
func Add(mgr manager.Manager, db *gorm.DB) error {
3436
reconciler := &Reconciler{
35-
Client: mgr.GetClient(),
36-
Log: log,
37-
DB: db,
37+
history: make(map[string]byte),
38+
Client: mgr.GetClient(),
39+
Log: log,
40+
DB: db,
3841
}
3942
cnt, err := controller.New(
4043
Name,
@@ -59,15 +62,18 @@ func Add(mgr manager.Manager, db *gorm.DB) error {
5962
}
6063

6164
// Reconciler reconciles addon CRs.
65+
// The history is used to ensure resources are reconciled
66+
// at least once at startup.
6267
type Reconciler struct {
6368
record.EventRecorder
6469
k8s.Client
65-
DB *gorm.DB
66-
Log logr.Logger
70+
DB *gorm.DB
71+
Log logr.Logger
72+
history map[string]byte
6773
}
6874

6975
// Reconcile a Addon CR.
70-
// Note: Must not a pointer receiver to ensure that the
76+
// Note: Must not be a pointer receiver to ensure that the
7177
// logger and other state is not shared.
7278
func (r Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (result reconcile.Result, err error) {
7379
r.Log = logr2.WithName(
@@ -86,18 +92,23 @@ func (r Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (r
8692
}
8793
return
8894
}
89-
// migrate
90-
migrated, err := r.alpha2Migration(addon)
91-
if migrated || err != nil {
95+
_, found := r.history[addon.Name]
96+
if found && addon.Reconciled() {
9297
return
9398
}
94-
// changed.
95-
err = r.addonChanged(addon)
96-
if err != nil {
99+
r.history[addon.Name] = 1
100+
addon.Status.Conditions = nil
101+
addon.Status.ObservedGeneration = addon.Generation
102+
// Changed
103+
migrated, err := r.addonChanged(addon)
104+
if migrated || err != nil {
97105
return
98106
}
107+
// Ready condition.
108+
addon.Status.Conditions = append(
109+
addon.Status.Conditions,
110+
r.ready(addon))
99111
// Apply changes.
100-
addon.Status.ObservedGeneration = addon.Generation
101112
err = r.Status().Update(context.TODO(), addon)
102113
if err != nil {
103114
return
@@ -106,47 +117,50 @@ func (r Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (r
106117
return
107118
}
108119

109-
// addonChanged an addon has been created/updated.
110-
func (r *Reconciler) addonChanged(addon *api.Addon) (err error) {
111-
return
112-
}
113-
114-
// addonDeleted an addon has been deleted.
115-
func (r *Reconciler) addonDeleted(name string) (err error) {
116-
return
117-
}
118-
119-
// alpha2Migration migrates to alpha2.
120-
func (r *Reconciler) alpha2Migration(addon *api.Addon) (migrated bool, err error) {
121-
if addon.Spec.Image != nil {
122-
if addon.Spec.Container.Image == "" {
123-
addon.Spec.Container.Image = *addon.Spec.Image
120+
// ready returns the ready condition.
121+
func (r *Reconciler) ready(addon *api.Addon) (ready v1.Condition) {
122+
ready = api.Ready
123+
ready.LastTransitionTime = v1.Now()
124+
ready.ObservedGeneration = addon.Status.ObservedGeneration
125+
err := make([]string, 0)
126+
for i := range addon.Status.Conditions {
127+
cnd := &addon.Status.Conditions[i]
128+
if cnd.Type == api.ValidationError {
129+
err = append(err, cnd.Message)
124130
}
125-
addon.Spec.Image = nil
126-
migrated = true
127131
}
128-
if addon.Spec.Resources != nil {
129-
if len(addon.Spec.Container.Resources.Limits) == 0 {
130-
addon.Spec.Container.Resources.Limits = (*addon.Spec.Resources).Limits
131-
}
132-
if len(addon.Spec.Container.Resources.Requests) == 0 {
133-
addon.Spec.Container.Resources.Requests = (*addon.Spec.Resources).Requests
134-
}
135-
addon.Spec.Resources = nil
136-
migrated = true
137-
}
138-
if addon.Spec.ImagePullPolicy != nil {
139-
if addon.Spec.Container.ImagePullPolicy == "" {
140-
addon.Spec.Container.ImagePullPolicy = *addon.Spec.ImagePullPolicy
141-
}
142-
addon.Spec.ImagePullPolicy = nil
143-
migrated = true
132+
if len(err) == 0 {
133+
ready.Status = v1.ConditionTrue
134+
ready.Reason = api.Validated
135+
ready.Message = strings.Join(err, ";")
136+
} else {
137+
ready.Status = v1.ConditionFalse
138+
ready.Reason = api.ValidationError
144139
}
140+
return
141+
}
142+
143+
// addonChanged an addon has been created/updated.
144+
func (r *Reconciler) addonChanged(addon *api.Addon) (migrated bool, err error) {
145+
migrated = addon.Migrate()
145146
if migrated {
146147
err = r.Update(context.TODO(), addon)
147148
if err != nil {
148149
return
149150
}
150151
}
152+
if addon.Spec.Container.Image == "" {
153+
cnd := api.ImageNotDefined
154+
cnd.LastTransitionTime = v1.Now()
155+
cnd.ObservedGeneration = addon.Status.ObservedGeneration
156+
addon.Status.Conditions = append(
157+
addon.Status.Conditions,
158+
cnd)
159+
}
160+
return
161+
}
162+
163+
// addonDeleted an addon has been deleted.
164+
func (r *Reconciler) addonDeleted(name string) (err error) {
151165
return
152166
}

0 commit comments

Comments
 (0)