-
Notifications
You must be signed in to change notification settings - Fork 25
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
Conversation
@@ -101,7 +100,6 @@ func Run(cmd *cobra.Command, args []string) { | |||
} | |||
|
|||
dataGatherers := map[string]datagatherer.DataGatherer{} | |||
var wg sync.WaitGroup |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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.
626b018
to
c3b59f1
Compare
// 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 { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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>
c3b59f1
to
7f756c3
Compare
There was a problem hiding this 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) |
There was a problem hiding this comment.
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!
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) | ||
} | ||
}() |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
jetstack-secure/pkg/agent/run.go
Lines 167 to 181 in 7f756c3
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Simplifications I found while reading through the code.