-
Notifications
You must be signed in to change notification settings - Fork 247
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
feat: add deny-label-ns flag which supports wildcard #1051
Merged
k8s-ci-robot
merged 1 commit into
kubernetes-sigs:master
from
AhmedGrati:feat-add-deny-label-ns-with-wildcard
Feb 15, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,7 @@ type Annotations map[string]string | |
type Args struct { | ||
CaFile string | ||
CertFile string | ||
DenyLabelNs utils.StringSetVal | ||
ExtraLabelNs utils.StringSetVal | ||
Instance string | ||
KeyFile string | ||
|
@@ -78,6 +79,11 @@ type Args struct { | |
ResourceLabels utils.StringSetVal | ||
} | ||
|
||
type deniedNs struct { | ||
normal utils.StringSetVal | ||
wildcard utils.StringSetVal | ||
} | ||
|
||
type NfdMaster interface { | ||
Run() error | ||
Stop() | ||
|
@@ -95,6 +101,7 @@ type nfdMaster struct { | |
ready chan bool | ||
apihelper apihelper.APIHelpers | ||
kubeconfig *restclient.Config | ||
deniedNs | ||
} | ||
|
||
// NewNfdMaster creates a new NfdMaster server instance. | ||
|
@@ -126,6 +133,16 @@ func NewNfdMaster(args *Args) (NfdMaster, error) { | |
return nfd, fmt.Errorf("-ca-file needs to be specified alongside -cert-file and -key-file") | ||
} | ||
} | ||
if args.DenyLabelNs == nil { | ||
args.DenyLabelNs = make(utils.StringSetVal) | ||
} | ||
// Pre-process DenyLabelNS into 2 lists: one for normal ns, and the other for wildcard ns | ||
normalDeniedNs, wildcardDeniedNs := preProcessDeniedNamespaces(args.DenyLabelNs) | ||
nfd.deniedNs.normal = normalDeniedNs | ||
nfd.deniedNs.wildcard = wildcardDeniedNs | ||
// We forcibly deny kubernetes.io | ||
nfd.deniedNs.normal["kubernetes.io"] = struct{}{} | ||
nfd.deniedNs.wildcard[".kubernetes.io"] = struct{}{} | ||
|
||
// Initialize Kubernetes API helpers | ||
if !args.NoPublish { | ||
|
@@ -392,7 +409,7 @@ func (m *nfdMaster) updateMasterNode() error { | |
// into extended resources. This function also handles proper namespacing of | ||
// labels and ERs, i.e. adds the possibly missing default namespace for labels | ||
// arriving through the gRPC API. | ||
func filterFeatureLabels(labels Labels, extraLabelNs map[string]struct{}, labelWhiteList regexp.Regexp, extendedResourceNames map[string]struct{}) (Labels, ExtendedResources) { | ||
func (m *nfdMaster) filterFeatureLabels(labels Labels) (Labels, ExtendedResources) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, this looks so much cleaner 👍 |
||
outLabels := Labels{} | ||
|
||
for label, value := range labels { | ||
|
@@ -404,23 +421,26 @@ func filterFeatureLabels(labels Labels, extraLabelNs map[string]struct{}, labelW | |
// Check label namespace, filter out if ns is not whitelisted | ||
if ns != nfdv1alpha1.FeatureLabelNs && ns != nfdv1alpha1.ProfileLabelNs && | ||
!strings.HasSuffix(ns, nfdv1alpha1.FeatureLabelSubNsSuffix) && !strings.HasSuffix(ns, nfdv1alpha1.ProfileLabelSubNsSuffix) { | ||
if _, ok := extraLabelNs[ns]; !ok { | ||
klog.Errorf("Namespace %q is not allowed. Ignoring label %q\n", ns, label) | ||
continue | ||
// If the namespace is denied, and not present in the extraLabelNs, label will be ignored | ||
if isNamespaceDenied(ns, m.deniedNs.wildcard, m.deniedNs.normal) { | ||
if _, ok := m.args.ExtraLabelNs[ns]; !ok { | ||
klog.Errorf("Namespace %q is not allowed. Ignoring label %q\n", ns, label) | ||
continue | ||
} | ||
} | ||
} | ||
|
||
// Skip if label doesn't match labelWhiteList | ||
if !labelWhiteList.MatchString(name) { | ||
klog.Errorf("%s (%s) does not match the whitelist (%s) and will not be published.", name, label, labelWhiteList.String()) | ||
if !m.args.LabelWhiteList.Regexp.MatchString(name) { | ||
klog.Errorf("%s (%s) does not match the whitelist (%s) and will not be published.", name, label, m.args.LabelWhiteList.Regexp.String()) | ||
continue | ||
} | ||
outLabels[label] = value | ||
} | ||
|
||
// Remove labels which are intended to be extended resources | ||
extendedResources := ExtendedResources{} | ||
for extendedResourceName := range extendedResourceNames { | ||
for extendedResourceName := range m.args.ResourceLabels { | ||
// Add possibly missing default ns | ||
extendedResourceName = addNs(extendedResourceName, nfdv1alpha1.FeatureLabelNs) | ||
if value, ok := outLabels[extendedResourceName]; ok { | ||
|
@@ -449,6 +469,20 @@ func verifyNodeName(cert *x509.Certificate, nodeName string) error { | |
return nil | ||
} | ||
|
||
func isNamespaceDenied(labelNs string, wildcardDeniedNs map[string]struct{}, normalDeniedNs map[string]struct{}) bool { | ||
for deniedNs := range normalDeniedNs { | ||
if labelNs == deniedNs { | ||
return true | ||
} | ||
} | ||
for deniedNs := range wildcardDeniedNs { | ||
if strings.HasSuffix(labelNs, deniedNs) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// SetLabels implements LabelerServer | ||
func (m *nfdMaster) SetLabels(c context.Context, r *pb.SetLabelsRequest) (*pb.SetLabelsReply, error) { | ||
err := authorizeClient(c, m.args.VerifyNodeName, r.NodeName) | ||
|
@@ -583,7 +617,7 @@ func (m *nfdMaster) refreshNodeFeatures(cli *kubernetes.Clientset, nodeName stri | |
labels[k] = v | ||
} | ||
|
||
labels, extendedResources := filterFeatureLabels(labels, m.args.ExtraLabelNs, m.args.LabelWhiteList.Regexp, m.args.ResourceLabels) | ||
labels, extendedResources := m.filterFeatureLabels(labels) | ||
|
||
var taints []corev1.Taint | ||
if m.args.EnableTaints { | ||
|
@@ -918,6 +952,22 @@ func stringToNsNames(cslist, ns string) []string { | |
return names | ||
} | ||
|
||
// Seperate denied namespaces into two lists: | ||
// one contains wildcard namespaces the other contains normal namespaces | ||
func preProcessDeniedNamespaces(deniedNs map[string]struct{}) (normalDeniedNs map[string]struct{}, wildcardDeniedNs map[string]struct{}) { | ||
normalDeniedNs = map[string]struct{}{} | ||
wildcardDeniedNs = map[string]struct{}{} | ||
for ns := range deniedNs { | ||
if strings.HasPrefix(ns, "*") { | ||
trimedNs := strings.TrimLeft(ns, "*") | ||
wildcardDeniedNs[trimedNs] = struct{}{} | ||
} else { | ||
normalDeniedNs[ns] = struct{}{} | ||
} | ||
} | ||
return | ||
} | ||
|
||
func (m *nfdMaster) instanceAnnotation(name string) string { | ||
if m.args.Instance == "" { | ||
return name | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@marquiz should we have both flags at least for a release, having
ExtraLabelNS
to print a deprecation warning? I feel that removing the flag all together could break some users deployments if they try a roll-update and the pod get's called with old flags.WDYT?
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.
@marquiz suggested that we keep the
extra-label-ns
flag because that would give flexibility to our users.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.
@ArangoGutierrez yes, I don't see any reason removing the existing flag. It will also give flexibility in enabling scenarios like "deny all except for example.com/ and acme.io/"