Use `LastHandledReconcileAt` in reconcile commands

pull/281/head
Hidde Beydals 4 years ago
parent f216273008
commit 84e36ed847

@ -63,7 +63,7 @@ jobs:
--health-check="Deployment/frontend.dev" \ --health-check="Deployment/frontend.dev" \
--health-check="Deployment/backend.dev" \ --health-check="Deployment/backend.dev" \
--health-check-timeout=3m --health-check-timeout=3m
- name: gotk sync kustomization --with-source - name: gotk reconcile kustomization --with-source
run: | run: |
./bin/gotk reconcile kustomization podinfo --with-source ./bin/gotk reconcile kustomization podinfo --with-source
- name: gotk get kustomizations - name: gotk get kustomizations
@ -99,6 +99,9 @@ jobs:
--target-namespace=default \ --target-namespace=default \
--source=GitRepository/podinfo \ --source=GitRepository/podinfo \
--chart=./charts/podinfo --chart=./charts/podinfo
- name: gotk reconcile helmrelease --with-source
run: |
./bin/gotk reconcile helmrelease podinfo-git --with-source
- name: gotk get helmreleases - name: gotk get helmreleases
run: | run: |
./bin/gotk get helmreleases ./bin/gotk get helmreleases

@ -286,3 +286,28 @@ func isHelmChartReady(ctx context.Context, kubeClient client.Client, name, names
return false, nil return false, nil
} }
} }
func isHelmReleaseReady(ctx context.Context, kubeClient client.Client, name, namespace string) wait.ConditionFunc {
return func() (bool, error) {
var helmRelease helmv2.HelmRelease
namespacedName := types.NamespacedName{
Namespace: namespace,
Name: name,
}
err := kubeClient.Get(ctx, namespacedName, &helmRelease)
if err != nil {
return false, err
}
if c := meta.GetCondition(helmRelease.Status.Conditions, meta.ReadyCondition); c != nil {
switch c.Status {
case corev1.ConditionTrue:
return true, nil
case corev1.ConditionFalse:
return false, fmt.Errorf(c.Message)
}
}
return false, nil
}
}

@ -19,11 +19,12 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/fluxcd/pkg/apis/meta"
"time" "time"
"github.com/spf13/cobra" "github.com/fluxcd/pkg/apis/meta"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"github.com/spf13/cobra"
"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"
@ -94,7 +95,8 @@ func reconcileHrCmdRun(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return err return err
} }
} else { }
logger.Actionf("annotating HelmRelease %s in %s namespace", name, namespace) logger.Actionf("annotating HelmRelease %s in %s namespace", name, namespace)
if helmRelease.Annotations == nil { if helmRelease.Annotations == nil {
helmRelease.Annotations = map[string]string{ helmRelease.Annotations = map[string]string{
@ -107,11 +109,11 @@ func reconcileHrCmdRun(cmd *cobra.Command, args []string) error {
return err return err
} }
logger.Successf("HelmRelease annotated") logger.Successf("HelmRelease annotated")
}
logger.Waitingf("waiting for HelmRelease reconciliation") logger.Waitingf("waiting for HelmRelease reconciliation")
if err := wait.PollImmediate(pollInterval, timeout, if err := wait.PollImmediate(pollInterval, timeout,
isHelmReleaseReady(ctx, kubeClient, name, namespace)); err != nil { helmReleaseReconciliationHandled(ctx, kubeClient, name, namespace, helmRelease.Status.LastHandledReconcileAt),
); err != nil {
return err return err
} }
@ -121,16 +123,19 @@ func reconcileHrCmdRun(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return err return err
} }
if c := meta.GetCondition(helmRelease.Status.Conditions, meta.ReadyCondition); c != nil {
if helmRelease.Status.LastAppliedRevision != "" { switch c.Status {
logger.Successf("reconciled revision %s", helmRelease.Status.LastAppliedRevision) case corev1.ConditionFalse:
} else {
return fmt.Errorf("HelmRelease reconciliation failed") return fmt.Errorf("HelmRelease reconciliation failed")
default:
logger.Successf("reconciled revision %s", helmRelease.Status.LastAppliedRevision)
}
} }
return nil return nil
} }
func isHelmReleaseReady(ctx context.Context, kubeClient client.Client, name, namespace string) wait.ConditionFunc { func helmReleaseReconciliationHandled(ctx context.Context, kubeClient client.Client,
name, namespace, lastHandledReconcileAt string) wait.ConditionFunc {
return func() (bool, error) { return func() (bool, error) {
var helmRelease helmv2.HelmRelease var helmRelease helmv2.HelmRelease
namespacedName := types.NamespacedName{ namespacedName := types.NamespacedName{
@ -143,14 +148,6 @@ func isHelmReleaseReady(ctx context.Context, kubeClient client.Client, name, nam
return false, err return false, err
} }
if c := meta.GetCondition(helmRelease.Status.Conditions, meta.ReadyCondition); c != nil { return helmRelease.Status.LastHandledReconcileAt != lastHandledReconcileAt, nil
switch c.Status {
case corev1.ConditionTrue:
return true, nil
case corev1.ConditionFalse:
return false, fmt.Errorf(c.Message)
}
}
return false, nil
} }
} }

@ -19,9 +19,12 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/fluxcd/pkg/apis/meta"
"time" "time"
"github.com/fluxcd/pkg/apis/meta"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait" "k8s.io/apimachinery/pkg/util/wait"
@ -90,7 +93,8 @@ func reconcileKsCmdRun(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return err return err
} }
} else { }
logger.Actionf("annotating kustomization %s in %s namespace", name, namespace) logger.Actionf("annotating kustomization %s in %s namespace", name, namespace)
if kustomization.Annotations == nil { if kustomization.Annotations == nil {
kustomization.Annotations = map[string]string{ kustomization.Annotations = map[string]string{
@ -103,11 +107,12 @@ func reconcileKsCmdRun(cmd *cobra.Command, args []string) error {
return err return err
} }
logger.Successf("kustomization annotated") logger.Successf("kustomization annotated")
}
logger.Waitingf("waiting for kustomization reconciliation") logger.Waitingf("waiting for kustomization reconciliation")
if err := wait.PollImmediate(pollInterval, timeout, if err := wait.PollImmediate(
isKustomizationReady(ctx, kubeClient, name, namespace)); err != nil { pollInterval, timeout,
kustomizeReconciliationHandled(ctx, kubeClient, name, namespace, kustomization.Status.LastHandledReconcileAt),
); err != nil {
return err return err
} }
@ -117,11 +122,31 @@ func reconcileKsCmdRun(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return err return err
} }
if c := meta.GetCondition(kustomization.Status.Conditions, meta.ReadyCondition); c != nil {
if kustomization.Status.LastAppliedRevision != "" { switch c.Status {
case corev1.ConditionFalse:
return fmt.Errorf("kustomization reconciliation failed")
default:
logger.Successf("reconciled revision %s", kustomization.Status.LastAppliedRevision) logger.Successf("reconciled revision %s", kustomization.Status.LastAppliedRevision)
} else { }
return fmt.Errorf("kustomization sync failed")
} }
return nil return nil
} }
func kustomizeReconciliationHandled(ctx context.Context, kubeClient client.Client,
name, namespace, lastHandledReconcileAt string) wait.ConditionFunc {
return func() (bool, error) {
var kustomize kustomizev1.Kustomization
namespacedName := types.NamespacedName{
Namespace: namespace,
Name: name,
}
err := kubeClient.Get(ctx, namespacedName, &kustomize)
if err != nil {
return false, err
}
return kustomize.Status.LastHandledReconcileAt != lastHandledReconcileAt, nil
}
}

Loading…
Cancel
Save