95 lines
3.3 KiB
Go
95 lines
3.3 KiB
Go
/*
|
|
Copyright 2025.
|
|
|
|
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"
|
|
"time"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
logf "sigs.k8s.io/controller-runtime/pkg/log"
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
|
|
|
yahov1alpha1 "github.com/allanger/yaho/api/v1alpha1"
|
|
"github.com/allanger/yaho/internal/downloader"
|
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
|
)
|
|
|
|
// HelmReleaseReconciler reconciles a HelmRelease object
|
|
type HelmReleaseReconciler struct {
|
|
client.Client
|
|
Scheme *runtime.Scheme
|
|
}
|
|
|
|
// +kubebuilder:rbac:groups=yaho.badhouseplants.net,resources=helmreleases,verbs=get;list;watch;create;update;patch;delete
|
|
// +kubebuilder:rbac:groups=yaho.badhouseplants.net,resources=helmreleases/status,verbs=get;update;patch
|
|
// +kubebuilder:rbac:groups=yaho.badhouseplants.net,resources=helmreleases/finalizers,verbs=update
|
|
|
|
func (r *HelmReleaseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
|
log := logf.FromContext(ctx)
|
|
log.Info("reconciliation is started")
|
|
|
|
reconcilePeriod := 30 * time.Second
|
|
reconcileResult := reconcile.Result{RequeueAfter: reconcilePeriod}
|
|
helmReleaseCR := &yahov1alpha1.HelmRelease{}
|
|
err := r.Get(ctx, req.NamespacedName, helmReleaseCR)
|
|
if err != nil {
|
|
if k8serrors.IsNotFound(err) {
|
|
// Requested object not found, could have been deleted after reconcile request.
|
|
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
|
|
// Return and don't requeue
|
|
return reconcileResult, nil
|
|
}
|
|
log.Error(err, "An unxexpected error has occurred during the reconciliation")
|
|
// Error reading the object - requeue the request.
|
|
return reconcileResult, err
|
|
}
|
|
|
|
// Update object status always when function exit abnormally or through a panic.
|
|
defer func() {
|
|
if err := r.Status().Update(ctx, helmReleaseCR); err != nil {
|
|
log.Error(err, "Failed to update status")
|
|
}
|
|
}()
|
|
|
|
// First setup all the controller logic
|
|
// Pull the chart t a temporary directory
|
|
//
|
|
// TODO(user): your logic here
|
|
path, err := downloader.PullChart(ctx, helmReleaseCR.Spec.Repository, helmReleaseCR.Spec.Chart, helmReleaseCR.Spec.Version, nil)
|
|
if err != nil {
|
|
log.Error(err, "An unexpected error has occurred while trying to fetch a chart")
|
|
return reconcileResult, nil
|
|
}
|
|
log.Info("Pulled a chart", "path", path)
|
|
//path, err := downloader.PullChart(repository, chart, version)
|
|
//if err != nil { return ... }
|
|
//err := helm.InstallOrUpdate(); if err != nil { return ...}
|
|
|
|
return ctrl.Result{}, nil
|
|
}
|
|
|
|
// SetupWithManager sets up the controller with the Manager.
|
|
func (r *HelmReleaseReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
|
return ctrl.NewControllerManagedBy(mgr).
|
|
For(&yahov1alpha1.HelmRelease{}).
|
|
Named("helmrelease").
|
|
Complete(r)
|
|
}
|