Skip to content

Commit

Permalink
🐛 Revert alpha2 crd (#735)
Browse files Browse the repository at this point in the history
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 <jortel@redhat.com>
  • Loading branch information
jortel authored Jul 24, 2024
1 parent ca8c391 commit 73a0668
Show file tree
Hide file tree
Showing 29 changed files with 581 additions and 2,129 deletions.
2 changes: 1 addition & 1 deletion api/addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"

"github.com/gin-gonic/gin"
crd "github.com/konveyor/tackle2-hub/k8s/api/tackle/v1alpha2"
crd "github.com/konveyor/tackle2-hub/k8s/api/tackle/v1alpha1"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
k8s "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down
2 changes: 1 addition & 1 deletion api/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
"github.com/gin-gonic/gin/binding"
liberr "github.com/jortel/go-utils/error"
"github.com/jortel/go-utils/logr"
"github.com/konveyor/tackle2-hub/api/reflect"
"github.com/konveyor/tackle2-hub/api/sort"
"github.com/konveyor/tackle2-hub/auth"
"github.com/konveyor/tackle2-hub/model"
"github.com/konveyor/tackle2-hub/reflect"
"gopkg.in/yaml.v2"
"gorm.io/gorm"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down
2 changes: 1 addition & 1 deletion api/sort/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"

"github.com/gin-gonic/gin"
"github.com/konveyor/tackle2-hub/api/reflect"
"github.com/konveyor/tackle2-hub/reflect"
"gorm.io/gorm"
)

Expand Down
2 changes: 1 addition & 1 deletion api/taskgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"

"github.com/gin-gonic/gin"
crd "github.com/konveyor/tackle2-hub/k8s/api/tackle/v1alpha2"
crd "github.com/konveyor/tackle2-hub/k8s/api/tackle/v1alpha1"
"github.com/konveyor/tackle2-hub/model"
tasking "github.com/konveyor/tackle2-hub/task"
"gorm.io/gorm/clause"
Expand Down
108 changes: 61 additions & 47 deletions controller/addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package controller

import (
"context"
"strings"

"github.com/go-logr/logr"
logr2 "github.com/jortel/go-utils/logr"
api "github.com/konveyor/tackle2-hub/k8s/api/tackle/v1alpha2"
api "github.com/konveyor/tackle2-hub/k8s/api/tackle/v1alpha1"
"github.com/konveyor/tackle2-hub/settings"
"gorm.io/gorm"
k8serr "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/storage/names"
"k8s.io/client-go/tools/record"
k8s "sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -32,9 +34,10 @@ var Settings = &settings.Settings
// Add the controller.
func Add(mgr manager.Manager, db *gorm.DB) error {
reconciler := &Reconciler{
Client: mgr.GetClient(),
Log: log,
DB: db,
history: make(map[string]byte),
Client: mgr.GetClient(),
Log: log,
DB: db,
}
cnt, err := controller.New(
Name,
Expand All @@ -59,15 +62,18 @@ func Add(mgr manager.Manager, db *gorm.DB) error {
}

// Reconciler reconciles addon CRs.
// The history is used to ensure resources are reconciled
// at least once at startup.
type Reconciler struct {
record.EventRecorder
k8s.Client
DB *gorm.DB
Log logr.Logger
DB *gorm.DB
Log logr.Logger
history map[string]byte
}

// Reconcile a Addon CR.
// Note: Must not a pointer receiver to ensure that the
// Note: Must not be a pointer receiver to ensure that the
// logger and other state is not shared.
func (r Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (result reconcile.Result, err error) {
r.Log = logr2.WithName(
Expand All @@ -86,18 +92,23 @@ func (r Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (r
}
return
}
// migrate
migrated, err := r.alpha2Migration(addon)
if migrated || err != nil {
_, found := r.history[addon.Name]
if found && addon.Reconciled() {
return
}
// changed.
err = r.addonChanged(addon)
if err != nil {
r.history[addon.Name] = 1
addon.Status.Conditions = nil
addon.Status.ObservedGeneration = addon.Generation
// Changed
migrated, err := r.addonChanged(addon)
if migrated || err != nil {
return
}
// Ready condition.
addon.Status.Conditions = append(
addon.Status.Conditions,
r.ready(addon))
// Apply changes.
addon.Status.ObservedGeneration = addon.Generation
err = r.Status().Update(context.TODO(), addon)
if err != nil {
return
Expand All @@ -106,47 +117,50 @@ func (r Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (r
return
}

// addonChanged an addon has been created/updated.
func (r *Reconciler) addonChanged(addon *api.Addon) (err error) {
return
}

// addonDeleted an addon has been deleted.
func (r *Reconciler) addonDeleted(name string) (err error) {
return
}

// alpha2Migration migrates to alpha2.
func (r *Reconciler) alpha2Migration(addon *api.Addon) (migrated bool, err error) {
if addon.Spec.Image != nil {
if addon.Spec.Container.Image == "" {
addon.Spec.Container.Image = *addon.Spec.Image
// ready returns the ready condition.
func (r *Reconciler) ready(addon *api.Addon) (ready v1.Condition) {
ready = api.Ready
ready.LastTransitionTime = v1.Now()
ready.ObservedGeneration = addon.Status.ObservedGeneration
err := make([]string, 0)
for i := range addon.Status.Conditions {
cnd := &addon.Status.Conditions[i]
if cnd.Type == api.ValidationError {
err = append(err, cnd.Message)
}
addon.Spec.Image = nil
migrated = true
}
if addon.Spec.Resources != nil {
if len(addon.Spec.Container.Resources.Limits) == 0 {
addon.Spec.Container.Resources.Limits = (*addon.Spec.Resources).Limits
}
if len(addon.Spec.Container.Resources.Requests) == 0 {
addon.Spec.Container.Resources.Requests = (*addon.Spec.Resources).Requests
}
addon.Spec.Resources = nil
migrated = true
}
if addon.Spec.ImagePullPolicy != nil {
if addon.Spec.Container.ImagePullPolicy == "" {
addon.Spec.Container.ImagePullPolicy = *addon.Spec.ImagePullPolicy
}
addon.Spec.ImagePullPolicy = nil
migrated = true
if len(err) == 0 {
ready.Status = v1.ConditionTrue
ready.Reason = api.Validated
ready.Message = strings.Join(err, ";")
} else {
ready.Status = v1.ConditionFalse
ready.Reason = api.ValidationError
}
return
}

// addonChanged an addon has been created/updated.
func (r *Reconciler) addonChanged(addon *api.Addon) (migrated bool, err error) {
migrated = addon.Migrate()
if migrated {
err = r.Update(context.TODO(), addon)
if err != nil {
return
}
}
if addon.Spec.Container.Image == "" {
cnd := api.ImageNotDefined
cnd.LastTransitionTime = v1.Now()
cnd.ObservedGeneration = addon.Status.ObservedGeneration
addon.Status.Conditions = append(
addon.Status.Conditions,
cnd)
}
return
}

// addonDeleted an addon has been deleted.
func (r *Reconciler) addonDeleted(name string) (err error) {
return
}
Loading

0 comments on commit 73a0668

Please sign in to comment.