Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create tiertemplaterevision for each tiertemplate #1103

Open
wants to merge 79 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
8d24435
remove spacebinding request migration controller
mfrancisc Dec 7, 2023
a25fb02
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc Jan 2, 2024
fd840c6
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc Feb 7, 2024
0221d40
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc Feb 14, 2024
0f4717d
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc Feb 19, 2024
0977ae6
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc Mar 26, 2024
7aa35c5
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc Apr 3, 2024
d09f3b8
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc Apr 4, 2024
79c20ca
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc Apr 25, 2024
b21f39f
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc May 27, 2024
b0aea00
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc May 31, 2024
e9e35bc
Merge branch 'master' of github.com:mfrancisc/host-operator
mfrancisc Jul 29, 2024
969562e
Merge branch 'master' of github.com:mfrancisc/host-operator
Aug 28, 2024
344bc04
Merge branch 'master' of github.com:mfrancisc/host-operator
Sep 12, 2024
ea71877
Merge branch 'master' of github.com:mfrancisc/host-operator
Sep 23, 2024
ef294f7
Merge branch 'master' of github.com:mfrancisc/host-operator
Sep 27, 2024
1f8d51d
Merge branch 'master' of github.com:mfrancisc/host-operator
Oct 31, 2024
9a42d87
create TTR
Nov 6, 2024
b27cde7
Merge branch 'master' of github.com:mfrancisc/host-operator
Nov 6, 2024
eabc92f
Merge branch 'master' into createttr
Nov 6, 2024
a5fd29a
create TTR only if TierTemplate.Spec.TemplateObjects field is set.
Nov 11, 2024
53d3cc1
add owner reference on ttrs
Nov 11, 2024
8571495
fix linter
Nov 12, 2024
dfd24b0
refactor and simplify tests
Nov 12, 2024
bbb2689
Merge branch 'master' of github.com:mfrancisc/host-operator
Nov 18, 2024
31820df
Merge branch 'master' into createttr
Nov 18, 2024
0aa5793
use namespace from nstemplatetier
Nov 18, 2024
6c85086
save overall ttrcreated
Nov 18, 2024
23523ec
Merge branch 'master' into createttr
mfrancisc Nov 19, 2024
9d3e46a
watch ttrs, replace variables
Nov 19, 2024
efe8bf4
Merge remote-tracking branch 'origin/createttr' into createttr
Nov 19, 2024
085cc00
add todo for deletion, remove owner reference
Nov 20, 2024
5297fe8
Merge branch 'master' into createttr
mfrancisc Nov 21, 2024
e8ecb9d
rollback watcher
Nov 25, 2024
d1c69e8
remove deprectaed errors pkg
Nov 28, 2024
582152c
improve note
Nov 28, 2024
f8df0b8
remove comparision with tiertemplate name
Nov 28, 2024
9c61b74
move log line
Nov 28, 2024
7e8747d
wip ttr tests
Nov 28, 2024
ec51de9
Merge branch 'master' of github.com:mfrancisc/host-operator
Nov 28, 2024
4cfc547
add parameters in TierTemplateRevision
Nov 28, 2024
8249f0d
handle parameters in TierTemplateRevision
Nov 28, 2024
0a47055
Merge branch 'addparamsinttr' into createttr
Nov 30, 2024
95c3126
add tier in ttr name
Nov 30, 2024
87faf47
update api
Dec 2, 2024
37012c2
Merge branch 'master' into createttr
mfrancisc Dec 2, 2024
79b4e2e
Merge branch 'master' into createttr
mfrancisc Dec 12, 2024
cc3a260
Merge branch 'master' of github.com:mfrancisc/host-operator
Dec 16, 2024
8b64a09
Merge branch 'master' into createttr
mfrancisc Dec 16, 2024
df0003d
Merge branch 'master' into createttr
mfrancisc Dec 19, 2024
14b1b26
Merge branch 'master' of github.com:mfrancisc/host-operator
Jan 7, 2025
42d877e
address pr reviews
Jan 8, 2025
bdf02b3
fix linter
Jan 8, 2025
3a521fe
add todo
Jan 8, 2025
ee41c14
cleanup
Jan 8, 2025
28b6608
Merge branch 'master' of github.com:mfrancisc/host-operator
Jan 9, 2025
fc2d1e3
Merge branch 'master' into createttr
Jan 9, 2025
c2a5ee5
Merge branch 'master' into createttr
mfrancisc Jan 13, 2025
7efa4c4
Update controllers/nstemplatetier/nstemplatetier_controller.go
mfrancisc Jan 13, 2025
9f55b2b
Update controllers/nstemplatetier/nstemplatetier_controller.go
mfrancisc Jan 13, 2025
682ae7f
Update controllers/nstemplatetier/nstemplatetier_controller.go
mfrancisc Jan 13, 2025
a8897cc
Update controllers/nstemplatetier/nstemplatetier_controller.go
mfrancisc Jan 13, 2025
a9c166b
Merge branch 'master' into createttr
MatousJobanek Jan 14, 2025
fdd4663
Update controllers/nstemplatetier/nstemplatetier_controller_test.go
mfrancisc Jan 14, 2025
09cd3fc
Update controllers/nstemplatetier/nstemplatetier_controller_test.go
mfrancisc Jan 14, 2025
cc1f580
Update controllers/nstemplatetier/nstemplatetier_controller_test.go
mfrancisc Jan 14, 2025
9a4a5ca
Update controllers/nstemplatetier/nstemplatetier_controller_test.go
mfrancisc Jan 14, 2025
d5e80bf
Update controllers/nstemplatetier/nstemplatetier_controller_test.go
mfrancisc Jan 14, 2025
9c2769b
fix tests and remove dns check
Jan 14, 2025
a98a8e0
Merge branch 'master' into createttr
fbm3307 Jan 15, 2025
321cca7
Update controllers/nstemplatetier/nstemplatetier_controller.go
mfrancisc Jan 15, 2025
0729bd4
fix linter
Jan 15, 2025
6d72c89
fix linter
Jan 15, 2025
d844c83
Merge branch 'master' of github.com:mfrancisc/host-operator
Jan 16, 2025
0ebf1df
Merge branch 'master' into createttr
Jan 16, 2025
55135d1
Merge branch 'master' into createttr
mfrancisc Jan 16, 2025
e00a40e
address pr comments
Jan 17, 2025
a08f667
Merge branch 'master' of github.com:mfrancisc/host-operator
Jan 17, 2025
c6b8f7b
Merge branch 'master' into createttr
Jan 17, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 166 additions & 3 deletions controllers/nstemplatetier/nstemplatetier_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package nstemplatetier

import (
"context"
"fmt"
"time"

toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
"github.com/codeready-toolchain/host-operator/controllers/toolchainconfig"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/log"

errs "github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -49,13 +52,173 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
}
// Error reading the object - requeue the request.
logger.Error(err, "unable to get the current NSTemplateTier")
return reconcile.Result{}, errs.Wrap(err, "unable to get the current NSTemplateTier")
return reconcile.Result{}, fmt.Errorf("unable to get the current NSTemplateTier: %w", err)
}

_, err := toolchainconfig.GetToolchainConfig(r.Client)
if err != nil {
return reconcile.Result{}, errs.Wrapf(err, "unable to get ToolchainConfig")
return reconcile.Result{}, fmt.Errorf("unable to get ToolchainConfig: %w", err)
}

// check if the `status.revisions` field is up-to-date and create a TTR for each TierTemplate
if created, err := r.ensureRevision(ctx, tier); err != nil {
// todo add/update ready condition false in the NSTemplateTier when something fails
return reconcile.Result{}, fmt.Errorf("unable to create new TierTemplateRevision after NSTemplateTier changed: %w", err)
} else if created {
logger.Info("Requeue after creating a new TTR")
mfrancisc marked this conversation as resolved.
Show resolved Hide resolved
return reconcile.Result{Requeue: true, RequeueAfter: time.Second}, nil
mfrancisc marked this conversation as resolved.
Show resolved Hide resolved
}

return reconcile.Result{}, nil
}

// ensureRevision ensures that there is a TierTemplateRevision CR for each of the TierTemplate.
// returns `true` if a new TierTemplateRevision CR was created, `err` if something wrong happened
func (r *Reconciler) ensureRevision(ctx context.Context, nsTmplTier *toolchainv1alpha1.NSTemplateTier) (bool, error) {
logger := log.FromContext(ctx)
refs := getNSTemplateTierRefs(nsTmplTier)

// init revisions
if nsTmplTier.Status.Revisions == nil {
nsTmplTier.Status.Revisions = map[string]string{}
}
// check for TierTemplates and TierTemplateRevisions associated with the NSTemplateTier
ttrCreated := false
for _, tierTemplateRef := range refs {
// get the TierTemplate
var tierTemplate toolchainv1alpha1.TierTemplate
if err := r.Client.Get(ctx, types.NamespacedName{Namespace: nsTmplTier.GetNamespace(), Name: tierTemplateRef}, &tierTemplate); err != nil {
// something went wrong or we haven't found the TierTemplate
return false, err
mfrancisc marked this conversation as resolved.
Show resolved Hide resolved
}

// check if there is TTR associated with this TierTemplate
ttrCreatedLatest, ttrName, err := r.ensureTTRforTemplate(ctx, nsTmplTier, &tierTemplate)
ttrCreated = ttrCreated || ttrCreatedLatest
mfrancisc marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return false, err
}
nsTmplTier.Status.Revisions[tierTemplate.GetName()] = ttrName
}
// TODO handle removal of TierTemplate from NSTemplateTier
// scenario:
// a. TierTemplate is removed/replaced from NSTemplateTier.Spec
// b. NSTemplateTier.Status.Revisions must be cleaned up
// Thus here we should iterate over the Status.Revisions field
// and check if there is any reference to a TierTemplate that is not in the Spec anymore
if ttrCreated {
// we need to update the status.revisions with the new ttrs
logger.Info("ttr created updating status")
mfrancisc marked this conversation as resolved.
Show resolved Hide resolved
if err := r.Client.Status().Update(ctx, nsTmplTier); err != nil {
return ttrCreated, err
}
}

return ttrCreated, nil
}

func (r *Reconciler) ensureTTRforTemplate(ctx context.Context, nsTmplTier *toolchainv1alpha1.NSTemplateTier, tierTemplate *toolchainv1alpha1.TierTemplate) (bool, string, error) {
logger := log.FromContext(ctx)
// tierTemplate doesn't support TTRs
// we set TierTemplate as revisions
// TODO this step will be removed once we convert all TierTemplates to TTRs
if tierTemplate.Spec.TemplateObjects == nil {
_, ok := nsTmplTier.Status.Revisions[tierTemplate.GetName()]
if !ok {
return true, tierTemplate.GetName(), nil
}
// nothing to update
return false, "", nil
}

if tierTemplateRevisionName, found := nsTmplTier.Status.Revisions[tierTemplate.GetName()]; found {
logger.Info("TTR set in the status.revisions for tiertemplate", "tierTemplate.Name", tierTemplate.GetName(), "ttr.Name", tierTemplateRevisionName)
var tierTemplateRevision toolchainv1alpha1.TierTemplateRevision
if err := r.Client.Get(ctx, types.NamespacedName{Namespace: nsTmplTier.GetNamespace(), Name: tierTemplateRevisionName}, &tierTemplateRevision); err != nil {
if errors.IsNotFound(err) {
// no tierTemplateRevision CR was found,
logger.Info("TTR CR not found", "tierTemplateRevision.Name", tierTemplateRevisionName)
// let's create one
ttrName, err := r.createNewTierTemplateRevision(ctx, nsTmplTier, tierTemplate)
return true, ttrName, err
} else {
// something wrong happened
return false, "", err
}
}
// TODO compare TierTemplate content with TTR content
// if the TierTemplate has changes we need to create new TTR
} else {
// no revision was set for this TierTemplate CR, let's create a TTR for it
ttrName, err := r.createNewTierTemplateRevision(ctx, nsTmplTier, tierTemplate)
return true, ttrName, err
}
// nothing changed
return false, "", nil
}

func (r *Reconciler) createNewTierTemplateRevision(ctx context.Context, nsTmplTier *toolchainv1alpha1.NSTemplateTier, tierTemplate *toolchainv1alpha1.TierTemplate) (string, error) {
ttr := NewTTR(tierTemplate, nsTmplTier)
ttr, err := r.createTTR(ctx, ttr, tierTemplate)
if err != nil {
// something went wrong while creating new ttr
return "", err
}
return ttr.GetName(), nil
}

// getNSTemplateTierRefs returns a list with all the refs from the NSTemplateTier
func getNSTemplateTierRefs(tmplTier *toolchainv1alpha1.NSTemplateTier) []string {
var refs []string
for _, ns := range tmplTier.Spec.Namespaces {
refs = append(refs, ns.TemplateRef)
}
if tmplTier.Spec.ClusterResources != nil {
refs = append(refs, tmplTier.Spec.ClusterResources.TemplateRef)
}

roles := make([]string, 0, len(tmplTier.Spec.SpaceRoles))
for r := range tmplTier.Spec.SpaceRoles {
roles = append(roles, r)
}
for _, r := range roles {
refs = append(refs, tmplTier.Spec.SpaceRoles[r].TemplateRef)
}
return refs
}

func (r *Reconciler) createTTR(ctx context.Context, ttr *toolchainv1alpha1.TierTemplateRevision, tmplTier *toolchainv1alpha1.TierTemplate) (*toolchainv1alpha1.TierTemplateRevision, error) {
err := r.Client.Create(ctx, ttr)
if err != nil {
return nil, fmt.Errorf("unable to create TierTemplateRevision: %w", err)
}

logger := log.FromContext(ctx)
logger.Info("created TierTemplateRevision", "tierTemplateRevision.Name", ttr.Name, "tierTemplate.Name", tmplTier.Name)
return ttr, nil
}

// NewTTR creates a TierTemplateRevision CR for a given TierTemplate object.
func NewTTR(tierTmpl *toolchainv1alpha1.TierTemplate, nsTmplTier *toolchainv1alpha1.NSTemplateTier) *toolchainv1alpha1.TierTemplateRevision {
tierName := nsTmplTier.GetName()
tierTemplateName := tierTmpl.GetName()
labels := map[string]string{
toolchainv1alpha1.TierLabelKey: tierName,
toolchainv1alpha1.TemplateRefLabelKey: tierTemplateName,
}

newTTRName := fmt.Sprintf("%s-%s-", tierName, tierTemplateName)
ttr := &toolchainv1alpha1.TierTemplateRevision{
ObjectMeta: metav1.ObjectMeta{
Namespace: tierTmpl.GetNamespace(),
GenerateName: newTTRName + "-",
Labels: labels,
},
Spec: toolchainv1alpha1.TierTemplateRevisionSpec{
TemplateObjects: tierTmpl.Spec.TemplateObjects,
mfrancisc marked this conversation as resolved.
Show resolved Hide resolved
Parameters: nsTmplTier.Spec.Parameters, // save the parameters from the NSTemplateTier,to detect further changes and for evaluating those in the TemplateObjects when provisioning Spaces.
},
}

return ttr
}
Loading
Loading