|
|
@ -21,12 +21,13 @@ import (
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/fluxcd/flux2/internal/utils"
|
|
|
|
|
|
|
|
"github.com/fluxcd/pkg/apis/meta"
|
|
|
|
"github.com/fluxcd/pkg/apis/meta"
|
|
|
|
|
|
|
|
"k8s.io/client-go/util/retry"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/fluxcd/flux2/internal/utils"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
apimeta "k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
apimeta "k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
@ -66,61 +67,57 @@ func reconcileSourceHelmCmdRun(cmd *cobra.Command, args []string) error {
|
|
|
|
Namespace: namespace,
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: name,
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var repository sourcev1.HelmRepository
|
|
|
|
logger.Actionf("annotating HelmRepository source %s in %s namespace", name, namespace)
|
|
|
|
err = kubeClient.Get(ctx, namespacedName, &repository)
|
|
|
|
var helmRepository sourcev1.HelmRepository
|
|
|
|
|
|
|
|
err = kubeClient.Get(ctx, namespacedName, &helmRepository)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if helmRepository.Annotations == nil {
|
|
|
|
logger.Actionf("annotating HelmRepository source %s in %s namespace", name, namespace)
|
|
|
|
helmRepository.Annotations = map[string]string{
|
|
|
|
if err := requestHelmRepositoryReconciliation(ctx, kubeClient, namespacedName, &repository); err != nil {
|
|
|
|
meta.ReconcileAtAnnotation: time.Now().Format(time.RFC3339Nano),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
helmRepository.Annotations[meta.ReconcileAtAnnotation] = time.Now().Format(time.RFC3339Nano)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := kubeClient.Update(ctx, &helmRepository); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.Successf("HelmRepository source annotated")
|
|
|
|
logger.Successf("HelmRepository source annotated")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lastHandledReconcileAt := repository.Status.LastHandledReconcileAt
|
|
|
|
logger.Waitingf("waiting for HelmRepository source reconciliation")
|
|
|
|
logger.Waitingf("waiting for HelmRepository source reconciliation")
|
|
|
|
if err := wait.PollImmediate(pollInterval, timeout,
|
|
|
|
if err := wait.PollImmediate(pollInterval, timeout,
|
|
|
|
isHelmRepositoryReady(ctx, kubeClient, namespacedName, &helmRepository)); err != nil {
|
|
|
|
helmRepositoryReconciliationHandled(ctx, kubeClient, namespacedName, &repository, lastHandledReconcileAt)); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.Successf("HelmRepository source reconciliation completed")
|
|
|
|
logger.Successf("HelmRepository source reconciliation completed")
|
|
|
|
|
|
|
|
|
|
|
|
if helmRepository.Status.Artifact == nil {
|
|
|
|
if apimeta.IsStatusConditionFalse(repository.Status.Conditions, meta.ReadyCondition) {
|
|
|
|
return fmt.Errorf("HelmRepository source reconciliation completed but no artifact was found")
|
|
|
|
return fmt.Errorf("HelmRepository source reconciliation failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.Successf("fetched revision %s", helmRepository.Status.Artifact.Revision)
|
|
|
|
logger.Successf("fetched revision %s", repository.Status.Artifact.Revision)
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func isHelmRepositoryReady(ctx context.Context, kubeClient client.Client,
|
|
|
|
func helmRepositoryReconciliationHandled(ctx context.Context, kubeClient client.Client,
|
|
|
|
namespacedName types.NamespacedName, helmRepository *sourcev1.HelmRepository) wait.ConditionFunc {
|
|
|
|
namespacedName types.NamespacedName, repository *sourcev1.HelmRepository, lastHandledReconcileAt string) wait.ConditionFunc {
|
|
|
|
return func() (bool, error) {
|
|
|
|
return func() (bool, error) {
|
|
|
|
err := kubeClient.Get(ctx, namespacedName, helmRepository)
|
|
|
|
err := kubeClient.Get(ctx, namespacedName, repository)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return repository.Status.LastHandledReconcileAt != lastHandledReconcileAt, nil
|
|
|
|
// Confirm the state we are observing is for the current generation
|
|
|
|
}
|
|
|
|
if helmRepository.Generation != helmRepository.Status.ObservedGeneration {
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if c := apimeta.FindStatusCondition(helmRepository.Status.Conditions, meta.ReadyCondition); c != nil {
|
|
|
|
func requestHelmRepositoryReconciliation(ctx context.Context, kubeClient client.Client,
|
|
|
|
switch c.Status {
|
|
|
|
namespacedName types.NamespacedName, repository *sourcev1.HelmRepository) error {
|
|
|
|
case metav1.ConditionTrue:
|
|
|
|
return retry.RetryOnConflict(retry.DefaultBackoff, func() (err error) {
|
|
|
|
return true, nil
|
|
|
|
if err := kubeClient.Get(ctx, namespacedName, repository); err != nil {
|
|
|
|
case metav1.ConditionFalse:
|
|
|
|
return err
|
|
|
|
return false, fmt.Errorf(c.Message)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if repository.Annotations == nil {
|
|
|
|
|
|
|
|
repository.Annotations = map[string]string{
|
|
|
|
|
|
|
|
meta.ReconcileAtAnnotation: time.Now().Format(time.RFC3339Nano),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
} else {
|
|
|
|
|
|
|
|
repository.Annotations[meta.ReconcileAtAnnotation] = time.Now().Format(time.RFC3339Nano)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return kubeClient.Update(ctx, repository)
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|