Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Stream changes to gui #53

Merged
merged 19 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/capacitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ func main() {
runController(err, helmReleaseController, stopCh)
eventController, err := controllers.EventController(client, dynamicClient, clientHub)
runController(err, eventController, stopCh)
deploymentController, err := controllers.DeploymentController(client, dynamicClient, clientHub)
runController(err, deploymentController, stopCh)
podController, err := controllers.PodController(client, dynamicClient, clientHub)
runController(err, podController, stopCh)
serviceController, err := controllers.ServiceController(client, dynamicClient, clientHub)
runController(err, serviceController, stopCh)
ingressController, err := controllers.IngressController(client, dynamicClient, clientHub)
runController(err, ingressController, stopCh)

r := api.SetupRouter(client, dynamicClient, config, clientHub, runningLogStreams)
go func() {
Expand Down
50 changes: 50 additions & 0 deletions pkg/controllers/deploymentController.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package controllers

import (
"encoding/json"

"github.com/gimlet-io/capacitor/pkg/flux"
"github.com/gimlet-io/capacitor/pkg/streaming"
apps_v1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)

func DeploymentController(
client *kubernetes.Clientset,
dynamicClient *dynamic.DynamicClient,
clientHub *streaming.ClientHub,
) (*Controller, error) {
deploymentListWatcher := cache.NewListWatchFromClient(client.AppsV1().RESTClient(), "deployments", v1.NamespaceAll, fields.Everything())
deploymentController := NewController(
"deployment",
deploymentListWatcher,
&apps_v1.Deployment{},
func(informerEvent Event, objectMeta meta_v1.ObjectMeta, obj interface{}) error {
switch informerEvent.eventType {
case "create":
fallthrough
case "update":
fallthrough
case "delete":
services, err := flux.Services(client, dynamicClient)
if err != nil {
panic(err.Error())
}
servicesBytes, err := json.Marshal(streaming.Envelope{
Type: streaming.SERVICES_RECEIVED,
Payload: services,
})
if err != nil {
panic(err.Error())
}
clientHub.Broadcast <- servicesBytes
}
return nil
})
return deploymentController, nil
}
50 changes: 50 additions & 0 deletions pkg/controllers/ingressController.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package controllers

import (
"encoding/json"

"github.com/gimlet-io/capacitor/pkg/flux"
"github.com/gimlet-io/capacitor/pkg/streaming"
v1 "k8s.io/api/core/v1"
networking_v1 "k8s.io/api/networking/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)

func IngressController(
client *kubernetes.Clientset,
dynamicClient *dynamic.DynamicClient,
clientHub *streaming.ClientHub,
) (*Controller, error) {
ingressListWatcher := cache.NewListWatchFromClient(client.NetworkingV1().RESTClient(), "ingresses", v1.NamespaceAll, fields.Everything())
ingressController := NewController(
"ingress",
ingressListWatcher,
&networking_v1.Ingress{},
func(informerEvent Event, objectMeta meta_v1.ObjectMeta, obj interface{}) error {
switch informerEvent.eventType {
case "create":
fallthrough
case "update":
fallthrough
case "delete":
services, err := flux.Services(client, dynamicClient)
if err != nil {
panic(err.Error())
}
servicesBytes, err := json.Marshal(streaming.Envelope{
Type: streaming.SERVICES_RECEIVED,
Payload: services,
})
if err != nil {
panic(err.Error())
}
clientHub.Broadcast <- servicesBytes
}
return nil
})
return ingressController, nil
}
49 changes: 49 additions & 0 deletions pkg/controllers/podController.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package controllers

import (
"encoding/json"

"github.com/gimlet-io/capacitor/pkg/flux"
"github.com/gimlet-io/capacitor/pkg/streaming"
v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)

func PodController(
client *kubernetes.Clientset,
dynamicClient *dynamic.DynamicClient,
clientHub *streaming.ClientHub,
) (*Controller, error) {
podListWatcher := cache.NewListWatchFromClient(client.CoreV1().RESTClient(), "pods", v1.NamespaceAll, fields.Everything())
podController := NewController(
"pod",
podListWatcher,
&v1.Pod{},
func(informerEvent Event, objectMeta meta_v1.ObjectMeta, obj interface{}) error {
switch informerEvent.eventType {
case "create":
fallthrough
case "update":
fallthrough
case "delete":
services, err := flux.Services(client, dynamicClient)
if err != nil {
panic(err.Error())
}
servicesBytes, err := json.Marshal(streaming.Envelope{
Type: streaming.SERVICES_RECEIVED,
Payload: services,
})
if err != nil {
panic(err.Error())
}
clientHub.Broadcast <- servicesBytes
}
return nil
})
return podController, nil
}
49 changes: 49 additions & 0 deletions pkg/controllers/serviceController.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package controllers

import (
"encoding/json"

"github.com/gimlet-io/capacitor/pkg/flux"
"github.com/gimlet-io/capacitor/pkg/streaming"
v1 "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)

func ServiceController(
client *kubernetes.Clientset,
dynamicClient *dynamic.DynamicClient,
clientHub *streaming.ClientHub,
) (*Controller, error) {
serviceListWatcher := cache.NewListWatchFromClient(client.CoreV1().RESTClient(), "services", v1.NamespaceAll, fields.Everything())
serviceController := NewController(
"service",
serviceListWatcher,
&v1.Service{},
func(informerEvent Event, objectMeta meta_v1.ObjectMeta, obj interface{}) error {
switch informerEvent.eventType {
case "create":
fallthrough
case "update":
fallthrough
case "delete":
services, err := flux.Services(client, dynamicClient)
if err != nil {
panic(err.Error())
}
servicesBytes, err := json.Marshal(streaming.Envelope{
Type: streaming.SERVICES_RECEIVED,
Payload: services,
})
if err != nil {
panic(err.Error())
}
clientHub.Broadcast <- servicesBytes
}
return nil
})
return serviceController, nil
}
1 change: 1 addition & 0 deletions pkg/streaming/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package streaming
const (
FLUX_STATE_RECEIVED string = "FLUX_STATE_RECEIVED"
FLUX_EVENTS_RECEIVED string = "FLUX_EVENTS_RECEIVED"
SERVICES_RECEIVED string = "SERVICES_RECEIVED"
POD_LOGS_RECEIVED string = "POD_LOGS_RECEIVED"
)

Expand Down
Loading