| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- /*
- Copyright 2026 LocoStack.
- 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.
- */
- package controller
- import (
- "context"
- "fmt"
- "maps"
- corev1 "k8s.io/api/core/v1"
- apierrors "k8s.io/apimachinery/pkg/api/errors"
- apimeta "k8s.io/apimachinery/pkg/api/meta"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/runtime"
- ctrl "sigs.k8s.io/controller-runtime"
- "sigs.k8s.io/controller-runtime/pkg/client"
- "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
- logf "sigs.k8s.io/controller-runtime/pkg/log"
- "github.com/LocoStack/loco-operator/api/v1alpha1"
- "github.com/LocoStack/loco-operator/internal/reconciler"
- "github.com/LocoStack/loco-operator/pkg/templates"
- )
- // ManagedToolReconciler reconciles a ManagedTool object
- type ManagedToolReconciler struct {
- client.Client
- Scheme *runtime.Scheme
- }
- // +kubebuilder:rbac:groups=locostack.com,resources=managedtools,verbs=get;list;watch;create;update;patch;delete
- // +kubebuilder:rbac:groups=locostack.com,resources=managedtools/status,verbs=get;update;patch
- // +kubebuilder:rbac:groups=locostack.com,resources=managedtools/finalizers,verbs=update
- // +kubebuilder:rbac:groups=locostack.com,resources=components,verbs=get;list;watch;create;update;patch;delete
- // +kubebuilder:rbac:groups=locostack.com,resources=stacks,verbs=get;list;watch
- func (r *ManagedToolReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
- log := logf.FromContext(ctx)
- mt := &v1alpha1.ManagedTool{}
- if err := r.Get(ctx, req.NamespacedName, mt); err != nil {
- if apierrors.IsNotFound(err) {
- return ctrl.Result{}, nil
- }
- log.Error(err, "Failed to get ManagedTool", "namespace", req.NamespacedName.Namespace, "name", req.NamespacedName.Name)
- return ctrl.Result{}, err
- }
- patch := client.MergeFrom(mt.DeepCopy())
- componentAvailable, err := r.reconcileComponent(ctx, mt)
- if err != nil {
- return ctrl.Result{}, err
- }
- gatewayBound, err := r.reconcileGatewayStatus(ctx, mt)
- if err != nil {
- log.Error(err, "Failed to reconcile gateway status for ManagedTool", "namespace", req.NamespacedName.Namespace, "name", req.NamespacedName.Name)
- return ctrl.Result{}, err
- }
- if !componentAvailable {
- apimeta.SetStatusCondition(&mt.Status.Conditions, metav1.Condition{
- Type: "Available",
- Status: metav1.ConditionFalse,
- Reason: "ToolUnavailable",
- Message: "ManagedTool is not available",
- ObservedGeneration: mt.Generation,
- })
- } else if !gatewayBound {
- apimeta.SetStatusCondition(&mt.Status.Conditions, metav1.Condition{
- Type: "Available",
- Status: metav1.ConditionFalse,
- Reason: "GatewayUnbound",
- Message: "ManagedTool is not bound to a gateway",
- ObservedGeneration: mt.Generation,
- })
- } else {
- apimeta.SetStatusCondition(&mt.Status.Conditions, metav1.Condition{
- Type: "Available",
- Status: metav1.ConditionTrue,
- Reason: "ToolAvailable",
- Message: "ManagedTool is available",
- ObservedGeneration: mt.Generation,
- })
- }
- mt.Status.ObservedGeneration = mt.Generation
- if err := r.Status().Patch(ctx, mt, patch); err != nil {
- return ctrl.Result{}, client.IgnoreNotFound(err)
- }
- log.Info("Reconciled ManagedTool", "namespace", req.NamespacedName.Namespace, "name", req.NamespacedName.Name)
- return ctrl.Result{}, nil
- }
- func (r *ManagedToolReconciler) reconcileComponent(ctx context.Context, mt *v1alpha1.ManagedTool) (bool, error) {
- compName := fmt.Sprintf("tool-%s", mt.Name)
- comp := &v1alpha1.Component{
- ObjectMeta: metav1.ObjectMeta{
- Name: compName,
- Namespace: mt.Namespace,
- },
- }
- err := r.Get(ctx, client.ObjectKey{Namespace: mt.Namespace, Name: compName}, comp)
- tmpl, err := templates.Manager.ResolveTemplate(mt.Spec.Template, "")
- if err != nil {
- apimeta.SetStatusCondition(&mt.Status.Conditions, metav1.Condition{
- Type: "ToolRuntimeResolved",
- Status: metav1.ConditionFalse,
- Reason: "TemplateNotResolved",
- Message: fmt.Sprintf("Failed to reconcile template for ManagedTool: %v", err),
- ObservedGeneration: mt.Generation,
- })
- return false, nil
- }
- apimeta.SetStatusCondition(&mt.Status.Conditions, metav1.Condition{
- Type: "ToolRuntimeResolved",
- Status: metav1.ConditionTrue,
- Reason: "TemplateResolved",
- Message: fmt.Sprintf("Successfully resolved template for ManagedTool: %v", tmpl.Name),
- ObservedGeneration: mt.Generation,
- })
- _, err = controllerutil.CreateOrUpdate(ctx, r.Client, comp, func() error {
- if comp.Labels == nil {
- comp.Labels = make(map[string]string)
- }
- stackName := ""
- if mt.Spec.StackRef != nil {
- stackName = mt.Spec.StackRef.Name
- }
- maps.Copy(comp.Labels, reconciler.ResourceLabels(stackName, "Tool", mt.Name, ""))
- comp.Spec.Category = "Tool"
- comp.Spec.Template = &tmpl
- if mt.Spec.StackRef != nil {
- comp.Spec.StackRef = &corev1.LocalObjectReference{Name: mt.Spec.StackRef.Name}
- }
- return controllerutil.SetControllerReference(mt, comp, r.Scheme)
- })
- available := apimeta.IsStatusConditionTrue(comp.Status.Conditions, "Available")
- if available {
- mt.Status.Endpoint = comp.Status.Endpoint
- apimeta.SetStatusCondition(&mt.Status.Conditions, metav1.Condition{
- Type: "Ready",
- Status: metav1.ConditionTrue,
- Reason: "ComponentAvailable",
- Message: "Component is available",
- ObservedGeneration: mt.Generation,
- })
- } else {
- apimeta.SetStatusCondition(&mt.Status.Conditions, metav1.Condition{
- Type: "Ready",
- Status: metav1.ConditionFalse,
- Reason: "ComponentUnavailable",
- Message: "Component is not available",
- ObservedGeneration: mt.Generation,
- })
- }
- return available, err
- }
- func (r *ManagedToolReconciler) reconcileGatewayStatus(ctx context.Context, mt *v1alpha1.ManagedTool) (bool, error) {
- condition, err := reconciler.ReconcileGatewayStatus(ctx, r.Client, r.Scheme, mt.Spec.StackRef.Name, mt)
- if err != nil {
- return false, err
- }
- apimeta.SetStatusCondition(&mt.Status.Conditions, *condition)
- return condition.Status == metav1.ConditionTrue, nil
- }
- // SetupWithManager sets up the controller with the Manager.
- func (r *ManagedToolReconciler) SetupWithManager(mgr ctrl.Manager) error {
- return ctrl.NewControllerManagedBy(mgr).
- For(&v1alpha1.ManagedTool{}).
- Owns(&v1alpha1.Component{}).
- Watches(&v1alpha1.Component{}, ComponentEventHandler("ManagedTool")).
- Named("managedtool").
- Complete(r)
- }
|