Skip to content
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

make webhook to use local nad cache #106

Merged
merged 3 commits into from
Jun 28, 2021

Conversation

pperiyasamy
Copy link
Contributor

@pperiyasamy pperiyasamy commented Jun 10, 2021

this implements local cache using network-attachment-definition-client's informer, web hook looks up this cache, fall back to K8 api when entry is not found.

Fixes #101

Signed-off-by: Periyasamy Palanisamy [email protected]

@pperiyasamy pperiyasamy changed the title add nad cache and make webhook to reuse it make webhook to use local nad cache Jun 10, 2021
@pperiyasamy
Copy link
Contributor Author

/cc @JanScheurich

&sync.Mutex{}, make(chan struct{})}
}

// Start start the informer for NetworkAttachmentDefinition, upon events populate the cache
Copy link
Member

Choose a reason for hiding this comment

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

nit: two "starts"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


// Start start the informer for NetworkAttachmentDefinition, upon events populate the cache
func (nc *NetAttachDefCache) Start() {
factory := externalversions.NewFilteredSharedInformerFactory(setupNetAttachDefClient(), 0, "", func(o *metav1.ListOptions) {})
Copy link
Member

Choose a reason for hiding this comment

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

https://github.com/k8snetworkplumbingwg/network-attachment-definition-client/blob/66a699ae3b055dab17360364506de9c7d44627ec/pkg/client/informers/externalversions/factory.go#L86

I see the autogenerated func comment says this func is depreciated and to use NewSharedInformerFactoryWithOptions instead. I am worried about this function being phased out in future releases and we may have broken code here. Can you switch to NewSharedInformerFactoryWithOptions - seems simple?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


// Stop stop the NetworkAttachmentDefinition informer
func (nc *NetAttachDefCache) Stop() {
close(nc.stopper)
Copy link
Member

@martinkennelly martinkennelly Jun 18, 2021

Choose a reason for hiding this comment

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

I thought the informer would force the user who is calling the "Run" function to pass a channel which would signal back to main thread when it has ended following the signal to the informer to stop here.

I was just thinking if we close this, and then if the main process ends before the informer go routine ends, then it will be killed without finishing naturally. Did you look into this? Looks like nothing you can do so I am not saying to rework this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good find! now added a code for graceful shutdown of nad cache with atomic isRunning variable, hope that's ok now.


func (nc *NetAttachDefCache) remove(networkName string) {
nc.networkAnnotationsMapMutex.Lock()
delete(nc.networkAnnotationsMap[networkName], networkName)
Copy link
Member

Choose a reason for hiding this comment

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

is this right? shouldn't it be delete(nc.networkAnnotationsMap, networkName)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.


// Get returns annotations map for the given network name, if it's not available
// return nil
func (nc *NetAttachDefCache) Get(networkName string) map[string]string {
Copy link
Member

Choose a reason for hiding this comment

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

small nit - feel free to ignore: up to you if you want to change it - it would be better to have two arguments here I think - namespace and nad name, then you can bury the joining of the two strings net.Namespace + "/" + net.Name in this function instead of having to do it consistently when calling it like I see below. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, it makes sense. done.

glog.Error(reason)
return reqs, nsMap, reason
var annotationsMap map[string]string
annotationsMap = nadCache.Get(net.Namespace + "/" + net.Name)
Copy link
Member

Choose a reason for hiding this comment

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

nit: why not ":=" save one line :P

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Signed-off-by: Periyasamy Palanisamy <[email protected]>
Signed-off-by: Periyasamy Palanisamy <[email protected]>
Copy link
Member

@martinkennelly martinkennelly left a comment

Choose a reason for hiding this comment

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

LGTM - just small change required. Thanks Peri for the changes!!

// Stop teardown the NetworkAttachmentDefinition informer
func (nc *NetAttachDefCache) Stop() {
close(nc.stopper)
func(limit time.Duration) {
Copy link
Member

Choose a reason for hiding this comment

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

why wrap this logic in a func? I think it makes it harder to read versus a normal loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes :) done.

atomic.StoreInt32(&(nc.isRunning), int32(1))
// informer Run blocks until informer is stopped
glog.Infof("starting net-attach-def informer")
informer.Run(nc.stopper)
Copy link
Member

Choose a reason for hiding this comment

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

Can we put in a log after .Run "net-attach-def informer stopped"? It may aid debugging.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Signed-off-by: Periyasamy Palanisamy <[email protected]>
Copy link
Member

@martinkennelly martinkennelly left a comment

Choose a reason for hiding this comment

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

I have verified this change with #98 ! Thanks @pperiyasamy for all your hard work and changes here! I am waiting on @zshi-redhat to review this now as there are changes since he last approved.

// Stop teardown the NetworkAttachmentDefinition informer
func (nc *NetAttachDefCache) Stop() {
close(nc.stopper)
tEnd := time.Now().Add(3 * time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you explain why you choose 3 seconds? Is this enough to stop net-attach-def informer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MichalGuzieniuk thanks for looking at this. I just chose 3 seconds based on Kubelet's default shutdown grace period (i.e. 30 sec) for a pod. but, yes, if net-attach-def events are not yet flushed away from informer then it might take a while for informer to return after close.
there is no much activity happening in this cache module apart from updating its local map, do you see this as a problem or any other concern ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Fine for me. I was just interested if you choose this value because in tests you discovered that is the best or had other reason behind.

@zshi-redhat
Copy link
Collaborator

I have verified this change with #98 ! Thanks @pperiyasamy for all your hard work and changes here! I am waiting on @zshi-redhat to review this now as there are changes since he last approved.

Thanks for all the changes and reviews!

@zshi-redhat zshi-redhat merged commit df45d81 into k8snetworkplumbingwg:master Jun 28, 2021
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.

resource not injected in a large K8s cluster
4 participants