@@ -28,112 +28,63 @@ import (
28
28
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
29
29
)
30
30
31
- func (r * KustomizationReconciler ) requestsForGitRepositoryRevisionChange (obj client.Object ) []reconcile.Request {
32
- repo , ok := obj .(* sourcev1.GitRepository )
33
- if ! ok {
34
- panic (fmt .Sprintf ("Expected a GitRepository but got a %T" , obj ))
35
- }
36
- // If we do not have an artifact, we have no requests to make
37
- if repo .GetArtifact () == nil {
38
- return nil
39
- }
40
-
41
- ctx := context .Background ()
42
- var list kustomizev1.KustomizationList
43
- if err := r .List (ctx , & list , client.MatchingFields {
44
- kustomizev1 .GitRepositoryIndexKey : ObjectKey (obj ).String (),
45
- }); err != nil {
46
- return nil
47
- }
48
- var dd []dependency.Dependent
49
- for _ , d := range list .Items {
50
- // If the revision of the artifact equals to the last attempted revision,
51
- // we should not make a request for this Kustomization
52
- if repo .GetArtifact ().Revision == d .Status .LastAttemptedRevision {
53
- continue
31
+ func (r * KustomizationReconciler ) requestsForRevisionChangeOf (indexKey string ) func (obj client.Object ) []reconcile.Request {
32
+ return func (obj client.Object ) []reconcile.Request {
33
+ repo , ok := obj .(interface {
34
+ GetArtifact () * sourcev1.Artifact
35
+ })
36
+ if ! ok {
37
+ panic (fmt .Sprintf ("Expected an object conformed with GetArtifact() method, but got a %T" , obj ))
54
38
}
55
- dd = append (dd , d )
56
- }
57
- sorted , err := dependency .Sort (dd )
58
- if err != nil {
59
- return nil
60
- }
61
- reqs := make ([]reconcile.Request , len (sorted ), len (sorted ))
62
- for i := range sorted {
63
- reqs [i ].NamespacedName .Name = sorted [i ].Name
64
- reqs [i ].NamespacedName .Namespace = sorted [i ].Namespace
65
- }
66
- return reqs
67
- }
68
-
69
- func (r * KustomizationReconciler ) indexByGitRepository (o client.Object ) []string {
70
- k , ok := o .(* kustomizev1.Kustomization )
71
- if ! ok {
72
- panic (fmt .Sprintf ("Expected a Kustomization, got %T" , o ))
73
- }
74
-
75
- if k .Spec .SourceRef .Kind == sourcev1 .GitRepositoryKind {
76
- namespace := k .GetNamespace ()
77
- if k .Spec .SourceRef .Namespace != "" {
78
- namespace = k .Spec .SourceRef .Namespace
39
+ // If we do not have an artifact, we have no requests to make
40
+ if repo .GetArtifact () == nil {
41
+ return nil
79
42
}
80
- return []string {fmt .Sprintf ("%s/%s" , namespace , k .Spec .SourceRef .Name )}
81
- }
82
-
83
- return nil
84
- }
85
43
86
- func (r * KustomizationReconciler ) requestsForBucketRevisionChange (obj client.Object ) []reconcile.Request {
87
- bucket , ok := obj .(* sourcev1.Bucket )
88
- if ! ok {
89
- panic (fmt .Sprintf ("Expected a Bucket but got a %T" , obj ))
90
- }
91
- // If we do not have an artifact, we have no requests to make
92
- if bucket .GetArtifact () == nil {
93
- return nil
94
- }
95
-
96
- ctx := context .Background ()
97
- var list kustomizev1.KustomizationList
98
- if err := r .List (ctx , & list , client.MatchingFields {
99
- kustomizev1 .BucketIndexKey : ObjectKey (obj ).String (),
100
- }); err != nil {
101
- return nil
102
- }
103
- var dd []dependency.Dependent
104
- for _ , d := range list .Items {
105
- // If the revision of the artifact equals to the last attempted revision,
106
- // we should not make a request for this Kustomization
107
- if bucket .GetArtifact ().Revision == d .Status .LastAttemptedRevision {
108
- continue
44
+ ctx := context .Background ()
45
+ var list kustomizev1.KustomizationList
46
+ if err := r .List (ctx , & list , client.MatchingFields {
47
+ indexKey : ObjectKey (obj ).String (),
48
+ }); err != nil {
49
+ return nil
109
50
}
110
- dd = append (dd , d )
111
- }
112
- sorted , err := dependency .Sort (dd )
113
- if err != nil {
114
- return nil
115
- }
116
- reqs := make ([]reconcile.Request , len (sorted ), len (sorted ))
117
- for i := range sorted {
118
- reqs [i ].NamespacedName .Name = sorted [i ].Name
119
- reqs [i ].NamespacedName .Namespace = sorted [i ].Namespace
51
+ var dd []dependency.Dependent
52
+ for _ , d := range list .Items {
53
+ // If the revision of the artifact equals to the last attempted revision,
54
+ // we should not make a request for this Kustomization
55
+ if repo .GetArtifact ().Revision == d .Status .LastAttemptedRevision {
56
+ continue
57
+ }
58
+ dd = append (dd , d )
59
+ }
60
+ sorted , err := dependency .Sort (dd )
61
+ if err != nil {
62
+ return nil
63
+ }
64
+ reqs := make ([]reconcile.Request , len (sorted ), len (sorted ))
65
+ for i := range sorted {
66
+ reqs [i ].NamespacedName .Name = sorted [i ].Name
67
+ reqs [i ].NamespacedName .Namespace = sorted [i ].Namespace
68
+ }
69
+ return reqs
120
70
}
121
- return reqs
122
71
}
123
72
124
- func (r * KustomizationReconciler ) indexByBucket (o client.Object ) []string {
125
- k , ok := o .(* kustomizev1.Kustomization )
126
- if ! ok {
127
- panic (fmt .Sprintf ("Expected a Kustomization, got %T" , o ))
128
- }
73
+ func (r * KustomizationReconciler ) indexBy (kind string ) func (o client.Object ) []string {
74
+ return func (o client.Object ) []string {
75
+ k , ok := o .(* kustomizev1.Kustomization )
76
+ if ! ok {
77
+ panic (fmt .Sprintf ("Expected a Kustomization, got %T" , o ))
78
+ }
129
79
130
- if k .Spec .SourceRef .Kind == sourcev1 .BucketKind {
131
- namespace := k .GetNamespace ()
132
- if k .Spec .SourceRef .Namespace != "" {
133
- namespace = k .Spec .SourceRef .Namespace
80
+ if k .Spec .SourceRef .Kind == kind {
81
+ namespace := k .GetNamespace ()
82
+ if k .Spec .SourceRef .Namespace != "" {
83
+ namespace = k .Spec .SourceRef .Namespace
84
+ }
85
+ return []string {fmt .Sprintf ("%s/%s" , namespace , k .Spec .SourceRef .Name )}
134
86
}
135
- return []string {fmt .Sprintf ("%s/%s" , namespace , k .Spec .SourceRef .Name )}
136
- }
137
87
138
- return nil
88
+ return nil
89
+ }
139
90
}
0 commit comments