TODO(idownes): Move this to the Containerizer interface to complete the delegation of containerization.
| 64 | // TODO(idownes): Move this to the Containerizer interface to complete |
| 65 | // the delegation of containerization. |
| 66 | Try<Resources> Containerizer::resources(const Flags& flags) |
| 67 | { |
| 68 | Try<Resources> parsed = Resources::parse( |
| 69 | flags.resources.getOrElse(""), flags.default_role); |
| 70 | |
| 71 | if (parsed.isError()) { |
| 72 | return Error(parsed.error()); |
| 73 | } |
| 74 | |
| 75 | Resources resources = parsed.get(); |
| 76 | |
| 77 | // NOTE: We need to check for the "cpus" resource within the flags |
| 78 | // because once the `Resources` object is created, we cannot distinguish |
| 79 | // between |
| 80 | // (1) "cpus:0", and |
| 81 | // (2) no cpus specified. |
| 82 | // due to `Resources:add` discarding empty resources. |
| 83 | // We only auto-detect cpus in case (2). |
| 84 | // The same logic applies for the other resources! |
| 85 | // `Resources::fromString().get()` is safe because `Resources::parse()` above |
| 86 | // is valid. |
| 87 | vector<Resource> resourceList = Resources::fromString( |
| 88 | flags.resources.getOrElse(""), flags.default_role).get(); |
| 89 | |
| 90 | bool hasCpus = false; |
| 91 | bool hasMem = false; |
| 92 | bool hasDisk = false; |
| 93 | bool hasPorts = false; |
| 94 | |
| 95 | foreach (const Resource& resource, resourceList) { |
| 96 | if (resource.name() == "cpus") { |
| 97 | hasCpus = true; |
| 98 | } else if (resource.name() == "mem") { |
| 99 | hasMem = true; |
| 100 | } else if (resource.name() == "disk") { |
| 101 | hasDisk = true; |
| 102 | } else if (resource.name() == "ports") { |
| 103 | hasPorts = true; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (!hasCpus) { |
| 108 | // No CPU specified so probe OS or resort to DEFAULT_CPUS. |
| 109 | double cpus; |
| 110 | Try<long> cpus_ = os::cpus(); |
| 111 | if (!cpus_.isSome()) { |
| 112 | LOG(WARNING) << "Failed to auto-detect the number of cpus to use: '" |
| 113 | << cpus_.error() |
| 114 | << "'; defaulting to " << DEFAULT_CPUS; |
| 115 | cpus = DEFAULT_CPUS; |
| 116 | } else { |
| 117 | cpus = cpus_.get(); |
| 118 | } |
| 119 | |
| 120 | resources += Resources::parse( |
| 121 | "cpus", |
| 122 | stringify(cpus), |
| 123 | flags.default_role).get(); |