Skip to content

Commit

Permalink
Add labels to manifest and started docker containers
Browse files Browse the repository at this point in the history
  • Loading branch information
sulksamino committed Apr 19, 2024
1 parent 317572d commit 51b5912
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 21 deletions.
4 changes: 4 additions & 0 deletions flecs/common/app/manifest/manifest.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class app_manifest_t
using startup_options_t = std::vector<startup_option_t>;
using ports_t = std::vector<mapped_port_range_t>;
using volumes_t = std::vector<volume_t>;
using labels_t = std::set<mapped_label_var_t>;

app_manifest_t();

Expand Down Expand Up @@ -73,6 +74,8 @@ class app_manifest_t
auto& version() const noexcept { return _version; }
auto& volumes() noexcept { return _volumes; }
auto& volumes() const noexcept { return _volumes; }
auto& labels() noexcept { return _labels; }
auto& labels() const noexcept { return _labels; }

private:
friend auto to_json(json_t& json, const app_manifest_t& app_manifest) //
Expand Down Expand Up @@ -101,6 +104,7 @@ class app_manifest_t
startup_options_t _startup_options;
std::string _version;
volumes_t _volumes;
labels_t _labels;
};

} // namespace flecs
52 changes: 33 additions & 19 deletions flecs/common/app/manifest/src/manifest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ app_manifest_t::app_manifest_t()
, _ports{}
, _version{}
, _volumes{}
, _labels{}
{}

app_manifest_t app_manifest_t::from_json(const json_t& json)
Expand Down Expand Up @@ -157,6 +158,16 @@ void app_manifest_t::parse_yaml(const yaml_t& yaml)
}
}

OPTIONAL_YAML_NODE(yaml, labels, labels);
for (const auto& label : labels) {
auto parse_result = mapped_label_var_t::try_parse(label.as<std::string>());
if (parse_result.has_value()) {
_labels.emplace(parse_result.value());
} else {
error_found = true;
}
}

OPTIONAL_TYPED_YAML_VALUE(yaml, hostname, _hostname);
REQUIRED_TYPED_YAML_VALUE(yaml, image, _image);
OPTIONAL_TYPED_YAML_VALUE(yaml, interactive, _interactive);
Expand Down Expand Up @@ -237,25 +248,27 @@ void app_manifest_t::validate()
auto to_json(json_t& json, const app_manifest_t& app_manifest) //
-> void
{
json = json_t(
{{"app", app_manifest._app},
{"version", app_manifest._version},
{"image", app_manifest._image},

{"multiInstance", app_manifest._multi_instance},
{"editor", app_manifest._editor},

{"args", app_manifest._args},
{"capabilities", app_manifest._capabilities},
{"conffiles", app_manifest._conffiles},
{"devices", app_manifest._devices},
{"env", app_manifest._env},
{"hostname", app_manifest._hostname},
{"interactive", app_manifest._interactive},
{"networks", app_manifest._networks},
{"ports", app_manifest._ports},
{"startupOptions", app_manifest._startup_options},
{"volumes", app_manifest._volumes}});
json = json_t({
{"app", app_manifest._app},
{"version", app_manifest._version},
{"image", app_manifest._image},

{"multiInstance", app_manifest._multi_instance},
{"editor", app_manifest._editor},

{"args", app_manifest._args},
{"capabilities", app_manifest._capabilities},
{"conffiles", app_manifest._conffiles},
{"devices", app_manifest._devices},
{"env", app_manifest._env},
{"hostname", app_manifest._hostname},
{"interactive", app_manifest._interactive},
{"networks", app_manifest._networks},
{"ports", app_manifest._ports},
{"startupOptions", app_manifest._startup_options},
{"volumes", app_manifest._volumes},
{"labels", app_manifest._labels},
});
}

auto from_json(const json_t& json, app_manifest_t& app_manifest) //
Expand All @@ -279,6 +292,7 @@ auto from_json(const json_t& json, app_manifest_t& app_manifest) //
OPTIONAL_JSON_VALUE(json, ports, app_manifest._ports);
OPTIONAL_JSON_VALUE(json, startupOptions, app_manifest._startup_options);
OPTIONAL_JSON_VALUE(json, volumes, app_manifest._volumes);
OPTIONAL_JSON_VALUE(json, labels, app_manifest._labels);

app_manifest.validate();
}
Expand Down
20 changes: 18 additions & 2 deletions flecs/common/app/manifest/test/test_app_manifest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ class manifest_writer_t
std::string _filename;
};

#define G_YAML_INDENT " "
#define G_YAML_ITEM(I) "- " I "\n"
#define G_YAML_KEY(KEY) KEY ":\n"
#define G_YAML_KEY_VALUE(KEY, VALUE) KEY ": " VALUE "\n"
#define G_YAML_MAPPING(FROM, TO) "- " FROM ":" TO "\n"
#define G_YAML_INDENT " "
#define G_YAML_MAPPING_EQ(FROM, TO) "- " FROM "=" TO "\n"
#define G_YAML_MAPPING_QUOTED_EQ(FROM, TO) "- \"" FROM "=" TO "\"\n"
#define G_YAML_MAPPING_SINGLE_QUOTED_EQ(FROM, TO) "- '" FROM "=" TO "'\n"

#define G_APP "tech.flecs.test-app"
#define G_ARG_1 "--launch-arg1"
Expand Down Expand Up @@ -108,6 +111,17 @@ class manifest_writer_t
G_YAML_INDENT G_YAML_MAPPING(G_VOLUME_LOCAL_1, G_VOLUME_CONTAINER_1) \
G_YAML_INDENT G_YAML_MAPPING(G_VOLUME_LOCAL_2, G_VOLUME_CONTAINER_2) \
G_YAML_INDENT G_YAML_MAPPING(G_VOLUME_LOCAL_3, G_VOLUME_CONTAINER_3)
#define G_LABEL_NAME_1 "empty"
#define G_LABEL_NAME_2 "with-whitespace"
#define G_LABEL_NAME_3 "some.json"
#define G_LABEL_VALUE_1 ""
#define G_LABEL_VALUE_2 "some\\tvalue with\\nwhitespace"
#define G_LABEL_VALUE_3 "{\\\"varname\\\": 123}"
#define G_LABELS \
G_YAML_KEY("labels") \
G_YAML_INDENT G_YAML_MAPPING_EQ(G_LABEL_NAME_1, G_LABEL_VALUE_1) \
G_YAML_INDENT G_YAML_MAPPING_QUOTED_EQ(G_LABEL_NAME_2, G_LABEL_VALUE_2) \
G_YAML_INDENT G_YAML_MAPPING_QUOTED_EQ(G_LABEL_NAME_3, G_LABEL_VALUE_3)

auto manifest_header() //
-> std::string
Expand All @@ -132,6 +146,7 @@ auto extend_manifest(std::string& yaml) //
yaml.append(G_PORTS);
yaml.append(G_STARTUP_OPTIONS);
yaml.append(G_VOLUMES);
yaml.append(G_LABELS);
}

TEST(daemon_app, minimal_app)
Expand Down Expand Up @@ -242,7 +257,8 @@ TEST(daemon_app, to_json)
R"-("networks":[{"mac_address":"","name":"flecs","parent":"","type":"bridge"}],)-"
R"-("ports":["1234:1234","8000-8005:10000-10005"],)-"
R"-("startupOptions":[1],)-"
R"-("volumes":["var:/var/","etc:/etc/","/home/app1/dir:/home/"]})-";
R"-("volumes":["var:/var/","etc:/etc/","/home/app1/dir:/home/"],)-"
R"-("labels":["empty=","some.json={\"varname\": 123}","with-whitespace=some\tvalue with\nwhitespace"]})-";

ASSERT_EQ(json.dump(), json_expected);
}
5 changes: 5 additions & 0 deletions flecs/modules/deployments/src/types/deployment_docker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ auto docker_t::create_container(std::shared_ptr<instances::instance_t> instance)
docker_process.arg(device);
}

for (const auto& label : manifest->labels()) {
docker_process.arg("--label");
docker_process.arg(to_string(label));
}

if (!instance->networks().empty()) {
auto& network = instance->networks()[0];

Expand Down

0 comments on commit 51b5912

Please # to comment.