|
| 1 | +/* |
| 2 | +Copyright 2020 The Flux CD contributors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "strings" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/fluxcd/pkg/runtime/dependency" |
| 26 | + "github.com/go-logr/logr" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/apimachinery/pkg/runtime" |
| 29 | + "k8s.io/apimachinery/pkg/types" |
| 30 | + "k8s.io/client-go/util/retry" |
| 31 | + ctrl "sigs.k8s.io/controller-runtime" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 33 | + |
| 34 | + kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1alpha1" |
| 35 | + consts "github.com/fluxcd/pkg/runtime" |
| 36 | + sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1" |
| 37 | +) |
| 38 | + |
| 39 | +// BucketWatcher watches Bucket objects for revision changes |
| 40 | +// and triggers a reconcile for all the Kustomizations that reference a changed source |
| 41 | +type BucketWatcher struct { |
| 42 | + client.Client |
| 43 | + Log logr.Logger |
| 44 | + Scheme *runtime.Scheme |
| 45 | +} |
| 46 | + |
| 47 | +// +kubebuilder:rbac:groups=source.toolkit.fluxcd.io,resources=buckets,verbs=get;list;watch |
| 48 | +// +kubebuilder:rbac:groups=source.toolkit.fluxcd.io,resources=buckets/status,verbs=get |
| 49 | + |
| 50 | +func (r *BucketWatcher) Reconcile(req ctrl.Request) (ctrl.Result, error) { |
| 51 | + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) |
| 52 | + defer cancel() |
| 53 | + |
| 54 | + var repo sourcev1.Bucket |
| 55 | + if err := r.Get(ctx, req.NamespacedName, &repo); err != nil { |
| 56 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 57 | + } |
| 58 | + |
| 59 | + log := r.Log.WithValues(strings.ToLower(repo.Kind), req.NamespacedName) |
| 60 | + log.Info("new artifact detected") |
| 61 | + |
| 62 | + // get the list of kustomizations that are using this Bucket |
| 63 | + var list kustomizev1.KustomizationList |
| 64 | + if err := r.List(ctx, &list, |
| 65 | + client.MatchingFields{kustomizev1.BucketIndexKey: fmt.Sprintf("%s/%s", req.Namespace, req.Name)}); err != nil { |
| 66 | + log.Error(err, "unable to list kustomizations") |
| 67 | + return ctrl.Result{}, err |
| 68 | + } |
| 69 | + |
| 70 | + var dd []dependency.Dependent |
| 71 | + for _, d := range list.Items { |
| 72 | + dd = append(dd, d) |
| 73 | + } |
| 74 | + sorted, err := dependency.Sort(dd) |
| 75 | + if err != nil { |
| 76 | + log.Error(err, "unable to dependency sort kustomizations") |
| 77 | + return ctrl.Result{}, err |
| 78 | + } |
| 79 | + |
| 80 | + // trigger apply for each kustomization using this Bucket taking into account the dependency order |
| 81 | + for _, k := range sorted { |
| 82 | + name := types.NamespacedName(k) |
| 83 | + if err := r.requestReconciliation(name); err != nil { |
| 84 | + log.Error(err, "unable to annotate Kustomization", "kustomization", name) |
| 85 | + continue |
| 86 | + } |
| 87 | + log.Info("requested immediate reconciliation", "kustomization", name) |
| 88 | + } |
| 89 | + |
| 90 | + return ctrl.Result{}, nil |
| 91 | +} |
| 92 | + |
| 93 | +func (r *BucketWatcher) SetupWithManager(mgr ctrl.Manager) error { |
| 94 | + // create a kustomization index based on Bucket name |
| 95 | + err := mgr.GetFieldIndexer().IndexField(context.TODO(), &kustomizev1.Kustomization{}, kustomizev1.BucketIndexKey, |
| 96 | + func(rawObj runtime.Object) []string { |
| 97 | + k := rawObj.(*kustomizev1.Kustomization) |
| 98 | + if k.Spec.SourceRef.Kind == sourcev1.BucketKind { |
| 99 | + namespace := k.GetNamespace() |
| 100 | + if k.Spec.SourceRef.Namespace != "" { |
| 101 | + namespace = k.Spec.SourceRef.Namespace |
| 102 | + } |
| 103 | + return []string{fmt.Sprintf("%s/%s", namespace, k.Spec.SourceRef.Name)} |
| 104 | + } |
| 105 | + return nil |
| 106 | + }, |
| 107 | + ) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + return ctrl.NewControllerManagedBy(mgr). |
| 113 | + For(&sourcev1.Bucket{}). |
| 114 | + WithEventFilter(BucketRevisionChangePredicate{}). |
| 115 | + Complete(r) |
| 116 | +} |
| 117 | + |
| 118 | +func (r *BucketWatcher) requestReconciliation(name types.NamespacedName) error { |
| 119 | + var kustomization kustomizev1.Kustomization |
| 120 | + return retry.RetryOnConflict(retry.DefaultBackoff, func() (err error) { |
| 121 | + if err := r.Get(context.TODO(), name, &kustomization); err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + if kustomization.Annotations == nil { |
| 126 | + kustomization.Annotations = make(map[string]string) |
| 127 | + } |
| 128 | + kustomization.Annotations[consts.ReconcileAtAnnotation] = metav1.Now().String() |
| 129 | + err = r.Update(context.TODO(), &kustomization) |
| 130 | + return |
| 131 | + }) |
| 132 | +} |
0 commit comments