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

Make use of apis/meta helpers and improve reconcile logic #281

Merged
merged 2 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
--health-check="Deployment/frontend.dev" \
--health-check="Deployment/backend.dev" \
--health-check-timeout=3m
- name: gotk sync kustomization --with-source
- name: gotk reconcile kustomization --with-source
run: |
./bin/gotk reconcile kustomization podinfo --with-source
- name: gotk get kustomizations
Expand Down Expand Up @@ -99,6 +99,9 @@ jobs:
--target-namespace=default \
--source=GitRepository/podinfo \
--chart=./charts/podinfo
- name: gotk reconcile helmrelease --with-source
run: |
./bin/gotk reconcile helmrelease podinfo-git --with-source
- name: gotk get helmreleases
run: |
./bin/gotk get helmreleases
Expand Down
38 changes: 31 additions & 7 deletions cmd/gotk/create_helmrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,37 @@ func isHelmChartReady(ctx context.Context, kubeClient client.Client, name, names
return false, err
}

for _, condition := range helmChart.Status.Conditions {
if condition.Type == meta.ReadyCondition {
if condition.Status == corev1.ConditionTrue {
return true, nil
} else if condition.Status == corev1.ConditionFalse {
return false, fmt.Errorf(condition.Message)
}
if c := meta.GetCondition(helmChart.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
}
}

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
Expand Down
13 changes: 6 additions & 7 deletions cmd/gotk/create_kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,12 @@ func isKustomizationReady(ctx context.Context, kubeClient client.Client, name, n
return false, err
}

for _, condition := range kustomization.Status.Conditions {
if condition.Type == meta.ReadyCondition {
if condition.Status == corev1.ConditionTrue {
return true, nil
} else if condition.Status == corev1.ConditionFalse {
return false, fmt.Errorf(condition.Message)
}
if c := meta.GetCondition(kustomization.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
Expand Down
13 changes: 6 additions & 7 deletions cmd/gotk/create_source_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,12 @@ func isGitRepositoryReady(ctx context.Context, kubeClient client.Client, name, n
return false, err
}

for _, condition := range gitRepository.Status.Conditions {
if condition.Type == meta.ReadyCondition {
if condition.Status == corev1.ConditionTrue {
return true, nil
} else if condition.Status == corev1.ConditionFalse {
return false, fmt.Errorf(condition.Message)
}
if c := meta.GetCondition(gitRepository.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
Expand Down
23 changes: 10 additions & 13 deletions cmd/gotk/get_helmrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,17 @@ func getHelmReleaseCmdRun(cmd *cobra.Command, args []string) error {
continue
}
isInitialized := false
for _, condition := range helmRelease.Status.Conditions {
if condition.Type == meta.ReadyCondition {
if condition.Status != corev1.ConditionFalse {
if helmRelease.Status.LastAppliedRevision != "" {
logger.Successf("%s last applied revision %s", helmRelease.GetName(), helmRelease.Status.LastAppliedRevision)
} else {
logger.Successf("%s reconciling", helmRelease.GetName())
}
} else {
logger.Failuref("%s %s", helmRelease.GetName(), condition.Message)
}
isInitialized = true
break
if c := meta.GetCondition(helmRelease.Status.Conditions, meta.ReadyCondition); c != nil {
switch c.Status {
case corev1.ConditionTrue:
logger.Successf("%s last applied revision %s", helmRelease.GetName(), helmRelease.Status.LastAppliedRevision)
case corev1.ConditionUnknown:
logger.Successf("%s reconciling", helmRelease.GetName())
default:
logger.Failuref("%s %s", helmRelease.GetName(), c.Message)
}
isInitialized = true
break
}
if !isInitialized {
logger.Failuref("%s is not ready", helmRelease.GetName())
Expand Down
23 changes: 10 additions & 13 deletions cmd/gotk/get_kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,17 @@ func getKsCmdRun(cmd *cobra.Command, args []string) error {
continue
}
isInitialized := false
for _, condition := range kustomization.Status.Conditions {
if condition.Type == meta.ReadyCondition {
if condition.Status != corev1.ConditionFalse {
if kustomization.Status.LastAppliedRevision != "" {
logger.Successf("%s last applied revision %s", kustomization.GetName(), kustomization.Status.LastAppliedRevision)
} else {
logger.Successf("%s reconciling", kustomization.GetName())
}
} else {
logger.Failuref("%s %s", kustomization.GetName(), condition.Message)
}
isInitialized = true
break
if c := meta.GetCondition(kustomization.Status.Conditions, meta.ReadyCondition); c != nil {
switch c.Status {
case corev1.ConditionTrue:
logger.Successf("%s last applied revision %s", kustomization.GetName(), kustomization.Status.LastAppliedRevision)
case corev1.ConditionUnknown:
logger.Successf("%s reconciling", kustomization.GetName())
default:
logger.Failuref("%s %s", kustomization.GetName(), c.Message)
}
isInitialized = true
break
}
if !isInitialized {
logger.Failuref("%s is not ready", kustomization.GetName())
Expand Down
22 changes: 13 additions & 9 deletions cmd/gotk/get_source_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,22 @@ func getSourceBucketCmdRun(cmd *cobra.Command, args []string) error {
return nil
}

// TODO(hidde): this should print a table, and should produce better output
// for items that have an artifact attached while they are in a reconciling
// 'Unknown' state.
for _, source := range list.Items {
isInitialized := false
for _, condition := range source.Status.Conditions {
if condition.Type == meta.ReadyCondition {
if condition.Status != corev1.ConditionFalse {
logger.Successf("%s last fetched revision: %s", source.GetName(), source.Status.Artifact.Revision)
} else {
logger.Failuref("%s %s", source.GetName(), condition.Message)
}
isInitialized = true
break
if c := meta.GetCondition(source.Status.Conditions, meta.ReadyCondition); c != nil {
switch c.Status {
case corev1.ConditionTrue:
logger.Successf("%s last fetched revision: %s", source.GetName(), source.GetArtifact().Revision)
case corev1.ConditionUnknown:
logger.Successf("%s reconciling", source.GetName())
default:
logger.Failuref("%s %s", source.GetName(), c.Message)
}
isInitialized = true
break
}
if !isInitialized {
logger.Failuref("%s is not ready", source.GetName())
Expand Down
22 changes: 13 additions & 9 deletions cmd/gotk/get_source_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,22 @@ func getSourceGitCmdRun(cmd *cobra.Command, args []string) error {
return nil
}

// TODO(hidde): this should print a table, and should produce better output
// for items that have an artifact attached while they are in a reconciling
// 'Unknown' state.
for _, source := range list.Items {
isInitialized := false
for _, condition := range source.Status.Conditions {
if condition.Type == meta.ReadyCondition {
if condition.Status != corev1.ConditionFalse {
logger.Successf("%s last fetched revision: %s", source.GetName(), source.Status.Artifact.Revision)
} else {
logger.Failuref("%s %s", source.GetName(), condition.Message)
}
isInitialized = true
break
if c := meta.GetCondition(source.Status.Conditions, meta.ReadyCondition); c != nil {
switch c.Status {
case corev1.ConditionTrue:
logger.Successf("%s last fetched revision: %s", source.GetName(), source.GetArtifact().Revision)
case corev1.ConditionUnknown:
logger.Successf("%s reconciling", source.GetName())
default:
logger.Failuref("%s %s", source.GetName(), c.Message)
}
isInitialized = true
break
}
if !isInitialized {
logger.Failuref("%s is not ready", source.GetName())
Expand Down
22 changes: 13 additions & 9 deletions cmd/gotk/get_source_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,22 @@ func getSourceHelmCmdRun(cmd *cobra.Command, args []string) error {
return nil
}

// TODO(hidde): this should print a table, and should produce better output
// for items that have an artifact attached while they are in a reconciling
// 'Unknown' state.
for _, source := range list.Items {
isInitialized := false
for _, condition := range source.Status.Conditions {
if condition.Type == meta.ReadyCondition {
if condition.Status != corev1.ConditionFalse {
logger.Successf("%s last fetched revision: %s", source.GetName(), source.Status.Artifact.Revision)
} else {
logger.Failuref("%s %s", source.GetName(), condition.Message)
}
isInitialized = true
break
if c := meta.GetCondition(source.Status.Conditions, meta.ReadyCondition); c != nil {
switch c.Status {
case corev1.ConditionTrue:
logger.Successf("%s last fetched revision: %s", source.GetName(), source.GetArtifact().Revision)
case corev1.ConditionUnknown:
logger.Successf("%s reconciling", source.GetName())
default:
logger.Failuref("%s %s", source.GetName(), c.Message)
}
isInitialized = true
break
}
if !isInitialized {
logger.Failuref("%s is not ready", source.GetName())
Expand Down
58 changes: 27 additions & 31 deletions cmd/gotk/reconcile_helmrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ package main
import (
"context"
"fmt"
"github.com/fluxcd/pkg/apis/meta"
"time"

"github.com/spf13/cobra"
"github.com/fluxcd/pkg/apis/meta"
corev1 "k8s.io/api/core/v1"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -94,24 +95,25 @@ func reconcileHrCmdRun(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
} else {
logger.Actionf("annotating HelmRelease %s in %s namespace", name, namespace)
if helmRelease.Annotations == nil {
helmRelease.Annotations = map[string]string{
meta.ReconcileAtAnnotation: time.Now().Format(time.RFC3339Nano),
}
} else {
helmRelease.Annotations[meta.ReconcileAtAnnotation] = time.Now().Format(time.RFC3339Nano)
}
if err := kubeClient.Update(ctx, &helmRelease); err != nil {
return err
}

logger.Actionf("annotating HelmRelease %s in %s namespace", name, namespace)
if helmRelease.Annotations == nil {
helmRelease.Annotations = map[string]string{
meta.ReconcileAtAnnotation: time.Now().Format(time.RFC3339Nano),
}
logger.Successf("HelmRelease annotated")
} else {
helmRelease.Annotations[meta.ReconcileAtAnnotation] = time.Now().Format(time.RFC3339Nano)
}
if err := kubeClient.Update(ctx, &helmRelease); err != nil {
return err
}
logger.Successf("HelmRelease annotated")

logger.Waitingf("waiting for HelmRelease reconciliation")
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
}

Expand All @@ -121,16 +123,19 @@ func reconcileHrCmdRun(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

if helmRelease.Status.LastAppliedRevision != "" {
logger.Successf("reconciled revision %s", helmRelease.Status.LastAppliedRevision)
} else {
return fmt.Errorf("HelmRelease reconciliation failed")
if c := meta.GetCondition(helmRelease.Status.Conditions, meta.ReadyCondition); c != nil {
switch c.Status {
case corev1.ConditionFalse:
return fmt.Errorf("HelmRelease reconciliation failed")
default:
logger.Successf("reconciled revision %s", helmRelease.Status.LastAppliedRevision)
}
}
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) {
var helmRelease helmv2.HelmRelease
namespacedName := types.NamespacedName{
Expand All @@ -143,15 +148,6 @@ func isHelmReleaseReady(ctx context.Context, kubeClient client.Client, name, nam
return false, err
}

for _, condition := range helmRelease.Status.Conditions {
if condition.Type == meta.ReadyCondition {
if condition.Status == corev1.ConditionTrue {
return true, nil
} else if condition.Status == corev1.ConditionFalse && helmRelease.Status.LastAttemptedRevision != "" {
return false, fmt.Errorf(condition.Message)
}
}
}
return false, nil
return helmRelease.Status.LastHandledReconcileAt != lastHandledReconcileAt, nil
}
}
Loading