Skip to content

Commit 7d150d0

Browse files
authored
chore: more docstrings (#606)
Signed-off-by: Michael Crenshaw <[email protected]>
1 parent adb68bc commit 7d150d0

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

pkg/cache/cluster.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ const (
6969
)
7070

7171
type apiMeta struct {
72-
namespaced bool
72+
namespaced bool
73+
// watchCancel stops the watch of all resources for this API. This gets called when the cache is invalidated or when
74+
// the watched API ceases to exist (e.g. a CRD gets deleted).
7375
watchCancel context.CancelFunc
7476
}
7577

@@ -468,7 +470,7 @@ func (c *clusterCache) stopWatching(gk schema.GroupKind, ns string) {
468470
}
469471
}
470472

471-
// startMissingWatches lists supported cluster resources and start watching for changes unless watch is already running
473+
// startMissingWatches lists supported cluster resources and starts watching for changes unless watch is already running
472474
func (c *clusterCache) startMissingWatches() error {
473475
apis, err := c.kubectl.GetAPIResources(c.config, true, c.settings.ResourcesFilter)
474476
if err != nil {
@@ -570,6 +572,7 @@ func (c *clusterCache) listResources(ctx context.Context, resClient dynamic.Reso
570572
return resourceVersion, callback(listPager)
571573
}
572574

575+
// loadInitialState loads the state of all the resources retrieved by the given resource client.
573576
func (c *clusterCache) loadInitialState(ctx context.Context, api kube.APIResourceInfo, resClient dynamic.ResourceInterface, ns string, lock bool) (string, error) {
574577
var items []*Resource
575578
resourceVersion, err := c.listResources(ctx, resClient, func(listPager *pager.ListPager) error {
@@ -728,6 +731,9 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo
728731
})
729732
}
730733

734+
// processApi processes all the resources for a given API. First we construct an API client for the given API. Then we
735+
// call the callback. If we're managing the whole cluster, we call the callback with the client and an empty namespace.
736+
// If we're managing specific namespaces, we call the callback for each namespace.
731737
func (c *clusterCache) processApi(client dynamic.Interface, api kube.APIResourceInfo, callback func(resClient dynamic.ResourceInterface, ns string) error) error {
732738
resClient := client.Resource(api.GroupVersionResource)
733739
switch {
@@ -797,6 +803,17 @@ func (c *clusterCache) checkPermission(ctx context.Context, reviewInterface auth
797803
return true, nil
798804
}
799805

806+
// sync retrieves the current state of the cluster and stores relevant information in the clusterCache fields.
807+
//
808+
// First we get some metadata from the cluster, like the server version, OpenAPI document, and the list of all API
809+
// resources.
810+
//
811+
// Then we get a list of the preferred versions of all API resources which are to be monitored (it's possible to exclude
812+
// resources from monitoring). We loop through those APIs asynchronously and for each API we list all resources. We also
813+
// kick off a goroutine to watch the resources for that API and update the cache constantly.
814+
//
815+
// When this function exits, the cluster cache is up to date, and the appropriate resources are being watched for
816+
// changes.
800817
func (c *clusterCache) sync() error {
801818
c.log.Info("Start syncing cluster")
802819

@@ -843,6 +860,8 @@ func (c *clusterCache) sync() error {
843860
if err != nil {
844861
return err
845862
}
863+
864+
// Each API is processed in parallel, so we need to take out a lock when we update clusterCache fields.
846865
lock := sync.Mutex{}
847866
err = kube.RunAllAsync(len(apis), func(i int) error {
848867
api := apis[i]

0 commit comments

Comments
 (0)