@@ -9,104 +9,106 @@ import (
99 "github.com/ydb-platform/ydb-go-genproto/draft/protos/Ydb_DynamicConfig"
1010 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Operations"
1111 "github.com/ydb-platform/ydb-go-sdk/v3"
12- "github.com/ydb-platform/ydb-go-sdk/v3/credentials"
1312 "google.golang.org/protobuf/types/known/durationpb"
1413 "sigs.k8s.io/controller-runtime/pkg/log"
1514
16- "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
1715 "github.com/ydb-platform/ydb-kubernetes-operator/internal/connection"
18- "github.com/ydb-platform/ydb-kubernetes-operator/internal/resources"
1916)
2017
2118const (
2219 GetConfigTimeoutSeconds = 10
2320 ReplaceConfigTimeoutSeconds = 30
2421)
2522
26- func GetConfig (
23+ type Config struct {
24+ StorageEndpoint string
25+ Domain string
26+ Config string
27+ Version uint64
28+ DryRun bool
29+ AllowUnknownFields bool
30+ }
31+
32+ func (c * Config ) GetConfig (
2733 ctx context.Context ,
28- storage * resources.StorageClusterBuilder ,
29- creds credentials.Credentials ,
3034 opts ... ydb.Option ,
3135) (* Ydb_DynamicConfig.GetConfigResponse , error ) {
32- endpoint := fmt .Sprintf (
33- "%s/%s" ,
34- storage .GetStorageEndpointWithProto (),
35- storage .Spec .Domain ,
36- )
37- conn , err := connection .Open (ctx ,
38- endpoint ,
39- ydb .WithCredentials (creds ),
40- ydb .MergeOptions (opts ... ),
41- )
36+ logger := log .FromContext (ctx )
37+
38+ endpoint := fmt .Sprintf ("%s/%s" , c .StorageEndpoint , c .Domain )
39+ ydbCtx , ydbCtxCancel := context .WithTimeout (ctx , time .Second )
40+ defer ydbCtxCancel ()
41+ conn , err := connection .Open (ydbCtx , endpoint , ydb .MergeOptions (opts ... ))
4242 if err != nil {
4343 return nil , fmt .Errorf ("error connecting to YDB: %w" , err )
4444 }
4545 defer func () {
46- connection .Close (ctx , conn )
46+ connection .Close (ydbCtx , conn )
4747 }()
4848
49- cmsCtx , cancel := context .WithTimeout (ctx , GetConfigTimeoutSeconds * time .Second )
50- defer cancel ()
49+ cmsCtx , cmsCtxCancel := context .WithTimeout (ctx , GetConfigTimeoutSeconds * time .Second )
50+ defer cmsCtxCancel ()
5151 client := Ydb_DynamicConfig_V1 .NewDynamicConfigServiceClient (ydb .GRPCConn (conn ))
52- request := & Ydb_DynamicConfig.GetConfigRequest {
53- OperationParams : & Ydb_Operations.OperationParams {
54- OperationMode : Ydb_Operations .OperationParams_SYNC ,
55- OperationTimeout : & durationpb.Duration {Seconds : GetConfigTimeoutSeconds },
56- },
57- }
52+ request := c .makeGetConfigRequest ()
53+
54+ logger .Info ("CMS GetConfig" , "endpoint" , endpoint , "request" , request )
5855 return client .GetConfig (cmsCtx , request )
5956}
6057
61- func GetConfigResult (
62- response * Ydb_DynamicConfig.GetConfigResponse ,
63- ) (* Ydb_DynamicConfig.GetConfigResult , error ) {
58+ func (c * Config ) ProcessConfigResponse (response * Ydb_DynamicConfig.GetConfigResponse ) error {
6459 configResult := & Ydb_DynamicConfig.GetConfigResult {}
6560 err := response .GetOperation ().GetResult ().UnmarshalTo (configResult )
6661 if err != nil {
67- return nil , err
62+ return err
6863 }
69- return configResult , nil
64+
65+ c .Config = configResult .GetConfig ()
66+ c .Version = configResult .GetIdentity ().GetVersion ()
67+ return nil
7068}
7169
72- func ReplaceConfig (
70+ func ( c * Config ) ReplaceConfig (
7371 ctx context.Context ,
74- storage * resources.StorageClusterBuilder ,
75- dryRun bool ,
76- creds credentials.Credentials ,
7772 opts ... ydb.Option ,
7873) (* Ydb_DynamicConfig.ReplaceConfigResponse , error ) {
7974 logger := log .FromContext (ctx )
80- endpoint := fmt .Sprintf (
81- "%s/%s" ,
82- storage .GetStorageEndpointWithProto (),
83- storage .Spec .Domain ,
84- )
85- conn , err := connection .Open (ctx ,
86- endpoint ,
87- ydb .WithCredentials (creds ),
88- ydb .MergeOptions (opts ... ),
89- )
75+
76+ endpoint := fmt .Sprintf ("%s/%s" , c .StorageEndpoint , c .Domain )
77+ ydbCtx , ydbCtxCancel := context .WithTimeout (ctx , time .Second )
78+ defer ydbCtxCancel ()
79+ conn , err := connection .Open (ydbCtx , endpoint , ydb .MergeOptions (opts ... ))
9080 if err != nil {
9181 return nil , fmt .Errorf ("error connecting to YDB: %w" , err )
9282 }
9383 defer func () {
94- connection .Close (ctx , conn )
84+ connection .Close (ydbCtx , conn )
9585 }()
9686
97- config , err := v1alpha1 .GetConfigForCMS (storage .Spec .Configuration )
98- if err != nil {
99- return nil , err
100- }
101-
102- cmsCtx , cancel := context .WithTimeout (ctx , ReplaceConfigTimeoutSeconds * time .Second )
103- defer cancel ()
87+ cmsCtx , cmsCtxCancel := context .WithTimeout (ctx , ReplaceConfigTimeoutSeconds * time .Second )
88+ defer cmsCtxCancel ()
10489 client := Ydb_DynamicConfig_V1 .NewDynamicConfigServiceClient (ydb .GRPCConn (conn ))
10590 request := & Ydb_DynamicConfig.ReplaceConfigRequest {
106- Config : string ( config ) ,
107- DryRun : dryRun ,
108- AllowUnknownFields : true ,
91+ Config : c . Config ,
92+ DryRun : c . DryRun ,
93+ AllowUnknownFields : c . AllowUnknownFields ,
10994 }
110- logger .Info ("CMS ReplaceConfig" , "request" , request )
95+
96+ logger .Info ("CMS ReplaceConfig" , "endpoint" , endpoint , "request" , request )
11197 return client .ReplaceConfig (cmsCtx , request )
11298}
99+
100+ func (c * Config ) CheckReplaceConfigResponse (ctx context.Context , response * Ydb_DynamicConfig.ReplaceConfigResponse ) (bool , string , error ) {
101+ logger := log .FromContext (ctx )
102+
103+ logger .Info ("CMS ReplaceConfig response" , "response" , response )
104+ return CheckOperationStatus (response .GetOperation ())
105+ }
106+
107+ func (c * Config ) makeGetConfigRequest () * Ydb_DynamicConfig.GetConfigRequest {
108+ request := & Ydb_DynamicConfig.GetConfigRequest {}
109+ request .OperationParams = & Ydb_Operations.OperationParams {
110+ OperationTimeout : & durationpb.Duration {Seconds : GetConfigTimeoutSeconds },
111+ }
112+
113+ return request
114+ }
0 commit comments