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

Issue #1595 ---Part2 --- Add namespace denied list #1595

Merged
merged 7 commits into from
Oct 20, 2021

Conversation

mallow111
Copy link
Contributor

What this PR does / why we need it:
Details: #1593

How does this change affect the cardinality of KSM: (increases, decreases or does not change cardinality)

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #1593

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Sep 30, 2021
@k8s-ci-robot
Copy link
Contributor

Welcome @mallow111!

It looks like this is your first PR to kubernetes/kube-state-metrics 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kube-state-metrics has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Sep 30, 2021
@mallow111
Copy link
Contributor Author

@dgrisonnet any idea how to fix the ci-validate-docs check?

@fpetkovski
Copy link
Contributor

Thanks for the contribution @mallow111! To fix the docs, you can run make generate.

Besides that, the PR seems to need a bit more work. Currently, the namespace-denylist option only excludes namespaces from the namespaces list. If the namespaces list is empty, KSM will export metrics from all namespaces, regardless of what is in the namespace denylist.

One way to get namespace exclusion to work when watching all namespaces is to propagate the namespace denylist all the way down to the listwatchers. Here is an example of how the createConfigMapListWatch could end up looking like:

func createConfigMapListWatch(kubeClient clientset.Interface, ns string, nsDenylist []string) cache.ListerWatcher {
	fieldSelector := excludeNamespacesFieldSelector(nsDenylist)
	return &cache.ListWatch{
		ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
			opts.FieldSelector = fieldSelector
			return kubeClient.CoreV1().ConfigMaps(ns).List(context.TODO(), opts)
		},
		WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
			opts.FieldSelector = fieldSelector
			return kubeClient.CoreV1().ConfigMaps(ns).Watch(context.TODO(), opts)
		},
	}
}

func excludeNamespacesFieldSelector(nsDenylist []string) string {
	if len(nsDenylist) == 0 {
		return ""
	}

	namespaceExcludeSelectors := make([]fields.Selector, len(nsDenylist))
	for i, ns := range nsDenylist {
		selector := fields.OneTermNotEqualSelector("metadata.namespace", ns)
		namespaceExcludeSelectors[i] = selector
	}
	return fields.AndSelectors(namespaceExcludeSelectors...).String()
}

@mallow111
Copy link
Contributor Author

@fpetkovski Please take a look at the PR for the listwatcher change, once it is done, I can go back to this pr and address the rest, trying to break big PR into smaller ones.
#1596

@mallow111 mallow111 changed the title Add namespace denied list Issue #1595 ---Part2 --- Add namespace denied list Oct 6, 2021
k8s-ci-robot added a commit that referenced this pull request Oct 8, 2021
Issue #1595  --Part1-- Add fieldSelector in listWatchFunc
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 10, 2021
@mallow111
Copy link
Contributor Author

mallow111 commented Oct 11, 2021

@fpetkovski
@mrueg
PR #1596 is merged, now I am back to this PR again, once this is approved, I will work on the TODO part, which is the last bit of this issue, it might involve a few unit test update to make sure the flow is working.

pkg/options/types.go Outdated Show resolved Hide resolved
@mallow111
Copy link
Contributor Author

@fpetkovski updated, can you check again, thanks!

@fpetkovski
Copy link
Contributor

Thanks @mallow111, this looks good so far. Any reason why you don't want to use the GetExcludeNSFieldSelector function now instead of the empty fieldselector?

@mallow111
Copy link
Contributor Author

@fabriziopandini updated, check again please

@fpetkovski
Copy link
Contributor

Great stuff!

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 20, 2021
@mallow111
Copy link
Contributor Author

@mrueg can you help to approve, thanks!

@mrueg
Copy link
Member

mrueg commented Oct 20, 2021

/lgtm

Thanks for your work!

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: fpetkovski, mallow111, mrueg

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 20, 2021
@k8s-ci-robot k8s-ci-robot merged commit b730cb4 into kubernetes:master Oct 20, 2021
@mallow111
Copy link
Contributor Author

It is merged, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add namespace denylist option
4 participants