Skip to content

Commit 1b65ee8

Browse files
authored
feat: add udf for returning a namespace ID from a namespace name (#1712)
1 parent f974c70 commit 1b65ee8

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

src/carnot/funcs/metadata/metadata_ops.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ void RegisterMetadataOpsOrDie(px::carnot::udf::Registry* registry) {
131131
registry->RegisterOrDie<VizierNameUDF>("vizier_name");
132132
registry->RegisterOrDie<VizierNamespaceUDF>("vizier_namespace");
133133
registry->RegisterOrDie<GetClusterCIDRRangeUDF>("get_cidrs");
134+
registry->RegisterOrDie<NamespaceNameToNamespaceIDUDF>("namespace_name_to_namespace_id");
134135

135136
/*****************************************
136137
* Aggregate UDFs.

src/carnot/funcs/metadata/metadata_ops.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,6 +3262,24 @@ class GetClusterCIDRRangeUDF : public udf::ScalarUDF {
32623262
std::string cidrs_str_;
32633263
};
32643264

3265+
class NamespaceNameToNamespaceIDUDF : public ScalarUDF {
3266+
public:
3267+
StringValue Exec(FunctionContext* ctx, StringValue namespace_name) {
3268+
auto md = GetMetadataState(ctx);
3269+
auto namespace_id =
3270+
md->k8s_metadata_state().NamespaceIDByName(std::make_pair(namespace_name, namespace_name));
3271+
return namespace_id;
3272+
}
3273+
3274+
static udf::ScalarUDFDocBuilder Doc() {
3275+
return udf::ScalarUDFDocBuilder("Get the Kubernetes UID of the given namespace name.")
3276+
.Details("Get the Kubernetes UID of the given namespace name.")
3277+
.Example("df.kube_system_namespace_uid = px.namespace_name_to_namespace_id('kube-system')")
3278+
.Arg("arg1", "The name of the namespace to get the UID of.")
3279+
.Returns("The Kubernetes UID of the given namespace name");
3280+
}
3281+
};
3282+
32653283
void RegisterMetadataOpsOrDie(px::carnot::udf::Registry* registry);
32663284

32673285
} // namespace metadata

src/carnot/funcs/metadata/metadata_ops_test.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ class MetadataOpsTest : public ::testing::Test {
6767
updates_->enqueue(px::metadatapb::testutils::CreateRunningServiceIPUpdatePB());
6868
updates_->enqueue(px::metadatapb::testutils::CreateRunningReplicaSetUpdatePB());
6969
updates_->enqueue(px::metadatapb::testutils::CreateRunningDeploymentUpdatePB());
70+
updates_->enqueue(px::metadatapb::testutils::CreateRunningNamespaceUpdatePB());
7071
updates_->enqueue(px::metadatapb::testutils::CreateTerminatingContainerUpdatePB());
7172
updates_->enqueue(px::metadatapb::testutils::CreateTerminatingPodUpdatePB());
7273
updates_->enqueue(px::metadatapb::testutils::CreateTerminatingServiceUpdatePB());
7374
updates_->enqueue(px::metadatapb::testutils::CreateTerminatingReplicaSetUpdatePB());
7475
updates_->enqueue(px::metadatapb::testutils::CreateTerminatingDeploymentUpdatePB());
76+
updates_->enqueue(px::metadatapb::testutils::CreateTerminatingNamespaceUpdatePB());
7577

7678
auto s = px::md::ApplyK8sUpdates(10, metadata_state_.get(), &md_filter_, updates_.get());
7779

@@ -1347,6 +1349,19 @@ TEST_F(MetadataOpsTest, get_cidrs) {
13471349
absl::Substitute(R"(["$0","$1","$2"])", "10.0.0.1/32", pod_cidr_str, service_cidr_str));
13481350
}
13491351

1352+
TEST_F(MetadataOpsTest, namespace_name_to_namespace_id_test) {
1353+
auto function_ctx = std::make_unique<FunctionContext>(metadata_state_, nullptr);
1354+
auto udf_tester =
1355+
px::carnot::udf::UDFTester<NamespaceNameToNamespaceIDUDF>(std::move(function_ctx));
1356+
udf_tester.ForInput("namespace1").Expect("namespace_uid");
1357+
udf_tester.ForInput("terminating_namespace1").Expect("terminating_namespace_uid");
1358+
udf_tester.ForInput("badformat").Expect("");
1359+
updates_->enqueue(px::metadatapb::testutils::CreateTerminatedNamespaceUpdatePB());
1360+
// keep information about the namespace after termination
1361+
EXPECT_OK(px::md::ApplyK8sUpdates(11, metadata_state_.get(), &md_filter_, updates_.get()));
1362+
udf_tester.ForInput("terminating_namespace1").Expect("terminating_namespace_uid");
1363+
}
1364+
13501365
} // namespace metadata
13511366
} // namespace funcs
13521367
} // namespace carnot

src/shared/k8s/metadatapb/test_proto.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,30 @@ conditions: {
362362
}
363363
)";
364364

365+
/*
366+
* Templates for namespace updates.
367+
*/
368+
const char* kRunningNamespaceUpdatePbTxt = R"(
369+
uid: "namespace_uid"
370+
name: "namespace1"
371+
start_timestamp_ns: 101
372+
stop_timestamp_ns: 0
373+
)";
374+
375+
const char* kTerminatingNamespaceUpdatePbTxt = R"(
376+
uid: "terminating_namespace_uid"
377+
name: "terminating_namespace1"
378+
start_timestamp_ns: 123
379+
stop_timestamp_ns: 0
380+
)";
381+
382+
const char* kTerminatedNamespaceUpdatePbTxt = R"(
383+
uid: "terminating_namespace_uid"
384+
name: "terminating_namespace1"
385+
start_timestamp_ns: 123
386+
stop_timestamp_ns: 150
387+
)";
388+
365389
std::unique_ptr<px::shared::k8s::metadatapb::ResourceUpdate> CreateRunningPodUpdatePB() {
366390
auto update = std::make_unique<px::shared::k8s::metadatapb::ResourceUpdate>();
367391
auto update_proto = absl::Substitute(kResourceUpdateTmpl, "pod_update", kRunningPodUpdatePbTxt);
@@ -530,6 +554,33 @@ std::unique_ptr<px::shared::k8s::metadatapb::ResourceUpdate> CreateTerminatedDep
530554
return update;
531555
}
532556

557+
std::unique_ptr<px::shared::k8s::metadatapb::ResourceUpdate> CreateRunningNamespaceUpdatePB() {
558+
auto update = std::make_unique<px::shared::k8s::metadatapb::ResourceUpdate>();
559+
auto update_proto =
560+
absl::Substitute(kResourceUpdateTmpl, "namespace_update", kRunningNamespaceUpdatePbTxt);
561+
CHECK(google::protobuf::TextFormat::MergeFromString(update_proto, update.get()))
562+
<< "Failed to parse proto";
563+
return update;
564+
}
565+
566+
std::unique_ptr<px::shared::k8s::metadatapb::ResourceUpdate> CreateTerminatingNamespaceUpdatePB() {
567+
auto update = std::make_unique<px::shared::k8s::metadatapb::ResourceUpdate>();
568+
auto update_proto =
569+
absl::Substitute(kResourceUpdateTmpl, "namespace_update", kTerminatingNamespaceUpdatePbTxt);
570+
CHECK(google::protobuf::TextFormat::MergeFromString(update_proto, update.get()))
571+
<< "Failed to parse proto";
572+
return update;
573+
}
574+
575+
std::unique_ptr<px::shared::k8s::metadatapb::ResourceUpdate> CreateTerminatedNamespaceUpdatePB() {
576+
auto update = std::make_unique<px::shared::k8s::metadatapb::ResourceUpdate>();
577+
auto update_proto =
578+
absl::Substitute(kResourceUpdateTmpl, "namespace_update", kTerminatedNamespaceUpdatePbTxt);
579+
CHECK(google::protobuf::TextFormat::MergeFromString(update_proto, update.get()))
580+
<< "Failed to parse proto";
581+
return update;
582+
}
583+
533584
} // namespace testutils
534585
} // namespace metadatapb
535586
} // namespace px

0 commit comments

Comments
 (0)