Skip to content

Commit

Permalink
propagate ydb.tech annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
kobzonega committed Jul 1, 2024
1 parent 7225c97 commit 826806c
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 109 deletions.
2 changes: 1 addition & 1 deletion internal/controllers/storage/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ var _ = Describe("Storage controller medium tests", func() {
}, &foundStorage)).Should(Succeed())

foundConfigurationChecksumAnnotation := false
if podAnnotations[annotations.ConfigurationChecksum] == resources.GetConfigurationChecksum(foundStorage.Spec.Configuration) {
if podAnnotations[annotations.ConfigurationChecksum] == resources.GetSHA256Checksum(foundStorage.Spec.Configuration) {
foundConfigurationChecksumAnnotation = true
}
Expect(foundConfigurationChecksumAnnotation).To(BeTrue())
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 @@ -306,7 +306,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
5 changes: 3 additions & 2 deletions internal/labels/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ const (
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 Down
55 changes: 40 additions & 15 deletions internal/resources/database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package resources

import (
"fmt"

corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/rest"

Expand All @@ -25,6 +27,28 @@ func NewDatabase(ydbCr *api.Database) DatabaseBuilder {
return DatabaseBuilder{Database: cr, Storage: nil}
}

func (b *DatabaseBuilder) NewLabels() labels.Labels {
l := labels.Common(b.Name, b.Labels)

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

return l
}

func (b *DatabaseBuilder) NewAnnotations() map[string]string {
an := annotations.Common(b.Annotations)

an.Merge(b.Spec.AdditionalAnnotations)
if b.Spec.Configuration != "" {
an.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)})
} else {
an.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Storage.Spec.Configuration)})
}

return an
}

func (b *DatabaseBuilder) Unwrap() *api.Database {
return b.DeepCopy()
}
Expand All @@ -34,14 +58,12 @@ func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []Resourc
return []ResourceBuilder{}
}

databaseLabels := labels.DatabaseLabels(b.Unwrap())
databaseLabels := b.NewLabels()
databaseAnnotations := b.NewAnnotations()

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

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

grpcServiceLabels := databaseLabels.Copy()
grpcServiceLabels.Merge(b.Spec.Service.GRPC.AdditionalLabels)
grpcServiceLabels.Merge(map[string]string{labels.ServiceComponent: labels.GRPCComponent})
Expand Down Expand Up @@ -183,33 +205,36 @@ func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []Resourc

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

return optionalBuilders
}

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

for _, nodeSetSpecInline := range b.Spec.NodeSets {
nodeSetName := fmt.Sprintf("%s-%s", b.Name, nodeSetSpecInline.Name)

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})
}

nodeSetAnnotations := CopyDict(b.Annotations)
if nodeSetSpecInline.Annotations != nil {
for k, v := range nodeSetSpecInline.Annotations {
nodeSetAnnotations[k] = v
}
}
nodeSetAnnotations := databaseAnnotations.Copy()
nodeSetAnnotations.Merge(nodeSetSpecInline.Annotations)

databaseNodeSetSpec := b.recastDatabaseNodeSetSpecInline(nodeSetSpecInline.DeepCopy())
if nodeSetSpecInline.Remote != nil {
Expand All @@ -218,7 +243,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 @@ -231,7 +256,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
48 changes: 31 additions & 17 deletions internal/resources/databasenodeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ type DatabaseNodeSetBuilder struct {
DatabaseNodeSetSpec api.DatabaseNodeSetSpec
}

type DatabaseNodeSetResource struct {
*api.DatabaseNodeSet
}

func (b *DatabaseNodeSetBuilder) Build(obj client.Object) error {
dns, ok := obj.(*api.DatabaseNodeSet)
if !ok {
Expand Down Expand Up @@ -54,32 +50,50 @@ func (b *DatabaseNodeSetBuilder) Placeholder(cr client.Object) client.Object {
}
}

func (b *DatabaseNodeSetResource) GetResourceBuilders(restConfig *rest.Config) []ResourceBuilder {
ydbCr := api.RecastDatabaseNodeSet(b.Unwrap())
databaseLabels := labels.DatabaseLabels(ydbCr)
type DatabaseNodeSetResource struct {
*api.DatabaseNodeSet
}

func (b *DatabaseNodeSetResource) NewLabels() labels.Labels {
l := labels.Common(b.Name, b.Labels)

statefulSetName := b.Name
statefulSetLabels := databaseLabels.Copy()
statefulSetLabels.Merge(map[string]string{labels.StatefulsetComponent: statefulSetName})
l.Merge(b.Spec.AdditionalLabels)
l.Merge(map[string]string{labels.ComponentKey: labels.DynamicComponent})

databaseNodeSetName := b.Labels[labels.DatabaseNodeSetComponent]
statefulSetLabels.Merge(map[string]string{labels.DatabaseNodeSetComponent: databaseNodeSetName})
l.Merge(map[string]string{labels.DatabaseNodeSetComponent: databaseNodeSetName})

if remoteCluster, exist := b.Labels[labels.RemoteClusterKey]; exist {
statefulSetLabels.Merge(map[string]string{labels.RemoteClusterKey: remoteCluster})
l.Merge(map[string]string{labels.RemoteClusterKey: remoteCluster})
}

statefulSetAnnotations := CopyDict(b.Spec.AdditionalAnnotations)
statefulSetAnnotations[annotations.ConfigurationChecksum] = GetConfigurationChecksum(b.Spec.Configuration)
return l
}

func (b *DatabaseNodeSetResource) NewAnnotations() map[string]string {
an := annotations.Common(b.Annotations)

an.Merge(b.Spec.AdditionalAnnotations)

return an
}

func (b *DatabaseNodeSetResource) GetResourceBuilders(restConfig *rest.Config) []ResourceBuilder {
nodeSetLabels := b.NewLabels()
nodeSetAnnotations := b.NewAnnotations()

statefulSetLabels := nodeSetLabels.Copy()
statefulSetLabels.Merge(map[string]string{labels.StatefulsetComponent: b.Name})

var resourceBuilders []ResourceBuilder
resourceBuilders = append(resourceBuilders,
&DatabaseStatefulSetBuilder{
Database: ydbCr,
Database: api.RecastDatabaseNodeSet(b.Unwrap()),
RestConfig: restConfig,

Name: statefulSetName,
Name: b.Name,
Labels: statefulSetLabels,
Annotations: statefulSetAnnotations,
Annotations: nodeSetAnnotations,
},
)
return resourceBuilders
Expand Down
13 changes: 1 addition & 12 deletions internal/resources/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ func buildCAStorePatchingCommandArgs(
return command, args
}

func GetConfigurationChecksum(configuration string) string {
func GetSHA256Checksum(configuration string) string {
hasher := sha256.New()
hasher.Write([]byte(configuration))
return hex.EncodeToString(hasher.Sum(nil))
Expand All @@ -592,17 +592,6 @@ func CompareMaps(map1, map2 map[string]string) bool {
return true
}

func PodIsReady(e corev1.Pod) bool {
if e.Status.Phase == corev1.PodRunning {
for _, condition := range e.Status.Conditions {
if condition.Type == corev1.PodReady && condition.Status == corev1.ConditionTrue {
return true
}
}
}
return false
}

func isSignAlgorithmSupported(alg string) bool {
supportedAlgs := jwt.GetAlgorithms()

Expand Down
88 changes: 73 additions & 15 deletions internal/resources/storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package resources

import (
"fmt"

"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -29,15 +31,67 @@ func (b *StorageClusterBuilder) Unwrap() *api.Storage {
return b.DeepCopy()
}

func (b *StorageClusterBuilder) NewLabels() labels.Labels {
l := labels.Common(b.Name, b.Labels)

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

return l
}

func (b *StorageClusterBuilder) NewAnnotations() map[string]string {
an := annotations.Common(b.Annotations)

an.Merge(b.Spec.AdditionalAnnotations)
an.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)})

return an
}

func (b *StorageClusterBuilder) NewInitJobLabels() labels.Labels {
l := labels.Common(b.Name, b.Labels)

if b.Spec.InitJob != nil {
l.Merge(b.Spec.InitJob.AdditionalLabels)
}
l.Merge(map[string]string{labels.ComponentKey: labels.BlobstorageInitComponent})

return l
}

func (b *StorageClusterBuilder) NewInitJobAnnotations() map[string]string {
an := annotations.Common(b.Annotations)

if b.Spec.InitJob != nil {
an.Merge(b.Spec.InitJob.AdditionalLabels)
}
an.Merge(map[string]string{annotations.ConfigurationChecksum: GetSHA256Checksum(b.Spec.Configuration)})

return an
}

func (b *StorageClusterBuilder) GetInitJobBuilder() ResourceBuilder {
jobName := fmt.Sprintf(InitJobNameFormat, b.Name)
jobLabels := b.NewInitJobLabels()
jobAnnotations := b.NewInitJobAnnotations()

return &StorageInitJobBuilder{
Storage: b.Unwrap(),

Name: jobName,
Labels: jobLabels,
Annotations: jobAnnotations,
}
}

func (b *StorageClusterBuilder) GetResourceBuilders(restConfig *rest.Config) []ResourceBuilder {
storageLabels := labels.StorageLabels(b.Unwrap())
storageLabels := b.NewLabels()
storageAnnotations := b.NewAnnotations()

statefulSetLabels := storageLabels.Copy()
statefulSetLabels.Merge(map[string]string{labels.StatefulsetComponent: b.Name})

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

grpcServiceLabels := storageLabels.Copy()
grpcServiceLabels.Merge(b.Spec.Service.GRPC.AdditionalLabels)
grpcServiceLabels.Merge(map[string]string{labels.ServiceComponent: labels.GRPCComponent})
Expand Down Expand Up @@ -106,11 +160,14 @@ func (b *StorageClusterBuilder) GetResourceBuilders(restConfig *rest.Config) []R

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

return append(
Expand Down Expand Up @@ -158,23 +215,24 @@ func (b *StorageClusterBuilder) GetResourceBuilders(restConfig *rest.Config) []R
)
}

func (b *StorageClusterBuilder) getNodeSetBuilders(storageLabels labels.Labels) []ResourceBuilder {
func (b *StorageClusterBuilder) getNodeSetBuilders(
storageLabels labels.Labels,
storageAnnotations annotations.Annotations,
) []ResourceBuilder {
var nodeSetBuilders []ResourceBuilder

for _, nodeSetSpecInline := range b.Spec.NodeSets {
nodeSetName := fmt.Sprintf("%s-%s", b.Name, nodeSetSpecInline.Name)

nodeSetLabels := storageLabels.Copy()
nodeSetLabels.Merge(nodeSetSpecInline.Labels)
nodeSetLabels.Merge(map[string]string{labels.StorageNodeSetComponent: nodeSetSpecInline.Name})
if nodeSetSpecInline.Remote != nil {
nodeSetLabels.Merge(map[string]string{labels.RemoteClusterKey: nodeSetSpecInline.Remote.Cluster})
}

nodeSetAnnotations := CopyDict(b.Annotations)
if nodeSetSpecInline.Annotations != nil {
for k, v := range nodeSetSpecInline.Annotations {
nodeSetAnnotations[k] = v
}
}
nodeSetAnnotations := storageAnnotations.Copy()
nodeSetAnnotations.Merge(nodeSetSpecInline.Annotations)

storageNodeSetSpec := b.recastStorageNodeSetSpecInline(nodeSetSpecInline.DeepCopy())
if nodeSetSpecInline.Remote != nil {
Expand All @@ -183,7 +241,7 @@ func (b *StorageClusterBuilder) getNodeSetBuilders(storageLabels labels.Labels)
&RemoteStorageNodeSetBuilder{
Object: b,

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

Expand All @@ -196,7 +254,7 @@ func (b *StorageClusterBuilder) getNodeSetBuilders(storageLabels labels.Labels)
&StorageNodeSetBuilder{
Object: b,

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

Expand Down
Loading

0 comments on commit 826806c

Please sign in to comment.