| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- package kubernetes
- import (
- "bufio"
- "context"
- "fmt"
- "io"
- "strings"
- "github.com/porter-dev/porter/internal/kubernetes/provisioner"
- "github.com/porter-dev/porter/internal/kubernetes/provisioner/aws"
- "github.com/porter-dev/porter/internal/kubernetes/provisioner/aws/ecr"
- "github.com/porter-dev/porter/internal/kubernetes/provisioner/aws/eks"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/models/integrations"
- "github.com/gorilla/websocket"
- "github.com/porter-dev/porter/internal/helm/grapher"
- appsv1 "k8s.io/api/apps/v1"
- batchv1 "k8s.io/api/batch/v1"
- v1 "k8s.io/api/core/v1"
- v1beta1 "k8s.io/api/extensions/v1beta1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/cli-runtime/pkg/genericclioptions"
- "k8s.io/client-go/informers"
- "k8s.io/client-go/kubernetes"
- "k8s.io/client-go/tools/cache"
- "github.com/porter-dev/porter/internal/config"
- )
- // Agent is a Kubernetes agent for performing operations that interact with the
- // api server
- type Agent struct {
- RESTClientGetter genericclioptions.RESTClientGetter
- Clientset kubernetes.Interface
- }
- type Message struct {
- EventType string
- Object interface{}
- Kind string
- }
- type ListOptions struct {
- FieldSelector string
- }
- // ListNamespaces simply lists namespaces
- func (a *Agent) ListNamespaces() (*v1.NamespaceList, error) {
- return a.Clientset.CoreV1().Namespaces().List(
- context.TODO(),
- metav1.ListOptions{},
- )
- }
- // GetIngress gets ingress given the name and namespace
- func (a *Agent) GetIngress(namespace string, name string) (*v1beta1.Ingress, error) {
- return a.Clientset.ExtensionsV1beta1().Ingresses(namespace).Get(
- context.TODO(),
- name,
- metav1.GetOptions{},
- )
- }
- // GetDeployment gets the deployment given the name and namespace
- func (a *Agent) GetDeployment(c grapher.Object) (*appsv1.Deployment, error) {
- return a.Clientset.AppsV1().Deployments(c.Namespace).Get(
- context.TODO(),
- c.Name,
- metav1.GetOptions{},
- )
- }
- // GetStatefulSet gets the statefulset given the name and namespace
- func (a *Agent) GetStatefulSet(c grapher.Object) (*appsv1.StatefulSet, error) {
- return a.Clientset.AppsV1().StatefulSets(c.Namespace).Get(
- context.TODO(),
- c.Name,
- metav1.GetOptions{},
- )
- }
- // GetReplicaSet gets the replicaset given the name and namespace
- func (a *Agent) GetReplicaSet(c grapher.Object) (*appsv1.ReplicaSet, error) {
- return a.Clientset.AppsV1().ReplicaSets(c.Namespace).Get(
- context.TODO(),
- c.Name,
- metav1.GetOptions{},
- )
- }
- // GetDaemonSet gets the daemonset by name and namespace
- func (a *Agent) GetDaemonSet(c grapher.Object) (*appsv1.DaemonSet, error) {
- return a.Clientset.AppsV1().DaemonSets(c.Namespace).Get(
- context.TODO(),
- c.Name,
- metav1.GetOptions{},
- )
- }
- // GetPodsByLabel retrieves pods with matching labels
- func (a *Agent) GetPodsByLabel(selector string) (*v1.PodList, error) {
- // Search in all namespaces for matching pods
- return a.Clientset.CoreV1().Pods("").List(
- context.TODO(),
- metav1.ListOptions{
- LabelSelector: selector,
- },
- )
- }
- // GetPodLogs streams real-time logs from a given pod.
- func (a *Agent) GetPodLogs(namespace string, name string, conn *websocket.Conn) error {
- // follow logs
- tails := int64(30)
- podLogOpts := v1.PodLogOptions{
- Follow: true,
- TailLines: &tails,
- }
- req := a.Clientset.CoreV1().Pods(namespace).GetLogs(name, &podLogOpts)
- podLogs, err := req.Stream(context.TODO())
- if err != nil {
- return fmt.Errorf("Cannot open log stream for pod %s", name)
- }
- defer podLogs.Close()
- r := bufio.NewReader(podLogs)
- errorchan := make(chan error)
- go func() {
- // listens for websocket closing handshake
- for {
- if _, _, err := conn.ReadMessage(); err != nil {
- defer conn.Close()
- errorchan <- nil
- fmt.Println("Successfully closed log stream")
- return
- }
- }
- }()
- go func() {
- for {
- select {
- case <-errorchan:
- defer close(errorchan)
- return
- default:
- }
- bytes, err := r.ReadBytes('\n')
- if writeErr := conn.WriteMessage(websocket.TextMessage, bytes); writeErr != nil {
- errorchan <- writeErr
- return
- }
- if err != nil {
- if err != io.EOF {
- errorchan <- err
- return
- }
- errorchan <- nil
- return
- }
- }
- }()
- for {
- select {
- case err = <-errorchan:
- return err
- }
- }
- }
- // StreamControllerStatus streams controller status. Supports Deployment, StatefulSet, ReplicaSet, and DaemonSet
- // TODO: Support Jobs
- func (a *Agent) StreamControllerStatus(conn *websocket.Conn, kind string) error {
- factory := informers.NewSharedInformerFactory(
- a.Clientset,
- 0,
- )
- var informer cache.SharedInformer
- // Spins up an informer depending on kind. Convert to lowercase for robustness
- switch strings.ToLower(kind) {
- case "deployment":
- informer = factory.Apps().V1().Deployments().Informer()
- case "statefulset":
- informer = factory.Apps().V1().StatefulSets().Informer()
- case "replicaset":
- informer = factory.Apps().V1().ReplicaSets().Informer()
- case "daemonset":
- informer = factory.Apps().V1().DaemonSets().Informer()
- }
- stopper := make(chan struct{})
- errorchan := make(chan error)
- defer close(errorchan)
- informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
- UpdateFunc: func(oldObj, newObj interface{}) {
- msg := Message{
- EventType: "UPDATE",
- Object: newObj,
- Kind: strings.ToLower(kind),
- }
- if writeErr := conn.WriteJSON(msg); writeErr != nil {
- errorchan <- writeErr
- return
- }
- },
- })
- go func() {
- // listens for websocket closing handshake
- for {
- if _, _, err := conn.ReadMessage(); err != nil {
- defer conn.Close()
- defer close(stopper)
- defer fmt.Println("Successfully closed controller status stream")
- errorchan <- nil
- return
- }
- }
- }()
- go informer.Run(stopper)
- for {
- select {
- case err := <-errorchan:
- return err
- }
- }
- }
- // ProvisionECR spawns a new provisioning pod that creates an ECR instance
- func (a *Agent) ProvisionECR(
- projectID uint,
- awsConf *integrations.AWSIntegration,
- ecrName string,
- awsInfra *models.AWSInfra,
- operation provisioner.ProvisionerOperation,
- pgConf *config.DBConf,
- redisConf *config.RedisConf,
- ) (*batchv1.Job, error) {
- id := awsInfra.GetID()
- prov := &provisioner.Conf{
- ID: id,
- Name: fmt.Sprintf("prov-%s-%s", id, string(operation)),
- Kind: provisioner.ECR,
- Operation: operation,
- Redis: redisConf,
- Postgres: pgConf,
- AWS: &aws.Conf{
- AWSRegion: awsConf.AWSRegion,
- AWSAccessKeyID: string(awsConf.AWSAccessKeyID),
- AWSSecretAccessKey: string(awsConf.AWSSecretAccessKey),
- },
- ECR: &ecr.Conf{
- ECRName: ecrName,
- },
- }
- return a.provision(prov)
- }
- // ProvisionEKS spawns a new provisioning pod that creates an EKS instance
- func (a *Agent) ProvisionEKS(
- projectID uint,
- awsConf *integrations.AWSIntegration,
- eksName string,
- awsInfra *models.AWSInfra,
- operation provisioner.ProvisionerOperation,
- pgConf *config.DBConf,
- redisConf *config.RedisConf,
- ) (*batchv1.Job, error) {
- id := awsInfra.GetID()
- prov := &provisioner.Conf{
- ID: id,
- Name: fmt.Sprintf("prov-%s-%s", id, string(operation)),
- Kind: provisioner.EKS,
- Operation: operation,
- Redis: redisConf,
- Postgres: pgConf,
- AWS: &aws.Conf{
- AWSRegion: awsConf.AWSRegion,
- AWSAccessKeyID: string(awsConf.AWSAccessKeyID),
- AWSSecretAccessKey: string(awsConf.AWSSecretAccessKey),
- },
- EKS: &eks.Conf{
- ClusterName: eksName,
- },
- }
- return a.provision(prov)
- }
- // ProvisionTest spawns a new provisioning pod that tests provisioning
- func (a *Agent) ProvisionTest(
- projectID uint,
- operation provisioner.ProvisionerOperation,
- pgConf *config.DBConf,
- redisConf *config.RedisConf,
- ) (*batchv1.Job, error) {
- prov := &provisioner.Conf{
- ID: fmt.Sprintf("%s-%d", "testing", projectID),
- Name: fmt.Sprintf("prov-%s-%d-%s", "testing", projectID, string(operation)),
- Operation: operation,
- Kind: provisioner.Test,
- Redis: redisConf,
- Postgres: pgConf,
- }
- return a.provision(prov)
- }
- func (a *Agent) provision(
- prov *provisioner.Conf,
- ) (*batchv1.Job, error) {
- prov.Namespace = "default"
- job, err := prov.GetProvisionerJobTemplate()
- if err != nil {
- return nil, err
- }
- return a.Clientset.BatchV1().Jobs(prov.Namespace).Create(
- context.TODO(),
- job,
- metav1.CreateOptions{},
- )
- }
|