Skip to content

Commit 5217087

Browse files
authored
Merge pull request #1484 from fluxcd/ssa-staged
Refactor reconciler to use `ssa.ApplyAllStaged`
2 parents f479c3e + 1476042 commit 5217087

File tree

3 files changed

+18
-95
lines changed

3 files changed

+18
-95
lines changed

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ require (
2323
github.com/fluxcd/pkg/apis/acl v0.7.0
2424
github.com/fluxcd/pkg/apis/event v0.17.0
2525
github.com/fluxcd/pkg/apis/kustomize v1.10.0
26-
github.com/fluxcd/pkg/apis/meta v1.12.0
26+
github.com/fluxcd/pkg/apis/meta v1.13.0
2727
github.com/fluxcd/pkg/auth v0.16.0
2828
github.com/fluxcd/pkg/cache v0.9.0
2929
github.com/fluxcd/pkg/http/fetch v0.16.0
3030
github.com/fluxcd/pkg/kustomize v1.18.0
31-
github.com/fluxcd/pkg/runtime v0.60.0
32-
github.com/fluxcd/pkg/ssa v0.49.0
31+
github.com/fluxcd/pkg/runtime v0.63.0
32+
github.com/fluxcd/pkg/ssa v0.50.1
3333
github.com/fluxcd/pkg/tar v0.12.0
3434
github.com/fluxcd/pkg/testserver v0.11.0
3535
github.com/fluxcd/source-controller/api v1.6.0
@@ -46,7 +46,7 @@ require (
4646
k8s.io/client-go v0.33.0
4747
k8s.io/utils v0.0.0-20250321185631-1f6e0b77f77e
4848
sigs.k8s.io/controller-runtime v0.21.0
49-
sigs.k8s.io/kustomize/api v0.19.0
49+
sigs.k8s.io/kustomize/api v0.20.0
5050
sigs.k8s.io/yaml v1.5.0
5151
)
5252

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ github.com/fluxcd/pkg/apis/event v0.17.0 h1:foEINE++pCJlWVhWjYDXfkVmGKu8mQ4BDBlb
188188
github.com/fluxcd/pkg/apis/event v0.17.0/go.mod h1:0fLhLFiHlRTDKPDXdRnv+tS7mCMIQ0fJxnEfmvGM/5A=
189189
github.com/fluxcd/pkg/apis/kustomize v1.10.0 h1:47EeSzkQvlQZdH92vHMe2lK2iR8aOSEJq95avw5idts=
190190
github.com/fluxcd/pkg/apis/kustomize v1.10.0/go.mod h1:UsqMV4sqNa1Yg0pmTsdkHRJr7bafBOENIJoAN+3ezaQ=
191-
github.com/fluxcd/pkg/apis/meta v1.12.0 h1:XW15TKZieC2b7MN8VS85stqZJOx+/b8jATQ/xTUhVYg=
192-
github.com/fluxcd/pkg/apis/meta v1.12.0/go.mod h1:+son1Va60x2eiDcTwd7lcctbI6C+K3gM7R+ULmEq1SI=
191+
github.com/fluxcd/pkg/apis/meta v1.13.0 h1:KKYdXFzEmSuM8CP1Zqpt68IF+jqQILeiDiTmXI7DMTM=
192+
github.com/fluxcd/pkg/apis/meta v1.13.0/go.mod h1:+son1Va60x2eiDcTwd7lcctbI6C+K3gM7R+ULmEq1SI=
193193
github.com/fluxcd/pkg/auth v0.16.0 h1:YEjSaNqlpYoXfoFAGhU/Z8y0322nGsT24W6zCh+sbGw=
194194
github.com/fluxcd/pkg/auth v0.16.0/go.mod h1:+BRnAO61Nr6fACEjJS6eNRdOk1nXhX/FCPylYn1ypNc=
195195
github.com/fluxcd/pkg/cache v0.9.0 h1:EGKfOLMG3fOwWnH/4Axl5xd425mxoQbZzlZoLfd8PDk=
@@ -200,12 +200,12 @@ github.com/fluxcd/pkg/http/fetch v0.16.0 h1:XzhBTSK5HNdAPEnEGMJHwtoN2LfqQ9QFDsu3
200200
github.com/fluxcd/pkg/http/fetch v0.16.0/go.mod h1:+A+yrOzwA5436ufD8NPeCCQFNzk4metoPUgRVCozvzw=
201201
github.com/fluxcd/pkg/kustomize v1.18.0 h1:wWK+qYwmBmba3N3VAqZ9ijnfVGGaIjcaHWo033URZTw=
202202
github.com/fluxcd/pkg/kustomize v1.18.0/go.mod h1:Ij9722MdWIE6B1EPg2ZJUf6npycgfRfN4Lohi7D/Kic=
203-
github.com/fluxcd/pkg/runtime v0.60.0 h1:d++EkV3FlycB+bzakB5NumwY4J8xts8i7lbvD6jBLeU=
204-
github.com/fluxcd/pkg/runtime v0.60.0/go.mod h1:UeU0/eZLErYC/1bTmgzBfNXhiHy9fuQzjfLK0HxRgxY=
203+
github.com/fluxcd/pkg/runtime v0.63.0 h1:55J7ascGmXyTXWGwhD21N9fU7jC1l5rhdzjgNXs6aZg=
204+
github.com/fluxcd/pkg/runtime v0.63.0/go.mod h1:7pxGvaU0Yy1cDIUhiHAHhCx2yCLnkcVsplbYZG6j4JY=
205205
github.com/fluxcd/pkg/sourceignore v0.12.0 h1:jCIe6d50rQ3wdXPF0+PhhqN0XrTRIq3upMomPelI8Mw=
206206
github.com/fluxcd/pkg/sourceignore v0.12.0/go.mod h1:dc0zvkuXM5OgL/b3IkrVuwvPjj1zJn4NBUMH45uJ4Y0=
207-
github.com/fluxcd/pkg/ssa v0.49.0 h1:3xBMxWQIpmKu+zUmyuKQ9M4f+ALhbMJIkiLXeGkhig4=
208-
github.com/fluxcd/pkg/ssa v0.49.0/go.mod h1:T50TO0U2obLodZnrFgOrxollfBEy4V673OkM2aTUF1c=
207+
github.com/fluxcd/pkg/ssa v0.50.1 h1:ESyHtd0B5vyrnKunfHfUesT8ZtdMHBRPKtoxpxOGIYM=
208+
github.com/fluxcd/pkg/ssa v0.50.1/go.mod h1:T50TO0U2obLodZnrFgOrxollfBEy4V673OkM2aTUF1c=
209209
github.com/fluxcd/pkg/tar v0.12.0 h1:og6F+ivnWNRbNJSq0ukCTVs7YrGIlzjxSVZU+E8NprM=
210210
github.com/fluxcd/pkg/tar v0.12.0/go.mod h1:Ra5Cj++MD5iCy7bZGKJJX3GpOeMPv+ZDkPO9bBwpDeU=
211211
github.com/fluxcd/pkg/testserver v0.11.0 h1:a/kxpFqv7XQxZjwVPP3voooRmSd/3ipLVolK0xUIxXQ=

internal/controller/kustomization_controller.go

Lines changed: 8 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"errors"
2323
"fmt"
2424
"os"
25-
"sort"
2625
"strings"
2726
"time"
2827

@@ -807,120 +806,44 @@ func (r *KustomizationReconciler) apply(ctx context.Context,
807806
},
808807
}
809808

810-
// contains only CRDs and Namespaces
811-
var defStage []*unstructured.Unstructured
812-
813-
// contains only Kubernetes Class types e.g.: RuntimeClass, PriorityClass,
814-
// StorageClass, VolumeSnapshotClass, IngressClass, GatewayClass, ClusterClass, etc
815-
var classStage []*unstructured.Unstructured
816-
817-
// contains all objects except for CRDs, Namespaces and Class type objects
818-
var resStage []*unstructured.Unstructured
819-
820-
// contains the objects' metadata after apply
821-
resultSet := ssa.NewChangeSet()
822-
823809
for _, u := range objects {
824810
if decryptor.IsEncryptedSecret(u) {
825811
return false, nil,
826812
fmt.Errorf("%s is SOPS encrypted, configuring decryption is required for this secret to be reconciled",
827813
ssautil.FmtUnstructured(u))
828814
}
829-
830-
switch {
831-
case ssautil.IsClusterDefinition(u):
832-
defStage = append(defStage, u)
833-
case strings.HasSuffix(u.GetKind(), "Class"):
834-
classStage = append(classStage, u)
835-
default:
836-
resStage = append(resStage, u)
837-
}
838-
839815
}
840816

817+
// contains the objects' metadata after apply
818+
resultSet := ssa.NewChangeSet()
841819
var changeSetLog strings.Builder
842820

843-
// validate, apply and wait for CRDs and Namespaces to register
844-
if len(defStage) > 0 {
845-
changeSet, err := manager.ApplyAll(ctx, defStage, applyOpts)
846-
if err != nil {
847-
return false, nil, err
848-
}
821+
if len(objects) > 0 {
822+
changeSet, err := manager.ApplyAllStaged(ctx, objects, applyOpts)
849823

850824
if changeSet != nil && len(changeSet.Entries) > 0 {
851825
resultSet.Append(changeSet.Entries)
852826

853-
if r.GroupChangeLog {
854-
log.Info("server-side apply for cluster definitions completed", "output", changeSet.ToGroupedMap())
855-
} else {
856-
log.Info("server-side apply for cluster definitions completed", "output", changeSet.ToMap())
857-
}
827+
// filter out the objects that have not changed
858828
for _, change := range changeSet.Entries {
859829
if HasChanged(change.Action) {
860830
changeSetLog.WriteString(change.String() + "\n")
861831
}
862832
}
863-
864-
if err := manager.WaitForSet(changeSet.ToObjMetadataSet(), ssa.WaitOptions{
865-
Interval: 2 * time.Second,
866-
Timeout: obj.GetTimeout(),
867-
}); err != nil {
868-
return false, nil, err
869-
}
870833
}
871-
}
872834

873-
// validate, apply and wait for Class type objects to register
874-
if len(classStage) > 0 {
875-
changeSet, err := manager.ApplyAll(ctx, classStage, applyOpts)
876-
if err != nil {
877-
return false, nil, err
878-
}
879-
880-
if changeSet != nil && len(changeSet.Entries) > 0 {
881-
resultSet.Append(changeSet.Entries)
882-
883-
if r.GroupChangeLog {
884-
log.Info("server-side apply for cluster definitions completed", "output", changeSet.ToGroupedMap())
885-
} else {
886-
log.Info("server-side apply for cluster class types completed", "output", changeSet.ToMap())
887-
}
888-
for _, change := range changeSet.Entries {
889-
if HasChanged(change.Action) {
890-
changeSetLog.WriteString(change.String() + "\n")
891-
}
892-
}
893-
894-
if err := manager.WaitForSet(changeSet.ToObjMetadataSet(), ssa.WaitOptions{
895-
Interval: 2 * time.Second,
896-
Timeout: obj.GetTimeout(),
897-
}); err != nil {
898-
return false, nil, err
899-
}
900-
}
901-
}
902-
903-
// sort by kind, validate and apply all the others objects
904-
sort.Sort(ssa.SortableUnstructureds(resStage))
905-
if len(resStage) > 0 {
906-
changeSet, err := manager.ApplyAll(ctx, resStage, applyOpts)
835+
// include the change log in the error message in case af a partial apply
907836
if err != nil {
908837
return false, nil, fmt.Errorf("%w\n%s", err, changeSetLog.String())
909838
}
910839

840+
// log all applied objects
911841
if changeSet != nil && len(changeSet.Entries) > 0 {
912-
resultSet.Append(changeSet.Entries)
913-
914842
if r.GroupChangeLog {
915-
log.Info("server-side apply for cluster definitions completed", "output", changeSet.ToGroupedMap())
843+
log.Info("server-side apply completed", "output", changeSet.ToGroupedMap(), "revision", revision)
916844
} else {
917845
log.Info("server-side apply completed", "output", changeSet.ToMap(), "revision", revision)
918846
}
919-
for _, change := range changeSet.Entries {
920-
if HasChanged(change.Action) {
921-
changeSetLog.WriteString(change.String() + "\n")
922-
}
923-
}
924847
}
925848
}
926849

0 commit comments

Comments
 (0)