@@ -60,39 +60,47 @@ type notifier interface {
60
60
}
61
61
62
62
func (w * Watcher ) Watch (ctx context.Context ) error {
63
+ resourceTypes , err := w .resolveFluxResourceTypes (ctx )
64
+ if err != nil {
65
+ return fmt .Errorf ("could not resolve flux resource types: %w" , err )
66
+ }
67
+
68
+ if err = w .init (ctx , resourceTypes ); err != nil {
69
+ return fmt .Errorf ("failed to initialize: %w" , err )
70
+ }
71
+
72
+ return w .watch (ctx , resourceTypes )
73
+ }
74
+
75
+ func (w * Watcher ) resolveFluxResourceTypes (ctx context.Context ) ([]k8s.ResourceType , error ) {
63
76
crds , err := w .k8sClient .GetCustomResourceDefinitions (ctx , metav1.ListOptions {
64
77
LabelSelector : "app.kubernetes.io/part-of=flux" ,
65
78
})
66
79
if err != nil {
67
- return err
80
+ return nil , fmt . Errorf ( "failed to fetch crds: %w" , err )
68
81
}
69
82
70
- uniqueResourceGroups := make (map [string ]k8s.ResourceType )
83
+ uniqueResourceTypes := make (map [string ]k8s.ResourceType )
71
84
for _ , crd := range crds .Items {
72
85
for _ , version := range crd .Spec .Versions {
73
86
if _ , exists := version .Schema .OpenAPIV3Schema .Properties ["spec" ].Properties ["suspend" ]; ! exists {
74
87
continue
75
88
}
76
89
key := fmt .Sprintf ("%s:%s" , crd .Spec .Group , crd .Spec .Names .Plural )
77
- uniqueResourceGroups [key ] = k8s.ResourceType {
90
+ uniqueResourceTypes [key ] = k8s.ResourceType {
78
91
Group : crd .Spec .Group ,
79
92
Version : version .Name ,
80
93
Kind : crd .Spec .Names .Plural ,
81
94
}
82
95
}
83
96
}
84
- resourceGroups := maps .Values (uniqueResourceGroups )
85
-
86
- if err = w .init (ctx , resourceGroups ); err != nil {
87
- return fmt .Errorf ("failed to initialize: %w" , err )
88
- }
89
- return w .watch (ctx , resourceGroups )
97
+ return maps .Values (uniqueResourceTypes ), nil
90
98
}
91
99
92
- func (w * Watcher ) init (ctx context.Context , groups []k8s.ResourceType ) error {
100
+ func (w * Watcher ) init (ctx context.Context , types []k8s.ResourceType ) error {
93
101
slog .Info ("initializing" )
94
- for _ , group := range groups {
95
- res , err := w .k8sClient .GetRawResources (ctx , group )
102
+ for _ , t := range types {
103
+ res , err := w .k8sClient .GetRawResources (ctx , t )
96
104
if err != nil {
97
105
return err
98
106
}
@@ -102,7 +110,7 @@ func (w *Watcher) init(ctx context.Context, groups []k8s.ResourceType) error {
102
110
}
103
111
for _ , resource := range resourceList .Items {
104
112
resourceRef := k8s.ResourceReference {
105
- Type : group ,
113
+ Type : t ,
106
114
Namespace : resource .Metadata .Namespace ,
107
115
Name : resource .Metadata .Name ,
108
116
}
@@ -114,14 +122,10 @@ func (w *Watcher) init(ctx context.Context, groups []k8s.ResourceType) error {
114
122
return nil
115
123
}
116
124
117
- func (w * Watcher ) watch (ctx context.Context , groups []k8s.ResourceType ) error {
125
+ func (w * Watcher ) watch (ctx context.Context , types []k8s.ResourceType ) error {
118
126
slog .Info ("watching for resource modifications" )
119
- resourceKinds := make ([]string , 0 , len (groups ))
120
- for _ , group := range groups {
121
- resourceKinds = append (resourceKinds , group .Kind )
122
- }
123
127
124
- return auditlog .Tail (ctx , w .googleCloudProjectID , w .gkeClusterName , resourceKinds , func (logEntry * audit.AuditLog ) error {
128
+ return auditlog .Tail (ctx , w .googleCloudProjectID , w .gkeClusterName , func (logEntry * audit.AuditLog ) error {
125
129
if code := logEntry .GetStatus ().GetCode (); code != 0 {
126
130
slog .Warn ("operation appeared to fail" , slog .Int ("code" , int (code )))
127
131
return nil
@@ -135,6 +139,11 @@ func (w *Watcher) watch(ctx context.Context, groups []k8s.ResourceType) error {
135
139
return err
136
140
}
137
141
142
+ if ! isWatchedResource (resourceRef , types ) {
143
+ slog .Info ("ignoring non-watched resource" , slog .String ("kind" , resourceRef .Type .Kind ))
144
+ return nil
145
+ }
146
+
138
147
res , err := w .k8sClient .GetRawResource (ctx , resourceRef )
139
148
if err != nil {
140
149
return fmt .Errorf ("failed to get raw resource: %w" , err )
@@ -211,3 +220,12 @@ func (w *Watcher) processResource(
211
220
GoogleCloudProjectID : w .googleCloudProjectID ,
212
221
})
213
222
}
223
+
224
+ func isWatchedResource (ref k8s.ResourceReference , types []k8s.ResourceType ) bool {
225
+ for _ , t := range types {
226
+ if t .Group == ref .Type .Group && t .Kind == ref .Type .Kind {
227
+ return true
228
+ }
229
+ }
230
+ return false
231
+ }
0 commit comments