Backend: API handlers, WebSocket manager, K8s client, CRDT, auth
This commit is contained in:
50
server/internal/k8s/client.go
Normal file
50
server/internal/k8s/client.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
clientset *kubernetes.Clientset
|
||||
}
|
||||
|
||||
func NewClient() (*Client, error) {
|
||||
config, err := rest.InClusterConfig()
|
||||
if err != nil {
|
||||
// Fall back to out-of-cluster config for local development
|
||||
// config, err = clientcmd.BuildConfigFromFlags("", filepath.Join(home, ".kube", "config"))
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
}
|
||||
|
||||
clientset, err := kubernetes.NewForConfig(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Client{
|
||||
clientset: clientset,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) Clientset() *kubernetes.Clientset {
|
||||
return c.clientset
|
||||
}
|
||||
|
||||
func (c *Client) GetPods(ctx context.Context, namespace string) ([]v1.Pod, error) {
|
||||
pods, err := c.clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pods.Items, nil
|
||||
}
|
||||
|
||||
func (c *Client) WatchPods(ctx context.Context, namespace string, resourceVersion string) (watch.Interface, error) {
|
||||
return c.clientset.CoreV1().Pods(namespace).Watch(ctx, metav1.ListOptions{
|
||||
ResourceVersion: resourceVersion,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user