From 4679f40654ff0080c07cf9057f3e6df2bbdd18c0 Mon Sep 17 00:00:00 2001 From: ajanikow <12255597+ajanikow@users.noreply.github.com> Date: Mon, 12 Oct 2020 06:36:06 +0000 Subject: [PATCH 1/2] Create annotations on pod creation --- pkg/deployment/images.go | 8 ++++++++ pkg/deployment/resources/pod_creator.go | 16 +++++++++++++++ .../resources/pod_creator_arangod.go | 10 ++++++++++ pkg/deployment/resources/pod_creator_sync.go | 10 ++++++++++ pkg/util/collection/map.go | 20 +++++++++++++++++++ pkg/util/k8sutil/interfaces/pod_creator.go | 3 +++ 6 files changed, 67 insertions(+) diff --git a/pkg/deployment/images.go b/pkg/deployment/images.go index 76f98eab3..5146b08d6 100644 --- a/pkg/deployment/images.go +++ b/pkg/deployment/images.go @@ -55,6 +55,14 @@ type ImageUpdatePod struct { image string } +func (i *ImageUpdatePod) Annotations() map[string]string { + return nil +} + +func (i *ImageUpdatePod) Labels() map[string]string { + return nil +} + type ArangoDImageUpdateContainer struct { spec api.DeploymentSpec image string diff --git a/pkg/deployment/resources/pod_creator.go b/pkg/deployment/resources/pod_creator.go index 7c0d5d5aa..5610081af 100644 --- a/pkg/deployment/resources/pod_creator.go +++ b/pkg/deployment/resources/pod_creator.go @@ -521,6 +521,22 @@ func RenderArangoPod(deployment k8sutil.APIObject, role, id, podName string, // Prepare basic pod p := k8sutil.NewPod(deployment.GetName(), role, id, podName, podCreator) + for k, v := range podCreator.Annotations() { + if p.Annotations == nil { + p.Annotations = map[string]string{} + } + + p.Annotations[k] = v + } + + for k, v := range podCreator.Labels() { + if p.Labels == nil { + p.Labels = map[string]string{} + } + + p.Labels[k] = v + } + podCreator.Init(&p) if initContainers, err := podCreator.GetInitContainers(); err != nil { diff --git a/pkg/deployment/resources/pod_creator_arangod.go b/pkg/deployment/resources/pod_creator_arangod.go index ab2cf101d..8a64f8a9c 100644 --- a/pkg/deployment/resources/pod_creator_arangod.go +++ b/pkg/deployment/resources/pod_creator_arangod.go @@ -27,6 +27,8 @@ import ( "math" "os" + "github.com/arangodb/kube-arangodb/pkg/util/collection" + "github.com/arangodb/kube-arangodb/pkg/deployment/resources/inspector" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/interfaces" @@ -476,3 +478,11 @@ func (m *MemberArangoDPod) ApplyPodSpec(p *core.PodSpec) error { return nil } + +func (m *MemberArangoDPod) Annotations() map[string]string { + return collection.MergeAnnotations(m.spec.Annotations, m.groupSpec.Annotations) +} + +func (m *MemberArangoDPod) Labels() map[string]string { + return collection.ReservedLabels().Filter(collection.MergeAnnotations(m.spec.Labels, m.groupSpec.Labels)) +} diff --git a/pkg/deployment/resources/pod_creator_sync.go b/pkg/deployment/resources/pod_creator_sync.go index 931e29e77..ff97a2c21 100644 --- a/pkg/deployment/resources/pod_creator_sync.go +++ b/pkg/deployment/resources/pod_creator_sync.go @@ -25,6 +25,8 @@ package resources import ( "math" + "github.com/arangodb/kube-arangodb/pkg/util/collection" + "github.com/arangodb/kube-arangodb/pkg/deployment/resources/inspector" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/interfaces" @@ -309,3 +311,11 @@ func (m *MemberSyncPod) Validate(cachedStatus inspector.Inspector) error { func (m *MemberSyncPod) ApplyPodSpec(spec *core.PodSpec) error { return nil } + +func (m *MemberSyncPod) Annotations() map[string]string { + return collection.MergeAnnotations(m.spec.Annotations, m.groupSpec.Annotations) +} + +func (m *MemberSyncPod) Labels() map[string]string { + return collection.ReservedLabels().Filter(collection.MergeAnnotations(m.spec.Labels, m.groupSpec.Labels)) +} diff --git a/pkg/util/collection/map.go b/pkg/util/collection/map.go index cef8da796..7b4fc18d3 100644 --- a/pkg/util/collection/map.go +++ b/pkg/util/collection/map.go @@ -87,6 +87,12 @@ func NewRestrictedList(param ...string) RestrictedList { return param } +func ReservedLabels() RestrictedList { + l := RestrictedList{} + l = append(l, reservedLabels...) + return l +} + type RestrictedList []string func (r RestrictedList) IsRestricted(s string) bool { @@ -105,6 +111,20 @@ func (r RestrictedList) IsRestricted(s string) bool { return false } +func (r RestrictedList) Filter(m map[string]string) map[string]string { + z := map[string]string{} + + for k, v := range m { + if r.IsRestricted(k) { + continue + } + + z[k] = v + } + + return z +} + func init() { r, err := regexp.Compile(kubernetesAnnotationMatch) if err != nil { diff --git a/pkg/util/k8sutil/interfaces/pod_creator.go b/pkg/util/k8sutil/interfaces/pod_creator.go index 15851a0fe..7ab624217 100644 --- a/pkg/util/k8sutil/interfaces/pod_creator.go +++ b/pkg/util/k8sutil/interfaces/pod_creator.go @@ -50,6 +50,9 @@ type PodCreator interface { IsDeploymentMode() bool Validate(cachedStatus inspector.Inspector) error + Annotations() map[string]string + Labels() map[string]string + PodModifier } From 904e5eb82f58ecf01723bc767461ffad17e21a10 Mon Sep 17 00:00:00 2001 From: ajanikow <12255597+ajanikow@users.noreply.github.com> Date: Mon, 12 Oct 2020 08:30:21 +0000 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b8a3a553..773f06336 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Fix Panics in Deployments without authentication - Fix ChaosMonkey mode - Allow append on empty annotations +- Add annotations and labels on pod creation ## [1.0.8](https://github.com/arangodb/kube-arangodb/tree/1.0.8) (2020-09-10) - Fix Volume rotation on AKS