aboutsummaryrefslogtreecommitdiff
path: root/plugin/kubernetes/object/namespace.go
diff options
context:
space:
mode:
authorGravatar Mat Lowery <mlowery@ebay.com> 2021-07-29 21:27:25 -0600
committerGravatar GitHub <noreply@github.com> 2021-07-29 23:27:25 -0400
commit9d5b8cd13d541cc3d2b7598ab255643d6298ba9d (patch)
tree5ac6f17f1d1a6b0e9033c759fb276a52c3ffab4e /plugin/kubernetes/object/namespace.go
parent8ff7c4b83444c488051384cea0606f085e5b6959 (diff)
downloadcoredns-9d5b8cd13d541cc3d2b7598ab255643d6298ba9d.tar.gz
coredns-9d5b8cd13d541cc3d2b7598ab255643d6298ba9d.tar.zst
coredns-9d5b8cd13d541cc3d2b7598ab255643d6298ba9d.zip
kubernetes: Improve namespace usage (#4767)
* Use GetByKey instead of List in GetNamespaceByName. * Add ToNamespace to reduce memory for namespace cache. Signed-off-by: Mat Lowery <mlowery@ebay.com>
Diffstat (limited to 'plugin/kubernetes/object/namespace.go')
-rw-r--r--plugin/kubernetes/object/namespace.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/plugin/kubernetes/object/namespace.go b/plugin/kubernetes/object/namespace.go
new file mode 100644
index 000000000..b800cbb77
--- /dev/null
+++ b/plugin/kubernetes/object/namespace.go
@@ -0,0 +1,61 @@
+package object
+
+import (
+ "fmt"
+
+ api "k8s.io/api/core/v1"
+ meta "k8s.io/apimachinery/pkg/apis/meta/v1"
+ "k8s.io/apimachinery/pkg/runtime"
+)
+
+// Namespace is a stripped down api.Namespace with only the items we need for CoreDNS.
+type Namespace struct {
+ // Don't add new fields to this struct without talking to the CoreDNS maintainers.
+ Version string
+ Name string
+
+ *Empty
+}
+
+// ToNamespace returns a function that converts an api.Namespace to a *Namespace.
+func ToNamespace(obj meta.Object) (meta.Object, error) {
+ ns, ok := obj.(*api.Namespace)
+ if !ok {
+ return nil, fmt.Errorf("unexpected object %v", obj)
+ }
+ n := &Namespace{
+ Version: ns.GetResourceVersion(),
+ Name: ns.GetName(),
+ }
+ *ns = api.Namespace{}
+ return n, nil
+}
+
+var _ runtime.Object = &Namespace{}
+
+// DeepCopyObject implements the ObjectKind interface.
+func (n *Namespace) DeepCopyObject() runtime.Object {
+ n1 := &Namespace{
+ Version: n.Version,
+ Name: n.Name,
+ }
+ return n1
+}
+
+// GetNamespace implements the metav1.Object interface.
+func (n *Namespace) GetNamespace() string { return "" }
+
+// SetNamespace implements the metav1.Object interface.
+func (n *Namespace) SetNamespace(namespace string) {}
+
+// GetName implements the metav1.Object interface.
+func (n *Namespace) GetName() string { return n.Name }
+
+// SetName implements the metav1.Object interface.
+func (n *Namespace) SetName(name string) {}
+
+// GetResourceVersion implements the metav1.Object interface.
+func (n *Namespace) GetResourceVersion() string { return n.Version }
+
+// SetResourceVersion implements the metav1.Object interface.
+func (n *Namespace) SetResourceVersion(version string) {}