Skip to content

Cleanup Kubernetes datagather #571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2024
Merged

Cleanup Kubernetes datagather #571

merged 1 commit into from
Sep 18, 2024

Conversation

inteon
Copy link
Contributor

@inteon inteon commented Sep 16, 2024

Simplifications I found while reading through the code.

@@ -101,7 +100,6 @@ func Run(cmd *cobra.Command, args []string) {
}

dataGatherers := map[string]datagatherer.DataGatherer{}
var wg sync.WaitGroup
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This waitgroup was not used.

// wait for the informer to complete an initial sync, we do this to
// attempt to have an initial set of data for the first upload of
// the run.
if err := newDg.WaitForCacheSync(bootCtx.Done()); err != nil {
Copy link
Contributor Author

@inteon inteon Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of waiting for each dataGatherer before starting to the next, we should first start all dataGatherers and loop again to wait for them to finish ( that is what I did in the updated code ).

@@ -143,20 +143,21 @@ var kubernetesNativeResources = map[schema.GroupVersionResource]sharedInformerFu

// NewDataGatherer constructs a new instance of the generic K8s data-gatherer for the provided
func (c *ConfigDynamic) NewDataGatherer(ctx context.Context) (datagatherer.DataGatherer, error) {
cl, err := NewDynamicClient(c.KubeConfigPath)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only have to create a dynamic client when the GVK is not a native resource.
So this function should be called as part of the else body.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! Is it fair to say that there's not really a reason for newDataGathererWithClient to be a separate function? It's fishy that we call it with one of cl or clientset with the other set to nil.

Not a requirement in this PR, of course!

}

factory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are creating these factories to create just 1 informer.
I still create the factory ( maybe this can be simplified in the future ), but I now let it go out-of-scope and continue with the created informer instead.

@inteon inteon force-pushed the cleanup_datagather branch 3 times, most recently from 626b018 to c3b59f1 Compare September 17, 2024 08:14
// namespaceResourceInterface will 'namespace' a NamespaceableResourceInterface
// if the 'namespace' parameter is non-empty, otherwise it will return the
// given ResourceInterface as-is.
func namespaceResourceInterface(iface dynamic.NamespaceableResourceInterface, namespace string) dynamic.ResourceInterface {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was unused.

@@ -145,19 +141,24 @@ func Run(cmd *cobra.Command, args []string) {
dataGatherers[dgConfig.Name] = newDg
}

// wait for initial sync period to complete. if unsuccessful, then crash
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic was not doing anything, since the WaitGroup was not being used.

…sed fields and duplicate code

Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
Copy link
Contributor

@SgtCoDFish SgtCoDFish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

The only reason I'm even a little concerned about this is that I don't really know how the agent is tested and the extent to which it's tested. On the assumption that it is well tested I'm happy to approve!

This is really good, thank you!

@@ -143,20 +143,21 @@ var kubernetesNativeResources = map[schema.GroupVersionResource]sharedInformerFu

// NewDataGatherer constructs a new instance of the generic K8s data-gatherer for the provided
func (c *ConfigDynamic) NewDataGatherer(ctx context.Context) (datagatherer.DataGatherer, error) {
cl, err := NewDynamicClient(c.KubeConfigPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! Is it fair to say that there's not really a reason for newDataGathererWithClient to be a separate function? It's fishy that we call it with one of cl or clientset with the other set to nil.

Not a requirement in this PR, of course!

@inteon inteon merged commit 924390f into master Sep 18, 2024
8 checks passed
@SgtCoDFish SgtCoDFish deleted the cleanup_datagather branch September 18, 2024 09:08
Comment on lines +106 to +112
defer func() {
// TODO: replace Fatalf log calls with Errorf and return the error
cancel()
if err := group.Wait(); err != nil {
logs.Log.Fatalf("failed to wait for controller-runtime component to stop: %v", err)
}
}()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this doing? It seems like we now cancel immediately, and then block on group.Wait?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, we cancel the context to start the shutdown and wait for the shutdown to complete using group.Wait

Copy link
Member

@maelvls maelvls Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we try to shutdown here? I don't understand the purpose of this defer func.

From what I can tell, it should be the opposite: if the errgroup ever ends, cancel the context so that the other things stop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error group will always cancel its context when one of the goroutines returns an error.

For successful execution, we only reach this defer call when the following for loop ends because OneShot is true:

for {
// if period is set in the config, then use that if not already set
if Flags.Period == 0 && config.Period > 0 {
logs.Log.Printf("Using period from config %s", config.Period)
Flags.Period = config.Period
}
gatherAndOutputData(config, preflightClient, dataGatherers)
if Flags.OneShot {
break
}
time.Sleep(Flags.Period)
}

If OneShot is false, the loop will keep looping.

Copy link
Member

@maelvls maelvls Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, this information should be written down as a comment in code since this section of code isn't obvious.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that, I was reading go func instead of defer func... It now makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants