Skip to content

New field additionalPodLabels #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0f3f528
new field additionalPodLabels
kobzonega Jul 14, 2024
3de16bd
method buildLabels for resources
kobzonega Jul 14, 2024
ef26907
fix linters
kobzonega Jul 14, 2024
f21e08e
fix blobstorage-init antiaffinity
kobzonega Jul 15, 2024
073ed86
fix nodeset labels
kobzonega Jul 15, 2024
5512cfa
fix database initializing conditions
kobzonega Jul 15, 2024
b37c5fd
fixes tests data
kobzonega Jul 17, 2024
41bc274
Merge remote-tracking branch 'origin' into additionalPodLabels
kobzonega Jul 18, 2024
2f9188f
Merge branch 'master' into additionalPodLabels
kobzonega Aug 27, 2024
6aee699
use database and storage builders for statefulset labels in nodeset
kobzonega Aug 27, 2024
4d436c2
bump helm chart version
kobzonega Aug 27, 2024
d497366
remove useless func GetInitJobBuilder
kobzonega Aug 27, 2024
df64bf5
Merge remote-tracking branch 'origin' into additionalPodLabels
kobzonega Oct 3, 2024
f0317b6
Merge branch 'master' into additionalPodLabels
kobzonega Oct 8, 2024
240b8d6
Merge remote-tracking branch 'origin' into additionalPodLabels
kobzonega Dec 4, 2024
e065f45
crd update with new controller-gen version
kobzonega Dec 4, 2024
fe1d66d
revert formatting changes
kobzonega Dec 4, 2024
c5b35a4
additional worker for initJob
kobzonega Dec 4, 2024
1778421
fix Makefile
kobzonega Dec 4, 2024
fa58df8
changie release notes
kobzonega Dec 4, 2024
9e1ca93
Merge remote-tracking branch 'origin/master' into additionalPodLabels
kobzonega Jan 14, 2025
9871049
Update kind-cluster-config.yaml
kobzonega Jan 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changes/unreleased/Added-20241204-122609.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Added
body: new field additionalPodLabels for Storage and Database CRD
time: 2024-12-04T12:26:09.597907+07:00
3 changes: 3 additions & 0 deletions .changes/unreleased/Added-20241204-122633.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Added
body: new method buildPodTemplateLabels to append additionalPodLabels for statefulset builders
time: 2024-12-04T12:26:33.654222+07:00
3 changes: 3 additions & 0 deletions .changes/unreleased/Changed-20241204-122659.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Changed
body: refactor package labels to separate methods buildLabels, buildSelectorLabels and buildeNodeSetLabels for each resource
time: 2024-12-04T12:26:59.096105+07:00
3 changes: 3 additions & 0 deletions .changes/unreleased/Changed-20241204-122713.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Changed
body: propagate labels ydb.tech/database-nodeset, ydb.tech/storage-nodeset and ydb.tech/remote-cluster with method makeCommonLabels between resource recasting
time: 2024-12-04T12:27:13.265234+07:00
3 changes: 3 additions & 0 deletions .changes/unreleased/Fixed-20241204-122736.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Fixed
body: additional kind worker to maintain affinity rules for blobstorage init job
time: 2024-12-04T12:27:36.97703+07:00
4 changes: 4 additions & 0 deletions api/v1alpha1/database_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ type DatabaseNodeSpec struct {
// +optional
AdditionalLabels map[string]string `json:"additionalLabels,omitempty"`

// (Optional) Additional custom resource labels that are added to Pods
// +optional
AdditionalPodLabels map[string]string `json:"additionalPodLabels,omitempty"`

// (Optional) Additional custom resource annotations that are added to all resources
// +optional
AdditionalAnnotations map[string]string `json:"additionalAnnotations,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions api/v1alpha1/storage_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ type StorageNodeSpec struct {
// +optional
AdditionalLabels map[string]string `json:"additionalLabels,omitempty"`

// (Optional) Additional custom resource labels that are added to Pods
// +optional
AdditionalPodLabels map[string]string `json:"additionalPodLabels,omitempty"`

// (Optional) Additional custom resource annotations that are added to all resources
// +optional
AdditionalAnnotations map[string]string `json:"additionalAnnotations,omitempty"`
Expand Down
14 changes: 14 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

382 changes: 199 additions & 183 deletions deploy/ydb-operator/crds/database.yaml

Large diffs are not rendered by default.

182 changes: 98 additions & 84 deletions deploy/ydb-operator/crds/databasenodeset.yaml

Large diffs are not rendered by default.

182 changes: 98 additions & 84 deletions deploy/ydb-operator/crds/remotedatabasenodeset.yaml

Large diffs are not rendered by default.

182 changes: 98 additions & 84 deletions deploy/ydb-operator/crds/remotestoragenodeset.yaml

Large diffs are not rendered by default.

382 changes: 199 additions & 183 deletions deploy/ydb-operator/crds/storage.yaml

Large diffs are not rendered by default.

182 changes: 98 additions & 84 deletions deploy/ydb-operator/crds/storagenodeset.yaml

Large diffs are not rendered by default.

43 changes: 37 additions & 6 deletions internal/controllers/storage/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,9 @@ var _ = Describe("Storage controller medium tests", func() {
if err != nil {
return err
}
value, exist := foundStatefulSets.Items[0].Labels[testLabelKey]
if !exist {
return fmt.Errorf("label key `ydb-label` does not exist in StatefulSet. Current labels: %s", foundStatefulSets.Items[0].Labels)
}
value := foundStatefulSets.Items[0].Labels[testLabelKey]
if value != testLabelValue {
return fmt.Errorf("label value `ydb-label` in StatefulSet does not equal `test`. Current labels: %s", foundStatefulSets.Items[0].Labels)
return fmt.Errorf("label value of `%s` in StatefulSet does not equal `%s`. Current labels: %s", testLabelKey, testLabelValue, foundStatefulSets.Items[0].Labels)
}
return nil
}, test.Timeout, test.Interval).ShouldNot(HaveOccurred())
Expand All @@ -186,8 +183,42 @@ var _ = Describe("Storage controller medium tests", func() {
},
},
))
})

By("Check that additionalPodLabels propagated into podTemplate...", func() {
testLabelKey := "ydb-pod-label"
testLabelValue := "test-podTemplate"
By("set additional pod labels to Storage...")
Eventually(func() error {
foundStorage := v1alpha1.Storage{}
Expect(k8sClient.Get(ctx, types.NamespacedName{
Name: storageSample.Name,
Namespace: testobjects.YdbNamespace,
}, &foundStorage))
foundStorage.Spec.AdditionalPodLabels = make(map[string]string)
foundStorage.Spec.AdditionalPodLabels[testLabelKey] = testLabelValue
return k8sClient.Update(ctx, &foundStorage)
}, test.Timeout, test.Interval).ShouldNot(HaveOccurred())

By("check that additional pod labels was added...")
foundStatefulSets := appsv1.StatefulSetList{}
Eventually(func() error {
err := k8sClient.List(ctx, &foundStatefulSets,
client.InNamespace(testobjects.YdbNamespace),
)
if err != nil {
return err
}
value := foundStatefulSets.Items[0].Spec.Template.Labels[testLabelKey]
if value != testLabelValue {
return fmt.Errorf("label value of `%s` in StatefulSet does not equal `%s`. Current labels: %s", testLabelKey, testLabelValue, foundStatefulSets.Items[0].Labels)
}
return nil
}, test.Timeout, test.Interval).ShouldNot(HaveOccurred())
})

By("check that delete StatefulSet event was detected...")
By("check that delete StatefulSet event was detected...", func() {
foundStatefulSets := appsv1.StatefulSetList{}
Expect(k8sClient.List(ctx, &foundStatefulSets, client.InNamespace(testobjects.YdbNamespace))).ShouldNot(HaveOccurred())
Expect(len(foundStatefulSets.Items)).Should(Equal(1))
Expect(k8sClient.Delete(ctx, &foundStatefulSets.Items[0])).ShouldNot(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/storage/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (r *Reconciler) createInitBlobstorageJob(
ctx context.Context,
storage *resources.StorageClusterBuilder,
) error {
builder := resources.GetInitJobBuilder(storage.DeepCopy())
builder := storage.GetInitJobBuilder()
newResource := builder.Placeholder(storage)
_, err := resources.CreateOrUpdateOrMaybeIgnore(ctx, r.Client, newResource, func() error {
var err error
Expand Down
56 changes: 15 additions & 41 deletions internal/labels/label.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package labels

import (
"github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
)

// https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/
const (
// NameKey The name of a higher level application this one is part of
Expand All @@ -28,11 +24,9 @@ const (
// RemoteClusterKey The specialization of a remote k8s cluster
RemoteClusterKey = "ydb.tech/remote-cluster"

StorageGeneration = "ydb.tech/storage-generation"
DatabaseGeneration = "ydb.tech/database-generation"

StorageComponent = "storage-node"
DynamicComponent = "dynamic-node"
StorageComponent = "storage-node"
DynamicComponent = "dynamic-node"
BlobstorageInitComponent = "blobstorage-init"

GRPCComponent = "grpc"
InterconnectComponent = "interconnect"
Expand All @@ -50,28 +44,6 @@ func Common(name string, defaultLabels Labels) Labels {
return l
}

func StorageLabels(cluster *v1alpha1.Storage) Labels {
l := Common(cluster.Name, cluster.Labels)

l.Merge(cluster.Spec.AdditionalLabels)
l.Merge(map[string]string{
ComponentKey: StorageComponent,
})

return l
}

func DatabaseLabels(database *v1alpha1.Database) Labels {
l := Common(database.Name, database.Labels)

l.Merge(database.Spec.AdditionalLabels)
l.Merge(map[string]string{
ComponentKey: DynamicComponent,
})

return l
}

func (l Labels) AsMap() map[string]string {
return l
}
Expand All @@ -98,16 +70,6 @@ func (l Labels) Merge(other map[string]string) map[string]string {
return l
}

func (l Labels) MergeInPlace(other map[string]string) map[string]string {
result := l.Copy()

for k, v := range other {
result[k] = v
}

return result
}

func makeCommonLabels(other map[string]string, instance string) map[string]string {
common := make(map[string]string)

Expand All @@ -122,5 +84,17 @@ func makeCommonLabels(other map[string]string, instance string) map[string]strin

common[ManagedByKey] = "ydb-operator"

if storageNodeSetName, exist := other[StorageNodeSetComponent]; exist {
common[StorageNodeSetComponent] = storageNodeSetName
}

if databaseNodeSetName, exist := other[DatabaseNodeSetComponent]; exist {
common[DatabaseNodeSetComponent] = databaseNodeSetName
}

if remoteCluster, exist := other[RemoteClusterKey]; exist {
common[RemoteClusterKey] = remoteCluster
}

return common
}
57 changes: 38 additions & 19 deletions internal/resources/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,38 @@ func (b *DatabaseBuilder) Unwrap() *api.Database {
return b.DeepCopy()
}

func (b *DatabaseBuilder) buildSelectorLabels() labels.Labels {
l := labels.Common(b.Name, b.Labels)
l.Merge(map[string]string{labels.ComponentKey: labels.DynamicComponent})

return l
}

func (b *DatabaseBuilder) buildLabels() labels.Labels {
l := b.buildSelectorLabels()
l.Merge(b.Spec.AdditionalLabels)

return l
}

func (b *DatabaseBuilder) buildNodeSetLabels(nodeSetSpecInline api.DatabaseNodeSetSpecInline) labels.Labels {
l := b.buildLabels()
l.Merge(nodeSetSpecInline.Labels)
l.Merge(map[string]string{labels.DatabaseNodeSetComponent: nodeSetSpecInline.Name})
if nodeSetSpecInline.Remote != nil {
l.Merge(map[string]string{labels.RemoteClusterKey: nodeSetSpecInline.Remote.Cluster})
}

return l
}

func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []ResourceBuilder {
if b.Spec.ServerlessResources != nil {
return []ResourceBuilder{}
}

databaseLabels := labels.DatabaseLabels(b.Unwrap())

statefulSetLabels := databaseLabels.Copy()
statefulSetLabels.Merge(map[string]string{labels.StatefulsetComponent: b.Name})
databaseLabels := b.buildLabels()
databaseSelectorLabels := b.buildSelectorLabels()

statefulSetAnnotations := CopyDict(b.Spec.AdditionalAnnotations)
statefulSetAnnotations[annotations.ConfigurationChecksum] = SHAChecksum(b.Spec.Configuration)
Expand Down Expand Up @@ -149,7 +172,7 @@ func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []Resourc
Object: b,
NameFormat: GRPCServiceNameFormat,
Labels: grpcServiceLabels,
SelectorLabels: databaseLabels,
SelectorLabels: databaseSelectorLabels,
Annotations: b.Spec.Service.GRPC.AdditionalAnnotations,
Ports: []corev1.ServicePort{{
Name: api.GRPCServicePortName,
Expand All @@ -162,7 +185,7 @@ func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []Resourc
Object: b,
NameFormat: InterconnectServiceNameFormat,
Labels: interconnectServiceLabels,
SelectorLabels: databaseLabels,
SelectorLabels: databaseSelectorLabels,
Annotations: b.Spec.Service.Interconnect.AdditionalAnnotations,
Headless: true,
Ports: []corev1.ServicePort{{
Expand All @@ -176,7 +199,7 @@ func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []Resourc
Object: b,
NameFormat: StatusServiceNameFormat,
Labels: statusServiceLabels,
SelectorLabels: databaseLabels,
SelectorLabels: databaseSelectorLabels,
Annotations: b.Spec.Service.Status.AdditionalAnnotations,
Ports: []corev1.ServicePort{{
Name: api.StatusServicePortName,
Expand All @@ -194,7 +217,7 @@ func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []Resourc
Object: b,
NameFormat: DatastreamsServiceNameFormat,
Labels: datastreamsServiceLabels,
SelectorLabels: databaseLabels,
SelectorLabels: databaseSelectorLabels,
Annotations: b.Spec.Service.Datastreams.AdditionalAnnotations,
Ports: []corev1.ServicePort{{
Name: api.DatastreamsServicePortName,
Expand All @@ -214,27 +237,23 @@ func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []Resourc
RestConfig: restConfig,

Name: b.Name,
Labels: statefulSetLabels,
Labels: databaseLabels,
Annotations: statefulSetAnnotations,
},
)
} else {
optionalBuilders = append(optionalBuilders, b.getNodeSetBuilders(databaseLabels)...)
optionalBuilders = append(optionalBuilders, b.getNodeSetBuilders()...)
}

return optionalBuilders
}

func (b *DatabaseBuilder) getNodeSetBuilders(databaseLabels labels.Labels) []ResourceBuilder {
func (b *DatabaseBuilder) getNodeSetBuilders() []ResourceBuilder {
var nodeSetBuilders []ResourceBuilder

for _, nodeSetSpecInline := range b.Spec.NodeSets {
nodeSetLabels := databaseLabels.Copy()
nodeSetLabels.Merge(nodeSetSpecInline.Labels)
nodeSetLabels.Merge(map[string]string{labels.DatabaseNodeSetComponent: nodeSetSpecInline.Name})
if nodeSetSpecInline.Remote != nil {
nodeSetLabels.Merge(map[string]string{labels.RemoteClusterKey: nodeSetSpecInline.Remote.Cluster})
}
nodeSetName := fmt.Sprintf("%s-%s", b.Name, nodeSetSpecInline.Name)
nodeSetLabels := b.buildNodeSetLabels(nodeSetSpecInline)

nodeSetAnnotations := CopyDict(b.Annotations)
if nodeSetSpecInline.Annotations != nil {
Expand All @@ -250,7 +269,7 @@ func (b *DatabaseBuilder) getNodeSetBuilders(databaseLabels labels.Labels) []Res
&RemoteDatabaseNodeSetBuilder{
Object: b,

Name: b.Name + "-" + nodeSetSpecInline.Name,
Name: nodeSetName,
Labels: nodeSetLabels,
Annotations: nodeSetAnnotations,

Expand All @@ -263,7 +282,7 @@ func (b *DatabaseBuilder) getNodeSetBuilders(databaseLabels labels.Labels) []Res
&DatabaseNodeSetBuilder{
Object: b,

Name: b.Name + "-" + nodeSetSpecInline.Name,
Name: nodeSetName,
Labels: nodeSetLabels,
Annotations: nodeSetAnnotations,

Expand Down
14 changes: 13 additions & 1 deletion internal/resources/database_statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,26 @@ func (b *DatabaseStatefulSetBuilder) buildEnv() []corev1.EnvVar {
return envVars
}

func (b *DatabaseStatefulSetBuilder) buildPodTemplateLabels() labels.Labels {
podTemplateLabels := labels.Labels{}

podTemplateLabels.Merge(b.Labels)
podTemplateLabels.Merge(map[string]string{labels.StatefulsetComponent: b.Name})
podTemplateLabels.Merge(b.Spec.AdditionalPodLabels)

return podTemplateLabels
}

func (b *DatabaseStatefulSetBuilder) buildPodTemplateSpec() corev1.PodTemplateSpec {
podTemplateLabels := b.buildPodTemplateLabels()

domain := api.DefaultDomainName
if dnsAnnotation, ok := b.GetAnnotations()[api.DNSDomainAnnotation]; ok {
domain = dnsAnnotation
}
podTemplate := corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: b.Labels,
Labels: podTemplateLabels,
Annotations: b.Annotations,
},
Spec: corev1.PodSpec{
Expand Down
Loading
Loading