k8s

package
v0.0.0-...-e5d423c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package k8s provides Kubernetes client functionality for StreamSpace CRD operations.

This file implements the Kubernetes client wrapper for StreamSpace custom resources.

Purpose: - Provide typed access to StreamSpace CRDs (Session, Template) - Wrap Kubernetes dynamic client for custom resource operations - Simplify CRUD operations on Sessions and Templates - Watch for changes to sessions and templates - Access core Kubernetes resources (Pods, Services, PVCs, Nodes)

Features: - Session management (create, get, list, update, delete, watch) - Template management (create, get, list, delete, watch) - State transitions (running → hibernated → terminated) - Resource filtering (by user, category, labels) - Cluster resource introspection (nodes, pods, services) - Auto-configuration (in-cluster or kubeconfig)

Custom Resource Definitions:

  • Sessions (stream.space/v1alpha1)

  • Represents a user's containerized workspace session

  • States: running, hibernated, terminated

  • Includes resource limits, idle timeout, persistence settings

  • Templates (stream.space/v1alpha1)

  • Defines application templates (Firefox, VS Code, etc.)

  • Contains container image, VNC/webapp config, resources

  • Categorized for catalog organization

Implementation Details: - Uses Kubernetes dynamic client for CRD operations - Auto-detects in-cluster vs local configuration - Parses unstructured data to typed Go structs - Supports namespace isolation (default: "streamspace") - Thread-safe client operations

Thread Safety: - Kubernetes clients are thread-safe - Safe for concurrent access across goroutines

Dependencies: - k8s.io/client-go for Kubernetes API access - k8s.io/api for core resource types

Example Usage:

// Initialize client
client, err := k8s.NewClient()
if err != nil {
    log.Fatal(err)
}

// Create a session
session := &k8s.Session{
    Name:           "user1-firefox",
    Namespace:      "streamspace",
    User:           "user1",
    Template:       "firefox-browser",
    State:          "running",
    PersistentHome: true,
    IdleTimeout:    "30m",
}
created, err := client.CreateSession(ctx, session)

// List sessions for a user
sessions, err := client.ListSessionsByUser(ctx, "streamspace", "user1")

// Update session state (hibernate)
updated, err := client.UpdateSessionState(ctx, "streamspace", "user1-firefox", "hibernated")

// Watch for session changes
watcher, err := client.WatchSessions(ctx, "streamspace")
for event := range watcher.ResultChan() {
    session := event.Object.(*unstructured.Unstructured)
    // Handle session change
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApplicationInstall

type ApplicationInstall struct {
	Name              string
	Namespace         string
	CatalogTemplateID int
	TemplateName      string
	DisplayName       string
	Description       string
	Category          string
	Icon              string
	Manifest          string // YAML manifest for the Template
	InstalledBy       string
	// Status fields
	Phase               string // Pending, Creating, Ready, Failed
	StatusMessage       string
	LastTransitionTime  *time.Time
	CreatedTemplateName string
	CreatedTemplateNS   string
}

ApplicationInstall represents a request to install an application The controller watches these and creates the corresponding Template CRD

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client wraps Kubernetes clients for StreamSpace CRD operations

func NewClient

func NewClient() (*Client, error)

NewClient creates a new Kubernetes client

func (*Client) CordonNode

func (c *Client) CordonNode(ctx context.Context, name string) error

CordonNode marks a node as unschedulable

func (*Client) CreateApplicationInstall

func (c *Client) CreateApplicationInstall(ctx context.Context, appInstall *ApplicationInstall) (*ApplicationInstall, error)

CreateApplicationInstall creates a new ApplicationInstall resource The controller will watch this and create the corresponding Template CRD

func (*Client) CreateSession

func (c *Client) CreateSession(ctx context.Context, session *Session) (*Session, error)

CreateSession creates a new Session resource

func (*Client) CreateTemplate

func (c *Client) CreateTemplate(ctx context.Context, template *Template) (*Template, error)

CreateTemplate creates a new Template resource

func (*Client) DeleteApplicationInstall

func (c *Client) DeleteApplicationInstall(ctx context.Context, namespace, name string) error

DeleteApplicationInstall deletes an ApplicationInstall

func (*Client) DeleteSession

func (c *Client) DeleteSession(ctx context.Context, namespace, name string) error

DeleteSession deletes a Session

func (*Client) DeleteTemplate

func (c *Client) DeleteTemplate(ctx context.Context, namespace, name string) error

DeleteTemplate deletes a Template

func (*Client) DrainNode

func (c *Client) DrainNode(ctx context.Context, name string, gracePeriodSeconds *int64) error

DrainNode evicts all pods from a node

func (*Client) GetApplicationInstall

func (c *Client) GetApplicationInstall(ctx context.Context, namespace, name string) (*ApplicationInstall, error)

GetApplicationInstall retrieves an ApplicationInstall by name

func (*Client) GetClientset

func (c *Client) GetClientset() *kubernetes.Clientset

GetClientset returns the underlying Kubernetes clientset

func (*Client) GetDynamicClient

func (c *Client) GetDynamicClient() dynamic.Interface

GetDynamicClient returns the underlying dynamic client

func (*Client) GetNamespaces

func (c *Client) GetNamespaces(ctx context.Context) (*corev1.NamespaceList, error)

GetNamespaces returns all namespaces

func (*Client) GetNode

func (c *Client) GetNode(ctx context.Context, name string) (*corev1.Node, error)

GetNode returns a specific node by name

func (*Client) GetNodes

func (c *Client) GetNodes(ctx context.Context) (*corev1.NodeList, error)

GetNodes returns all cluster nodes

func (*Client) GetPVCs

func (c *Client) GetPVCs(ctx context.Context, namespace string) (*corev1.PersistentVolumeClaimList, error)

GetPVCs returns PVCs in a namespace

func (*Client) GetPods

func (c *Client) GetPods(ctx context.Context, namespace string) (*corev1.PodList, error)

GetPods returns pods in a namespace

func (*Client) GetServices

func (c *Client) GetServices(ctx context.Context, namespace string) (*corev1.ServiceList, error)

GetServices returns services in a namespace

func (*Client) GetSession

func (c *Client) GetSession(ctx context.Context, namespace, name string) (*Session, error)

GetSession retrieves a Session by name

func (*Client) GetTemplate

func (c *Client) GetTemplate(ctx context.Context, namespace, name string) (*Template, error)

GetTemplate retrieves a Template by name

func (*Client) ListApplicationInstalls

func (c *Client) ListApplicationInstalls(ctx context.Context, namespace string) ([]*ApplicationInstall, error)

ListApplicationInstalls lists all ApplicationInstalls in a namespace

func (*Client) ListSessions

func (c *Client) ListSessions(ctx context.Context, namespace string) ([]*Session, error)

ListSessions lists all Sessions in a namespace

func (*Client) ListSessionsByUser

func (c *Client) ListSessionsByUser(ctx context.Context, namespace, user string) ([]*Session, error)

ListSessionsByUser lists Sessions for a specific user

func (*Client) ListTemplates

func (c *Client) ListTemplates(ctx context.Context, namespace string) ([]*Template, error)

ListTemplates lists all Templates in a namespace

func (*Client) ListTemplatesByCategory

func (c *Client) ListTemplatesByCategory(ctx context.Context, namespace, category string) ([]*Template, error)

ListTemplatesByCategory lists Templates by category

func (*Client) PatchNode

func (c *Client) PatchNode(ctx context.Context, name string, patchData []byte) error

PatchNode applies a patch to a node

func (*Client) UncordonNode

func (c *Client) UncordonNode(ctx context.Context, name string) error

UncordonNode marks a node as schedulable

func (*Client) UpdateApplicationInstallStatus

func (c *Client) UpdateApplicationInstallStatus(ctx context.Context, namespace, name string, phase, message string) error

UpdateApplicationInstallStatus updates the status of an ApplicationInstall

func (*Client) UpdateNodeTaints

func (c *Client) UpdateNodeTaints(ctx context.Context, name string, taints []corev1.Taint) error

UpdateNodeTaints updates the taints on a node

func (*Client) UpdateSession

func (c *Client) UpdateSession(ctx context.Context, session *Session) error

UpdateSession updates a Session resource

func (*Client) UpdateSessionState

func (c *Client) UpdateSessionState(ctx context.Context, namespace, name, state string) (*Session, error)

UpdateSessionState updates the state of a Session (running, hibernated, terminated)

func (*Client) UpdateSessionStatus

func (c *Client) UpdateSessionStatus(ctx context.Context, session *Session) error

UpdateSessionStatus updates a Session's status subresource

func (*Client) WatchApplicationInstalls

func (c *Client) WatchApplicationInstalls(ctx context.Context, namespace string) (watch.Interface, error)

WatchApplicationInstalls watches for ApplicationInstall changes

func (*Client) WatchSessions

func (c *Client) WatchSessions(ctx context.Context, namespace string) (watch.Interface, error)

WatchSessions watches for Session changes

func (*Client) WatchTemplates

func (c *Client) WatchTemplates(ctx context.Context, namespace string) (watch.Interface, error)

WatchTemplates watches for Template changes

type Session

type Session struct {
	Name      string
	Namespace string
	User      string
	Template  string
	State     string // running, hibernated, terminated
	Resources struct {
		Memory string
		CPU    string
	}
	PersistentHome     bool
	IdleTimeout        string
	MaxSessionDuration string
	Tags               []string
	Status             SessionStatus
	CreatedAt          time.Time
}

Session represents a StreamSpace Session CRD

type SessionStatus

type SessionStatus struct {
	Phase         string // Pending, Running, Hibernated, Failed, Terminated
	PodName       string
	URL           string
	LastActivity  *time.Time
	ResourceUsage struct {
		Memory string
		CPU    string
	}
	Conditions []metav1.Condition
}

SessionStatus represents the status of a Session

type Template

type Template struct {
	Name             string
	Namespace        string
	DisplayName      string
	Description      string
	Category         string
	Icon             string
	BaseImage        string
	AppType          string // desktop, webapp
	DefaultResources struct {
		Memory string
		CPU    string
	}
	Ports []struct {
		Name          string
		ContainerPort int32
		Protocol      string
	}
	Env          []corev1.EnvVar
	VolumeMounts []corev1.VolumeMount
	VNC          *VNCConfig
	WebApp       *WebAppConfig
	Capabilities []string
	Tags         []string
	Featured     bool // Whether template is featured in catalog
	UsageCount   int  // Number of times template has been used
	CreatedAt    time.Time
}

Template represents a StreamSpace Template CRD

type VNCConfig

type VNCConfig struct {
	Enabled  bool
	Port     int32
	Protocol string
}

VNCConfig represents VNC configuration for desktop apps

type WebAppConfig

type WebAppConfig struct {
	Enabled bool
	Port    int32
	Path    string
}

WebAppConfig represents webapp configuration for native web apps

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL