Skip to content

Commit

Permalink
Use subPath for ydb config (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobzonega authored Apr 19, 2024
1 parent b54e0b9 commit 2213de1
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 10 deletions.
4 changes: 2 additions & 2 deletions deploy/ydb-operator/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.5.4
version: 0.5.5

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.5.4"
appVersion: "0.5.5"
1 change: 1 addition & 0 deletions internal/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const (
PrimaryResourceStorageAnnotation = "ydb.tech/primary-resource-storage"
PrimaryResourceDatabaseAnnotation = "ydb.tech/primary-resource-database"
RemoteResourceVersionAnnotation = "ydb.tech/remote-resource-version"
ConfigurationChecksum = "ydb.tech/configuration-checksum"
RemoteFinalizerKey = "ydb.tech/remote-finalizer"
LastAppliedAnnotation = "ydb.tech/last-applied"
)
Expand Down
32 changes: 31 additions & 1 deletion internal/controllers/storage/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import (
"context"
"fmt"
"path/filepath"
"strconv"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
testobjects "github.com/ydb-platform/ydb-kubernetes-operator/e2e/tests/test-objects"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/storage"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/labels"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/resources"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/test"
)

Expand Down Expand Up @@ -55,7 +61,7 @@ var _ = Describe("Storage controller medium tests", func() {
Expect(k8sClient.Delete(ctx, &namespace)).Should(Succeed())
})

It("Check volume has been propagated to pods", func() {
It("Checking field propagation to objects", func() {
storageSample := testobjects.DefaultStorage(filepath.Join("..", "..", "..", "e2e", "tests", "data", "storage-block-4-2-config.yaml"))

tmpFilesDir := "/tmp/mounted_volume"
Expand All @@ -76,6 +82,7 @@ var _ = Describe("Storage controller medium tests", func() {

Expect(k8sClient.Create(ctx, storageSample)).Should(Succeed())

By("Check volume has been propagated to pods...")
storageStatefulSets := appsv1.StatefulSetList{}
Eventually(func() bool {
Expect(k8sClient.List(ctx, &storageStatefulSets, client.InNamespace(
Expand Down Expand Up @@ -105,5 +112,28 @@ var _ = Describe("Storage controller medium tests", func() {
}
}
Expect(foundVolume).To(BeTrue())

By("Check that label and annotation propagated to pods...", func() {
podLabels := storageSS.Spec.Template.Labels
podAnnotations := storageSS.Spec.Template.Annotations

foundStorage := v1alpha1.Storage{}
Expect(k8sClient.Get(ctx, types.NamespacedName{
Name: testobjects.StorageName,
Namespace: testobjects.YdbNamespace,
}, &foundStorage)).Should(Succeed())

foundStorageGenerationLabel := false
if podLabels[labels.StorageGeneration] == strconv.FormatInt(foundStorage.ObjectMeta.Generation, 10) {
foundStorageGenerationLabel = true
}
Expect(foundStorageGenerationLabel).To(BeTrue())

foundConfigurationChecksumAnnotation := false
if podAnnotations[annotations.ConfigurationChecksum] == resources.GetConfigurationChecksum(foundStorage.Spec.Configuration) {
foundConfigurationChecksumAnnotation = true
}
Expect(foundConfigurationChecksumAnnotation).To(BeTrue())
})
})
})
3 changes: 3 additions & 0 deletions internal/labels/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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"

Expand Down
15 changes: 12 additions & 3 deletions internal/resources/database_statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/labels"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/ptr"
)

Expand Down Expand Up @@ -83,10 +85,16 @@ func (b *DatabaseStatefulSetBuilder) buildEnv() []corev1.EnvVar {
}

func (b *DatabaseStatefulSetBuilder) buildPodTemplateSpec() corev1.PodTemplateSpec {
podTemplateLabels := CopyDict(b.Labels)
podTemplateLabels[labels.DatabaseGeneration] = strconv.FormatInt(b.ObjectMeta.Generation, 10)

podTemplateAnnotations := CopyDict(b.Spec.AdditionalAnnotations)
podTemplateAnnotations[annotations.ConfigurationChecksum] = GetConfigurationChecksum(b.Spec.Configuration)

podTemplate := corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: b.Labels,
Annotations: CopyDict(b.Spec.AdditionalAnnotations),
Labels: podTemplateLabels,
Annotations: podTemplateAnnotations,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{b.buildContainer()},
Expand Down Expand Up @@ -417,7 +425,8 @@ func (b *DatabaseStatefulSetBuilder) buildVolumeMounts() []corev1.VolumeMount {
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: configVolumeName,
ReadOnly: true,
MountPath: api.ConfigDir,
MountPath: fmt.Sprintf("%s/%s", api.ConfigDir, api.ConfigFileName),
SubPath: api.ConfigFileName,
})

if b.Spec.Service.GRPC.TLSConfiguration.Enabled {
Expand Down
8 changes: 8 additions & 0 deletions internal/resources/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package resources

import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"

Expand Down Expand Up @@ -488,3 +490,9 @@ func buildCAStorePatchingCommandArgs(

return command, args
}

func GetConfigurationChecksum(configuration string) string {
hasher := sha256.New()
hasher.Write([]byte(configuration))
return hex.EncodeToString(hasher.Sum(nil))
}
7 changes: 6 additions & 1 deletion internal/resources/storage_init_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package resources
import (
"errors"
"fmt"
"strconv"

batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/labels"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/ptr"
)
Expand Down Expand Up @@ -64,9 +66,11 @@ func GetInitJobBuilder(storage *api.Storage) ResourceBuilder {
if storage.Spec.InitJob != nil {
if storage.Spec.InitJob.AdditionalLabels != nil {
jobLabels.Merge(storage.Spec.InitJob.AdditionalLabels)
jobLabels[labels.StorageGeneration] = strconv.FormatInt(storage.ObjectMeta.Generation, 10)
}
if storage.Spec.InitJob.AdditionalAnnotations != nil {
jobAnnotations = CopyDict(storage.Spec.InitJob.AdditionalAnnotations)
jobAnnotations[annotations.ConfigurationChecksum] = GetConfigurationChecksum(storage.Spec.Configuration)
}
}

Expand Down Expand Up @@ -232,7 +236,8 @@ func (b *StorageInitJobBuilder) buildJobVolumeMounts() []corev1.VolumeMount {
{
Name: configVolumeName,
ReadOnly: true,
MountPath: api.ConfigDir,
MountPath: fmt.Sprintf("%s/%s", api.ConfigDir, api.ConfigFileName),
SubPath: api.ConfigFileName,
},
}

Expand Down
16 changes: 13 additions & 3 deletions internal/resources/storage_statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/annotations"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/labels"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/ptr"
)

Expand Down Expand Up @@ -102,10 +104,17 @@ func (b *StorageStatefulSetBuilder) buildPodTemplateSpec() corev1.PodTemplateSpe
dnsConfigSearches := []string{
fmt.Sprintf(api.InterconnectServiceFQDNFormat, b.Storage.Name, b.GetNamespace()),
}

podTemplateLabels := CopyDict(b.Labels)
podTemplateLabels[labels.StorageGeneration] = strconv.FormatInt(b.ObjectMeta.Generation, 10)

podTemplateAnnotations := CopyDict(b.Spec.AdditionalAnnotations)
podTemplateAnnotations[annotations.ConfigurationChecksum] = GetConfigurationChecksum(b.Spec.Configuration)

podTemplate := corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: b.Labels,
Annotations: CopyDict(b.Spec.AdditionalAnnotations),
Labels: podTemplateLabels,
Annotations: podTemplateAnnotations,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{b.buildContainer()},
Expand Down Expand Up @@ -389,7 +398,8 @@ func (b *StorageStatefulSetBuilder) buildVolumeMounts() []corev1.VolumeMount {
{
Name: configVolumeName,
ReadOnly: true,
MountPath: api.ConfigDir,
MountPath: fmt.Sprintf("%s/%s", api.ConfigDir, api.ConfigFileName),
SubPath: api.ConfigFileName,
},
}

Expand Down

0 comments on commit 2213de1

Please sign in to comment.