-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathreconcile.go
More file actions
73 lines (67 loc) · 2.27 KB
/
Copy pathreconcile.go
File metadata and controls
73 lines (67 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package tfctl
import (
"context"
"errors"
"fmt"
"io"
"time"
infrav1 "github.com/flux-iac/tofu-controller/api/v1alpha2"
"github.com/fluxcd/pkg/apis/meta"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// Reconcile triggers a reconciliation of Terraform resources.
// If resource == "", it reconciles all resources in the namespace.
func (c *CLI) Reconcile(out io.Writer, resource string) error {
if resource == "" {
return reconcileAllResources(context.TODO(), out, c.client, c.namespace)
}
key := types.NamespacedName{
Name: resource,
Namespace: c.namespace,
}
err := requestReconciliation(context.TODO(), c.client, key)
if err != nil {
return err
}
fmt.Fprintf(out, " Reconcile requested for %s/%s\n", c.namespace, resource)
return nil
}
func reconcileAllResources(ctx context.Context, out io.Writer, kubeClient client.Client, namespace string) error {
terraformList := &infrav1.TerraformList{}
if err := kubeClient.List(ctx, terraformList, client.InNamespace(namespace)); err != nil {
return err
}
var errs []error
for _, terraform := range terraformList.Items {
key := types.NamespacedName{
Name: terraform.Name,
Namespace: terraform.Namespace,
}
if err := requestReconciliation(ctx, kubeClient, key); err != nil {
errs = append(errs, fmt.Errorf("failed to reconcile %s/%s: %w", terraform.Namespace, terraform.Name, err))
} else {
fmt.Fprintf(out, " Reconcile requested for %s/%s\n", terraform.Namespace, terraform.Name)
}
}
return errors.Join(errs...)
}
func requestReconciliation(ctx context.Context, kubeClient client.Client, namespacedName types.NamespacedName) error {
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
terraform := &infrav1.Terraform{}
if err := kubeClient.Get(ctx, namespacedName, terraform); err != nil {
return err
}
patch := client.MergeFrom(terraform.DeepCopy())
if ann := terraform.GetAnnotations(); ann == nil {
terraform.SetAnnotations(map[string]string{
meta.ReconcileRequestAnnotation: time.Now().Format(time.RFC3339Nano),
})
} else {
ann[meta.ReconcileRequestAnnotation] = time.Now().Format(time.RFC3339Nano)
terraform.SetAnnotations(ann)
}
return kubeClient.Patch(ctx, terraform, patch)
})
}