From 141e028e495c0b863c8573c3a4f53c8f529e0bf9 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Tue, 21 Apr 2026 23:19:36 +0200 Subject: [PATCH 01/24] update license Signed-off-by: GnomeZworc --- LICENSE | 229 +++++++++++++++++++++++--------------------------------- 1 file changed, 95 insertions(+), 134 deletions(-) diff --git a/LICENSE b/LICENSE index baee873..1a71cb6 100644 --- a/LICENSE +++ b/LICENSE @@ -1,170 +1,131 @@ -Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International +# PolyForm Noncommercial License 1.0.0 - Creative Commons Corporation (“Creative Commons”) is not a law firm and does not provide legal services or legal advice. Distribution of Creative Commons public licenses does not create a lawyer-client or other relationship. Creative Commons makes its licenses and related information available on an “as-is” basis. Creative Commons gives no warranties regarding its licenses, any material licensed under their terms and conditions, or any related information. Creative Commons disclaims all liability for damages resulting from their use to the fullest extent possible. + -Using Creative Commons Public Licenses +## Acceptance -Creative Commons public licenses provide a standard set of terms and conditions that creators and other rights holders may use to share original works of authorship and other material subject to copyright and certain other rights specified in the public license below. The following considerations are for informational purposes only, are not exhaustive, and do not form part of our licenses. +In order to get any license under these terms, you must agree +to them as both strict obligations and conditions to all +your licenses. -Considerations for licensors: Our public licenses are intended for use by those authorized to give the public permission to use material in ways otherwise restricted by copyright and certain other rights. Our licenses are irrevocable. Licensors should read and understand the terms and conditions of the license they choose before applying it. Licensors should also secure all rights necessary before applying our licenses so that the public can reuse the material as expected. Licensors should clearly mark any material not subject to the license. This includes other CC-licensed material, or material used under an exception or limitation to copyright. More considerations for licensors. +## Copyright License -Considerations for the public: By using one of our public licenses, a licensor grants the public permission to use the licensed material under specified terms and conditions. If the licensor’s permission is not necessary for any reason–for example, because of any applicable exception or limitation to copyright–then that use is not regulated by the license. Our licenses grant only permissions under copyright and certain other rights that a licensor has authority to grant. Use of the licensed material may still be restricted for other reasons, including because others have copyright or other rights in the material. A licensor may make special requests, such as asking that all changes be marked or described. Although not required by our licenses, you are encouraged to respect those requests where reasonable. More considerations for the public. +The licensor grants you a copyright license for the +software to do everything you might do with the software +that would otherwise infringe the licensor's copyright +in it for any permitted purpose. However, you may +only distribute the software according to [Distribution +License](#distribution-license) and make changes or new works +based on the software according to [Changes and New Works +License](#changes-and-new-works-license). -Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License +## Distribution License -By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and conditions of this Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License ("Public License"). To the extent this Public License may be interpreted as a contract, You are granted the Licensed Rights in consideration of Your acceptance of these terms and conditions, and the Licensor grants You such rights in consideration of benefits the Licensor receives from making the Licensed Material available under these terms and conditions. +The licensor grants you an additional copyright license +to distribute copies of the software. Your license +to distribute covers distributing the software with +changes and new works permitted by [Changes and New Works +License](#changes-and-new-works-license). -Section 1 – Definitions. +## Notices - a. Adapted Material means material subject to Copyright and Similar Rights that is derived from or based upon the Licensed Material and in which the Licensed Material is translated, altered, arranged, transformed, or otherwise modified in a manner requiring permission under the Copyright and Similar Rights held by the Licensor. For purposes of this Public License, where the Licensed Material is a musical work, performance, or sound recording, Adapted Material is always produced where the Licensed Material is synched in timed relation with a moving image. +You must ensure that anyone who gets a copy of any part of +the software from you also gets a copy of these terms or the +URL for them above, as well as copies of any plain-text lines +beginning with `Required Notice:` that the licensor provided +with the software. For example: - b. Adapter's License means the license You apply to Your Copyright and Similar Rights in Your contributions to Adapted Material in accordance with the terms and conditions of this Public License. +> Required Notice: Copyright Yoyodyne, Inc. (http://example.com) - c. BY-NC-SA Compatible License means a license listed at creativecommons.org/compatiblelicenses, approved by Creative Commons as essentially the equivalent of this Public License. +## Changes and New Works License - d. Copyright and Similar Rights means copyright and/or similar rights closely related to copyright including, without limitation, performance, broadcast, sound recording, and Sui Generis Database Rights, without regard to how the rights are labeled or categorized. For purposes of this Public License, the rights specified in Section 2(b)(1)-(2) are not Copyright and Similar Rights. +The licensor grants you an additional copyright license to +make changes and new works based on the software for any +permitted purpose. - e. Effective Technological Measures means those measures that, in the absence of proper authority, may not be circumvented under laws fulfilling obligations under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, and/or similar international agreements. +## Patent License - f. Exceptions and Limitations means fair use, fair dealing, and/or any other exception or limitation to Copyright and Similar Rights that applies to Your use of the Licensed Material. +The licensor grants you a patent license for the software that +covers patent claims the licensor can license, or becomes able +to license, that you would infringe by using the software. - g. License Elements means the license attributes listed in the name of a Creative Commons Public License. The License Elements of this Public License are Attribution, NonCommercial, and ShareAlike. +## Noncommercial Purposes - h. Licensed Material means the artistic or literary work, database, or other material to which the Licensor applied this Public License. +Any noncommercial purpose is a permitted purpose. - i. Licensed Rights means the rights granted to You subject to the terms and conditions of this Public License, which are limited to all Copyright and Similar Rights that apply to Your use of the Licensed Material and that the Licensor has authority to license. +## Personal Uses - j. Licensor means the individual(s) or entity(ies) granting rights under this Public License. +Personal use for research, experiment, and testing for +the benefit of public knowledge, personal study, private +entertainment, hobby projects, amateur pursuits, or religious +observance, without any anticipated commercial application, +is use for a permitted purpose. - k. NonCommercial means not primarily intended for or directed towards commercial advantage or monetary compensation. For purposes of this Public License, the exchange of the Licensed Material for other material subject to Copyright and Similar Rights by digital file-sharing or similar means is NonCommercial provided there is no payment of monetary compensation in connection with the exchange. +## Noncommercial Organizations - l. Share means to provide material to the public by any means or process that requires permission under the Licensed Rights, such as reproduction, public display, public performance, distribution, dissemination, communication, or importation, and to make material available to the public including in ways that members of the public may access the material from a place and at a time individually chosen by them. +Use by any charitable organization, educational institution, +public research organization, public safety or health +organization, environmental protection organization, +or government institution is use for a permitted purpose +regardless of the source of funding or obligations resulting +from the funding. - m. Sui Generis Database Rights means rights other than copyright resulting from Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, as amended and/or succeeded, as well as other essentially equivalent rights anywhere in the world. +## Fair Use - n. You means the individual or entity exercising the Licensed Rights under this Public License. Your has a corresponding meaning. +You may have "fair use" rights for the software under the +law. These terms do not limit them. -Section 2 – Scope. +## No Other Rights - a. License grant. +These terms do not allow you to sublicense or transfer any of +your licenses to anyone else, or prevent the licensor from +granting licenses to anyone else. These terms do not imply +any other licenses. - 1. Subject to the terms and conditions of this Public License, the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licensed Rights in the Licensed Material to: +## Patent Defense - A. reproduce and Share the Licensed Material, in whole or in part, for NonCommercial purposes only; and +If you make any written claim that the software infringes or +contributes to infringement of any patent, your patent license +for the software granted under these terms ends immediately. If +your company makes such a claim, your patent license ends +immediately for work on behalf of your company. - B. produce, reproduce, and Share Adapted Material for NonCommercial purposes only. +## Violations - 2. Exceptions and Limitations. For the avoidance of doubt, where Exceptions and Limitations apply to Your use, this Public License does not apply, and You do not need to comply with its terms and conditions. +The first time you are notified in writing that you have +violated any of these terms, or done anything with the software +not covered by your licenses, your licenses can nonetheless +continue if you come into full compliance with these terms, +and take practical steps to correct past violations, within +32 days of receiving notice. Otherwise, all your licenses +end immediately. - 3. Term. The term of this Public License is specified in Section 6(a). +## No Liability - 4. Media and formats; technical modifications allowed. The Licensor authorizes You to exercise the Licensed Rights in all media and formats whether now known or hereafter created, and to make technical modifications necessary to do so. The Licensor waives and/or agrees not to assert any right or authority to forbid You from making technical modifications necessary to exercise the Licensed Rights, including technical modifications necessary to circumvent Effective Technological Measures. For purposes of this Public License, simply making modifications authorized by this Section 2(a)(4) never produces Adapted Material. +***As far as the law allows, the software comes as is, without +any warranty or condition, and the licensor will not be liable +to you for any damages arising out of these terms or the use +or nature of the software, under any kind of legal claim.*** - 5. Downstream recipients. +## Definitions - A. Offer from the Licensor – Licensed Material. Every recipient of the Licensed Material automatically receives an offer from the Licensor to exercise the Licensed Rights under the terms and conditions of this Public License. +The **licensor** is the individual or entity offering these +terms, and the **software** is the software the licensor makes +available under these terms. - B. Additional offer from the Licensor – Adapted Material. Every recipient of Adapted Material from You automatically receives an offer from the Licensor to exercise the Licensed Rights in the Adapted Material under the conditions of the Adapter’s License You apply. +**You** refers to the individual or entity agreeing to these +terms. - C. No downstream restrictions. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Licensed Material if doing so restricts exercise of the Licensed Rights by any recipient of the Licensed Material. +**Your company** is any legal entity, sole proprietorship, +or other kind of organization that you work for, plus all +organizations that have control over, are under the control of, +or are under common control with that organization. **Control** +means ownership of substantially all the assets of an entity, +or the power to direct its management and policies by vote, +contract, or otherwise. Control can be direct or indirect. - 6. No endorsement. Nothing in this Public License constitutes or may be construed as permission to assert or imply that You are, or that Your use of the Licensed Material is, connected with, or sponsored, endorsed, or granted official status by, the Licensor or others designated to receive attribution as provided in Section 3(a)(1)(A)(i). +**Your licenses** are all the licenses granted to you for the +software under these terms. - b. Other rights. - - 1. Moral rights, such as the right of integrity, are not licensed under this Public License, nor are publicity, privacy, and/or other similar personality rights; however, to the extent possible, the Licensor waives and/or agrees not to assert any such rights held by the Licensor to the limited extent necessary to allow You to exercise the Licensed Rights, but not otherwise. - - 2. Patent and trademark rights are not licensed under this Public License. - - 3. To the extent possible, the Licensor waives any right to collect royalties from You for the exercise of the Licensed Rights, whether directly or through a collecting society under any voluntary or waivable statutory or compulsory licensing scheme. In all other cases the Licensor expressly reserves any right to collect such royalties, including when the Licensed Material is used other than for NonCommercial purposes. - -Section 3 – License Conditions. - -Your exercise of the Licensed Rights is expressly made subject to the following conditions. - - a. Attribution. - - 1. If You Share the Licensed Material (including in modified form), You must: - - A. retain the following if it is supplied by the Licensor with the Licensed Material: - - i. identification of the creator(s) of the Licensed Material and any others designated to receive attribution, in any reasonable manner requested by the Licensor (including by pseudonym if designated); - - ii. a copyright notice; - - iii. a notice that refers to this Public License; - - iv. a notice that refers to the disclaimer of warranties; - - v. a URI or hyperlink to the Licensed Material to the extent reasonably practicable; - - B. indicate if You modified the Licensed Material and retain an indication of any previous modifications; and - - C. indicate the Licensed Material is licensed under this Public License, and include the text of, or the URI or hyperlink to, this Public License. - - 2. You may satisfy the conditions in Section 3(a)(1) in any reasonable manner based on the medium, means, and context in which You Share the Licensed Material. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information. - - 3. If requested by the Licensor, You must remove any of the information required by Section 3(a)(1)(A) to the extent reasonably practicable. - - b. ShareAlike.In addition to the conditions in Section 3(a), if You Share Adapted Material You produce, the following conditions also apply. - - 1. The Adapter’s License You apply must be a Creative Commons license with the same License Elements, this version or later, or a BY-NC-SA Compatible License. - - 2. You must include the text of, or the URI or hyperlink to, the Adapter's License You apply. You may satisfy this condition in any reasonable manner based on the medium, means, and context in which You Share Adapted Material. - - 3. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, Adapted Material that restrict exercise of the rights granted under the Adapter's License You apply. - -Section 4 – Sui Generis Database Rights. - -Where the Licensed Rights include Sui Generis Database Rights that apply to Your use of the Licensed Material: - - a. for the avoidance of doubt, Section 2(a)(1) grants You the right to extract, reuse, reproduce, and Share all or a substantial portion of the contents of the database for NonCommercial purposes only; - - b. if You include all or a substantial portion of the database contents in a database in which You have Sui Generis Database Rights, then the database in which You have Sui Generis Database Rights (but not its individual contents) is Adapted Material, including for purposes of Section 3(b); and - - c. You must comply with the conditions in Section 3(a) if You Share all or a substantial portion of the contents of the database. -For the avoidance of doubt, this Section 4 supplements and does not replace Your obligations under this Public License where the Licensed Rights include other Copyright and Similar Rights. - -Section 5 – Disclaimer of Warranties and Limitation of Liability. - - a. Unless otherwise separately undertaken by the Licensor, to the extent possible, the Licensor offers the Licensed Material as-is and as-available, and makes no representations or warranties of any kind concerning the Licensed Material, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You. - - b. To the extent possible, in no event will the Licensor be liable to You on any legal theory (including, without limitation, negligence) or otherwise for any direct, special, indirect, incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages arising out of this Public License or use of the Licensed Material, even if the Licensor has been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation of liability is not allowed in full or in part, this limitation may not apply to You. - - c. The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability. - -Section 6 – Term and Termination. - - a. This Public License applies for the term of the Copyright and Similar Rights licensed here. However, if You fail to comply with this Public License, then Your rights under this Public License terminate automatically. - - b. Where Your right to use the Licensed Material has terminated under Section 6(a), it reinstates: - - 1. automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or - - 2. upon express reinstatement by the Licensor. - - For the avoidance of doubt, this Section 6(b) does not affect any right the Licensor may have to seek remedies for Your violations of this Public License. - - c. For the avoidance of doubt, the Licensor may also offer the Licensed Material under separate terms or conditions or stop distributing the Licensed Material at any time; however, doing so will not terminate this Public License. - - d. Sections 1, 5, 6, 7, and 8 survive termination of this Public License. - -Section 7 – Other Terms and Conditions. - - a. The Licensor shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed. - - b. Any arrangements, understandings, or agreements regarding the Licensed Material not stated herein are separate from and independent of the terms and conditions of this Public License. - -Section 8 – Interpretation. - - a. For the avoidance of doubt, this Public License does not, and shall not be interpreted to, reduce, limit, restrict, or impose conditions on any use of the Licensed Material that could lawfully be made without permission under this Public License. - - b. To the extent possible, if any provision of this Public License is deemed unenforceable, it shall be automatically reformed to the minimum extent necessary to make it enforceable. If the provision cannot be reformed, it shall be severed from this Public License without affecting the enforceability of the remaining terms and conditions. - - c. No term or condition of this Public License will be waived and no failure to comply consented to unless expressly agreed to by the Licensor. - - d. Nothing in this Public License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Licensor or You, including from the legal processes of any jurisdiction or authority. - -Creative Commons is not a party to its public licenses. Notwithstanding, Creative Commons may elect to apply one of its public licenses to material it publishes and in those instances will be considered the “Licensor.” Except for the limited purpose of indicating that material is shared under a Creative Commons public license or as otherwise permitted by the Creative Commons policies published at creativecommons.org/policies, Creative Commons does not authorize the use of the trademark “Creative Commons” or any other trademark or logo of Creative Commons without its prior written consent including, without limitation, in connection with any unauthorized modifications to any of its public licenses or any other arrangements, understandings, or agreements concerning use of licensed material. For the avoidance of doubt, this paragraph does not form part of the public licenses. - -Creative Commons may be contacted at creativecommons.org. +**Use** means anything you do with the software requiring one +of your licenses. From 565428b8de826cbacf8112da1697315537f8fe26 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Sun, 12 Apr 2026 17:29:14 +0200 Subject: [PATCH 02/24] f-21: api: add openapi file Signed-off-by: GnomeZworc --- api/openapi.yaml | 296 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 296 insertions(+) create mode 100644 api/openapi.yaml diff --git a/api/openapi.yaml b/api/openapi.yaml new file mode 100644 index 0000000..be328d0 --- /dev/null +++ b/api/openapi.yaml @@ -0,0 +1,296 @@ +openapi: "3.1.0" +info: + title: Two API + version: "0.1.0" + description: REST API for managing VPCs and Subnets in the Two orchestrator. + +servers: + - url: http://localhost:8080 + description: Local development server + +paths: + + # ── VPC ──────────────────────────────────────────────────────────────────── + + /vpcs: + get: + summary: List all VPCs + operationId: listVPCs + responses: + "200": + description: List of VPCs + content: + application/json: + schema: + type: array + items: + $ref: "#/components/schemas/VPC" + "500": + $ref: "#/components/responses/InternalError" + + post: + summary: Create a VPC + operationId: createVPC + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/VPCCreateRequest" + responses: + "202": + description: VPC creation accepted + content: + application/json: + schema: + $ref: "#/components/schemas/VPC" + "409": + description: VPC already exists + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + "500": + $ref: "#/components/responses/InternalError" + + /vpcs/{name}: + parameters: + - $ref: "#/components/parameters/ResourceName" + + get: + summary: Get VPC status and info + operationId: getVPC + responses: + "200": + description: VPC found + content: + application/json: + schema: + $ref: "#/components/schemas/VPC" + "404": + $ref: "#/components/responses/NotFound" + "500": + $ref: "#/components/responses/InternalError" + + delete: + summary: Delete a VPC + operationId: deleteVPC + responses: + "202": + description: VPC deletion accepted + content: + application/json: + schema: + $ref: "#/components/schemas/VPC" + "404": + $ref: "#/components/responses/NotFound" + "409": + description: VPC not in a deletable state + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + "500": + $ref: "#/components/responses/InternalError" + + # ── Subnet ───────────────────────────────────────────────────────────────── + + /subnets: + get: + summary: List all subnets + operationId: listSubnets + responses: + "200": + description: List of subnets + content: + application/json: + schema: + type: array + items: + $ref: "#/components/schemas/Subnet" + "500": + $ref: "#/components/responses/InternalError" + + post: + summary: Create a subnet + operationId: createSubnet + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/SubnetCreateRequest" + responses: + "202": + description: Subnet creation accepted + content: + application/json: + schema: + $ref: "#/components/schemas/Subnet" + "409": + description: Subnet already exists + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + "422": + description: Parent VPC does not exist or is not ready + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + "500": + $ref: "#/components/responses/InternalError" + + /subnets/{name}: + parameters: + - $ref: "#/components/parameters/ResourceName" + + get: + summary: Get subnet status and info + operationId: getSubnet + responses: + "200": + description: Subnet found + content: + application/json: + schema: + $ref: "#/components/schemas/Subnet" + "404": + $ref: "#/components/responses/NotFound" + "500": + $ref: "#/components/responses/InternalError" + + delete: + summary: Delete a subnet + operationId: deleteSubnet + responses: + "202": + description: Subnet deletion accepted + content: + application/json: + schema: + $ref: "#/components/schemas/Subnet" + "404": + $ref: "#/components/responses/NotFound" + "409": + description: Subnet not in a deletable state + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + "500": + $ref: "#/components/responses/InternalError" + +# ── Components ────────────────────────────────────────────────────────────── + +components: + + parameters: + ResourceName: + name: name + in: path + required: true + schema: + type: string + description: Resource name + + schemas: + + VPCCreateRequest: + type: object + required: [name] + properties: + name: + type: string + description: Unique name for the VPC + example: vpc1 + + VPC: + type: object + properties: + name: + type: string + example: vpc1 + state: + type: string + enum: [creating, created, deleting, deleted] + example: created + + SubnetCreateRequest: + type: object + required: [name, vpc, vxlan_id, local_ip, gateway_ip, cidr] + properties: + name: + type: string + description: Unique name for the subnet + example: sn-00001 + vpc: + type: string + description: Parent VPC name + example: vpc1 + vxlan_id: + type: integer + description: VXLAN VNI identifier + example: 100 + local_ip: + type: string + format: ipv4 + description: Local VTEP IP address + example: "10.0.0.5" + gateway_ip: + type: string + format: ipv4 + description: Gateway IP for the subnet + example: "10.10.10.1" + cidr: + type: string + description: Subnet CIDR block + example: "10.10.10.0/24" + + Subnet: + type: object + properties: + name: + type: string + example: sn-00001 + state: + type: string + enum: [creating, created, deleting, deleted] + example: created + vpc: + type: string + example: vpc1 + vxlan_id: + type: integer + example: 100 + local_ip: + type: string + example: "10.0.0.5" + gateway_ip: + type: string + example: "10.10.10.1" + cidr: + type: string + example: "10.10.10.0/24" + + Error: + type: object + properties: + error: + type: string + example: "resource not found" + + responses: + NotFound: + description: Resource not found + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + InternalError: + description: Internal server error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" From bcedeece18402140681352f96c94aab823ec0491 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Sun, 12 Apr 2026 17:29:37 +0200 Subject: [PATCH 03/24] f-21: add first api file Signed-off-by: GnomeZworc --- internal/api/agent/server.go | 16 ++++++++++++++++ internal/api/agent/subnet.go | 25 +++++++++++++++++++++++++ internal/api/agent/subnets.go | 19 +++++++++++++++++++ internal/api/agent/vpc.go | 25 +++++++++++++++++++++++++ internal/api/agent/vpcs.go | 19 +++++++++++++++++++ 5 files changed, 104 insertions(+) create mode 100644 internal/api/agent/server.go create mode 100644 internal/api/agent/subnet.go create mode 100644 internal/api/agent/subnets.go create mode 100644 internal/api/agent/vpc.go create mode 100644 internal/api/agent/vpcs.go diff --git a/internal/api/agent/server.go b/internal/api/agent/server.go new file mode 100644 index 0000000..05653a4 --- /dev/null +++ b/internal/api/agent/server.go @@ -0,0 +1,16 @@ +package agentapi + +import ( + "log" + "net/http" +) + +func Start(address string) { + mux := http.NewServeMux() + mux.HandleFunc("/vpcs", VpcsHandler) + mux.HandleFunc("/vpcs/", VpcByNameHandler) + mux.HandleFunc("/subnets", SubnetsHandler) + mux.HandleFunc("/subnets/", SubnetByNameHandler) + log.Printf("API server listening on %s", address) + log.Fatal(http.ListenAndServe(address, mux)) +} diff --git a/internal/api/agent/subnet.go b/internal/api/agent/subnet.go new file mode 100644 index 0000000..e46e4f4 --- /dev/null +++ b/internal/api/agent/subnet.go @@ -0,0 +1,25 @@ +package agentapi + +import ( + "encoding/json" + "net/http" + "strings" +) + +func SubnetByNameHandler(w http.ResponseWriter, r *http.Request) { + name := strings.TrimPrefix(r.URL.Path, "/subnets/") + if name == "" { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + switch r.Method { + case http.MethodGet: + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(map[string]string{"name": name}) + case http.MethodDelete: + w.WriteHeader(http.StatusAccepted) + default: + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + } +} diff --git a/internal/api/agent/subnets.go b/internal/api/agent/subnets.go new file mode 100644 index 0000000..53dce2e --- /dev/null +++ b/internal/api/agent/subnets.go @@ -0,0 +1,19 @@ +package agentapi + +import ( + "encoding/json" + "net/http" +) + +func SubnetsHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch r.Method { + case http.MethodGet: + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode([]interface{}{}) + case http.MethodPost: + w.WriteHeader(http.StatusAccepted) + default: + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + } +} diff --git a/internal/api/agent/vpc.go b/internal/api/agent/vpc.go new file mode 100644 index 0000000..aecfaa1 --- /dev/null +++ b/internal/api/agent/vpc.go @@ -0,0 +1,25 @@ +package agentapi + +import ( + "encoding/json" + "net/http" + "strings" +) + +func VpcByNameHandler(w http.ResponseWriter, r *http.Request) { + name := strings.TrimPrefix(r.URL.Path, "/vpcs/") + if name == "" { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + switch r.Method { + case http.MethodGet: + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(map[string]string{"name": name}) + case http.MethodDelete: + w.WriteHeader(http.StatusAccepted) + default: + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + } +} diff --git a/internal/api/agent/vpcs.go b/internal/api/agent/vpcs.go new file mode 100644 index 0000000..c4e9b27 --- /dev/null +++ b/internal/api/agent/vpcs.go @@ -0,0 +1,19 @@ +package agentapi + +import ( + "encoding/json" + "net/http" +) + +func VpcsHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch r.Method { + case http.MethodGet: + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode([]interface{}{}) + case http.MethodPost: + w.WriteHeader(http.StatusAccepted) + default: + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + } +} From d393b647ac82714ade4f1202ed5b74c119d9e1d2 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Sun, 12 Apr 2026 17:30:07 +0200 Subject: [PATCH 04/24] f-21: mon: add prometheus data Signed-off-by: GnomeZworc --- internal/prometheus/agent/collector.go | 67 ++++++++++++++++++++++++++ pkg/prometheus/server.go | 20 ++++++++ 2 files changed, 87 insertions(+) create mode 100644 internal/prometheus/agent/collector.go create mode 100644 pkg/prometheus/server.go diff --git a/internal/prometheus/agent/collector.go b/internal/prometheus/agent/collector.go new file mode 100644 index 0000000..04712ad --- /dev/null +++ b/internal/prometheus/agent/collector.go @@ -0,0 +1,67 @@ +package agentmetrics + +import ( + "strings" + + "git.g3e.fr/syonad/two/pkg/db/kv" + "github.com/dgraph-io/badger/v4" + "github.com/prometheus/client_golang/prometheus" +) + +var allStates = []string{"creating", "created", "deleting", "deleted"} + +// AgentCollector implements prometheus.Collector and exposes agent metrics +// by querying the BadgerDB on each scrape. +type AgentCollector struct { + db *badger.DB + vpcsTotal *prometheus.Desc + subnetsTotal *prometheus.Desc +} + +func NewAgentCollector(db *badger.DB) *AgentCollector { + return &AgentCollector{ + db: db, + vpcsTotal: prometheus.NewDesc( + "syonad_vpcs_total", + "Number of VPCs by state.", + []string{"state"}, nil, + ), + subnetsTotal: prometheus.NewDesc( + "syonad_subnets_total", + "Number of subnets by state.", + []string{"state"}, nil, + ), + } +} + +func (c *AgentCollector) Describe(ch chan<- *prometheus.Desc) { + ch <- c.vpcsTotal + ch <- c.subnetsTotal +} + +func (c *AgentCollector) Collect(ch chan<- prometheus.Metric) { + c.collectStates(ch, "vpc/", c.vpcsTotal) + c.collectStates(ch, "subnet/", c.subnetsTotal) +} + +// collectStates counts resources under the given DB prefix by their state value +// and emits one gauge per state label. +func (c *AgentCollector) collectStates(ch chan<- prometheus.Metric, prefix string, desc *prometheus.Desc) { + counts := make(map[string]float64, len(allStates)) + for _, s := range allStates { + counts[s] = 0 + } + + items, err := kv.ListByPrefix(c.db, prefix) + if err == nil { + for key, val := range items { + if strings.HasSuffix(key, "/state") { + counts[val]++ + } + } + } + + for _, state := range allStates { + ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, counts[state], state) + } +} diff --git a/pkg/prometheus/server.go b/pkg/prometheus/server.go new file mode 100644 index 0000000..9f8e557 --- /dev/null +++ b/pkg/prometheus/server.go @@ -0,0 +1,20 @@ +package promserver + +import ( + "log" + "net/http" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +// Start launches the Prometheus metrics HTTP server on the given address. +// The provided registry is used to expose metrics at /metrics. +func Start(address string, registry *prometheus.Registry) { + mux := http.NewServeMux() + mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{ + EnableOpenMetrics: true, + })) + log.Printf("Prometheus server listening on %s", address) + log.Fatal(http.ListenAndServe(address, mux)) +} From 906201a1b6cdbb097ba49082596b48ad431f62d7 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Sun, 12 Apr 2026 17:30:36 +0200 Subject: [PATCH 05/24] f-21: code: add list prefix Signed-off-by: GnomeZworc --- pkg/db/kv/listByPrefix.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 pkg/db/kv/listByPrefix.go diff --git a/pkg/db/kv/listByPrefix.go b/pkg/db/kv/listByPrefix.go new file mode 100644 index 0000000..e6c095e --- /dev/null +++ b/pkg/db/kv/listByPrefix.go @@ -0,0 +1,33 @@ +package kv + +import ( + "github.com/dgraph-io/badger/v4" +) + +// ListByPrefix returns all key-value pairs whose key starts with prefix. +func ListByPrefix(db *badger.DB, prefix string) (map[string]string, error) { + result := make(map[string]string) + p := []byte(prefix) + + err := db.View(func(txn *badger.Txn) error { + opts := badger.DefaultIteratorOptions + opts.PrefetchSize = 10 + + it := txn.NewIterator(opts) + defer it.Close() + + for it.Seek(p); it.ValidForPrefix(p); it.Next() { + item := it.Item() + key := string(item.Key()) + + val, err := item.ValueCopy(nil) + if err != nil { + return err + } + result[key] = string(val) + } + return nil + }) + + return result, err +} From cc6c08b522b41ddad19e071fd934a5a6be133caa Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Sun, 12 Apr 2026 17:30:58 +0200 Subject: [PATCH 06/24] f-21: config: add config data Signed-off-by: GnomeZworc --- internal/config/agent/struct.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/config/agent/struct.go b/internal/config/agent/struct.go index c9537bf..0cca127 100644 --- a/internal/config/agent/struct.go +++ b/internal/config/agent/struct.go @@ -8,6 +8,14 @@ type Config struct { Database struct { Path string `mapstructure:"path"` } `mapstructure:"database"` + Api struct { + Address string `mapstructure:"address"` + Port int `mapstructure:"port"` + } `mapstructure:"api"` + Prometheus struct { + Address string `mapstructure:"address"` + Port int `mapstructure:"port"` + } `mapstructure:"prometheus"` } func LoadConfig(path string) (*Config, error) { @@ -16,6 +24,10 @@ func LoadConfig(path string) (*Config, error) { v.SetConfigType("yaml") v.SetDefault("database.path", "/var/lib/two/data/") + v.SetDefault("api.address", "") + v.SetDefault("api.port", 8080) + v.SetDefault("prometheus.address", "") + v.SetDefault("prometheus.port", 9090) v.ReadInConfig() From 04e8adb9b026a21bdcc944042fc538f877374d6a Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Sun, 12 Apr 2026 17:31:24 +0200 Subject: [PATCH 07/24] f-21: agent: implement api first Signed-off-by: GnomeZworc --- cmd/agent/main.go | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 0f82e93..ce3df4e 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -1,17 +1,38 @@ package main import ( + "flag" "fmt" - "os" -) + "log" -var ( - bin_name = os.Args[0] + agentapi "git.g3e.fr/syonad/two/internal/api/agent" + agentmetrics "git.g3e.fr/syonad/two/internal/prometheus/agent" + configuration "git.g3e.fr/syonad/two/internal/config/agent" + promserver "git.g3e.fr/syonad/two/pkg/prometheus" + "git.g3e.fr/syonad/two/pkg/db/kv" + "github.com/prometheus/client_golang/prometheus" ) func main() { + confFile := flag.String("config", "/etc/two/agent.yml", "config file path") + flag.Parse() - fmt.Printf("%s: Start process\n", bin_name) + cfg, err := configuration.LoadConfig(*confFile) + if err != nil { + log.Fatalf("failed to load config: %v", err) + } - os.Exit(0) + db := kv.InitDB(kv.Config{Path: cfg.Database.Path}, true) + defer db.Close() + + apiAddr := fmt.Sprintf("%s:%d", cfg.Api.Address, cfg.Api.Port) + promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port) + + registry := prometheus.NewRegistry() + registry.MustRegister(agentmetrics.NewAgentCollector(db)) + + go agentapi.Start(apiAddr) + go promserver.Start(promAddr, registry) + + select {} } From dc701886eb726ca76cf0d407607597219eb97ece Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Sun, 12 Apr 2026 17:31:42 +0200 Subject: [PATCH 08/24] f-21: go: add import lib Signed-off-by: GnomeZworc --- go.mod | 11 +++++++++-- go.sum | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 7430e72..a4b0b38 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.24.0 toolchain go1.24.11 require ( + github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/coreos/go-systemd/v22 v22.6.0 // indirect github.com/dgraph-io/badger/v4 v4.8.0 // indirect @@ -17,7 +18,12 @@ require ( github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/google/flatbuffers v25.2.10+incompatible // indirect github.com/klauspost/compress v1.18.0 // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect + github.com/prometheus/client_golang v1.23.2 // indirect + github.com/prometheus/client_model v0.6.2 // indirect + github.com/prometheus/common v0.66.1 // indirect + github.com/prometheus/procfs v0.16.1 // indirect github.com/sagikazarmark/locafero v0.11.0 // indirect github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect github.com/spf13/afero v1.15.0 // indirect @@ -31,9 +37,10 @@ require ( go.opentelemetry.io/otel v1.37.0 // indirect go.opentelemetry.io/otel/metric v1.37.0 // indirect go.opentelemetry.io/otel/trace v1.37.0 // indirect + go.yaml.in/yaml/v2 v2.4.2 // indirect go.yaml.in/yaml/v3 v3.0.4 // indirect - golang.org/x/net v0.41.0 // indirect + golang.org/x/net v0.43.0 // indirect golang.org/x/sys v0.39.0 // indirect golang.org/x/text v0.28.0 // indirect - google.golang.org/protobuf v1.36.6 // indirect + google.golang.org/protobuf v1.36.8 // indirect ) diff --git a/go.sum b/go.sum index 73d16f3..402452d 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/coreos/go-systemd/v22 v22.6.0 h1:aGVa/v8B7hpb0TKl0MWoAavPDmHvobFe5R5zn0bCJWo= @@ -23,8 +25,18 @@ github.com/google/flatbuffers v25.2.10+incompatible h1:F3vclr7C3HpB1k9mxCGRMXq6F github.com/google/flatbuffers v25.2.10+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o= +github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg= +github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk= +github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE= +github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9ZoGs= +github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA= +github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg= +github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is= github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc= github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik= github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw= @@ -51,10 +63,14 @@ go.opentelemetry.io/otel/metric v1.37.0 h1:mvwbQS5m0tbmqML4NqK+e3aDiO02vsf/Wgbsd go.opentelemetry.io/otel/metric v1.37.0/go.mod h1:04wGrZurHYKOc+RKeye86GwKiTb9FKm1WHtO+4EVr2E= go.opentelemetry.io/otel/trace v1.37.0 h1:HLdcFNbRQBE2imdSEgm/kwqmQj1Or1l/7bW6mxVK7z4= go.opentelemetry.io/otel/trace v1.37.0/go.mod h1:TlgrlQ+PtQO5XFerSPUYG0JSgGyryXewPGyayAWSBS0= +go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI= +go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU= go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw= golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA= +golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE= +golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA= @@ -65,4 +81,6 @@ golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng= golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU= google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY= google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY= +google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc= +google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From 2fbde24e8928d36de6dcddeb941e68d709f77ca0 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Tue, 14 Apr 2026 21:24:13 +0200 Subject: [PATCH 09/24] f-21: code: ajout d'un system de worker Signed-off-by: GnomeZworc --- cmd/agent/main.go | 14 +++++++++----- internal/api/agent/server.go | 20 +++++++++++++++----- internal/api/agent/subnet.go | 7 ++++++- internal/api/agent/subnets.go | 7 ++++++- internal/api/agent/vpc.go | 7 ++++++- internal/api/agent/vpcs.go | 7 ++++++- internal/config/agent/struct.go | 6 ++++++ pkg/worker/queue.go | 33 +++++++++++++++++++++++++++++++++ 8 files changed, 87 insertions(+), 14 deletions(-) create mode 100644 pkg/worker/queue.go diff --git a/cmd/agent/main.go b/cmd/agent/main.go index ce3df4e..56011bf 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -6,10 +6,11 @@ import ( "log" agentapi "git.g3e.fr/syonad/two/internal/api/agent" - agentmetrics "git.g3e.fr/syonad/two/internal/prometheus/agent" configuration "git.g3e.fr/syonad/two/internal/config/agent" - promserver "git.g3e.fr/syonad/two/pkg/prometheus" + agentmetrics "git.g3e.fr/syonad/two/internal/prometheus/agent" "git.g3e.fr/syonad/two/pkg/db/kv" + promserver "git.g3e.fr/syonad/two/pkg/prometheus" + "git.g3e.fr/syonad/two/pkg/worker" "github.com/prometheus/client_golang/prometheus" ) @@ -25,13 +26,16 @@ func main() { db := kv.InitDB(kv.Config{Path: cfg.Database.Path}, true) defer db.Close() - apiAddr := fmt.Sprintf("%s:%d", cfg.Api.Address, cfg.Api.Port) - promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port) + q := worker.New(cfg.Worker.BufferSize) + q.Start(cfg.Worker.Count) registry := prometheus.NewRegistry() registry.MustRegister(agentmetrics.NewAgentCollector(db)) - go agentapi.Start(apiAddr) + apiAddr := fmt.Sprintf("%s:%d", cfg.Api.Address, cfg.Api.Port) + promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port) + + go agentapi.New(q).Start(apiAddr) go promserver.Start(promAddr, registry) select {} diff --git a/internal/api/agent/server.go b/internal/api/agent/server.go index 05653a4..aebe2c3 100644 --- a/internal/api/agent/server.go +++ b/internal/api/agent/server.go @@ -3,14 +3,24 @@ package agentapi import ( "log" "net/http" + + "git.g3e.fr/syonad/two/pkg/worker" ) -func Start(address string) { +type Server struct { + queue *worker.Queue +} + +func New(queue *worker.Queue) *Server { + return &Server{queue: queue} +} + +func (s *Server) Start(address string) { mux := http.NewServeMux() - mux.HandleFunc("/vpcs", VpcsHandler) - mux.HandleFunc("/vpcs/", VpcByNameHandler) - mux.HandleFunc("/subnets", SubnetsHandler) - mux.HandleFunc("/subnets/", SubnetByNameHandler) + mux.HandleFunc("/vpcs", s.VpcsHandler) + mux.HandleFunc("/vpcs/", s.VpcByNameHandler) + mux.HandleFunc("/subnets", s.SubnetsHandler) + mux.HandleFunc("/subnets/", s.SubnetByNameHandler) log.Printf("API server listening on %s", address) log.Fatal(http.ListenAndServe(address, mux)) } diff --git a/internal/api/agent/subnet.go b/internal/api/agent/subnet.go index e46e4f4..52b27d0 100644 --- a/internal/api/agent/subnet.go +++ b/internal/api/agent/subnet.go @@ -6,7 +6,7 @@ import ( "strings" ) -func SubnetByNameHandler(w http.ResponseWriter, r *http.Request) { +func (s *Server) SubnetByNameHandler(w http.ResponseWriter, r *http.Request) { name := strings.TrimPrefix(r.URL.Path, "/subnets/") if name == "" { http.NotFound(w, r) @@ -18,8 +18,13 @@ func SubnetByNameHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(map[string]string{"name": name}) case http.MethodDelete: + s.queue.Submit(func() { + deleteSubnet(name) + }) w.WriteHeader(http.StatusAccepted) default: http.Error(w, "method not allowed", http.StatusMethodNotAllowed) } } + +func deleteSubnet(name string) {} diff --git a/internal/api/agent/subnets.go b/internal/api/agent/subnets.go index 53dce2e..b3d587b 100644 --- a/internal/api/agent/subnets.go +++ b/internal/api/agent/subnets.go @@ -5,15 +5,20 @@ import ( "net/http" ) -func SubnetsHandler(w http.ResponseWriter, r *http.Request) { +func (s *Server) SubnetsHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") switch r.Method { case http.MethodGet: w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode([]interface{}{}) case http.MethodPost: + s.queue.Submit(func() { + createSubnet() + }) w.WriteHeader(http.StatusAccepted) default: http.Error(w, "method not allowed", http.StatusMethodNotAllowed) } } + +func createSubnet() {} diff --git a/internal/api/agent/vpc.go b/internal/api/agent/vpc.go index aecfaa1..73cecd9 100644 --- a/internal/api/agent/vpc.go +++ b/internal/api/agent/vpc.go @@ -6,7 +6,7 @@ import ( "strings" ) -func VpcByNameHandler(w http.ResponseWriter, r *http.Request) { +func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) { name := strings.TrimPrefix(r.URL.Path, "/vpcs/") if name == "" { http.NotFound(w, r) @@ -18,8 +18,13 @@ func VpcByNameHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(map[string]string{"name": name}) case http.MethodDelete: + s.queue.Submit(func() { + deleteVpc(name) + }) w.WriteHeader(http.StatusAccepted) default: http.Error(w, "method not allowed", http.StatusMethodNotAllowed) } } + +func deleteVpc(name string) {} diff --git a/internal/api/agent/vpcs.go b/internal/api/agent/vpcs.go index c4e9b27..57386b6 100644 --- a/internal/api/agent/vpcs.go +++ b/internal/api/agent/vpcs.go @@ -5,15 +5,20 @@ import ( "net/http" ) -func VpcsHandler(w http.ResponseWriter, r *http.Request) { +func (s *Server) VpcsHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") switch r.Method { case http.MethodGet: w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode([]interface{}{}) case http.MethodPost: + s.queue.Submit(func() { + createVpc() + }) w.WriteHeader(http.StatusAccepted) default: http.Error(w, "method not allowed", http.StatusMethodNotAllowed) } } + +func createVpc() {} diff --git a/internal/config/agent/struct.go b/internal/config/agent/struct.go index 0cca127..24ad0f5 100644 --- a/internal/config/agent/struct.go +++ b/internal/config/agent/struct.go @@ -16,6 +16,10 @@ type Config struct { Address string `mapstructure:"address"` Port int `mapstructure:"port"` } `mapstructure:"prometheus"` + Worker struct { + Count int `mapstructure:"count"` + BufferSize int `mapstructure:"buffer_size"` + } `mapstructure:"worker"` } func LoadConfig(path string) (*Config, error) { @@ -28,6 +32,8 @@ func LoadConfig(path string) (*Config, error) { v.SetDefault("api.port", 8080) v.SetDefault("prometheus.address", "") v.SetDefault("prometheus.port", 9090) + v.SetDefault("worker.count", 4) + v.SetDefault("worker.buffer_size", 100) v.ReadInConfig() diff --git a/pkg/worker/queue.go b/pkg/worker/queue.go new file mode 100644 index 0000000..109c726 --- /dev/null +++ b/pkg/worker/queue.go @@ -0,0 +1,33 @@ +package worker + +import "log" + +// Task is a function to be executed asynchronously by a worker. +type Task func() + +// Queue is a FIFO channel-backed task queue consumed by worker goroutines. +type Queue struct { + tasks chan Task +} + +// New creates a Queue with the given channel buffer size. +func New(bufferSize int) *Queue { + return &Queue{tasks: make(chan Task, bufferSize)} +} + +// Submit enqueues a task. Blocks if the queue is full. +func (q *Queue) Submit(t Task) { + q.tasks <- t +} + +// Start launches n worker goroutines that consume and execute tasks. +func (q *Queue) Start(n int) { + log.Printf("worker: starting %d workers", n) + for i := range n { + go func(id int) { + for task := range q.tasks { + task() + } + }(i) + } +} From ac43979b38cb43909a111c53f9a63bb8b0e47644 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Thu, 16 Apr 2026 22:40:27 +0200 Subject: [PATCH 10/24] f-21: code: set db tu readwrite Signed-off-by: GnomeZworc --- cmd/agent/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 56011bf..5ebf09f 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -23,7 +23,7 @@ func main() { log.Fatalf("failed to load config: %v", err) } - db := kv.InitDB(kv.Config{Path: cfg.Database.Path}, true) + db := kv.InitDB(kv.Config{Path: cfg.Database.Path}, false) defer db.Close() q := worker.New(cfg.Worker.BufferSize) From 327a3590073fa8613a11007210e8459b71952c9b Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Thu, 16 Apr 2026 22:57:20 +0200 Subject: [PATCH 11/24] f-21: code: separate and create route fonction Signed-off-by: GnomeZworc --- internal/api/agent/models.go | 33 ++++++++++++++++++++++++++ internal/api/agent/server.go | 9 ++++++- internal/api/agent/subnet.go | 29 ++++++++++++++++------- internal/api/agent/subnets.go | 44 ++++++++++++++++++++++++++++------- internal/api/agent/vpc.go | 29 ++++++++++++++++------- internal/api/agent/vpcs.go | 36 +++++++++++++++++++++------- 6 files changed, 145 insertions(+), 35 deletions(-) create mode 100644 internal/api/agent/models.go diff --git a/internal/api/agent/models.go b/internal/api/agent/models.go new file mode 100644 index 0000000..bd09c76 --- /dev/null +++ b/internal/api/agent/models.go @@ -0,0 +1,33 @@ +package agentapi + +type VPCCreateRequest struct { + Name string `json:"name"` +} + +type VPC struct { + Name string `json:"name"` + State string `json:"state"` +} + +type SubnetCreateRequest struct { + Name string `json:"name"` + VPC string `json:"vpc"` + VxlanID int `json:"vxlan_id"` + LocalIP string `json:"local_ip"` + GatewayIP string `json:"gateway_ip"` + CIDR string `json:"cidr"` +} + +type Subnet struct { + Name string `json:"name"` + State string `json:"state"` + VPC string `json:"vpc"` + VxlanID int `json:"vxlan_id"` + LocalIP string `json:"local_ip"` + GatewayIP string `json:"gateway_ip"` + CIDR string `json:"cidr"` +} + +type ErrorResponse struct { + Error string `json:"error"` +} diff --git a/internal/api/agent/server.go b/internal/api/agent/server.go index aebe2c3..75833c9 100644 --- a/internal/api/agent/server.go +++ b/internal/api/agent/server.go @@ -22,5 +22,12 @@ func (s *Server) Start(address string) { mux.HandleFunc("/subnets", s.SubnetsHandler) mux.HandleFunc("/subnets/", s.SubnetByNameHandler) log.Printf("API server listening on %s", address) - log.Fatal(http.ListenAndServe(address, mux)) + log.Fatal(http.ListenAndServe(address, logMiddleware(mux))) +} + +func logMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL.Path) + next.ServeHTTP(w, r) + }) } diff --git a/internal/api/agent/subnet.go b/internal/api/agent/subnet.go index 52b27d0..289667f 100644 --- a/internal/api/agent/subnet.go +++ b/internal/api/agent/subnet.go @@ -9,22 +9,33 @@ import ( func (s *Server) SubnetByNameHandler(w http.ResponseWriter, r *http.Request) { name := strings.TrimPrefix(r.URL.Path, "/subnets/") if name == "" { - http.NotFound(w, r) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusNotFound) + json.NewEncoder(w).Encode(ErrorResponse{Error: "resource not found"}) return } w.Header().Set("Content-Type", "application/json") switch r.Method { case http.MethodGet: - w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(map[string]string{"name": name}) + s.getSubnet(w, r, name) case http.MethodDelete: - s.queue.Submit(func() { - deleteSubnet(name) - }) - w.WriteHeader(http.StatusAccepted) + s.deleteSubnet(w, r, name) default: - http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed) } } -func deleteSubnet(name string) {} +func (s *Server) getSubnet(w http.ResponseWriter, r *http.Request, name string) { + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(Subnet{Name: name, State: "created"}) +} + +func (s *Server) deleteSubnet(w http.ResponseWriter, r *http.Request, name string) { + s.queue.Submit(func() { + destroySubnet(name) + }) + w.WriteHeader(http.StatusAccepted) + json.NewEncoder(w).Encode(Subnet{Name: name, State: "deleting"}) +} + +func destroySubnet(name string) {} diff --git a/internal/api/agent/subnets.go b/internal/api/agent/subnets.go index b3d587b..5e0ebdc 100644 --- a/internal/api/agent/subnets.go +++ b/internal/api/agent/subnets.go @@ -9,16 +9,44 @@ func (s *Server) SubnetsHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") switch r.Method { case http.MethodGet: - w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode([]interface{}{}) + s.listSubnets(w, r) case http.MethodPost: - s.queue.Submit(func() { - createSubnet() - }) - w.WriteHeader(http.StatusAccepted) + s.postSubnet(w, r) default: - http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed) } } -func createSubnet() {} +func (s *Server) listSubnets(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode([]Subnet{}) +} + +func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) { + var req SubnetCreateRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid request body"}) + return + } + if req.Name == "" || req.VPC == "" || req.LocalIP == "" || req.GatewayIP == "" || req.CIDR == "" { + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc, local_ip, gateway_ip and cidr are required"}) + return + } + s.queue.Submit(func() { + createSubnet(req) + }) + w.WriteHeader(http.StatusAccepted) + json.NewEncoder(w).Encode(Subnet{ + Name: req.Name, + State: "creating", + VPC: req.VPC, + VxlanID: req.VxlanID, + LocalIP: req.LocalIP, + GatewayIP: req.GatewayIP, + CIDR: req.CIDR, + }) +} + +func createSubnet(req SubnetCreateRequest) {} diff --git a/internal/api/agent/vpc.go b/internal/api/agent/vpc.go index 73cecd9..f1d34c1 100644 --- a/internal/api/agent/vpc.go +++ b/internal/api/agent/vpc.go @@ -9,22 +9,33 @@ import ( func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) { name := strings.TrimPrefix(r.URL.Path, "/vpcs/") if name == "" { - http.NotFound(w, r) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusNotFound) + json.NewEncoder(w).Encode(ErrorResponse{Error: "resource not found"}) return } w.Header().Set("Content-Type", "application/json") switch r.Method { case http.MethodGet: - w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(map[string]string{"name": name}) + s.getVpc(w, r, name) case http.MethodDelete: - s.queue.Submit(func() { - deleteVpc(name) - }) - w.WriteHeader(http.StatusAccepted) + s.deleteVpc(w, r, name) default: - http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed) } } -func deleteVpc(name string) {} +func (s *Server) getVpc(w http.ResponseWriter, r *http.Request, name string) { + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(VPC{Name: name, State: "created"}) +} + +func (s *Server) deleteVpc(w http.ResponseWriter, r *http.Request, name string) { + s.queue.Submit(func() { + destroyVpc(name) + }) + w.WriteHeader(http.StatusAccepted) + json.NewEncoder(w).Encode(VPC{Name: name, State: "deleting"}) +} + +func destroyVpc(name string) {} diff --git a/internal/api/agent/vpcs.go b/internal/api/agent/vpcs.go index 57386b6..aba4e7a 100644 --- a/internal/api/agent/vpcs.go +++ b/internal/api/agent/vpcs.go @@ -9,16 +9,36 @@ func (s *Server) VpcsHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") switch r.Method { case http.MethodGet: - w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode([]interface{}{}) + s.listVpcs(w, r) case http.MethodPost: - s.queue.Submit(func() { - createVpc() - }) - w.WriteHeader(http.StatusAccepted) + s.postVpc(w, r) default: - http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed) } } -func createVpc() {} +func (s *Server) listVpcs(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode([]VPC{}) +} + +func (s *Server) postVpc(w http.ResponseWriter, r *http.Request) { + var req VPCCreateRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid request body"}) + return + } + if req.Name == "" { + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(ErrorResponse{Error: "name is required"}) + return + } + s.queue.Submit(func() { + createVpc(req.Name) + }) + w.WriteHeader(http.StatusAccepted) + json.NewEncoder(w).Encode(VPC{Name: req.Name, State: "creating"}) +} + +func createVpc(name string) {} From 82fa401680c15fa5588ce6c1ffa00dc63a422e5d Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Thu, 16 Apr 2026 22:57:53 +0200 Subject: [PATCH 12/24] f-21: doc: rename api doc Signed-off-by: GnomeZworc --- api/{openapi.yaml => agent.yaml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename api/{openapi.yaml => agent.yaml} (100%) diff --git a/api/openapi.yaml b/api/agent.yaml similarity index 100% rename from api/openapi.yaml rename to api/agent.yaml From 59e83da1d6820e89123a6409db29812bf3416be0 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Fri, 17 Apr 2026 23:25:35 +0200 Subject: [PATCH 13/24] f-21: code: add database in api Signed-off-by: GnomeZworc --- cmd/agent/main.go | 2 +- internal/api/agent/server.go | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 5ebf09f..2f5e084 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -35,7 +35,7 @@ func main() { apiAddr := fmt.Sprintf("%s:%d", cfg.Api.Address, cfg.Api.Port) promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port) - go agentapi.New(q).Start(apiAddr) + go agentapi.New(q, db).Start(apiAddr) go promserver.Start(promAddr, registry) select {} diff --git a/internal/api/agent/server.go b/internal/api/agent/server.go index 75833c9..7e4eb21 100644 --- a/internal/api/agent/server.go +++ b/internal/api/agent/server.go @@ -5,14 +5,16 @@ import ( "net/http" "git.g3e.fr/syonad/two/pkg/worker" + "github.com/dgraph-io/badger/v4" ) type Server struct { queue *worker.Queue + db *badger.DB } -func New(queue *worker.Queue) *Server { - return &Server{queue: queue} +func New(queue *worker.Queue, db *badger.DB) *Server { + return &Server{queue: queue, db: db} } func (s *Server) Start(address string) { From 49aa9a7ab6a3ba81f78711436a40ed07eda908fd Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Fri, 17 Apr 2026 23:26:04 +0200 Subject: [PATCH 14/24] f-21: code: add vpc gestion in api Signed-off-by: GnomeZworc --- internal/api/agent/vpc.go | 20 +++++++++++++++++--- internal/api/agent/vpcs.go | 13 +++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/internal/api/agent/vpc.go b/internal/api/agent/vpc.go index f1d34c1..3396d23 100644 --- a/internal/api/agent/vpc.go +++ b/internal/api/agent/vpc.go @@ -2,8 +2,13 @@ package agentapi import ( "encoding/json" + "fmt" "net/http" + "os" "strings" + + "git.g3e.fr/syonad/two/internal/vpc" + "git.g3e.fr/syonad/two/pkg/db/kv" ) func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) { @@ -25,14 +30,23 @@ func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) { } } -func (s *Server) getVpc(w http.ResponseWriter, r *http.Request, name string) { +func (s *Server) getVpc(w http.ResponseWriter, _ *http.Request, name string) { w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(VPC{Name: name, State: "created"}) } -func (s *Server) deleteVpc(w http.ResponseWriter, r *http.Request, name string) { +func (s *Server) deleteVpc(w http.ResponseWriter, _ *http.Request, name string) { s.queue.Submit(func() { - destroyVpc(name) + kv.AddInDB(s.db, "vpc/"+name+"/state", "deleting") + if err := vpc.DeleteVPC(s.db, name); err != nil { + fmt.Println(err) + } + if state, err := kv.GetFromDB(s.db, "vpc/"+name+"/state"); err != nil { + fmt.Println(err) + os.Exit(1) + } else if state == "deleted" { + kv.DeleteInDB(s.db, "vpc/"+name) + } }) w.WriteHeader(http.StatusAccepted) json.NewEncoder(w).Encode(VPC{Name: name, State: "deleting"}) diff --git a/internal/api/agent/vpcs.go b/internal/api/agent/vpcs.go index aba4e7a..2dddd42 100644 --- a/internal/api/agent/vpcs.go +++ b/internal/api/agent/vpcs.go @@ -2,7 +2,11 @@ package agentapi import ( "encoding/json" + "fmt" "net/http" + + "git.g3e.fr/syonad/two/internal/vpc" + "git.g3e.fr/syonad/two/pkg/db/kv" ) func (s *Server) VpcsHandler(w http.ResponseWriter, r *http.Request) { @@ -17,7 +21,7 @@ func (s *Server) VpcsHandler(w http.ResponseWriter, r *http.Request) { } } -func (s *Server) listVpcs(w http.ResponseWriter, r *http.Request) { +func (s *Server) listVpcs(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode([]VPC{}) } @@ -35,10 +39,11 @@ func (s *Server) postVpc(w http.ResponseWriter, r *http.Request) { return } s.queue.Submit(func() { - createVpc(req.Name) + kv.AddInDB(s.db, "vpc/"+req.Name+"/state", "creating") + if err := vpc.CreateVPC(s.db, req.Name); err != nil { + fmt.Println(err) + } }) w.WriteHeader(http.StatusAccepted) json.NewEncoder(w).Encode(VPC{Name: req.Name, State: "creating"}) } - -func createVpc(name string) {} From 7ee85275829818eb5151b47689ceaa056efe2aab Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Sun, 19 Apr 2026 00:04:46 +0200 Subject: [PATCH 15/24] f-21: git: add claude to gitignore Signed-off-by: GnomeZworc --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3a0b507..68ae417 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,9 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out +# Output of the go coverage tool, specifically when used with LiteIDE +.claude + # Dependency directories (remove the comment below to include it) # vendor/ @@ -26,4 +29,4 @@ go.work.sum .env # ignore local info -data/ \ No newline at end of file +data/ From 63a288f69eafc0f41ca6550fa06543cc816af801 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Sun, 19 Apr 2026 00:06:50 +0200 Subject: [PATCH 16/24] f-21: refactor: add dispatcher layer for MQTT migration Introduce internal/dispatcher package with a Command interface and typed commands (CreateVPC, DeleteVPC, CreateSubnet, DeleteSubnet). The API handlers now call dispatcher.Dispatch() instead of enqueuing closures directly, decoupling transport (HTTP today, MQTT tomorrow) from execution. Signed-off-by: GnomeZworc --- cmd/agent/main.go | 4 ++- internal/api/agent/server.go | 10 ++++---- internal/api/agent/subnet.go | 7 +++--- internal/api/agent/subnets.go | 12 ++++++--- internal/api/agent/vpc.go | 19 ++------------ internal/api/agent/vpcs.go | 11 ++------ internal/dispatcher/dispatcher.go | 29 +++++++++++++++++++++ internal/dispatcher/subnet_commands.go | 26 +++++++++++++++++++ internal/dispatcher/vpc_commands.go | 35 ++++++++++++++++++++++++++ 9 files changed, 114 insertions(+), 39 deletions(-) create mode 100644 internal/dispatcher/dispatcher.go create mode 100644 internal/dispatcher/subnet_commands.go create mode 100644 internal/dispatcher/vpc_commands.go diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 2f5e084..b61bb6f 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -7,6 +7,7 @@ import ( agentapi "git.g3e.fr/syonad/two/internal/api/agent" configuration "git.g3e.fr/syonad/two/internal/config/agent" + "git.g3e.fr/syonad/two/internal/dispatcher" agentmetrics "git.g3e.fr/syonad/two/internal/prometheus/agent" "git.g3e.fr/syonad/two/pkg/db/kv" promserver "git.g3e.fr/syonad/two/pkg/prometheus" @@ -35,7 +36,8 @@ func main() { apiAddr := fmt.Sprintf("%s:%d", cfg.Api.Address, cfg.Api.Port) promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port) - go agentapi.New(q, db).Start(apiAddr) + d := dispatcher.New(q, db) + go agentapi.New(d, db).Start(apiAddr) go promserver.Start(promAddr, registry) select {} diff --git a/internal/api/agent/server.go b/internal/api/agent/server.go index 7e4eb21..7f3247c 100644 --- a/internal/api/agent/server.go +++ b/internal/api/agent/server.go @@ -4,17 +4,17 @@ import ( "log" "net/http" - "git.g3e.fr/syonad/two/pkg/worker" + "git.g3e.fr/syonad/two/internal/dispatcher" "github.com/dgraph-io/badger/v4" ) type Server struct { - queue *worker.Queue - db *badger.DB + dispatcher *dispatcher.Dispatcher + db *badger.DB } -func New(queue *worker.Queue, db *badger.DB) *Server { - return &Server{queue: queue, db: db} +func New(d *dispatcher.Dispatcher, db *badger.DB) *Server { + return &Server{dispatcher: d, db: db} } func (s *Server) Start(address string) { diff --git a/internal/api/agent/subnet.go b/internal/api/agent/subnet.go index 289667f..b20c65a 100644 --- a/internal/api/agent/subnet.go +++ b/internal/api/agent/subnet.go @@ -4,6 +4,8 @@ import ( "encoding/json" "net/http" "strings" + + "git.g3e.fr/syonad/two/internal/dispatcher" ) func (s *Server) SubnetByNameHandler(w http.ResponseWriter, r *http.Request) { @@ -31,11 +33,8 @@ func (s *Server) getSubnet(w http.ResponseWriter, r *http.Request, name string) } func (s *Server) deleteSubnet(w http.ResponseWriter, r *http.Request, name string) { - s.queue.Submit(func() { - destroySubnet(name) - }) + s.dispatcher.Dispatch(dispatcher.DeleteSubnetCommand{Name: name}) w.WriteHeader(http.StatusAccepted) json.NewEncoder(w).Encode(Subnet{Name: name, State: "deleting"}) } -func destroySubnet(name string) {} diff --git a/internal/api/agent/subnets.go b/internal/api/agent/subnets.go index 5e0ebdc..b251c3c 100644 --- a/internal/api/agent/subnets.go +++ b/internal/api/agent/subnets.go @@ -3,6 +3,8 @@ package agentapi import ( "encoding/json" "net/http" + + "git.g3e.fr/syonad/two/internal/dispatcher" ) func (s *Server) SubnetsHandler(w http.ResponseWriter, r *http.Request) { @@ -34,8 +36,13 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc, local_ip, gateway_ip and cidr are required"}) return } - s.queue.Submit(func() { - createSubnet(req) + s.dispatcher.Dispatch(dispatcher.CreateSubnetCommand{ + Name: req.Name, + VPC: req.VPC, + VxlanID: req.VxlanID, + LocalIP: req.LocalIP, + GatewayIP: req.GatewayIP, + CIDR: req.CIDR, }) w.WriteHeader(http.StatusAccepted) json.NewEncoder(w).Encode(Subnet{ @@ -49,4 +56,3 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) { }) } -func createSubnet(req SubnetCreateRequest) {} diff --git a/internal/api/agent/vpc.go b/internal/api/agent/vpc.go index 3396d23..60d614e 100644 --- a/internal/api/agent/vpc.go +++ b/internal/api/agent/vpc.go @@ -2,13 +2,10 @@ package agentapi import ( "encoding/json" - "fmt" "net/http" - "os" "strings" - "git.g3e.fr/syonad/two/internal/vpc" - "git.g3e.fr/syonad/two/pkg/db/kv" + "git.g3e.fr/syonad/two/internal/dispatcher" ) func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) { @@ -36,20 +33,8 @@ func (s *Server) getVpc(w http.ResponseWriter, _ *http.Request, name string) { } func (s *Server) deleteVpc(w http.ResponseWriter, _ *http.Request, name string) { - s.queue.Submit(func() { - kv.AddInDB(s.db, "vpc/"+name+"/state", "deleting") - if err := vpc.DeleteVPC(s.db, name); err != nil { - fmt.Println(err) - } - if state, err := kv.GetFromDB(s.db, "vpc/"+name+"/state"); err != nil { - fmt.Println(err) - os.Exit(1) - } else if state == "deleted" { - kv.DeleteInDB(s.db, "vpc/"+name) - } - }) + s.dispatcher.Dispatch(dispatcher.DeleteVPCCommand{Name: name}) w.WriteHeader(http.StatusAccepted) json.NewEncoder(w).Encode(VPC{Name: name, State: "deleting"}) } -func destroyVpc(name string) {} diff --git a/internal/api/agent/vpcs.go b/internal/api/agent/vpcs.go index 2dddd42..eb633e3 100644 --- a/internal/api/agent/vpcs.go +++ b/internal/api/agent/vpcs.go @@ -2,11 +2,9 @@ package agentapi import ( "encoding/json" - "fmt" "net/http" - "git.g3e.fr/syonad/two/internal/vpc" - "git.g3e.fr/syonad/two/pkg/db/kv" + "git.g3e.fr/syonad/two/internal/dispatcher" ) func (s *Server) VpcsHandler(w http.ResponseWriter, r *http.Request) { @@ -38,12 +36,7 @@ func (s *Server) postVpc(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(ErrorResponse{Error: "name is required"}) return } - s.queue.Submit(func() { - kv.AddInDB(s.db, "vpc/"+req.Name+"/state", "creating") - if err := vpc.CreateVPC(s.db, req.Name); err != nil { - fmt.Println(err) - } - }) + s.dispatcher.Dispatch(dispatcher.CreateVPCCommand{Name: req.Name}) w.WriteHeader(http.StatusAccepted) json.NewEncoder(w).Encode(VPC{Name: req.Name, State: "creating"}) } diff --git a/internal/dispatcher/dispatcher.go b/internal/dispatcher/dispatcher.go new file mode 100644 index 0000000..937adf2 --- /dev/null +++ b/internal/dispatcher/dispatcher.go @@ -0,0 +1,29 @@ +package dispatcher + +import ( + "log" + + "git.g3e.fr/syonad/two/pkg/worker" + "github.com/dgraph-io/badger/v4" +) + +type Command interface { + Execute(db *badger.DB) error +} + +type Dispatcher struct { + queue *worker.Queue + db *badger.DB +} + +func New(queue *worker.Queue, db *badger.DB) *Dispatcher { + return &Dispatcher{queue: queue, db: db} +} + +func (d *Dispatcher) Dispatch(cmd Command) { + d.queue.Submit(func() { + if err := cmd.Execute(d.db); err != nil { + log.Printf("command error (%T): %v", cmd, err) + } + }) +} diff --git a/internal/dispatcher/subnet_commands.go b/internal/dispatcher/subnet_commands.go new file mode 100644 index 0000000..d8ef8e3 --- /dev/null +++ b/internal/dispatcher/subnet_commands.go @@ -0,0 +1,26 @@ +package dispatcher + +import "github.com/dgraph-io/badger/v4" + +type CreateSubnetCommand struct { + Name string + VPC string + VxlanID int + LocalIP string + GatewayIP string + CIDR string +} + +func (c CreateSubnetCommand) Execute(db *badger.DB) error { + // TODO: brancher internal/subnet/create.go + return nil +} + +type DeleteSubnetCommand struct { + Name string +} + +func (c DeleteSubnetCommand) Execute(db *badger.DB) error { + // TODO: brancher internal/subnet/delete.go + return nil +} diff --git a/internal/dispatcher/vpc_commands.go b/internal/dispatcher/vpc_commands.go new file mode 100644 index 0000000..195b014 --- /dev/null +++ b/internal/dispatcher/vpc_commands.go @@ -0,0 +1,35 @@ +package dispatcher + +import ( + "git.g3e.fr/syonad/two/internal/vpc" + "git.g3e.fr/syonad/two/pkg/db/kv" + "github.com/dgraph-io/badger/v4" +) + +type CreateVPCCommand struct { + Name string +} + +func (c CreateVPCCommand) Execute(db *badger.DB) error { + kv.AddInDB(db, "vpc/"+c.Name+"/state", "creating") + return vpc.CreateVPC(db, c.Name) +} + +type DeleteVPCCommand struct { + Name string +} + +func (c DeleteVPCCommand) Execute(db *badger.DB) error { + kv.AddInDB(db, "vpc/"+c.Name+"/state", "deleting") + if err := vpc.DeleteVPC(db, c.Name); err != nil { + return err + } + state, err := kv.GetFromDB(db, "vpc/"+c.Name+"/state") + if err != nil { + return err + } + if state == "deleted" { + kv.DeleteInDB(db, "vpc/"+c.Name) + } + return nil +} From 3127c052160c9d19f5f17d68e9a9c6c5ff56f92c Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Sun, 19 Apr 2026 20:49:50 +0200 Subject: [PATCH 17/24] f-21: clean: delete vpc notions Signed-off-by: GnomeZworc --- .forgejo/workflows/prerelease.yml | 1 - cmd/vpc/main.go | 65 ------------------------------- 2 files changed, 66 deletions(-) delete mode 100644 cmd/vpc/main.go diff --git a/.forgejo/workflows/prerelease.yml b/.forgejo/workflows/prerelease.yml index 8003e82..ed635e9 100644 --- a/.forgejo/workflows/prerelease.yml +++ b/.forgejo/workflows/prerelease.yml @@ -37,7 +37,6 @@ jobs: - metadata - metacli - agent - - vpc - dhcp - subnet uses: ./.forgejo/workflows/build.yml diff --git a/cmd/vpc/main.go b/cmd/vpc/main.go deleted file mode 100644 index e73f7e9..0000000 --- a/cmd/vpc/main.go +++ /dev/null @@ -1,65 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "os" - - configuration "git.g3e.fr/syonad/two/internal/config/agent" - "git.g3e.fr/syonad/two/internal/vpc" - "git.g3e.fr/syonad/two/pkg/db/kv" - "github.com/dgraph-io/badger/v4" -) - -var ( - netns = flag.String("netns", "", "Network namespace à faire") - name = flag.String("name", "", "interface name") - action = flag.String("action", "", "Action a faire") - conf_file = flag.String("conf", "/etc/two/agent.yml", "configuration file") -) - -var DB *badger.DB - -func main() { - flag.Parse() - - conf, err := configuration.LoadConfig(*conf_file) - if err != nil { - fmt.Println(err) - return - } - - DB = kv.InitDB(kv.Config{ - Path: conf.Database.Path, - }, false) - defer DB.Close() - - switch *action { - case "create": - kv.AddInDB(DB, "vpc/"+*name+"/state", "creating") - if err := vpc.CreateVPC(DB, *name); err != nil { - fmt.Println(err) - } - case "delete": - kv.AddInDB(DB, "vpc/"+*name+"/state", "deleting") - if err := vpc.DeleteVPC(DB, *name); err != nil { - fmt.Println(err) - } - if state, err := kv.GetFromDB(DB, "vpc/"+*name+"/state"); err != nil { - fmt.Println(err) - os.Exit(1) - } else if state == "deleted" { - kv.DeleteInDB(DB, "vpc/"+*name) - } - case "check": - if state, err := kv.GetFromDB(DB, "vpc/"+*name+"/state"); err != nil { - os.Exit(1) - } else if state != "created" { - os.Exit(1) - } - default: - fmt.Printf("Available commande:\n - create\n - delete\n - check\n") - os.Exit(1) - } - os.Exit(0) -} From 6a5a646eaad1928431336ab7c0a82a5c2d73d8b4 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Sun, 19 Apr 2026 21:02:54 +0200 Subject: [PATCH 18/24] f-21: code: add dispatch command for subnet Signed-off-by: GnomeZworc --- internal/dispatcher/subnet_commands.go | 31 ++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/internal/dispatcher/subnet_commands.go b/internal/dispatcher/subnet_commands.go index d8ef8e3..b23949e 100644 --- a/internal/dispatcher/subnet_commands.go +++ b/internal/dispatcher/subnet_commands.go @@ -1,6 +1,14 @@ package dispatcher -import "github.com/dgraph-io/badger/v4" +import ( + "fmt" + "os" + "strconv" + + "git.g3e.fr/syonad/two/internal/subnet" + "git.g3e.fr/syonad/two/pkg/db/kv" + "github.com/dgraph-io/badger/v4" +) type CreateSubnetCommand struct { Name string @@ -12,8 +20,13 @@ type CreateSubnetCommand struct { } func (c CreateSubnetCommand) Execute(db *badger.DB) error { - // TODO: brancher internal/subnet/create.go - return nil + kv.AddInDB(db, "subnet/"+c.Name+"/state", "creating") + kv.AddInDB(db, "subnet/"+c.Name+"/vpc", c.VPC) + kv.AddInDB(db, "subnet/"+c.Name+"/vxlan_id", strconv.Itoa(c.VxlanID)) + kv.AddInDB(db, "subnet/"+c.Name+"/local_ip", c.LocalIP) + kv.AddInDB(db, "subnet/"+c.Name+"/gateway_ip", c.GatewayIP) + kv.AddInDB(db, "subnet/"+c.Name+"/cidr", c.CIDR) + return subnet.CreateSubnet(db, c.Name) } type DeleteSubnetCommand struct { @@ -21,6 +34,16 @@ type DeleteSubnetCommand struct { } func (c DeleteSubnetCommand) Execute(db *badger.DB) error { - // TODO: brancher internal/subnet/delete.go + kv.AddInDB(db, "subnet/"+c.Name+"/state", "deleting") + if err := subnet.DeleteSubnet(db, c.Name); err != nil { + fmt.Println(err) + os.Exit(1) + } + if state, err := kv.GetFromDB(db, "subnet/"+c.Name+"/state"); err != nil { + fmt.Println(err) + os.Exit(1) + } else if state == "deleted" { + kv.DeleteInDB(db, "subnet/"+c.Name) + } return nil } From 066b90dff45a1e4db560799def3dc7e1251eb4a5 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Tue, 21 Apr 2026 21:27:19 +0200 Subject: [PATCH 19/24] f-21: code: add interface name in config Signed-off-by: GnomeZworc --- conf/agent/config.exemple.yml | 7 ++++++- internal/config/agent/struct.go | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/conf/agent/config.exemple.yml b/conf/agent/config.exemple.yml index a2b9f1b..5bd1dfd 100644 --- a/conf/agent/config.exemple.yml +++ b/conf/agent/config.exemple.yml @@ -1,2 +1,7 @@ database: - path: "/var/lib/two/data/" \ No newline at end of file + path: "/var/lib/two/data/" + +interfaces: + vms: br-000000 + internet: br-000000 + admin: br-000000 \ No newline at end of file diff --git a/internal/config/agent/struct.go b/internal/config/agent/struct.go index 24ad0f5..21dde0d 100644 --- a/internal/config/agent/struct.go +++ b/internal/config/agent/struct.go @@ -20,6 +20,7 @@ type Config struct { Count int `mapstructure:"count"` BufferSize int `mapstructure:"buffer_size"` } `mapstructure:"worker"` + Interfaces map[string]string `mapstructure:"interfaces"` } func LoadConfig(path string) (*Config, error) { From 9ec6e64327a2c63ad9cb970a59934682c185edb1 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Tue, 21 Apr 2026 21:28:20 +0200 Subject: [PATCH 20/24] f-21: code: change from ip to interface type Signed-off-by: GnomeZworc --- cmd/agent/main.go | 2 +- internal/api/agent/models.go | 16 ++++++++-------- internal/api/agent/subnets.go | 8 +++----- internal/dispatcher/dispatcher.go | 13 +++++++------ internal/dispatcher/subnet_commands.go | 12 ++++++++---- internal/dispatcher/vpc_commands.go | 4 ++-- internal/netif/vxlan.go | 16 +++++++++------- internal/subnet/create.go | 10 +++------- 8 files changed, 41 insertions(+), 40 deletions(-) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index b61bb6f..faa6139 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -36,7 +36,7 @@ func main() { apiAddr := fmt.Sprintf("%s:%d", cfg.Api.Address, cfg.Api.Port) promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port) - d := dispatcher.New(q, db) + d := dispatcher.New(q, db, cfg.Interfaces) go agentapi.New(d, db).Start(apiAddr) go promserver.Start(promAddr, registry) diff --git a/internal/api/agent/models.go b/internal/api/agent/models.go index bd09c76..e826082 100644 --- a/internal/api/agent/models.go +++ b/internal/api/agent/models.go @@ -13,19 +13,19 @@ type SubnetCreateRequest struct { Name string `json:"name"` VPC string `json:"vpc"` VxlanID int `json:"vxlan_id"` - LocalIP string `json:"local_ip"` + IfaceType string `json:"iface_type"` GatewayIP string `json:"gateway_ip"` CIDR string `json:"cidr"` } type Subnet struct { - Name string `json:"name"` - State string `json:"state"` - VPC string `json:"vpc"` - VxlanID int `json:"vxlan_id"` - LocalIP string `json:"local_ip"` - GatewayIP string `json:"gateway_ip"` - CIDR string `json:"cidr"` + Name string `json:"name"` + State string `json:"state"` + VPC string `json:"vpc"` + VxlanID int `json:"vxlan_id"` + LocalIface string `json:"local_iface"` + GatewayIP string `json:"gateway_ip"` + CIDR string `json:"cidr"` } type ErrorResponse struct { diff --git a/internal/api/agent/subnets.go b/internal/api/agent/subnets.go index b251c3c..263c9ce 100644 --- a/internal/api/agent/subnets.go +++ b/internal/api/agent/subnets.go @@ -31,16 +31,16 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid request body"}) return } - if req.Name == "" || req.VPC == "" || req.LocalIP == "" || req.GatewayIP == "" || req.CIDR == "" { + if req.Name == "" || req.VPC == "" || req.IfaceType == "" || req.GatewayIP == "" || req.CIDR == "" { w.WriteHeader(http.StatusBadRequest) - json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc, local_ip, gateway_ip and cidr are required"}) + json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc, iface_type, gateway_ip and cidr are required"}) return } s.dispatcher.Dispatch(dispatcher.CreateSubnetCommand{ Name: req.Name, VPC: req.VPC, VxlanID: req.VxlanID, - LocalIP: req.LocalIP, + IfaceType: req.IfaceType, GatewayIP: req.GatewayIP, CIDR: req.CIDR, }) @@ -50,9 +50,7 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) { State: "creating", VPC: req.VPC, VxlanID: req.VxlanID, - LocalIP: req.LocalIP, GatewayIP: req.GatewayIP, CIDR: req.CIDR, }) } - diff --git a/internal/dispatcher/dispatcher.go b/internal/dispatcher/dispatcher.go index 937adf2..f240245 100644 --- a/internal/dispatcher/dispatcher.go +++ b/internal/dispatcher/dispatcher.go @@ -8,21 +8,22 @@ import ( ) type Command interface { - Execute(db *badger.DB) error + Execute(db *badger.DB, interfaces map[string]string) error } type Dispatcher struct { - queue *worker.Queue - db *badger.DB + queue *worker.Queue + db *badger.DB + interfaces map[string]string } -func New(queue *worker.Queue, db *badger.DB) *Dispatcher { - return &Dispatcher{queue: queue, db: db} +func New(queue *worker.Queue, db *badger.DB, interfaces map[string]string) *Dispatcher { + return &Dispatcher{queue: queue, db: db, interfaces: interfaces} } func (d *Dispatcher) Dispatch(cmd Command) { d.queue.Submit(func() { - if err := cmd.Execute(d.db); err != nil { + if err := cmd.Execute(d.db, d.interfaces); err != nil { log.Printf("command error (%T): %v", cmd, err) } }) diff --git a/internal/dispatcher/subnet_commands.go b/internal/dispatcher/subnet_commands.go index b23949e..dcb0398 100644 --- a/internal/dispatcher/subnet_commands.go +++ b/internal/dispatcher/subnet_commands.go @@ -14,16 +14,20 @@ type CreateSubnetCommand struct { Name string VPC string VxlanID int - LocalIP string + IfaceType string GatewayIP string CIDR string } -func (c CreateSubnetCommand) Execute(db *badger.DB) error { +func (c CreateSubnetCommand) Execute(db *badger.DB, interfaces map[string]string) error { + localIface, ok := interfaces[c.IfaceType] + if !ok { + return fmt.Errorf("unknown iface_type %q: not found in config", c.IfaceType) + } kv.AddInDB(db, "subnet/"+c.Name+"/state", "creating") kv.AddInDB(db, "subnet/"+c.Name+"/vpc", c.VPC) kv.AddInDB(db, "subnet/"+c.Name+"/vxlan_id", strconv.Itoa(c.VxlanID)) - kv.AddInDB(db, "subnet/"+c.Name+"/local_ip", c.LocalIP) + kv.AddInDB(db, "subnet/"+c.Name+"/local_iface", localIface) kv.AddInDB(db, "subnet/"+c.Name+"/gateway_ip", c.GatewayIP) kv.AddInDB(db, "subnet/"+c.Name+"/cidr", c.CIDR) return subnet.CreateSubnet(db, c.Name) @@ -33,7 +37,7 @@ type DeleteSubnetCommand struct { Name string } -func (c DeleteSubnetCommand) Execute(db *badger.DB) error { +func (c DeleteSubnetCommand) Execute(db *badger.DB, _ map[string]string) error { kv.AddInDB(db, "subnet/"+c.Name+"/state", "deleting") if err := subnet.DeleteSubnet(db, c.Name); err != nil { fmt.Println(err) diff --git a/internal/dispatcher/vpc_commands.go b/internal/dispatcher/vpc_commands.go index 195b014..2c316a3 100644 --- a/internal/dispatcher/vpc_commands.go +++ b/internal/dispatcher/vpc_commands.go @@ -10,7 +10,7 @@ type CreateVPCCommand struct { Name string } -func (c CreateVPCCommand) Execute(db *badger.DB) error { +func (c CreateVPCCommand) Execute(db *badger.DB, _ map[string]string) error { kv.AddInDB(db, "vpc/"+c.Name+"/state", "creating") return vpc.CreateVPC(db, c.Name) } @@ -19,7 +19,7 @@ type DeleteVPCCommand struct { Name string } -func (c DeleteVPCCommand) Execute(db *badger.DB) error { +func (c DeleteVPCCommand) Execute(db *badger.DB, _ map[string]string) error { kv.AddInDB(db, "vpc/"+c.Name+"/state", "deleting") if err := vpc.DeleteVPC(db, c.Name); err != nil { return err diff --git a/internal/netif/vxlan.go b/internal/netif/vxlan.go index eacae7c..6523f37 100644 --- a/internal/netif/vxlan.go +++ b/internal/netif/vxlan.go @@ -1,20 +1,22 @@ package netif import ( - "net" - "github.com/vishvananda/netlink" ) -func CreateVxlan(name string, vxlanID int, localIP net.IP) error { +func CreateVxlan(name string, vxlanID int, localIface string) error { + link, err := netlink.LinkByName(localIface) + if err != nil { + return err + } vxlan := &netlink.Vxlan{ LinkAttrs: netlink.LinkAttrs{ Name: name, }, - VxlanId: vxlanID, - Port: 4789, - SrcAddr: localIP, - Learning: false, + VxlanId: vxlanID, + Port: 4789, + VtepDevIndex: link.Attrs().Index, + Learning: false, } return netlink.LinkAdd(vxlan) } diff --git a/internal/subnet/create.go b/internal/subnet/create.go index 9bfe02a..1ed514e 100644 --- a/internal/subnet/create.go +++ b/internal/subnet/create.go @@ -40,13 +40,9 @@ func CreateSubnet(db *badger.DB, subnetName string) error { return fmt.Errorf("parse vxlan_id: %w", err) } - localIPStr, err := kv.GetFromDB(db, "subnet/"+subnetName+"/local_ip") + localIface, err := kv.GetFromDB(db, "subnet/"+subnetName+"/local_iface") if err != nil { - return fmt.Errorf("get local_ip: %w", err) - } - localIP := net.ParseIP(localIPStr) - if localIP == nil { - return fmt.Errorf("invalid local_ip: %s", localIPStr) + return fmt.Errorf("get local_iface: %w", err) } gatewayIPStr, err := kv.GetFromDB(db, "subnet/"+subnetName+"/gateway_ip") @@ -90,7 +86,7 @@ func CreateSubnet(db *badger.DB, subnetName string) error { } // vxlan - if err := netif.CreateVxlan(vxlanIface, vxlanID, localIP); err != nil { + if err := netif.CreateVxlan(vxlanIface, vxlanID, localIface); err != nil { return fmt.Errorf("create vxlan: %w", err) } From 8a68d3818fd3b76923a86810fc0ab95028d94101 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Tue, 21 Apr 2026 21:32:59 +0200 Subject: [PATCH 21/24] f-21: code: swap from interface only to full conf Signed-off-by: GnomeZworc --- cmd/agent/main.go | 2 +- internal/dispatcher/dispatcher.go | 15 ++++++++------- internal/dispatcher/subnet_commands.go | 7 ++++--- internal/dispatcher/vpc_commands.go | 5 +++-- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index faa6139..dd6fd6d 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -36,7 +36,7 @@ func main() { apiAddr := fmt.Sprintf("%s:%d", cfg.Api.Address, cfg.Api.Port) promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port) - d := dispatcher.New(q, db, cfg.Interfaces) + d := dispatcher.New(q, db, cfg) go agentapi.New(d, db).Start(apiAddr) go promserver.Start(promAddr, registry) diff --git a/internal/dispatcher/dispatcher.go b/internal/dispatcher/dispatcher.go index f240245..8e94827 100644 --- a/internal/dispatcher/dispatcher.go +++ b/internal/dispatcher/dispatcher.go @@ -3,27 +3,28 @@ package dispatcher import ( "log" + configuration "git.g3e.fr/syonad/two/internal/config/agent" "git.g3e.fr/syonad/two/pkg/worker" "github.com/dgraph-io/badger/v4" ) type Command interface { - Execute(db *badger.DB, interfaces map[string]string) error + Execute(db *badger.DB, cfg *configuration.Config) error } type Dispatcher struct { - queue *worker.Queue - db *badger.DB - interfaces map[string]string + queue *worker.Queue + db *badger.DB + cfg *configuration.Config } -func New(queue *worker.Queue, db *badger.DB, interfaces map[string]string) *Dispatcher { - return &Dispatcher{queue: queue, db: db, interfaces: interfaces} +func New(queue *worker.Queue, db *badger.DB, cfg *configuration.Config) *Dispatcher { + return &Dispatcher{queue: queue, db: db, cfg: cfg} } func (d *Dispatcher) Dispatch(cmd Command) { d.queue.Submit(func() { - if err := cmd.Execute(d.db, d.interfaces); err != nil { + if err := cmd.Execute(d.db, d.cfg); err != nil { log.Printf("command error (%T): %v", cmd, err) } }) diff --git a/internal/dispatcher/subnet_commands.go b/internal/dispatcher/subnet_commands.go index dcb0398..c2a0cff 100644 --- a/internal/dispatcher/subnet_commands.go +++ b/internal/dispatcher/subnet_commands.go @@ -5,6 +5,7 @@ import ( "os" "strconv" + configuration "git.g3e.fr/syonad/two/internal/config/agent" "git.g3e.fr/syonad/two/internal/subnet" "git.g3e.fr/syonad/two/pkg/db/kv" "github.com/dgraph-io/badger/v4" @@ -19,8 +20,8 @@ type CreateSubnetCommand struct { CIDR string } -func (c CreateSubnetCommand) Execute(db *badger.DB, interfaces map[string]string) error { - localIface, ok := interfaces[c.IfaceType] +func (c CreateSubnetCommand) Execute(db *badger.DB, cfg *configuration.Config) error { + localIface, ok := cfg.Interfaces[c.IfaceType] if !ok { return fmt.Errorf("unknown iface_type %q: not found in config", c.IfaceType) } @@ -37,7 +38,7 @@ type DeleteSubnetCommand struct { Name string } -func (c DeleteSubnetCommand) Execute(db *badger.DB, _ map[string]string) error { +func (c DeleteSubnetCommand) Execute(db *badger.DB, _ *configuration.Config) error { kv.AddInDB(db, "subnet/"+c.Name+"/state", "deleting") if err := subnet.DeleteSubnet(db, c.Name); err != nil { fmt.Println(err) diff --git a/internal/dispatcher/vpc_commands.go b/internal/dispatcher/vpc_commands.go index 2c316a3..b2687fc 100644 --- a/internal/dispatcher/vpc_commands.go +++ b/internal/dispatcher/vpc_commands.go @@ -1,6 +1,7 @@ package dispatcher import ( + configuration "git.g3e.fr/syonad/two/internal/config/agent" "git.g3e.fr/syonad/two/internal/vpc" "git.g3e.fr/syonad/two/pkg/db/kv" "github.com/dgraph-io/badger/v4" @@ -10,7 +11,7 @@ type CreateVPCCommand struct { Name string } -func (c CreateVPCCommand) Execute(db *badger.DB, _ map[string]string) error { +func (c CreateVPCCommand) Execute(db *badger.DB, _ *configuration.Config) error { kv.AddInDB(db, "vpc/"+c.Name+"/state", "creating") return vpc.CreateVPC(db, c.Name) } @@ -19,7 +20,7 @@ type DeleteVPCCommand struct { Name string } -func (c DeleteVPCCommand) Execute(db *badger.DB, _ map[string]string) error { +func (c DeleteVPCCommand) Execute(db *badger.DB, _ *configuration.Config) error { kv.AddInDB(db, "vpc/"+c.Name+"/state", "deleting") if err := vpc.DeleteVPC(db, c.Name); err != nil { return err From 7dd795f4aad8234e5d9e6fa6d4fad69d9a7854d7 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Tue, 21 Apr 2026 21:38:46 +0200 Subject: [PATCH 22/24] f-21: code: add default interface Signed-off-by: GnomeZworc --- conf/agent/config.exemple.yml | 4 +++- internal/config/agent/struct.go | 4 +++- internal/dispatcher/subnet_commands.go | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/conf/agent/config.exemple.yml b/conf/agent/config.exemple.yml index 5bd1dfd..bc1d437 100644 --- a/conf/agent/config.exemple.yml +++ b/conf/agent/config.exemple.yml @@ -1,7 +1,9 @@ database: path: "/var/lib/two/data/" +default_interface: br-000000 + interfaces: vms: br-000000 internet: br-000000 - admin: br-000000 \ No newline at end of file + admin: br-000000 diff --git a/internal/config/agent/struct.go b/internal/config/agent/struct.go index 21dde0d..1c9fc9f 100644 --- a/internal/config/agent/struct.go +++ b/internal/config/agent/struct.go @@ -20,7 +20,8 @@ type Config struct { Count int `mapstructure:"count"` BufferSize int `mapstructure:"buffer_size"` } `mapstructure:"worker"` - Interfaces map[string]string `mapstructure:"interfaces"` + DefaultInterface string `mapstructure:"default_interface"` + Interfaces map[string]string `mapstructure:"interfaces"` } func LoadConfig(path string) (*Config, error) { @@ -35,6 +36,7 @@ func LoadConfig(path string) (*Config, error) { v.SetDefault("prometheus.port", 9090) v.SetDefault("worker.count", 4) v.SetDefault("worker.buffer_size", 100) + v.SetDefault("default_interface", "br-000000") v.ReadInConfig() diff --git a/internal/dispatcher/subnet_commands.go b/internal/dispatcher/subnet_commands.go index c2a0cff..0d471c3 100644 --- a/internal/dispatcher/subnet_commands.go +++ b/internal/dispatcher/subnet_commands.go @@ -23,7 +23,7 @@ type CreateSubnetCommand struct { func (c CreateSubnetCommand) Execute(db *badger.DB, cfg *configuration.Config) error { localIface, ok := cfg.Interfaces[c.IfaceType] if !ok { - return fmt.Errorf("unknown iface_type %q: not found in config", c.IfaceType) + localIface = cfg.DefaultInterface } kv.AddInDB(db, "subnet/"+c.Name+"/state", "creating") kv.AddInDB(db, "subnet/"+c.Name+"/vpc", c.VPC) From b70fdb66f096db5cc389196c4deb2e3f7a011557 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Tue, 21 Apr 2026 21:43:17 +0200 Subject: [PATCH 23/24] f-21: api: update api Signed-off-by: GnomeZworc --- api/agent.yaml | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/api/agent.yaml b/api/agent.yaml index be328d0..2b3acc7 100644 --- a/api/agent.yaml +++ b/api/agent.yaml @@ -127,6 +127,12 @@ paths: application/json: schema: $ref: "#/components/schemas/Subnet" + "400": + description: Missing required field or unknown iface_type + content: + application/json: + schema: + $ref: "#/components/schemas/Error" "409": description: Subnet already exists content: @@ -219,7 +225,7 @@ components: SubnetCreateRequest: type: object - required: [name, vpc, vxlan_id, local_ip, gateway_ip, cidr] + required: [name, vpc, vxlan_id, gateway_ip, cidr] properties: name: type: string @@ -233,11 +239,10 @@ components: type: integer description: VXLAN VNI identifier example: 100 - local_ip: + iface_type: type: string - format: ipv4 - description: Local VTEP IP address - example: "10.0.0.5" + description: Interface type key defined in the agent config (e.g. vms, internet, admin). Falls back to default_interface if omitted or unknown. + example: vms gateway_ip: type: string format: ipv4 @@ -264,9 +269,10 @@ components: vxlan_id: type: integer example: 100 - local_ip: + local_iface: type: string - example: "10.0.0.5" + description: Resolved interface name + example: br-000000 gateway_ip: type: string example: "10.10.10.1" From 2779002c634fcc212696120cb69ee01b6c269865 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Tue, 21 Apr 2026 21:52:03 +0200 Subject: [PATCH 24/24] f-21: vpc: change to be ok in the name Signed-off-by: GnomeZworc --- api/agent.yaml | 7 ++++--- internal/vpc/create.go | 25 +++++++++---------------- internal/vpc/delete.go | 6 +++++- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/api/agent.yaml b/api/agent.yaml index 2b3acc7..1d3a77f 100644 --- a/api/agent.yaml +++ b/api/agent.yaml @@ -209,15 +209,16 @@ components: properties: name: type: string - description: Unique name for the VPC - example: vpc1 + description: Unique name for the VPC, must follow the format vp-[id] + pattern: '^vp-.+' + example: vp-00001 VPC: type: object properties: name: type: string - example: vpc1 + example: vp-00001 state: type: string enum: [creating, created, deleting, deleted] diff --git a/internal/vpc/create.go b/internal/vpc/create.go index a6e6aaa..148f70c 100644 --- a/internal/vpc/create.go +++ b/internal/vpc/create.go @@ -1,6 +1,8 @@ package vpc import ( + "strings" + "git.g3e.fr/syonad/two/internal/netif" "git.g3e.fr/syonad/two/internal/netns" "git.g3e.fr/syonad/two/pkg/db/kv" @@ -9,49 +11,40 @@ import ( ) func CreateVPC(db *badger.DB, name string) error { - // missing - // search data in db - // change state in db - - // create netns if state, err := kv.GetFromDB(db, "vpc/"+name+"/state"); err != nil { return err } else if state == "creating" { + vpcID := strings.SplitN(name, "-", 2)[1] + if err := netns.Create(name); err != nil { return err } - // create veth public for this netns - if err := netif.CreateVethToNetns("vp-"+name+"-e", "vp-public-i", "/var/run/netns/"+name, 9000); err != nil { + if err := netif.CreateVethToNetns("vp-"+vpcID+"-e", "vp-"+vpcID+"-i", "/var/run/netns/"+name, 9000); err != nil { return err } - // create public bridge in netns if err := netns.Call(name, func() error { return netif.CreateBridge("br-public", 1500) }); err != nil { return err } - // set veth to ext public bridge - if err := netif.BridgeSetMaster("vp-"+name+"-e", "br-public"); err != nil { + if err := netif.BridgeSetMaster("vp-"+vpcID+"-e", "br-public"); err != nil { return err } - // set veth to int public bridge if err := netns.Call(name, func() error { - return netif.BridgeSetMaster("vp-public-i", "br-public") + return netif.BridgeSetMaster("vp-"+vpcID+"-i", "br-public") }); err != nil { return err } - // set set ext veth up - if err := netif.LinkSetUp("vp-" + name + "-e"); err != nil { + if err := netif.LinkSetUp("vp-" + vpcID + "-e"); err != nil { return err } - // set set int veth up if err := netns.Call(name, func() error { - return netif.LinkSetUp("vp-public-i") + return netif.LinkSetUp("vp-" + vpcID + "-i") }); err != nil { return err } diff --git a/internal/vpc/delete.go b/internal/vpc/delete.go index 10c863d..dbd3a59 100644 --- a/internal/vpc/delete.go +++ b/internal/vpc/delete.go @@ -1,6 +1,8 @@ package vpc import ( + "strings" + "git.g3e.fr/syonad/two/internal/netif" "git.g3e.fr/syonad/two/internal/netns" "git.g3e.fr/syonad/two/pkg/db/kv" @@ -12,7 +14,9 @@ func DeleteVPC(db *badger.DB, name string) error { if state, err := kv.GetFromDB(db, "vpc/"+name+"/state"); err != nil { return err } else if state == "deleting" { - if err := netif.DeleteLink("vp-" + name + "-e"); err != nil { + vpcID := strings.SplitN(name, "-", 2)[1] + + if err := netif.DeleteLink("vp-" + vpcID + "-e"); err != nil { return err }