blob: 8328817aab629a0ac5dc3cc7a85fd551757ba235 [file] [log] [blame]
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +04001package installer
2
3import (
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +04004 "bytes"
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +04005 "context"
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +04006 "fmt"
gioe72b54f2024-04-22 10:44:41 +04007 "io"
8 "net/http"
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +04009
giof6ad2982024-08-23 17:42:49 +040010 "github.com/giolekva/pcloud/core/installer/kube"
11
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040012 corev1 "k8s.io/api/core/v1"
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040013 "k8s.io/apimachinery/pkg/api/errors"
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040014 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15 "k8s.io/client-go/kubernetes"
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040016)
17
18type NamespaceCreator interface {
19 Create(name string) error
20}
21
Giorgi Lekveishvili1caed362023-12-13 16:29:43 +040022type ZoneInfo struct {
23 Zone string
24 Records string
25}
26
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040027type ZoneStatusFetcher interface {
gioe72b54f2024-04-22 10:44:41 +040028 Fetch(addr string) (string, error)
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040029}
30
gio0eaf2712024-04-14 13:08:46 +040031type noOpNamespaceCreator struct{}
32
33func (n *noOpNamespaceCreator) Create(name string) error {
34 return nil
35}
36
37func NewNoOpNamespaceCreator() NamespaceCreator {
38 return &noOpNamespaceCreator{}
39}
40
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040041type realNamespaceCreator struct {
42 clientset *kubernetes.Clientset
43}
44
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040045func (n *realNamespaceCreator) Create(name string) error {
46 _, err := n.clientset.CoreV1().Namespaces().Create(context.Background(), &corev1.Namespace{
47 TypeMeta: metav1.TypeMeta{
48 Kind: " ",
49 APIVersion: "",
50 },
51 ObjectMeta: metav1.ObjectMeta{
52 Name: name,
53 },
54 }, metav1.CreateOptions{})
Giorgi Lekveishvilia1e77902023-11-06 14:48:27 +040055 if err != nil && errors.IsAlreadyExists(err) {
56 return nil
57 }
Giorgi Lekveishvili7fb28bf2023-06-24 19:51:16 +040058 return err
59}
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040060
gioe72b54f2024-04-22 10:44:41 +040061// TODO(gio): take http client
62type realZoneStatusFetcher struct{}
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040063
gioe72b54f2024-04-22 10:44:41 +040064func (f *realZoneStatusFetcher) Fetch(addr string) (string, error) {
65 fmt.Printf("--- %s\n", addr)
66 resp, err := http.Get(addr)
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040067 if err != nil {
gioe72b54f2024-04-22 10:44:41 +040068 return "", err
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040069 }
gioe72b54f2024-04-22 10:44:41 +040070 var buf bytes.Buffer
71 if _, err := io.Copy(&buf, resp.Body); err != nil {
72 return "", err
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040073 }
gioe72b54f2024-04-22 10:44:41 +040074 return buf.String(), nil
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040075}
76
giof6ad2982024-08-23 17:42:49 +040077func NewNamespaceCreator(opts kube.KubeConfigOpts) (NamespaceCreator, error) {
78 clientset, err := kube.NewKubeClient(opts)
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040079 if err != nil {
80 return nil, err
81 }
82 return &realNamespaceCreator{clientset}, nil
83}
84
85func NewZoneStatusFetcher(kubeconfig string) (ZoneStatusFetcher, error) {
gioe72b54f2024-04-22 10:44:41 +040086 return &realZoneStatusFetcher{}, nil
Giorgi Lekveishvilicd9e42c2023-12-13 09:49:44 +040087}