diff options
author | innokentii <innokentii@yandex-team.com> | 2023-06-20 17:29:58 +0300 |
---|---|---|
committer | innokentii <innokentii@yandex-team.com> | 2023-06-20 17:29:58 +0300 |
commit | 168dea4bb889016881cef74c6a3c9d817174976a (patch) | |
tree | dfba79009aa2921e510dc921397c9d7b51c3443f | |
parent | 297398b7c91118ef39cb5bdbab096c369473f7ab (diff) | |
download | ydb-168dea4bb889016881cef74c6a3c9d817174976a.tar.gz |
Console config item dumper
-rw-r--r-- | ydb/core/cms/console/yaml_config/console_dumper.cpp | 122 | ||||
-rw-r--r-- | ydb/core/cms/console/yaml_config/console_dumper.h | 7 | ||||
-rw-r--r-- | ydb/core/cms/console/yaml_config/console_dumper_ut.cpp | 21 | ||||
-rw-r--r-- | ydb/core/driver_lib/cli_utils/cli_cmds_console.cpp | 71 |
4 files changed, 198 insertions, 23 deletions
diff --git a/ydb/core/cms/console/yaml_config/console_dumper.cpp b/ydb/core/cms/console/yaml_config/console_dumper.cpp index b173afea78..78543c4575 100644 --- a/ydb/core/cms/console/yaml_config/console_dumper.cpp +++ b/ydb/core/cms/console/yaml_config/console_dumper.cpp @@ -120,6 +120,17 @@ void CopyFrom(::google::protobuf::Message &to, } } +ui32 DetectConfigItemKind(const NKikimrConsole::TConfigItem &item) +{ + std::vector<const ::google::protobuf::FieldDescriptor*> fields; + auto *reflection = item.GetConfig().GetReflection(); + reflection->ListFields(item.GetConfig(), &fields); + + Y_VERIFY(fields.size() == 1, "Can't detect config item kind"); + + return fields[0]->number(); +} + std::pair<TDomainItemsContainer, TSelectorItemsContainer> ExtractSuitableItems( const ::google::protobuf::RepeatedPtrField<NKikimrConsole::TConfigItem> &configItems) { @@ -127,9 +138,16 @@ std::pair<TDomainItemsContainer, TSelectorItemsContainer> ExtractSuitableItems( TSelectorItemsContainer selectorItemsByOrder; for (auto &item : configItems) { - if (item.GetKind() == NKikimrConsole::TConfigItem::NameserviceConfigItem || - item.GetKind() == NKikimrConsole::TConfigItem::NetClassifierDistributableConfigItem || - item.GetKind() == NKikimrConsole::TConfigItem::NamedConfigsItem || + ui32 kind = item.GetKind(); + + if (kind == 0) { + kind = DetectConfigItemKind(item); + } + + + if (kind == NKikimrConsole::TConfigItem::NameserviceConfigItem || + kind == NKikimrConsole::TConfigItem::NetClassifierDistributableConfigItem || + kind == NKikimrConsole::TConfigItem::NamedConfigsItem || item.GetCookie().StartsWith("ydbcp")) { continue; } @@ -157,23 +175,28 @@ NKikimrConfig::TAppConfig BundleDomainConfig(const TDomainItemsContainer &items) NKikimrConfig::TAppConfig config; for (auto &[_, item] : items) { - Y_VERIFY(item.GetKind() != 0, "Tool doesn't support items with kind Auto"); + ui32 kind = item.GetKind(); + + if (kind == 0) { + kind = DetectConfigItemKind(item); + } + if (item.GetMergeStrategy() == NKikimrConsole::TConfigItem::MERGE) { config.MergeFrom(item.GetConfig()); } else if (item.GetMergeStrategy() == NKikimrConsole::TConfigItem::OVERWRITE) { - CopyFrom(config, item.GetConfig(), item.GetKind()); + CopyFrom(config, item.GetConfig(), kind); } else if (item.GetMergeStrategy() == NKikimrConsole::TConfigItem::MERGE_OVERWRITE_REPEATED) { - MergeMessageOverwriteRepeated(config, item.GetConfig(), item.GetKind()); + MergeMessageOverwriteRepeated(config, item.GetConfig(), kind); } } return config; } -TVector<TSelectorData> FillSelectorsData(const TSelectorItemsContainer &items) { +TVector<TSelectorData> FillSelectorsData(const TSelectorItemsContainer &sItems) { TVector<TSelectorData> selectors; - for (auto &[tuple, items] : items) { + for (auto &[_, items] : sItems) { auto &item = items.back(); NYamlConfig::TSelector rules; @@ -219,8 +242,10 @@ TVector<TSelectorData> FillSelectorsData(const TSelectorItemsContainer &items) { TStringStream desc; desc << "cookie=" << item.GetCookie() - << " merge_strategy=" << NConsole::TConfigItem::MergeStrategyName(item.GetMergeStrategy()) - << " id=" << it.GetId().GetId() << "." << it.GetId().GetGeneration();; + << " merge_strategy=" << NConsole::TConfigItem::MergeStrategyName(item.GetMergeStrategy()); + if (it.GetId().GetId() != 0) { + desc << " id=" << it.GetId().GetId() << "." << it.GetId().GetGeneration();; + } selectors.emplace_back(TSelectorData{ item.GetMergeStrategy(), @@ -254,12 +279,15 @@ TVector<TSelectorData> FillSelectorsData(const TSelectorItemsContainer &items) { if (!rules.In.empty()) { TStringStream desc; desc << "cookie=" << item.GetCookie() - << " merge_strategy=" << NConsole::TConfigItem::MergeStrategyName(item.GetMergeStrategy()) - << " id="; - bool first = true; - for (auto &it : items) { - desc << (first ? "" : ",") << it.GetId().GetId() << "." << it.GetId().GetGeneration(); - first = false; + << " merge_strategy=" << NConsole::TConfigItem::MergeStrategyName(item.GetMergeStrategy()); + + if (items[0].GetId().GetId() != 0) { + desc << " id="; + bool first = true; + for (auto &it : items) { + desc << (first ? "" : ",") << it.GetId().GetId() << "." << it.GetId().GetGeneration(); + first = false; + } } selectors.emplace_back(TSelectorData{ @@ -364,4 +392,66 @@ selector_config: [] return res.Str(); } +TDumpConsoleConfigItemResult DumpConsoleConfigItem(const NKikimrConsole::TConfigItem &item) { + google::protobuf::RepeatedPtrField<NKikimrConsole::TConfigItem> configItems; + auto* newItem = configItems.Add(); + newItem->CopyFrom(item); + const auto [domainItemsByOrder, selectorItemsByOrder] = ExtractSuitableItems(configItems); + + const TString configTemplate = R"( +[] +)"; + auto outDoc = NFyaml::TDocument::Parse(configTemplate); + auto selectorsSeq = outDoc.Root().Sequence(); + if (!domainItemsByOrder.empty()) { + const NKikimrConfig::TAppConfig configProto = BundleDomainConfig(domainItemsByOrder); + auto mainConfigYaml = NFyaml::TDocument::Parse( + NProtobufJson::Proto2Json(configProto, GetProto2JsonConfig())); + + auto configNodeRef = mainConfigYaml.Root(); + + switch (item.GetMergeStrategy()) { + case NKikimrConsole::TConfigItem::MERGE_OVERWRITE_REPEATED: + MarkYamlForMergeOverwriteRepeated(configNodeRef); + break; + case NKikimrConsole::TConfigItem::MERGE: + MarkYamlForMerge(configNodeRef); + break; + default: break; + } + + const TString selectorTemplate = R"( +description: "" +selector: {} +config: {} +)"; + + auto selectorTemplateYaml = NFyaml::TDocument::Parse(selectorTemplate); + auto selectorConfigRoot = selectorTemplateYaml.Root(); + + auto mainConfigRoot = mainConfigYaml.Root().Copy(selectorTemplateYaml); + selectorConfigRoot.Map().pair_at("config").SetValue(mainConfigRoot.Ref()); + + TStringStream desc; + + desc << "cookie=" << item.GetCookie() + << " merge_strategy=" << NConsole::TConfigItem::MergeStrategyName(item.GetMergeStrategy()); + + selectorConfigRoot.Map().pair_at("description").SetValue(selectorTemplateYaml.Buildf("%s", desc.Str().c_str())); + + auto selector = selectorConfigRoot.Copy(outDoc); + outDoc.Root().Sequence().Append(selector.Ref()); + } else { + const auto selectors = FillSelectorsData(selectorItemsByOrder); + SerializeSelectorsToYaml(selectors, outDoc, selectorsSeq); + } + + Beautify(outDoc); + + TStringStream res; + res << outDoc; + + return {!domainItemsByOrder.empty(), res.Str()}; +} + } // namespace NYamlConfig diff --git a/ydb/core/cms/console/yaml_config/console_dumper.h b/ydb/core/cms/console/yaml_config/console_dumper.h index 599a022a28..ab0c910eac 100644 --- a/ydb/core/cms/console/yaml_config/console_dumper.h +++ b/ydb/core/cms/console/yaml_config/console_dumper.h @@ -8,4 +8,11 @@ namespace NYamlConfig { TString DumpConsoleConfigs(const ::google::protobuf::RepeatedPtrField<NKikimrConsole::TConfigItem> &configItems); +struct TDumpConsoleConfigItemResult { + bool Domain; + TString Config; +}; + +TDumpConsoleConfigItemResult DumpConsoleConfigItem(const NKikimrConsole::TConfigItem &item); + } // namespace NYamlConfig diff --git a/ydb/core/cms/console/yaml_config/console_dumper_ut.cpp b/ydb/core/cms/console/yaml_config/console_dumper_ut.cpp index d106d62bb4..758ec195e8 100644 --- a/ydb/core/cms/console/yaml_config/console_dumper_ut.cpp +++ b/ydb/core/cms/console/yaml_config/console_dumper_ut.cpp @@ -9,6 +9,7 @@ Y_UNIT_TEST_SUITE(ConsoleDumper) { const TVector<ui32> &orders) { NKikimrConsole::TConfigItem *configItem = items.Add(); configItem->SetMergeStrategy(mergeStrategy); + configItem->MutableId()->SetId(1); configItem->SetOrder(orders[0]); configItem->SetKind((ui32)NKikimrConsole::TConfigItem::LogConfigItem); configItem->SetCookie("test"); @@ -52,6 +53,7 @@ Y_UNIT_TEST_SUITE(ConsoleDumper) { ::google::protobuf::RepeatedPtrField<NKikimrConsole::TConfigItem> items; NKikimrConsole::TConfigItem *configItem = items.Add(); configItem->SetMergeStrategy(mergeStrategy); + configItem->MutableId()->SetId(1); configItem->SetOrder(21); configItem->SetKind((ui32)NKikimrConsole::TConfigItem::LogConfigItem); configItem->SetCookie("test"); @@ -289,6 +291,7 @@ selector_config: [] Y_UNIT_TEST(SimpleNode) { ::google::protobuf::RepeatedPtrField<NKikimrConsole::TConfigItem> items; NKikimrConsole::TConfigItem *configItem = items.Add(); + configItem->MutableId()->SetId(1); configItem->SetMergeStrategy(1); configItem->MutableUsageScope()->MutableTenantAndNodeTypeFilter()->SetNodeType("test_node_type"); configItem->SetOrder(21); @@ -308,7 +311,7 @@ allowed_labels: tenant: type: string selector_config: -- description: cookie=test merge_strategy=OVERWRITE id=0.0 +- description: cookie=test merge_strategy=OVERWRITE id=1.0 selector: node_type: test_node_type config: @@ -464,6 +467,7 @@ selector_config: Y_UNIT_TEST(SimpleTenant) { ::google::protobuf::RepeatedPtrField<NKikimrConsole::TConfigItem> items; NKikimrConsole::TConfigItem *configItem = items.Add(); + configItem->MutableId()->SetId(1); configItem->SetMergeStrategy(1); configItem->MutableUsageScope()->MutableTenantAndNodeTypeFilter()->SetTenant("test_tenant"); configItem->SetOrder(21); @@ -483,7 +487,7 @@ allowed_labels: tenant: type: string selector_config: -- description: cookie=test merge_strategy=OVERWRITE id=0.0 +- description: cookie=test merge_strategy=OVERWRITE id=1.0 selector: tenant: test_tenant config: @@ -498,6 +502,7 @@ selector_config: Y_UNIT_TEST(SimpleNodeTenant) { ::google::protobuf::RepeatedPtrField<NKikimrConsole::TConfigItem> items; NKikimrConsole::TConfigItem *configItem = items.Add(); + configItem->MutableId()->SetId(1); configItem->SetMergeStrategy(1); configItem->MutableUsageScope()->MutableTenantAndNodeTypeFilter()->SetTenant("test_tenant"); configItem->MutableUsageScope()->MutableTenantAndNodeTypeFilter()->SetNodeType("test_node_type"); @@ -518,7 +523,7 @@ allowed_labels: tenant: type: string selector_config: -- description: cookie=test merge_strategy=OVERWRITE id=0.0 +- description: cookie=test merge_strategy=OVERWRITE id=1.0 selector: node_type: test_node_type tenant: test_tenant @@ -534,6 +539,7 @@ selector_config: Y_UNIT_TEST(SimpleHostId) { ::google::protobuf::RepeatedPtrField<NKikimrConsole::TConfigItem> items; NKikimrConsole::TConfigItem *configItem = items.Add(); + configItem->MutableId()->SetId(1); configItem->SetMergeStrategy(1); configItem->MutableUsageScope()->MutableHostFilter()->AddHosts("test_host_1"); configItem->SetOrder(21); @@ -553,7 +559,7 @@ allowed_labels: tenant: type: string selector_config: -- description: cookie=test merge_strategy=OVERWRITE id=0.0 +- description: cookie=test merge_strategy=OVERWRITE id=1.0 selector: host: test_host_1 config: @@ -576,7 +582,7 @@ allowed_labels: tenant: type: string selector_config: -- description: cookie=test merge_strategy=OVERWRITE id=0.0 +- description: cookie=test merge_strategy=OVERWRITE id=1.0 selector: host: in: @@ -594,6 +600,7 @@ selector_config: Y_UNIT_TEST(SimpleNodeId) { ::google::protobuf::RepeatedPtrField<NKikimrConsole::TConfigItem> items; NKikimrConsole::TConfigItem *configItem = items.Add(); + configItem->MutableId()->SetId(1); configItem->SetMergeStrategy(1); configItem->MutableUsageScope()->MutableNodeFilter()->AddNodes(1); configItem->SetOrder(21); @@ -613,7 +620,7 @@ allowed_labels: tenant: type: string selector_config: -- description: cookie=test merge_strategy=OVERWRITE id=0.0 +- description: cookie=test merge_strategy=OVERWRITE id=1.0 selector: node_id: 1 config: @@ -636,7 +643,7 @@ allowed_labels: tenant: type: string selector_config: -- description: cookie=test merge_strategy=OVERWRITE id=0.0 +- description: cookie=test merge_strategy=OVERWRITE id=1.0 selector: node_id: in: diff --git a/ydb/core/driver_lib/cli_utils/cli_cmds_console.cpp b/ydb/core/driver_lib/cli_utils/cli_cmds_console.cpp index 3e75e90895..f4d0a0315b 100644 --- a/ydb/core/driver_lib/cli_utils/cli_cmds_console.cpp +++ b/ydb/core/driver_lib/cli_utils/cli_cmds_console.cpp @@ -346,6 +346,76 @@ public: } }; +class TClientCommandConvertToYaml: public TClientCommandConfig { + NKikimrConsole::TConfigureRequest Request; +public: + TClientCommandConvertToYaml() + : TClientCommandConfig("convert-to-yaml", {}, "Convert config-item to yaml format") + { + } + + void Config(TConfig &config) override + { + TClientCommand::Config(config); + config.SetFreeArgsNum(1); + SetFreeArgTitle(0, "<CONFIGURE-PROTO>", "Console configure request protobuf or file with protobuf"); + } + + void Parse(TConfig& config) override + { + TClientCommand::Parse(config); + ParseProtobuf(&Request, config.ParseResult->GetFreeArgs()[0]); + } + + int Run(TConfig &) override + { + bool domain = false; + + TStringStream result; + TStringStream warnings; + + for (auto& action : Request.GetActions()) { + if (action.HasModifyConfigItem()) { + Cerr << "Error: Modify items are not supported" << Endl; + return 1; + } + + if (action.HasRemoveConfigItem()) { + warnings << "Warning: this config contains remove, you should remove entry with id=" + << action.GetRemoveConfigItem().GetConfigItemId().GetId() << "." << action.GetRemoveConfigItem().GetConfigItemId().GetGeneration() + << " by hand" << Endl; + continue; + } + + if (action.HasRemoveConfigItems()) { + warnings << "Warning: this config contains remove, you should remove entry with cookie="; + for (auto& cookie : action.GetRemoveConfigItems().GetCookieFilter().GetCookies()) { + warnings << "\"" << cookie << "\","; + } + warnings << " by hand" << Endl; + continue; + } + + auto add = action.GetAddConfigItem().GetConfigItem(); + + auto [hasDomain, cfg] = NYamlConfig::DumpConsoleConfigItem(add); + + domain |= hasDomain; + + result << cfg << Endl; + } + + if (domain) { + warnings << "Warning: this config contains domain config item, it should be merged by hand or inserted before scoped selectors" << Endl; + } + + Cerr << warnings.Str(); + Cout << result.Str(); + + return 0; + } +}; + class TClientCommandConsoleConfigs : public TClientCommandTree { public: TClientCommandConsoleConfigs() @@ -353,6 +423,7 @@ public: { AddCommand(std::make_unique<TClientCommandConsoleConfigsLoad>()); AddCommand(std::make_unique<TClientCommandConsoleConfigsDumpYaml>()); + AddCommand(std::make_unique<TClientCommandConvertToYaml>()); AddCommand(std::make_unique<TClientCommandConsoleConfigsUpdate>()); } }; |