Skip to content

Commit

Permalink
feature: podLabels and podAnnotations
Browse files Browse the repository at this point in the history
  • Loading branch information
veezhang authored and MegaByte875 committed Jun 28, 2021
1 parent f153bff commit b75fda9
Show file tree
Hide file tree
Showing 18 changed files with 265 additions and 43 deletions.
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: manifests generate check ## Run tests.
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.7.0/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./pkg/... -coverprofile cover.out
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./pkg/... ./apis/... -coverprofile cover.out

##@ e2e
e2e: $(GOBIN)/ginkgo $(GOBIN)/kind helm
Expand All @@ -101,6 +101,13 @@ run: run-controller-manager
run-controller-manager: manifests generate check
go run -ldflags '$(LDFLAGS)' cmd/controller-manager/main.go

build-helm: helm
helm repo index charts --url https://vesoft-inc.github.io/nebula-operator/charts
helm package charts/nebula-operator
helm package charts/nebula-cluster
mv nebula-operator-*.tgz nebula-cluster-*.tgz charts/
cp config/crd/bases/apps.nebula-graph.io_nebulaclusters.yaml charts/nebula-operator/crds/nebulacluster.yaml

run-scheduler: manifests generate check
go run -ldflags '$(LDFLAGS)' cmd/scheduler/main.go

Expand Down
32 changes: 30 additions & 2 deletions apis/apps/v1alpha1/nebulacluster_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func generateContainers(c NebulaClusterComponentter, cm *corev1.ConfigMap) []cor
Name: componentType,
Image: c.GetImage(),
Command: cmd,
Env: c.GetEnvVars(),
Env: c.GetPodEnvVars(),
Ports: ports,
VolumeMounts: mounts,
ReadinessProbe: &corev1.Probe{
Expand Down Expand Up @@ -298,7 +298,8 @@ func generateStatefulSet(c NebulaClusterComponentter, cm *corev1.ConfigMap, enab
Selector: &metav1.LabelSelector{MatchLabels: componentLabel},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: componentLabel,
Labels: mergeStringMaps(false, componentLabel, c.GetPodLabels()),
Annotations: c.GetPodAnnotations(),
},
Spec: podSpec,
},
Expand Down Expand Up @@ -435,3 +436,30 @@ func generateConfigMap(c NebulaClusterComponentter) *corev1.ConfigMap {

return cm
}

func mergeStringMaps(overwrite bool, ms ...map[string]string) map[string]string {
n := 0
for _, m := range ms {
n += len(m)
}
mp := make(map[string]string, n)
if n == 0 {
return mp
}
for _, m := range ms {
for k, v := range m {
if overwrite || !isStringMapExist(mp, k) {
mp[k] = v
}
}
}
return mp
}

func isStringMapExist(m map[string]string, key string) bool {
if m == nil {
return false
}
_, exist := m[key]
return exist
}
136 changes: 136 additions & 0 deletions apis/apps/v1alpha1/nebulacluster_common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
Copyright 2021 Vesoft Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"reflect"
"testing"
)

func Test_mergeStringMaps(t *testing.T) {
testCases := []struct {
name string
overwrite bool
ms []map[string]string
expectMap map[string]string
}{
{
name: "nil",
overwrite: false,
ms: nil,
expectMap: map[string]string{},
}, {
name: "nil maps",
overwrite: false,
ms: []map[string]string{nil, nil, nil},
expectMap: map[string]string{},
}, {
name: "empty maps",
overwrite: false,
ms: []map[string]string{{}, {}, {}},
expectMap: map[string]string{},
}, {
name: "nil/empty maps",
overwrite: false,
ms: []map[string]string{nil, {}, nil, {}, {}, nil},
expectMap: map[string]string{},
}, {
name: "not overwrite",
overwrite: false,
ms: []map[string]string{
nil, {
"k1": "v1",
"k2": "v2",
}, {}, nil, {
"k1": "v11",
"k3": "v3",
}, nil, {},
},
expectMap: map[string]string{
"k1": "v1",
"k2": "v2",
"k3": "v3",
},
}, {
name: "overwrite",
overwrite: true,
ms: []map[string]string{
{}, {
"k1": "v1",
"k2": "v2",
}, nil, {}, {
"k1": "v11",
"k3": "v3",
}, {}, nil,
},
expectMap: map[string]string{
"k1": "v11",
"k2": "v2",
"k3": "v3",
},
},
}
for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := mergeStringMaps(tc.overwrite, tc.ms...)
if !reflect.DeepEqual(tc.expectMap, got) {
t.Errorf("%d: Expected: \n%#v\n but actual: \n%#v\n",
i, tc.expectMap, got)
}
})
}
}

func Test_isStringMapExist(t *testing.T) {
testCases := []struct {
name string
m map[string]string
key string
expectExist bool
}{
{
name: "nil",
m: nil,
key: "k",
expectExist: false,
}, {
name: "empty",
m: map[string]string{},
key: "k",
expectExist: false,
}, {
name: "not exist",
m: map[string]string{"k": ""},
key: "not exist",
expectExist: false,
}, {
name: "exist",
m: map[string]string{"k": ""},
key: "k",
expectExist: true,
},
}
for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := isStringMapExist(tc.m, tc.key)
if !reflect.DeepEqual(tc.expectExist, got) {
t.Errorf("%d: Expected: \n%#v\n but actual: \n%#v\n",
i, tc.expectExist, got)
}
})
}
}
4 changes: 3 additions & 1 deletion apis/apps/v1alpha1/nebulacluster_componentter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ type NebulaClusterComponentter interface {
GetResources() *corev1.ResourceRequirements
GetStorageClass() *string
GetStorageResources() *corev1.ResourceRequirements
GetEnvVars() []corev1.EnvVar
GetPodEnvVars() []corev1.EnvVar
GetPodAnnotations() map[string]string
GetPodLabels() map[string]string
IsHeadlessService() bool
GetServiceSpec() *ServiceSpec
GetServiceName() string
Expand Down
12 changes: 10 additions & 2 deletions apis/apps/v1alpha1/nebulacluster_graphd.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,16 @@ func (c *graphdComponent) GetStorageResources() *corev1.ResourceRequirements {
return c.nc.Spec.Graphd.StorageClaim.Resources.DeepCopy()
}

func (c *graphdComponent) GetEnvVars() []corev1.EnvVar {
return c.nc.Spec.Graphd.EnvVars
func (c *graphdComponent) GetPodEnvVars() []corev1.EnvVar {
return c.nc.Spec.Graphd.PodSpec.EnvVars
}

func (c *graphdComponent) GetPodAnnotations() map[string]string {
return c.nc.Spec.Graphd.PodSpec.Annotations
}

func (c *graphdComponent) GetPodLabels() map[string]string {
return c.nc.Spec.Graphd.PodSpec.Labels
}

func (c *graphdComponent) IsHeadlessService() bool {
Expand Down
12 changes: 10 additions & 2 deletions apis/apps/v1alpha1/nebulacluster_metad.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,16 @@ func (c *metadComponent) GetStorageResources() *corev1.ResourceRequirements {
return c.nc.Spec.Metad.StorageClaim.Resources.DeepCopy()
}

func (c *metadComponent) GetEnvVars() []corev1.EnvVar {
return c.nc.Spec.Metad.EnvVars
func (c *metadComponent) GetPodEnvVars() []corev1.EnvVar {
return c.nc.Spec.Metad.PodSpec.EnvVars
}

func (c *metadComponent) GetPodAnnotations() map[string]string {
return c.nc.Spec.Graphd.PodSpec.Annotations
}

func (c *metadComponent) GetPodLabels() map[string]string {
return c.nc.Spec.Graphd.PodSpec.Labels
}

func (c *metadComponent) IsHeadlessService() bool {
Expand Down
12 changes: 10 additions & 2 deletions apis/apps/v1alpha1/nebulacluster_storaged.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,16 @@ func (c *storagedComponent) GetStorageResources() *corev1.ResourceRequirements {
return c.nc.Spec.Storaged.StorageClaim.Resources.DeepCopy()
}

func (c *storagedComponent) GetEnvVars() []corev1.EnvVar {
return c.nc.Spec.Storaged.EnvVars
func (c *storagedComponent) GetPodEnvVars() []corev1.EnvVar {
return c.nc.Spec.Storaged.PodSpec.EnvVars
}

func (c *storagedComponent) GetPodAnnotations() map[string]string {
return c.nc.Spec.Graphd.PodSpec.Annotations
}

func (c *storagedComponent) GetPodLabels() map[string]string {
return c.nc.Spec.Graphd.PodSpec.Labels
}

func (c *storagedComponent) IsHeadlessService() bool {
Expand Down
2 changes: 1 addition & 1 deletion apis/apps/v1alpha1/nebulacluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ type PodSpec struct {

// K8S pod annotations.
// +optional
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`

// +optional
Labels map[string]string `json:"labels,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions apis/apps/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions charts/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ entries:
nebula-cluster:
- apiVersion: v2
appVersion: 0.1.0
created: "2021-06-21T16:59:36.417046+08:00"
created: "2021-06-28T10:15:33.333416+08:00"
description: Nebula Cluster Helm chart for Kubernetes
digest: 7328fbf57484a9625a5c559d75de18b89c0e66988eb12eee02c6ad8ddc9d9b55
digest: df8dfc3a710753bec8544b6cde9c9ad35c0fcd89bb6144f59f5ce48b5bbb2f1d
home: https://nebula-graph.io
keywords:
- kubernetes
Expand All @@ -29,9 +29,9 @@ entries:
nebula-operator:
- apiVersion: v2
appVersion: 0.1.0
created: "2021-06-21T16:59:36.417725+08:00"
created: "2021-06-28T10:15:33.334456+08:00"
description: Nebula Operator Helm chart for Kubernetes
digest: e13a783739c636748908daa59bbfcf491155e42603ce24a2365843d9a1071cbb
digest: 53c46adb019cf7e1c5226df321486c451e83d2071f95f95d9620bde141f1330a
home: https://nebula-graph.io
keywords:
- kubernetes
Expand All @@ -50,4 +50,4 @@ entries:
urls:
- https://vesoft-inc.github.io/nebula-operator/charts/nebula-operator-0.1.0.tgz
version: 0.1.0
generated: "2021-06-21T16:59:36.416476+08:00"
generated: "2021-06-28T10:15:33.332855+08:00"
Binary file modified charts/nebula-cluster-0.1.0.tgz
Binary file not shown.
6 changes: 6 additions & 0 deletions charts/nebula-cluster/templates/nebula-cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ spec:
requests:
storage: {{ .Values.nebula.graphd.storage }}
storageClassName: {{ .Values.nebula.storageClassName }}
labels: {{ toYaml .Values.nebula.podLabels | nindent 6 }}
annotations: {{ toYaml .Values.nebula.podAnnotations | nindent 6 }}
metad:
replicas: {{ .Values.nebula.metad.replicas }}
resources: {{ toYaml .Values.nebula.metad.resources | nindent 6 }}
Expand All @@ -31,6 +33,8 @@ spec:
requests:
storage: {{ .Values.nebula.metad.storage }}
storageClassName: {{ .Values.nebula.storageClassName }}
labels: {{ toYaml .Values.nebula.podLabels | nindent 6 }}
annotations: {{ toYaml .Values.nebula.podAnnotations | nindent 6 }}
storaged:
replicas: {{ .Values.nebula.storaged.replicas }}
resources: {{ toYaml .Values.nebula.storaged.resources | nindent 6 }}
Expand All @@ -42,6 +46,8 @@ spec:
requests:
storage: {{ .Values.nebula.storaged.storage }}
storageClassName: {{ .Values.nebula.storageClassName }}
labels: {{ toYaml .Values.nebula.podLabels | nindent 6 }}
annotations: {{ toYaml .Values.nebula.podAnnotations | nindent 6 }}
reference: {{ toYaml .Values.nebula.reference | nindent 4 }}
imagePullPolicy: {{ .Values.nebula.imagePullPolicy }}
{{- if .Values.imagePullSecrets }}
Expand Down
2 changes: 2 additions & 0 deletions charts/nebula-cluster/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ nebula:
reference:
name: statefulsets.apps
version: v1
podLabels: {}
podAnnotations: {}
graphd:
image: vesoft/nebula-graphd
replicas: 2
Expand Down
Binary file modified charts/nebula-operator-0.1.0.tgz
Binary file not shown.
Loading

0 comments on commit b75fda9

Please sign in to comment.