Skip to content

Commit 45b67f0

Browse files
committed
Merge branch 'master' into YDBOPS-9679
2 parents 88d869f + 6ced8c6 commit 45b67f0

35 files changed

+707
-510
lines changed

.gitignore

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,15 @@
55
*.dylib
66
*.test
77
*.out
8-
.idea/**/workspace.xml
9-
.idea/**/tasks.xml
10-
.idea/**/usage.statistics.xml
11-
.idea/**/dictionaries
12-
.idea/**/shelf
13-
.idea/**/contentModel.xml
14-
.idea/**/dataSources/
15-
.idea/**/dataSources.ids
16-
.idea/**/dataSources.local.xml
17-
.idea/**/sqlDataSources.xml
18-
.idea/**/dynamic.xml
19-
.idea/**/uiDesigner.xml
20-
.idea/**/dbnavigator.xml
21-
.idea/**/gradle.xml
22-
.idea/**/libraries
23-
cmake-build-*/
24-
.idea/**/mongoSettings.xml
258
*.iws
269
out/
10+
.idea/*
2711
.idea_modules/
2812
atlassian-ide-plugin.xml
29-
.idea/replstate.xml
3013
com_crashlytics_export_strings.xml
3114
crashlytics.properties
3215
crashlytics-build.properties
3316
fabric.properties
34-
.idea/httpRequests
35-
.idea/caches/build_file_checksums.ser
3617

3718
bin/
3819
config/

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ opts ?= ''
8181

8282
.PHONY: unit-test
8383
unit-test: manifests generate fmt vet envtest ## Run unit tests
84-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use --arch=amd64 $(ENVTEST_K8S_VERSION) -p path)" go test -v -timeout 1800s -p 1 ./internal/... -ginkgo.v -coverprofile cover.out $(opts)
84+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use --arch=amd64 $(ENVTEST_K8S_VERSION) -p path)" go test -v -timeout 900s -p 1 ./internal/... -ginkgo.v -coverprofile cover.out $(opts)
8585

8686
.PHONY: e2e-test
8787
e2e-test: manifests generate fmt vet docker-build kind-init kind-load ## Run e2e tests
88-
go test -v -timeout 1800s -p 1 ./e2e/... -ginkgo.v $(opts)
88+
go test -v -timeout 3600s -p 1 ./e2e/... -ginkgo.v $(opts)
8989

9090
.PHONY: test
9191
test: unit-test e2e-test ## Run all tests

api/v1alpha1/configuration.go

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,15 @@ package v1alpha1
22

33
import (
44
"bytes"
5-
"crypto/sha256"
65
"errors"
76
"fmt"
8-
"path"
97
"strconv"
108

119
"gopkg.in/yaml.v3"
1210

1311
"github.com/ydb-platform/ydb-kubernetes-operator/internal/configuration/schema"
1412
)
1513

16-
const (
17-
DatabaseEncryptionKeyPath = "/opt/ydb/secrets/database_encryption"
18-
DatabaseEncryptionKeyFile = "key"
19-
DatastreamsIAMServiceAccountKeyPath = "/opt/ydb/secrets/datastreams"
20-
DatastreamsIAMServiceAccountKeyFile = "sa_key.json"
21-
)
22-
23-
func hash(text string) string {
24-
h := sha256.New()
25-
h.Write([]byte(text))
26-
return fmt.Sprintf("%x", h.Sum(nil))
27-
}
28-
2914
func generateHosts(cr *Storage) []schema.Host {
3015
var hosts []schema.Host
3116

@@ -62,36 +47,6 @@ func generateHosts(cr *Storage) []schema.Host {
6247
return hosts
6348
}
6449

65-
func generateKeyConfig(cr *Storage, crDB *Database) *schema.KeyConfig {
66-
var keyConfig *schema.KeyConfig
67-
if crDB != nil && crDB.Spec.Encryption != nil && crDB.Spec.Encryption.Enabled {
68-
keyConfig = &schema.KeyConfig{
69-
Keys: []schema.Key{
70-
{
71-
ContainerPath: path.Join(DatabaseEncryptionKeyPath, DatabaseEncryptionKeyFile),
72-
ID: hash(cr.Name),
73-
Pin: crDB.Spec.Encryption.Pin,
74-
Version: 1,
75-
},
76-
},
77-
}
78-
}
79-
80-
return keyConfig
81-
}
82-
83-
func tryFillMissingSections(
84-
resultConfig map[string]interface{},
85-
generatedConfig schema.Configuration,
86-
) {
87-
if resultConfig["hosts"] == nil {
88-
resultConfig["hosts"] = generatedConfig.Hosts
89-
}
90-
if generatedConfig.KeyConfig != nil {
91-
resultConfig["key_config"] = generatedConfig.KeyConfig
92-
}
93-
}
94-
9550
func BuildConfiguration(cr *Storage, crDB *Database) ([]byte, error) {
9651
config := make(map[string]interface{})
9752

@@ -106,28 +61,29 @@ func BuildConfiguration(cr *Storage, crDB *Database) ([]byte, error) {
10661
rawYamlConfiguration = cr.Spec.Configuration
10762
}
10863

109-
hosts := generateHosts(cr)
110-
keyConfig := generateKeyConfig(cr, crDB)
111-
generatedConfig := schema.Configuration{
112-
Hosts: hosts,
113-
KeyConfig: keyConfig,
114-
}
115-
116-
success, dynconfig, err := TryParseDynconfig(rawYamlConfiguration)
64+
success, dynConfig, err := ParseDynConfig(rawYamlConfiguration)
11765
if success {
11866
if err != nil {
11967
return nil, fmt.Errorf("failed to parse dynconfig, error: %w", err)
12068
}
121-
tryFillMissingSections(dynconfig.Config, generatedConfig)
122-
return yaml.Marshal(dynconfig)
69+
if dynConfig.Config["hosts"] == nil {
70+
hosts := generateHosts(cr)
71+
dynConfig.Config["hosts"] = hosts
72+
}
73+
74+
return yaml.Marshal(dynConfig)
12375
}
12476

12577
err = yaml.Unmarshal([]byte(rawYamlConfiguration), &config)
12678
if err != nil {
12779
return nil, fmt.Errorf("failed to serialize YAML config, error: %w", err)
12880
}
12981

130-
tryFillMissingSections(config, generatedConfig)
82+
if config["hosts"] == nil {
83+
hosts := generateHosts(cr)
84+
config["hosts"] = hosts
85+
}
86+
13187
return yaml.Marshal(config)
13288
}
13389

@@ -144,25 +100,25 @@ func ParseConfiguration(rawYamlConfiguration string) (schema.Configuration, erro
144100
return configuration, nil
145101
}
146102

147-
func TryParseDynconfig(rawYamlConfiguration string) (bool, schema.Dynconfig, error) {
103+
func ParseDynConfig(rawYamlConfiguration string) (bool, schema.DynConfig, error) {
148104
dec := yaml.NewDecoder(bytes.NewReader([]byte(rawYamlConfiguration)))
149105
dec.KnownFields(true)
150106

151-
var dynconfig schema.Dynconfig
152-
err := dec.Decode(&dynconfig)
107+
var dynConfig schema.DynConfig
108+
err := dec.Decode(&dynConfig)
153109
if err != nil {
154-
return false, schema.Dynconfig{}, fmt.Errorf("error unmarshal yaml to dynconfig: %w", err)
110+
return false, schema.DynConfig{}, fmt.Errorf("error unmarshal yaml to dynconfig: %w", err)
155111
}
156112

157-
err = validateDynconfig(dynconfig)
113+
err = validateDynConfig(dynConfig)
158114
if err != nil {
159-
return true, dynconfig, fmt.Errorf("error validate dynconfig: %w", err)
115+
return true, dynConfig, fmt.Errorf("error validate dynconfig: %w", err)
160116
}
161117

162-
return true, dynconfig, nil
118+
return true, dynConfig, err
163119
}
164120

165-
func validateDynconfig(dynConfig schema.Dynconfig) error {
121+
func validateDynConfig(dynConfig schema.DynConfig) error {
166122
if _, exist := dynConfig.Config["yaml_config_enabled"]; !exist {
167123
return errors.New("failed to find mandatory `yaml_config_enabled` field inside config")
168124
}
@@ -182,12 +138,12 @@ func validateDynconfig(dynConfig schema.Dynconfig) error {
182138
return nil
183139
}
184140

185-
func GetConfigForCMS(dynconfig schema.Dynconfig) ([]byte, error) {
186-
delete(dynconfig.Config, "static_erasure")
187-
delete(dynconfig.Config, "host_configs")
188-
delete(dynconfig.Config, "nameservice_config")
189-
delete(dynconfig.Config, "blob_storage_config")
190-
delete(dynconfig.Config, "hosts")
141+
func GetConfigForCMS(dynConfig schema.DynConfig) ([]byte, error) {
142+
delete(dynConfig.Config, "static_erasure")
143+
delete(dynConfig.Config, "host_configs")
144+
delete(dynConfig.Config, "nameservice_config")
145+
delete(dynConfig.Config, "blob_storage_config")
146+
delete(dynConfig.Config, "hosts")
191147

192-
return yaml.Marshal(dynconfig)
148+
return yaml.Marshal(dynConfig)
193149
}

api/v1alpha1/connection_types.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
)
66

77
type ConnectionOptions struct {
8-
AccessToken *AccessTokenAuth `json:"accessToken,omitempty"`
9-
StaticCredentials *StaticCredentialsAuth `json:"staticCredentials,omitempty"`
10-
Oauth2TokenExhange *Oauth2TokenExchange `json:"oauth2TokenExchange,omitempty"`
8+
AccessToken *AccessTokenAuth `json:"accessToken,omitempty"`
9+
StaticCredentials *StaticCredentialsAuth `json:"staticCredentials,omitempty"`
10+
Oauth2TokenExchange *Oauth2TokenExchange `json:"oauth2TokenExchange,omitempty"`
1111
}
1212

1313
type AccessTokenAuth struct {
@@ -22,13 +22,13 @@ type StaticCredentialsAuth struct {
2222
type Oauth2TokenExchange struct {
2323
Endpoint string `json:"endpoint"`
2424
PrivateKey *CredentialSource `json:"privateKey"`
25-
JWTHeader *JWTHeader `json:",inline"`
26-
JWTClaims *JWTClaims `json:",inline"`
25+
JWTHeader `json:",inline"`
26+
JWTClaims `json:",inline"`
2727
}
2828

2929
type JWTHeader struct {
30-
KeyID string `json:"keyID,omitempty"`
31-
SignAlg string `json:"signAlg,omitempty"`
30+
KeyID *string `json:"keyID"`
31+
SignAlg string `json:"signAlg,omitempty"`
3232
}
3333
type JWTClaims struct {
3434
Issuer string `json:"issuer,omitempty"`

api/v1alpha1/const.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,21 @@ const (
3030
ConfigDir = "/opt/ydb/cfg"
3131
ConfigFileName = "config.yaml"
3232

33+
DatabaseEncryptionKeySecretDir = "encryption"
34+
DatabaseEncryptionKeySecretFile = "key.pem"
35+
DatabaseEncryptionKeyConfigFile = "key.txt"
36+
37+
DatastreamsIAMServiceAccountKeyDir = "datastreams"
38+
DatastreamsIAMServiceAccountKeyFile = "sa_key.json"
39+
3340
BinariesDir = "/opt/ydb/bin"
3441
DaemonBinaryName = "ydbd"
3542

36-
DefaultRootUsername = "root"
37-
DefaultRootPassword = ""
43+
DefaultRootUsername = "root"
44+
DefaultRootPassword = ""
45+
DefaultDatabaseDomain = "Root"
46+
DefaultDatabaseEncryptionPin = "EmptyPin"
47+
DefaultSignAlgorithm = "RS256"
3848

3949
LabelDeploymentKey = "deployment"
4050
LabelDeploymentValueKubernetes = "kubernetes"

api/v1alpha1/database_webhook.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ import (
1717
. "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants" //nolint:revive,stylecheck
1818
)
1919

20-
const (
21-
DefaultDatabaseDomain = "Root"
22-
)
23-
2420
// log is for logging in this package.
2521
var databaselog = logf.Log.WithName("database-resource")
2622

@@ -126,6 +122,13 @@ func (r *DatabaseDefaulter) Default(ctx context.Context, obj runtime.Object) err
126122
database.Spec.Encryption = &EncryptionConfig{Enabled: false}
127123
}
128124

125+
if database.Spec.Encryption.Enabled && database.Spec.Encryption.Key == nil {
126+
if database.Spec.Encryption.Pin == nil || len(*database.Spec.Encryption.Pin) == 0 {
127+
encryptionPin := DefaultDatabaseEncryptionPin
128+
database.Spec.Encryption.Pin = &encryptionPin
129+
}
130+
}
131+
129132
if database.Spec.Datastreams == nil {
130133
database.Spec.Datastreams = &DatastreamsConfig{Enabled: false}
131134
}
@@ -149,7 +152,7 @@ func (r *DatabaseDefaulter) Default(ctx context.Context, obj runtime.Object) err
149152
database.Spec.StorageEndpoint = storage.GetStorageEndpointWithProto()
150153
}
151154

152-
if database.Spec.Configuration != "" || (database.Spec.Encryption != nil && database.Spec.Encryption.Enabled) {
155+
if database.Spec.Configuration != "" {
153156
configuration, err := BuildConfiguration(storage, database)
154157
if err != nil {
155158
return err

0 commit comments

Comments
 (0)