-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New property: IgnoreNames in NamespaceSelector
To allow specif namespaces to be ignored even if they were included via labels or matchNames. This can be useful to ignore system-relevant namespaces
- Loading branch information
Showing
11 changed files
with
222 additions
and
122 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package controllers | ||
|
||
import ( | ||
"fmt" | ||
"github.com/vshn/espejo/api/v1alpha1" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"regexp" | ||
) | ||
|
||
func (r *SyncConfigReconciler) validateSpec(rc *ReconciliationContext) error { | ||
spec := rc.cfg.Spec | ||
if hasNoNamespaceSelector(rc.cfg.Spec) { | ||
return fmt.Errorf("either .spec.namespaceSelector.matchNames or .spec.namespaceSelector.labelSelector is required") | ||
} | ||
if len(spec.DeleteItems) == 0 && len(spec.SyncItems) == 0 { | ||
return fmt.Errorf("either spec.deleteItems or .spec.syncItems is required") | ||
} | ||
for _, pattern := range spec.NamespaceSelector.MatchNames { | ||
rgx, err := regexp.Compile(pattern) | ||
if err != nil { | ||
return fmt.Errorf(".spec.namespaceSelector.matchNames pattern invalid: %w", err) | ||
} | ||
rc.matchNamesRegex = append(rc.matchNamesRegex, rgx) | ||
} | ||
for _, pattern := range spec.NamespaceSelector.IgnoreNames { | ||
rgx, err := regexp.Compile(pattern) | ||
if err != nil { | ||
return fmt.Errorf(".spec.namespaceSelector.ignoreNames pattern invalid: %w", err) | ||
} | ||
rc.ignoreNamesRegex = append(rc.ignoreNamesRegex, rgx) | ||
} | ||
if rc.cfg.Spec.NamespaceSelector.LabelSelector != nil { | ||
labelSelector, err := metav1.LabelSelectorAsSelector(rc.cfg.Spec.NamespaceSelector.LabelSelector) | ||
if err != nil { | ||
return fmt.Errorf(".spec.namespaceSelector.labelSelector is invalid: %w", err) | ||
} | ||
rc.nsSelector = labelSelector | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func filterNamespaces(rc *ReconciliationContext, namespaceList []v1.Namespace) (namespaces []v1.Namespace) { | ||
NamespaceLoop: | ||
for _, ns := range namespaceList { | ||
for _, regex := range rc.ignoreNamesRegex { | ||
if regex.MatchString(ns.Name) { | ||
continue NamespaceLoop | ||
} | ||
} | ||
if rc.nsSelector != nil && rc.nsSelector.Matches(labels.Set(ns.GetLabels())) { | ||
namespaces = append(namespaces, ns) | ||
continue NamespaceLoop | ||
} | ||
for _, regex := range rc.matchNamesRegex { | ||
if regex.MatchString(ns.Name) { | ||
namespaces = append(namespaces, ns) | ||
continue NamespaceLoop | ||
} | ||
} | ||
} | ||
return namespaces | ||
} | ||
|
||
// isReconcileFailed returns true if no objects could be synced or deleted and failedCount is > 0 | ||
func isReconcileFailed(rc *ReconciliationContext) bool { | ||
return rc.syncCount == 0 && rc.deleteCount == 0 && rc.failCount > 0 | ||
} | ||
|
||
// hasNoNamespaceSelector will return true if the SyncConfigSpec does not have a valid namespace selector | ||
func hasNoNamespaceSelector(spec v1alpha1.SyncConfigSpec) bool { | ||
if spec.NamespaceSelector == nil { | ||
return true | ||
} | ||
return spec.NamespaceSelector.LabelSelector == nil && len(spec.NamespaceSelector.MatchNames) == 0 | ||
} |
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package controllers | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/vshn/espejo/api/v1alpha1" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"regexp" | ||
) | ||
|
||
var _ = Describe("SyncConfig utils", func() { | ||
It("should include regular namespace name in selector", func() { | ||
ns := "match-regular-namespace" | ||
|
||
sourceNs := []v1.Namespace{namespaceFromString(ns)} | ||
rc := ReconciliationContext{matchNamesRegex: []*regexp.Regexp{toRegex(ns)}} | ||
|
||
result := filterNamespaces(&rc, sourceNs) | ||
Expect(result).To(ConsistOf(sourceNs)) | ||
}) | ||
|
||
It("should include namespace that match a pattern", func() { | ||
ns := "match-regex-namespace" | ||
|
||
sourceNs := []v1.Namespace{namespaceFromString(ns)} | ||
|
||
rc := ReconciliationContext{matchNamesRegex: []*regexp.Regexp{toRegex("match-.*")}} | ||
|
||
result := filterNamespaces(&rc, sourceNs) | ||
Expect(result).To(ConsistOf(sourceNs)) | ||
}) | ||
|
||
It("should exclude namespace that does not match a pattern", func() { | ||
ns := "match-regex-namespace" | ||
|
||
sourceNs := []v1.Namespace{namespaceFromString(ns)} | ||
|
||
rc := ReconciliationContext{matchNamesRegex: []*regexp.Regexp{toRegex("match\\snamespaceWithSpace")}} | ||
|
||
result := filterNamespaces(&rc, sourceNs) | ||
Expect(result).To(BeEmpty()) | ||
}) | ||
|
||
It("should fail validation if invalid regex is specified in matchNames", func() { | ||
cfg := &v1alpha1.SyncConfig{Spec: v1alpha1.SyncConfigSpec{ | ||
NamespaceSelector: &v1alpha1.NamespaceSelector{ | ||
MatchNames: []string{"["}, | ||
}, | ||
SyncItems: []unstructured.Unstructured{toUnstructured(&v1.ConfigMap{})}, | ||
}} | ||
rc := ReconciliationContext{cfg: cfg} | ||
|
||
err := syncConfigReconciler.validateSpec(&rc) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("error parsing regexp")) | ||
}) | ||
|
||
It("should fail validation if invalid regex is specified in ignoreNames", func() { | ||
cfg := &v1alpha1.SyncConfig{Spec: v1alpha1.SyncConfigSpec{ | ||
NamespaceSelector: &v1alpha1.NamespaceSelector{ | ||
IgnoreNames: []string{"["}, | ||
MatchNames: []string{".*"}, | ||
}, | ||
SyncItems: []unstructured.Unstructured{toUnstructured(&v1.ConfigMap{})}, | ||
}} | ||
rc := ReconciliationContext{cfg: cfg} | ||
|
||
err := syncConfigReconciler.validateSpec(&rc) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("error parsing regexp")) | ||
}) | ||
|
||
It("should fail validation if no namespace selector is given", func() { | ||
cfg := &v1alpha1.SyncConfig{Spec: v1alpha1.SyncConfigSpec{ | ||
NamespaceSelector: &v1alpha1.NamespaceSelector{ | ||
MatchNames: []string{}, | ||
}, | ||
}} | ||
rc := ReconciliationContext{cfg: cfg} | ||
|
||
err := syncConfigReconciler.validateSpec(&rc) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("labelSelector is required")) | ||
}) | ||
|
||
}) | ||
|
||
func toRegex(pattern string) *regexp.Regexp { | ||
rgx, _ := regexp.Compile(pattern) | ||
return rgx | ||
} |
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
Oops, something went wrong.