feature/tt/initial-implementation (#1)
Some checks failed
Gitea Docker Build Demo / Test (push) Successful in 2m24s
Gitea Docker Build Demo / Build_Image (push) Failing after 1m40s
Release / Test (release) Successful in 2m22s
Release / Build_Image (release) Failing after 44s

Reviewed-on: #1
Co-authored-by: Tobias Trabelsi <lerentis@uploadfilter24.eu>
Co-committed-by: Tobias Trabelsi <lerentis@uploadfilter24.eu>
This commit is contained in:
2025-10-06 09:21:57 +00:00
committed by lerentis
parent 33921aa992
commit e51594fdd7
17 changed files with 774 additions and 1 deletions

111
internal/k8s.go Normal file
View File

@@ -0,0 +1,111 @@
package internal
import (
"bytes"
"context"
"fmt"
"html/template"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)
var IP_POOL_TEMPLATE = `
{
"apiVersion": "cilium.io/v2alpha1",
"kind": "CiliumLoadBalancerIPPool",
"metadata": {
"name": "{{ .Name }}",
"annotations": {
"argocd.argoproj.io/tracking-id": "cilium-lb:cilium.io/CiliumLoadBalancerIPPool:kube-system/covidnetes-pool"
}
},
"spec": {
"blocks": [
{{- range $i, $ip := .IPs }}
{{- if $i}},{{ end }}
{
"cidr": "{{ $ip }}"
}
{{- end }}
],
"disabled": false
}
}
`
type CrdConfig struct {
Name string
IPs []string
}
func RecreateIPPoolCrd(cfg *Config, name string, ips []string) error {
routeclient, err := createRestClient()
if err != nil {
return fmt.Errorf("error creating REST Client: %v", err.Error())
}
body, err := generateIpPool(name, ips)
if err != nil {
return fmt.Errorf("error generating CRD: %v", err.Error())
}
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(body), nil, nil)
if err != nil {
return fmt.Errorf("could not deserialize CRD: %v", err.Error())
}
res := routeclient.Post().
Resource("routes").
Body(&obj).
Do(context.TODO())
var status int
res.StatusCode(&status)
if status >= 200 && status <= 400 {
return fmt.Errorf("failed to post CRD to kube api: %v", res.Error().Error())
}
return nil
}
func createRestClient() (*rest.RESTClient, error) {
k8s_config, err := rest.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("could not create in cluster k8s config: %v", err)
}
k8s_config.APIPath = "/apis"
k8s_config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()
routeclient, err := rest.RESTClientFor(k8s_config)
if err != nil {
return nil, fmt.Errorf("could not create k8s client: %v", err)
}
return routeclient, nil
}
func generateIpPool(name string, ips []string) (string, error) {
config := CrdConfig{
Name: name,
IPs: ips,
}
tmpl, err := template.New("ippool").Parse(IP_POOL_TEMPLATE)
if err != nil {
return "", fmt.Errorf("errors in ippool template: %s", err.Error())
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, &config)
if err != nil {
return "", fmt.Errorf("could not render ippool template: %s", err.Error())
}
return buf.String(), nil
}