Skip to content

Commit b505b8a

Browse files
Merge pull request #2780 from ncdc/deflake-upsyncer-pv
🌱 syncer vw: add API definition logging
2 parents 80a16e4 + 993f42b commit b505b8a

File tree

3 files changed

+48
-28
lines changed

3 files changed

+48
-28
lines changed

pkg/virtual/syncer/controllers/apireconciler/syncer_apireconciler_controller.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,15 @@ func (c *APIReconciler) enqueueAPIExport(obj interface{}, logger logr.Logger, lo
165165
return
166166
}
167167

168-
synctargets, err := c.syncTargetIndexer.ByIndex(IndexSyncTargetsByExport, key)
168+
syncTargets, err := indexers.ByIndex[*workloadv1alpha1.SyncTarget](c.syncTargetIndexer, IndexSyncTargetsByExport, key)
169169
if err != nil {
170170
runtime.HandleError(err)
171171
return
172172
}
173173

174-
for _, obj := range synctargets {
175-
logger := logging.WithObject(logger, obj.(*workloadv1alpha1.SyncTarget))
176-
c.enqueueSyncTarget(obj, logger, " because of APIExport")
174+
for _, syncTarget := range syncTargets {
175+
logger := logging.WithObject(logger, syncTarget)
176+
c.enqueueSyncTarget(syncTarget, logger, " because of APIExport")
177177
}
178178
}
179179

@@ -185,15 +185,15 @@ func (c *APIReconciler) enqueueAPIResourceSchema(obj interface{}, logger logr.Lo
185185
return
186186
}
187187

188-
apiExports, err := c.apiExportIndexer.ByIndex(IndexAPIExportsByAPIResourceSchema, key)
188+
apiExports, err := indexers.ByIndex[*apisv1alpha1.APIExport](c.apiExportIndexer, IndexAPIExportsByAPIResourceSchema, key)
189189
if err != nil {
190190
runtime.HandleError(err)
191191
return
192192
}
193193

194-
for _, obj := range apiExports {
195-
logger := logging.WithObject(logger, obj.(*apisv1alpha1.APIExport))
196-
c.enqueueAPIExport(obj, logger, " because of APIResourceSchema")
194+
for _, apiExport := range apiExports {
195+
logger := logging.WithObject(logger, apiExport)
196+
c.enqueueAPIExport(apiExport, logger, " because of APIResourceSchema")
197197
}
198198
}
199199

pkg/virtual/syncer/controllers/apireconciler/syncer_apireconciler_indexes.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
package apireconciler
1818

1919
import (
20-
"fmt"
21-
2220
"github.com/kcp-dev/logicalcluster/v3"
2321

2422
apisv1alpha1 "github.com/kcp-dev/kcp/pkg/apis/apis/v1alpha1"
@@ -29,10 +27,7 @@ import (
2927

3028
// IndexAPIExportsByAPIResourceSchemas is an index function that maps an APIExport to its spec.latestResourceSchemas.
3129
func IndexAPIExportsByAPIResourceSchemas(obj interface{}) ([]string, error) {
32-
apiExport, ok := obj.(*apisv1alpha1.APIExport)
33-
if !ok {
34-
return []string{}, fmt.Errorf("obj is supposed to be an APIExport, but is %T", obj)
35-
}
30+
apiExport := obj.(*apisv1alpha1.APIExport)
3631

3732
ret := make([]string, len(apiExport.Spec.LatestResourceSchemas))
3833
for i := range apiExport.Spec.LatestResourceSchemas {
@@ -43,23 +38,20 @@ func IndexAPIExportsByAPIResourceSchemas(obj interface{}) ([]string, error) {
4338
}
4439

4540
func IndexSyncTargetsByExports(obj interface{}) ([]string, error) {
46-
synctarget, ok := obj.(*workloadv1alpha1.SyncTarget)
47-
if !ok {
48-
return []string{}, fmt.Errorf("obj is supposed to be a SyncTarget, but is %T", obj)
49-
}
41+
syncTarget := obj.(*workloadv1alpha1.SyncTarget)
5042

51-
clusterName := logicalcluster.From(synctarget)
52-
if len(synctarget.Spec.SupportedAPIExports) == 0 {
43+
clusterName := logicalcluster.From(syncTarget)
44+
if len(syncTarget.Spec.SupportedAPIExports) == 0 {
5345
return []string{client.ToClusterAwareKey(clusterName.Path(), reconcilerapiexport.TemporaryComputeServiceExportName)}, nil
5446
}
5547

56-
keys := make([]string, 0, len(synctarget.Spec.SupportedAPIExports))
57-
for _, export := range synctarget.Spec.SupportedAPIExports {
58-
if len(export.Export) == 0 {
59-
keys = append(keys, client.ToClusterAwareKey(clusterName.Path(), export.Export))
60-
continue
48+
keys := make([]string, 0, len(syncTarget.Spec.SupportedAPIExports))
49+
for _, export := range syncTarget.Spec.SupportedAPIExports {
50+
path := export.Path
51+
if path == "" {
52+
path = clusterName.String()
6153
}
62-
keys = append(keys, client.ToClusterAwareKey(logicalcluster.Name(export.Path).Path(), export.Export))
54+
keys = append(keys, client.ToClusterAwareKey(logicalcluster.NewPath(path), export.Export))
6355
}
6456

6557
return keys, nil

pkg/virtual/syncer/controllers/apireconciler/syncer_apireconciler_reconcile.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (c *APIReconciler) reconcile(ctx context.Context, apiDomainKey dynamicconte
4545
logger := klog.FromContext(ctx)
4646

4747
// collect APIResourceSchemas by syncTarget.
48-
apiResourceSchemas, schemaIdentites, err := c.getAllAcceptedResourceSchemas(syncTarget)
48+
apiResourceSchemas, schemaIdentites, err := c.getAllAcceptedResourceSchemas(ctx, syncTarget)
4949
if err != nil {
5050
return err
5151
}
@@ -150,40 +150,63 @@ func gvrString(gvr schema.GroupVersionResource) string {
150150

151151
// getAllAcceptedResourceSchemas return all resourceSchemas from APIExports defined in this syncTarget filtered by the status.syncedResource
152152
// of syncTarget such that only resources with accepted state is returned, together with their identityHash.
153-
func (c *APIReconciler) getAllAcceptedResourceSchemas(syncTarget *workloadv1alpha1.SyncTarget) (map[schema.GroupResource]*apisv1alpha1.APIResourceSchema, map[schema.GroupResource]string, error) {
153+
func (c *APIReconciler) getAllAcceptedResourceSchemas(ctx context.Context, syncTarget *workloadv1alpha1.SyncTarget) (map[schema.GroupResource]*apisv1alpha1.APIResourceSchema, map[schema.GroupResource]string, error) {
154154
apiResourceSchemas := map[schema.GroupResource]*apisv1alpha1.APIResourceSchema{}
155155

156156
identityHashByGroupResource := map[schema.GroupResource]string{}
157157

158+
logger := klog.FromContext(ctx)
159+
logger.V(4).Info("getting identity hashes for compatible APIs", "count", len(syncTarget.Status.SyncedResources))
160+
158161
// get all identityHash for compatible APIs
159162
for _, syncedResource := range syncTarget.Status.SyncedResources {
163+
logger := logger.WithValues(
164+
"group", syncedResource.Group,
165+
"resource", syncedResource.Resource,
166+
"identity", syncedResource.IdentityHash,
167+
)
160168
if syncedResource.State == workloadv1alpha1.ResourceSchemaAcceptedState {
169+
logger.V(4).Info("including synced resource because it is accepted")
161170
identityHashByGroupResource[schema.GroupResource{
162171
Group: syncedResource.Group,
163172
Resource: syncedResource.Resource,
164173
}] = syncedResource.IdentityHash
174+
} else {
175+
logger.V(4).Info("excluding synced resource because it is unaccepted")
165176
}
166177
}
167178

179+
logger.V(4).Info("processing supported APIExports", "count", len(syncTarget.Spec.SupportedAPIExports))
168180
var errs []error
169181
for _, exportRef := range syncTarget.Spec.SupportedAPIExports {
182+
logger.V(4).Info("looking at export", "path", exportRef.Path, "name", exportRef.Export)
183+
170184
path := logicalcluster.NewPath(exportRef.Path)
171185
if path.Empty() {
186+
logger.V(4).Info("falling back to sync target's logical cluster for path")
172187
path = logicalcluster.From(syncTarget).Path()
173188
}
174189

190+
logger := logger.WithValues("path", path, "name", exportRef.Export)
191+
logger.V(4).Info("getting APIExport")
175192
apiExport, err := indexers.ByPathAndName[*apisv1alpha1.APIExport](apisv1alpha1.Resource("apiexports"), c.apiExportIndexer, path, exportRef.Export)
176193
if err != nil {
194+
logger.V(4).Error(err, "error getting APIExport")
177195
errs = append(errs, err)
178196
continue
179197
}
180198

199+
logger.V(4).Info("checking APIExport's schemas", "count", len(apiExport.Spec.LatestResourceSchemas))
181200
for _, schemaName := range apiExport.Spec.LatestResourceSchemas {
201+
logger := logger.WithValues("schema", schemaName)
202+
logger.V(4).Info("getting APIResourceSchema")
182203
apiResourceSchema, err := c.apiResourceSchemaLister.Cluster(logicalcluster.From(apiExport)).Get(schemaName)
183204
if apierrors.IsNotFound(err) {
205+
logger.V(4).Info("APIResourceSchema not found")
184206
continue
185207
}
186208
if err != nil {
209+
logger.V(4).Error(err, "error getting APIResourceSchema")
187210
errs = append(errs, err)
188211
continue
189212
}
@@ -193,9 +216,14 @@ func (c *APIReconciler) getAllAcceptedResourceSchemas(syncTarget *workloadv1alph
193216
Resource: apiResourceSchema.Spec.Names.Plural,
194217
}
195218

219+
logger = logger.WithValues("group", gr.Group, "resource", gr.Resource)
220+
196221
// if identityHash does not exist, it is not a compatible API.
197222
if _, ok := identityHashByGroupResource[gr]; ok {
223+
logger.V(4).Info("identity found, including resource")
198224
apiResourceSchemas[gr] = apiResourceSchema
225+
} else {
226+
logger.V(4).Info("identity not found, excluding resource")
199227
}
200228
}
201229
}

0 commit comments

Comments
 (0)