Skip to content

Commit

Permalink
refactors labels merge (#265)
Browse files Browse the repository at this point in the history
fixes panic #264
  • Loading branch information
f41gh7 authored Jun 16, 2021
1 parent 2807048 commit 16d372e
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 133 deletions.
30 changes: 10 additions & 20 deletions api/v1beta1/vmagent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/utils/pointer"
)

Expand Down Expand Up @@ -421,31 +422,20 @@ func (cr VMAgent) SelectorLabels() map[string]string {
}

func (cr VMAgent) PodLabels() map[string]string {
labels := cr.SelectorLabels()
if cr.Spec.PodMetadata != nil {
for label, value := range cr.Spec.PodMetadata.Labels {
if _, ok := labels[label]; ok {
// forbid changes for selector labels
continue
}
labels[label] = value
}
lbls := cr.SelectorLabels()
if cr.Spec.PodMetadata == nil {
return lbls
}
return labels

return labels.Merge(cr.Spec.PodMetadata.Labels, lbls)
}

func (cr VMAgent) Labels() map[string]string {
labels := cr.SelectorLabels()
if cr.ObjectMeta.Labels != nil {
for label, value := range cr.ObjectMeta.Labels {
if _, ok := labels[label]; ok {
// forbid changes for selector labels
continue
}
labels[label] = value
}
lbls := cr.SelectorLabels()
if cr.ObjectMeta.Labels == nil {
return lbls
}
return labels
return labels.Merge(cr.ObjectMeta.Labels, lbls)
}

func (cr VMAgent) PrefixedName() string {
Expand Down
29 changes: 9 additions & 20 deletions api/v1beta1/vmalert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,31 +369,20 @@ func (cr VMAlert) SelectorLabels() map[string]string {
}

func (cr VMAlert) PodLabels() map[string]string {
labels := cr.SelectorLabels()
if cr.Spec.PodMetadata != nil {
for label, value := range cr.Spec.PodMetadata.Labels {
if _, ok := labels[label]; ok {
// forbid changes for selector labels
continue
}
labels[label] = value
}

lbls := cr.SelectorLabels()
if cr.Spec.PodMetadata == nil {
return lbls
}
return labels
return labels.Merge(cr.Spec.PodMetadata.Labels, lbls)
}

func (cr VMAlert) Labels() map[string]string {
labels := cr.SelectorLabels()
if cr.ObjectMeta.Labels != nil {
for label, value := range cr.ObjectMeta.Labels {
if _, ok := labels[label]; ok {
// forbid changes for selector labels
continue
}
labels[label] = value
}
lbls := cr.SelectorLabels()
if cr.ObjectMeta.Labels == nil {
return lbls
}
return labels
return labels.Merge(cr.ObjectMeta.Labels, lbls)
}

func (cr VMAlert) PrefixedName() string {
Expand Down
27 changes: 11 additions & 16 deletions api/v1beta1/vmalertmanager_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/utils/pointer"
)

Expand Down Expand Up @@ -284,27 +285,21 @@ func (cr VMAlertmanager) SelectorLabels() map[string]string {
}

func (cr VMAlertmanager) PodLabels() map[string]string {
labels := cr.SelectorLabels()
if cr.Spec.PodMetadata != nil {
for label, value := range cr.Spec.PodMetadata.Labels {
labels[label] = value
}
lbls := cr.SelectorLabels()
if cr.Spec.PodMetadata == nil {
return lbls
}
return labels
return labels.Merge(cr.Spec.PodMetadata.Labels, lbls)

}

func (cr VMAlertmanager) Labels() map[string]string {
labels := cr.SelectorLabels()
if cr.ObjectMeta.Labels != nil {
for label, value := range cr.ObjectMeta.Labels {
if _, ok := labels[label]; ok {
// forbid changes for selector labels
continue
}
labels[label] = value
}

lbls := cr.SelectorLabels()
if cr.ObjectMeta.Labels == nil {
return lbls
}
return labels
return labels.Merge(cr.ObjectMeta.Labels, lbls)
}

func (cr VMAlertmanager) PrefixedName() string {
Expand Down
29 changes: 9 additions & 20 deletions api/v1beta1/vmauth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/utils/pointer"
)

Expand Down Expand Up @@ -246,31 +247,19 @@ func (cr VMAuth) SelectorLabels() map[string]string {
}

func (cr VMAuth) PodLabels() map[string]string {
labels := cr.SelectorLabels()
if cr.Spec.PodMetadata != nil {
for label, value := range cr.Spec.PodMetadata.Labels {
if _, ok := labels[label]; ok {
// forbid changes for selector labels
continue
}
labels[label] = value
}
lbls := cr.SelectorLabels()
if cr.Spec.PodMetadata == nil {
return lbls
}
return labels
return labels.Merge(cr.Spec.PodMetadata.Labels, lbls)
}

func (cr VMAuth) Labels() map[string]string {
labels := cr.SelectorLabels()
if cr.ObjectMeta.Labels != nil {
for label, value := range cr.ObjectMeta.Labels {
if _, ok := labels[label]; ok {
// forbid changes for selector labels
continue
}
labels[label] = value
}
lbls := cr.SelectorLabels()
if cr.ObjectMeta.Labels == nil {
return lbls
}
return labels
return labels.Merge(cr.ObjectMeta.Labels, lbls)
}

func (cr VMAuth) PrefixedName() string {
Expand Down
48 changes: 11 additions & 37 deletions api/v1beta1/vmcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/utils/pointer"
)

Expand Down Expand Up @@ -670,12 +671,7 @@ func (cr VMCluster) VMSelectPodLabels() map[string]string {
if cr.Spec.VMSelect == nil || cr.Spec.VMSelect.PodMetadata == nil {
return selectorLabels
}
//we dont allow to override selector labels
labels := cr.Spec.VMSelect.PodMetadata.Labels
for label, value := range selectorLabels {
labels[label] = value
}
return labels
return labels.Merge(cr.Spec.VMSelect.PodMetadata.Labels, selectorLabels)
}

func (cr VMCluster) VMInsertSelectorLabels() map[string]string {
Expand All @@ -692,12 +688,9 @@ func (cr VMCluster) VMInsertPodLabels() map[string]string {
if cr.Spec.VMInsert == nil || cr.Spec.VMInsert.PodMetadata == nil {
return selectorLabels
}
labels := cr.Spec.VMInsert.PodMetadata.Labels
for label, value := range selectorLabels {
labels[label] = value
}
return labels
return labels.Merge(cr.Spec.VMInsert.PodMetadata.Labels, selectorLabels)
}

func (cr VMCluster) VMStorageSelectorLabels() map[string]string {
return map[string]string{
"app.kubernetes.io/name": "vmstorage",
Expand Down Expand Up @@ -739,24 +732,11 @@ func (cr VMCluster) VMStoragePodLabels() map[string]string {
if cr.Spec.VMStorage == nil || cr.Spec.VMStorage.PodMetadata == nil {
return selectorLabels
}
labels := cr.Spec.VMStorage.PodMetadata.Labels
for label, value := range selectorLabels {
labels[label] = value
}
return labels
return labels.Merge(cr.Spec.VMStorage.PodMetadata.Labels, selectorLabels)
}

func (cr VMCluster) FinalLabels(baseLabels map[string]string) map[string]string {
labels := map[string]string{}
if cr.ObjectMeta.Labels != nil {
for label, value := range cr.ObjectMeta.Labels {
labels[label] = value
}
}
for label, value := range baseLabels {
labels[label] = value
}
return labels
return labels.Merge(cr.ObjectMeta.Labels, baseLabels)
}

func (cr VMCluster) VMSelectPodAnnotations() map[string]string {
Expand Down Expand Up @@ -867,20 +847,14 @@ func (cr VMCluster) SelectorLabels() map[string]string {
}

func (cr VMCluster) Labels() map[string]string {
labels := cr.SelectorLabels()
if cr.ObjectMeta.Labels != nil {
for label, value := range cr.ObjectMeta.Labels {
if _, ok := labels[label]; ok {
// forbid changes for selector labels
continue
}
labels[label] = value
}
lbls := cr.SelectorLabels()
if cr.ObjectMeta.Labels == nil {
return lbls
}
return labels
return labels.Merge(cr.ObjectMeta.Labels, lbls)
}

// stub for interface.
// AsURL implements stub for interface.
func (cr *VMCluster) AsURL() string {
return "unknown"
}
Expand Down
29 changes: 9 additions & 20 deletions api/v1beta1/vmsingle_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/utils/pointer"
)

Expand Down Expand Up @@ -251,31 +252,19 @@ func (cr VMSingle) SelectorLabels() map[string]string {
}

func (cr VMSingle) PodLabels() map[string]string {
labels := cr.SelectorLabels()
if cr.Spec.PodMetadata != nil {
for label, value := range cr.Spec.PodMetadata.Labels {
if _, ok := labels[label]; ok {
// forbid changes for selector labels
continue
}
labels[label] = value
}
lbls := cr.SelectorLabels()
if cr.Spec.PodMetadata == nil {
return lbls
}
return labels
return labels.Merge(cr.Spec.PodMetadata.Labels, lbls)
}

func (cr VMSingle) Labels() map[string]string {
labels := cr.SelectorLabels()
if cr.ObjectMeta.Labels != nil {
for label, value := range cr.ObjectMeta.Labels {
if _, ok := labels[label]; ok {
// forbid changes for selector labels
continue
}
labels[label] = value
}
lbls := cr.SelectorLabels()
if cr.ObjectMeta.Labels == nil {
return lbls
}
return labels
return labels.Merge(cr.ObjectMeta.Labels, lbls)
}

func (cr VMSingle) PrefixedName() string {
Expand Down
11 changes: 11 additions & 0 deletions controllers/factory/vmcluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,17 +553,28 @@ func TestCreateOrUpdateVMCluster(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "cluster-1",
Labels: map[string]string{"label": "value"},
},
Spec: v1beta1.VMClusterSpec{
RetentionPeriod: "2",
ReplicationFactor: pointer.Int32Ptr(2),
VMInsert: &v1beta1.VMInsert{
PodMetadata: &v1beta1.EmbeddedObjectMetadata{
Annotations: map[string]string{"key": "value"},
},
ReplicaCount: pointer.Int32Ptr(2),
},
VMStorage: &v1beta1.VMStorage{
PodMetadata: &v1beta1.EmbeddedObjectMetadata{
Annotations: map[string]string{"key": "value"},
Labels: map[string]string{"label": "value2"},
},
ReplicaCount: pointer.Int32Ptr(2),
},
VMSelect: &v1beta1.VMSelect{
PodMetadata: &v1beta1.EmbeddedObjectMetadata{
Annotations: map[string]string{"key": "value"},
},
ReplicaCount: pointer.Int32Ptr(2),
},
},
Expand Down

0 comments on commit 16d372e

Please sign in to comment.