mirror of
https://github.com/YuzuZensai/netbird-kubernetes-operator.git
synced 2026-09-13 18:59:09 +00:00
Add network router and resource (#189)
This change adds two new resources, NetworkRouter and NetworkResource, which enable clusters to expose Kubernetes services to Netbird. The NetworkRouter is responsible for creating the network, group, setup key and routing peer all of which are unique to the isntance. Along with the deployment of the client in the cluster. The NetworkResource exposes a service by linking to the specific router it wants to expose to. This makes coupling between the resource and network easy to understand. Routers also set a DNS zone which is used to give names to resources based on the name and namespace of the service being exposed. Part of #172 Signed-off-by: Philip Laine <philip.laine@gmail.com>
This commit is contained in:
@@ -31,6 +31,46 @@ func Client() *netbird.Client {
|
||||
}
|
||||
return output
|
||||
})
|
||||
addHandler(mux, "networks", func(id string, input api.NetworkRequest, output api.Network) api.Network {
|
||||
output.Id = id
|
||||
output.Name = input.Name
|
||||
output.Description = input.Description
|
||||
return output
|
||||
})
|
||||
addHandler(mux, "networks/{network}/routers", func(id string, input api.NetworkRouterRequest, output api.NetworkRouter) api.NetworkRouter {
|
||||
output.Id = id
|
||||
output.Enabled = input.Enabled
|
||||
output.Masquerade = input.Masquerade
|
||||
output.Metric = input.Metric
|
||||
output.PeerGroups = input.PeerGroups
|
||||
return output
|
||||
})
|
||||
addHandler(mux, "networks/{network}/resources", func(id string, input api.NetworkResourceRequest, output api.NetworkResource) api.NetworkResource {
|
||||
output.Id = id
|
||||
output.Address = input.Address
|
||||
output.Description = input.Description
|
||||
output.Enabled = input.Enabled
|
||||
return output
|
||||
})
|
||||
addHandler(mux, "dns/zones", func(id string, input api.ZoneRequest, output api.Zone) api.Zone {
|
||||
output.Id = id
|
||||
output.Name = input.Name
|
||||
output.Domain = input.Domain
|
||||
output.DistributionGroups = input.DistributionGroups
|
||||
output.EnableSearchDomain = input.EnableSearchDomain
|
||||
if input.Enabled != nil {
|
||||
output.Enabled = *input.Enabled
|
||||
}
|
||||
return output
|
||||
})
|
||||
addHandler(mux, "dns/zones/{zone}/records", func(id string, input api.DNSRecordRequest, output api.DNSRecord) api.DNSRecord {
|
||||
output.Id = id
|
||||
output.Name = input.Name
|
||||
output.Ttl = input.Ttl
|
||||
output.Type = input.Type
|
||||
output.Content = input.Content
|
||||
return output
|
||||
})
|
||||
|
||||
srv := httptest.NewServer(mux)
|
||||
return netbird.New(srv.URL, "ABC")
|
||||
@@ -38,14 +78,33 @@ func Client() *netbird.Client {
|
||||
|
||||
func addHandler[T, U any](mux *http.ServeMux, resource string, convertFn func(string, U, T) T) {
|
||||
var itemMx sync.RWMutex
|
||||
items := map[string]T{}
|
||||
store := map[string]T{}
|
||||
|
||||
mux.Handle(fmt.Sprintf("GET /api/%s", resource), http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
itemMx.RLock()
|
||||
defer itemMx.RUnlock()
|
||||
|
||||
items := make([]T, 0, len(store))
|
||||
for _, v := range store {
|
||||
items = append(items, v)
|
||||
}
|
||||
b, err := json.Marshal(items)
|
||||
if err != nil {
|
||||
util.WriteErrorResponse("Marshal Error", http.StatusInternalServerError, rw)
|
||||
return
|
||||
}
|
||||
_, err = rw.Write(b)
|
||||
if err != nil {
|
||||
util.WriteErrorResponse("Write Error", http.StatusInternalServerError, rw)
|
||||
return
|
||||
}
|
||||
}))
|
||||
mux.Handle(fmt.Sprintf("GET /api/%s/{id}", resource), http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
itemMx.RLock()
|
||||
defer itemMx.RUnlock()
|
||||
|
||||
id := req.PathValue("id")
|
||||
respData, ok := items[id]
|
||||
respData, ok := store[id]
|
||||
if !ok {
|
||||
util.WriteErrorResponse("Not Found", http.StatusNotFound, rw)
|
||||
return
|
||||
@@ -79,7 +138,7 @@ func addHandler[T, U any](mux *http.ServeMux, resource string, convertFn func(st
|
||||
id := fmt.Sprintf("id-%d", rand.Int64())
|
||||
var zero T
|
||||
respData := convertFn(id, reqData, zero)
|
||||
items[id] = respData
|
||||
store[id] = respData
|
||||
b, err = json.Marshal(respData)
|
||||
if err != nil {
|
||||
util.WriteErrorResponse("Marshal Error", http.StatusInternalServerError, rw)
|
||||
@@ -96,7 +155,7 @@ func addHandler[T, U any](mux *http.ServeMux, resource string, convertFn func(st
|
||||
defer itemMx.Unlock()
|
||||
|
||||
id := req.PathValue("id")
|
||||
respData, ok := items[id]
|
||||
respData, ok := store[id]
|
||||
if !ok {
|
||||
util.WriteErrorResponse("Not Found", http.StatusNotFound, rw)
|
||||
return
|
||||
@@ -114,7 +173,7 @@ func addHandler[T, U any](mux *http.ServeMux, resource string, convertFn func(st
|
||||
return
|
||||
}
|
||||
respData = convertFn(id, reqData, respData)
|
||||
items[id] = respData
|
||||
store[id] = respData
|
||||
b, err = json.Marshal(respData)
|
||||
if err != nil {
|
||||
util.WriteErrorResponse("Marshal Error", http.StatusInternalServerError, rw)
|
||||
@@ -131,11 +190,11 @@ func addHandler[T, U any](mux *http.ServeMux, resource string, convertFn func(st
|
||||
defer itemMx.Unlock()
|
||||
|
||||
id := req.PathValue("id")
|
||||
_, ok := items[id]
|
||||
_, ok := store[id]
|
||||
if !ok {
|
||||
util.WriteErrorResponse("Not Found", http.StatusNotFound, rw)
|
||||
return
|
||||
}
|
||||
delete(items, id)
|
||||
delete(store, id)
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user