| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345 |
- package kubernetes
- import (
- "bufio"
- "bytes"
- "compress/gzip"
- "context"
- "encoding/base64"
- "encoding/json"
- "fmt"
- "io"
- "io/ioutil"
- "strings"
- "time"
- "github.com/porter-dev/porter/api/server/shared/config/env"
- "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/kubernetes/provisioner/do"
- "github.com/porter-dev/porter/internal/kubernetes/provisioner/do/docr"
- "github.com/porter-dev/porter/internal/kubernetes/provisioner/do/doks"
- "github.com/porter-dev/porter/internal/kubernetes/provisioner/gcp"
- "github.com/porter-dev/porter/internal/kubernetes/provisioner/gcp/gke"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/models/integrations"
- "github.com/porter-dev/porter/internal/oauth"
- "github.com/porter-dev/porter/internal/registry"
- "github.com/porter-dev/porter/internal/repository"
- "golang.org/x/oauth2"
- errors2 "errors"
- "github.com/gorilla/websocket"
- "github.com/porter-dev/porter/internal/helm/grapher"
- appsv1 "k8s.io/api/apps/v1"
- batchv1 "k8s.io/api/batch/v1"
- batchv1beta1 "k8s.io/api/batch/v1beta1"
- v1 "k8s.io/api/core/v1"
- v1beta1 "k8s.io/api/extensions/v1beta1"
- "k8s.io/apimachinery/pkg/api/errors"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/runtime"
- "k8s.io/apimachinery/pkg/runtime/schema"
- "k8s.io/apimachinery/pkg/types"
- "k8s.io/cli-runtime/pkg/genericclioptions"
- "k8s.io/client-go/informers"
- "k8s.io/client-go/kubernetes"
- "k8s.io/client-go/rest"
- "k8s.io/client-go/tools/cache"
- "k8s.io/client-go/tools/remotecommand"
- rspb "helm.sh/helm/v3/pkg/release"
- )
- // 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 `json:"event_type"`
- Object interface{}
- Kind string
- }
- type ListOptions struct {
- FieldSelector string
- }
- type AuthError struct{}
- func (e *AuthError) Error() string {
- return "Unauthorized error"
- }
- // UpdateClientset updates the Agent's Clientset (this refreshes auth tokens)
- func (a *Agent) UpdateClientset() error {
- restConf, err := a.RESTClientGetter.ToRESTConfig()
- if err != nil {
- return err
- }
- clientset, err := kubernetes.NewForConfig(restConf)
- if err != nil {
- return err
- }
- a.Clientset = clientset
- return nil
- }
- // CreateConfigMap creates the configmap given the key-value pairs and namespace
- func (a *Agent) CreateConfigMap(name string, namespace string, configMap map[string]string) (*v1.ConfigMap, error) {
- return a.Clientset.CoreV1().ConfigMaps(namespace).Create(
- context.TODO(),
- &v1.ConfigMap{
- ObjectMeta: metav1.ObjectMeta{
- Name: name,
- Namespace: namespace,
- Labels: map[string]string{
- "porter": "true",
- },
- },
- Data: configMap,
- },
- metav1.CreateOptions{},
- )
- }
- // CreateLinkedSecret creates a secret given the key-value pairs and namespace. Values are
- // base64 encoded
- func (a *Agent) CreateLinkedSecret(name, namespace, cmName string, data map[string][]byte) (*v1.Secret, error) {
- return a.Clientset.CoreV1().Secrets(namespace).Create(
- context.TODO(),
- &v1.Secret{
- ObjectMeta: metav1.ObjectMeta{
- Name: name,
- Namespace: namespace,
- Labels: map[string]string{
- "porter": "true",
- "configmap": cmName,
- },
- },
- Data: data,
- },
- metav1.CreateOptions{},
- )
- }
- type mergeConfigMapData struct {
- Data map[string]*string `json:"data"`
- }
- // UpdateConfigMap updates the configmap given its name and namespace
- func (a *Agent) UpdateConfigMap(name string, namespace string, configMap map[string]string) (*v1.ConfigMap, error) {
- cmData := make(map[string]*string)
- for key, val := range configMap {
- valCopy := val
- cmData[key] = &valCopy
- if len(val) == 0 {
- cmData[key] = nil
- }
- }
- mergeCM := &mergeConfigMapData{
- Data: cmData,
- }
- patchBytes, err := json.Marshal(mergeCM)
- if err != nil {
- return nil, err
- }
- return a.Clientset.CoreV1().ConfigMaps(namespace).Patch(
- context.Background(),
- name,
- types.MergePatchType,
- patchBytes,
- metav1.PatchOptions{},
- )
- }
- type mergeLinkedSecretData struct {
- Data map[string]*[]byte `json:"data"`
- }
- // UpdateLinkedSecret updates the secret given its name and namespace
- func (a *Agent) UpdateLinkedSecret(name, namespace, cmName string, data map[string][]byte) error {
- secretData := make(map[string]*[]byte)
- for key, val := range data {
- valCopy := val
- secretData[key] = &valCopy
- if len(val) == 0 {
- secretData[key] = nil
- }
- }
- mergeSecret := &mergeLinkedSecretData{
- Data: secretData,
- }
- patchBytes, err := json.Marshal(mergeSecret)
- if err != nil {
- return err
- }
- _, err = a.Clientset.CoreV1().Secrets(namespace).Patch(
- context.TODO(),
- name,
- types.MergePatchType,
- patchBytes,
- metav1.PatchOptions{},
- )
- return err
- }
- // DeleteConfigMap deletes the configmap given its name and namespace
- func (a *Agent) DeleteConfigMap(name string, namespace string) error {
- return a.Clientset.CoreV1().ConfigMaps(namespace).Delete(
- context.TODO(),
- name,
- metav1.DeleteOptions{},
- )
- }
- // DeleteLinkedSecret deletes the secret given its name and namespace
- func (a *Agent) DeleteLinkedSecret(name, namespace string) error {
- return a.Clientset.CoreV1().Secrets(namespace).Delete(
- context.TODO(),
- name,
- metav1.DeleteOptions{},
- )
- }
- // GetConfigMap retrieves the configmap given its name and namespace
- func (a *Agent) GetConfigMap(name string, namespace string) (*v1.ConfigMap, error) {
- return a.Clientset.CoreV1().ConfigMaps(namespace).Get(
- context.TODO(),
- name,
- metav1.GetOptions{},
- )
- }
- // GetSecret retrieves the secret given its name and namespace
- func (a *Agent) GetSecret(name string, namespace string) (*v1.Secret, error) {
- return a.Clientset.CoreV1().Secrets(namespace).Get(
- context.TODO(),
- name,
- metav1.GetOptions{},
- )
- }
- // ListConfigMaps simply lists namespaces
- func (a *Agent) ListConfigMaps(namespace string) (*v1.ConfigMapList, error) {
- return a.Clientset.CoreV1().ConfigMaps(namespace).List(
- context.TODO(),
- metav1.ListOptions{
- LabelSelector: "porter=true",
- },
- )
- }
- // ListEvents lists the events of a given object.
- func (a *Agent) ListEvents(name string, namespace string) (*v1.EventList, error) {
- return a.Clientset.CoreV1().Events(namespace).List(
- context.TODO(),
- metav1.ListOptions{
- FieldSelector: fmt.Sprintf("involvedObject.name=%s,involvedObject.namespace=%s", name, namespace),
- },
- )
- }
- // ListNamespaces simply lists namespaces
- func (a *Agent) ListNamespaces() (*v1.NamespaceList, error) {
- return a.Clientset.CoreV1().Namespaces().List(
- context.TODO(),
- metav1.ListOptions{},
- )
- }
- // CreateNamespace creates a namespace with the given name.
- func (a *Agent) CreateNamespace(name string) (*v1.Namespace, error) {
- namespace := v1.Namespace{
- ObjectMeta: metav1.ObjectMeta{
- Name: name,
- },
- }
- return a.Clientset.CoreV1().Namespaces().Create(
- context.TODO(),
- &namespace,
- metav1.CreateOptions{},
- )
- }
- // DeleteNamespace deletes the namespace given the name.
- func (a *Agent) DeleteNamespace(name string) error {
- return a.Clientset.CoreV1().Namespaces().Delete(
- context.TODO(),
- name,
- metav1.DeleteOptions{},
- )
- }
- // ListJobsByLabel lists jobs in a namespace matching a label
- type Label struct {
- Key string
- Val string
- }
- func (a *Agent) ListJobsByLabel(namespace string, labels ...Label) ([]batchv1.Job, error) {
- selectors := make([]string, 0)
- for _, label := range labels {
- selectors = append(selectors, fmt.Sprintf("%s=%s", label.Key, label.Val))
- }
- resp, err := a.Clientset.BatchV1().Jobs(namespace).List(
- context.TODO(),
- metav1.ListOptions{
- LabelSelector: strings.Join(selectors, ","),
- },
- )
- if err != nil {
- return nil, err
- }
- return resp.Items, nil
- }
- // DeleteJob deletes the job in the given name and namespace.
- func (a *Agent) DeleteJob(name, namespace string) error {
- return a.Clientset.BatchV1().Jobs(namespace).Delete(
- context.TODO(),
- name,
- metav1.DeleteOptions{},
- )
- }
- // GetJobPods lists all pods belonging to a job in a namespace
- func (a *Agent) GetJobPods(namespace, jobName string) ([]v1.Pod, error) {
- resp, err := a.Clientset.CoreV1().Pods(namespace).List(
- context.TODO(),
- metav1.ListOptions{
- LabelSelector: fmt.Sprintf("%s=%s", "job-name", jobName),
- },
- )
- if err != nil {
- return nil, err
- }
- return resp.Items, nil
- }
- // GetIngress gets ingress given the name and namespace
- func (a *Agent) GetIngress(namespace string, name string) (*v1beta1.Ingress, error) {
- resp, err := a.Clientset.ExtensionsV1beta1().Ingresses(namespace).Get(
- context.TODO(),
- name,
- metav1.GetOptions{},
- )
- if err != nil && errors.IsNotFound(err) {
- return nil, IsNotFoundError
- } else if err != nil {
- return nil, err
- }
- return resp, nil
- }
- var IsNotFoundError = fmt.Errorf("not found")
- // GetDeployment gets the deployment given the name and namespace
- func (a *Agent) GetDeployment(c grapher.Object) (*appsv1.Deployment, error) {
- res, err := a.Clientset.AppsV1().Deployments(c.Namespace).Get(
- context.TODO(),
- c.Name,
- metav1.GetOptions{},
- )
- if err != nil && errors.IsNotFound(err) {
- return nil, IsNotFoundError
- } else if err != nil {
- return nil, err
- }
- res.Kind = c.Kind
- return res, nil
- }
- // GetStatefulSet gets the statefulset given the name and namespace
- func (a *Agent) GetStatefulSet(c grapher.Object) (*appsv1.StatefulSet, error) {
- res, err := a.Clientset.AppsV1().StatefulSets(c.Namespace).Get(
- context.TODO(),
- c.Name,
- metav1.GetOptions{},
- )
- if err != nil && errors.IsNotFound(err) {
- return nil, IsNotFoundError
- } else if err != nil {
- return nil, err
- }
- res.Kind = c.Kind
- return res, nil
- }
- // GetReplicaSet gets the replicaset given the name and namespace
- func (a *Agent) GetReplicaSet(c grapher.Object) (*appsv1.ReplicaSet, error) {
- res, err := a.Clientset.AppsV1().ReplicaSets(c.Namespace).Get(
- context.TODO(),
- c.Name,
- metav1.GetOptions{},
- )
- if err != nil && errors.IsNotFound(err) {
- return nil, IsNotFoundError
- } else if err != nil {
- return nil, err
- }
- res.Kind = c.Kind
- return res, nil
- }
- // GetDaemonSet gets the daemonset by name and namespace
- func (a *Agent) GetDaemonSet(c grapher.Object) (*appsv1.DaemonSet, error) {
- res, err := a.Clientset.AppsV1().DaemonSets(c.Namespace).Get(
- context.TODO(),
- c.Name,
- metav1.GetOptions{},
- )
- if err != nil && errors.IsNotFound(err) {
- return nil, IsNotFoundError
- } else if err != nil {
- return nil, err
- }
- res.Kind = c.Kind
- return res, nil
- }
- // GetJob gets the job by name and namespace
- func (a *Agent) GetJob(c grapher.Object) (*batchv1.Job, error) {
- res, err := a.Clientset.BatchV1().Jobs(c.Namespace).Get(
- context.TODO(),
- c.Name,
- metav1.GetOptions{},
- )
- if err != nil && errors.IsNotFound(err) {
- return nil, IsNotFoundError
- } else if err != nil {
- return nil, err
- }
- res.Kind = c.Kind
- return res, nil
- }
- // GetCronJob gets the CronJob by name and namespace
- func (a *Agent) GetCronJob(c grapher.Object) (*batchv1beta1.CronJob, error) {
- res, err := a.Clientset.BatchV1beta1().CronJobs(c.Namespace).Get(
- context.TODO(),
- c.Name,
- metav1.GetOptions{},
- )
- if err != nil && errors.IsNotFound(err) {
- return nil, IsNotFoundError
- } else if err != nil {
- return nil, err
- }
- res.Kind = c.Kind
- return res, nil
- }
- // GetPodsByLabel retrieves pods with matching labels
- func (a *Agent) GetPodsByLabel(selector string, namespace string) (*v1.PodList, error) {
- // Search in all namespaces for matching pods
- return a.Clientset.CoreV1().Pods(namespace).List(
- context.TODO(),
- metav1.ListOptions{
- LabelSelector: selector,
- },
- )
- }
- // DeletePod deletes a pod by name and namespace
- func (a *Agent) DeletePod(namespace string, name string) error {
- err := a.Clientset.CoreV1().Pods(namespace).Delete(
- context.TODO(),
- name,
- metav1.DeleteOptions{},
- )
- if err != nil && errors.IsNotFound(err) {
- return IsNotFoundError
- }
- return err
- }
- // GetPodLogs streams real-time logs from a given pod.
- func (a *Agent) GetPodLogs(namespace string, name string, conn *websocket.Conn) error {
- // get the pod to read in the list of contains
- pod, err := a.Clientset.CoreV1().Pods(namespace).Get(
- context.Background(),
- name,
- metav1.GetOptions{},
- )
- if err != nil && errors.IsNotFound(err) {
- return IsNotFoundError
- } else if err != nil {
- return fmt.Errorf("Cannot get logs from pod %s: %s", name, err.Error())
- }
- container := pod.Spec.Containers[0].Name
- tails := int64(400)
- // follow logs
- podLogOpts := v1.PodLogOptions{
- Follow: true,
- TailLines: &tails,
- Container: container,
- }
- 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: %s", name, err.Error())
- }
- 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
- 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
- }
- }
- }
- // StopJobWithJobSidecar sends a termination signal to a job running with a sidecar
- func (a *Agent) StopJobWithJobSidecar(namespace, name string) error {
- jobPods, err := a.GetJobPods(namespace, name)
- if err != nil {
- return err
- }
- podName := jobPods[0].ObjectMeta.Name
- restConf, err := a.RESTClientGetter.ToRESTConfig()
- restConf.GroupVersion = &schema.GroupVersion{
- Group: "api",
- Version: "v1",
- }
- restConf.NegotiatedSerializer = runtime.NewSimpleNegotiatedSerializer(runtime.SerializerInfo{})
- restClient, err := rest.RESTClientFor(restConf)
- if err != nil {
- return err
- }
- req := restClient.Post().
- Resource("pods").
- Name(podName).
- Namespace(namespace).
- SubResource("exec")
- req.Param("command", "./signal.sh")
- req.Param("container", "sidecar")
- req.Param("stdin", "true")
- req.Param("stdout", "false")
- req.Param("tty", "false")
- exec, err := remotecommand.NewSPDYExecutor(restConf, "POST", req.URL())
- if err != nil {
- return err
- }
- return exec.Stream(remotecommand.StreamOptions{
- Tty: false,
- Stdin: strings.NewReader("./signal.sh"),
- })
- }
- // RunWebsocketTask will run a websocket task. If the websocket returns an anauthorized error, it will restart
- // the task some number of times until failing
- func (a *Agent) RunWebsocketTask(task func() error) error {
- lastTime := int64(0)
- for {
- if err := a.UpdateClientset(); err != nil {
- return err
- }
- err := task()
- if err == nil {
- return nil
- }
- if !errors2.Is(err, &AuthError{}) {
- return err
- }
- if time.Now().Unix()-lastTime < 60 { // don't regenerate connection if too many unauthorized errors
- return err
- }
- lastTime = time.Now().Unix()
- }
- }
- // StreamControllerStatus streams controller status. Supports Deployment, StatefulSet, ReplicaSet, and DaemonSet
- // TODO: Support Jobs
- func (a *Agent) StreamControllerStatus(conn *websocket.Conn, kind string, selectors string) error {
- run := func() error {
- // selectors is an array of max length 1. StreamControllerStatus accepts calls without the selectors argument.
- // selectors argument is a single string with comma separated key=value pairs. (e.g. "app=porter,porter=true")
- tweakListOptionsFunc := func(options *metav1.ListOptions) {
- options.LabelSelector = selectors
- }
- factory := informers.NewSharedInformerFactoryWithOptions(
- a.Clientset,
- 0,
- informers.WithTweakListOptions(tweakListOptionsFunc),
- )
- 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()
- case "job":
- informer = factory.Batch().V1().Jobs().Informer()
- case "cronjob":
- informer = factory.Batch().V1beta1().CronJobs().Informer()
- case "namespace":
- informer = factory.Core().V1().Namespaces().Informer()
- case "pod":
- informer = factory.Core().V1().Pods().Informer()
- }
- stopper := make(chan struct{})
- errorchan := make(chan error)
- defer close(stopper)
- informer.SetWatchErrorHandler(func(r *cache.Reflector, err error) {
- if strings.HasSuffix(err.Error(), ": Unauthorized") {
- errorchan <- &AuthError{}
- }
- })
- 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
- }
- },
- AddFunc: func(obj interface{}) {
- msg := Message{
- EventType: "ADD",
- Object: obj,
- Kind: strings.ToLower(kind),
- }
- if writeErr := conn.WriteJSON(msg); writeErr != nil {
- errorchan <- writeErr
- return
- }
- },
- DeleteFunc: func(obj interface{}) {
- msg := Message{
- EventType: "DELETE",
- Object: obj,
- 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 {
- conn.Close()
- errorchan <- nil
- return
- }
- }
- }()
- go informer.Run(stopper)
- for {
- select {
- case err := <-errorchan:
- return err
- }
- }
- }
- return a.RunWebsocketTask(run)
- }
- var b64 = base64.StdEncoding
- var magicGzip = []byte{0x1f, 0x8b, 0x08}
- func decodeRelease(data string) (*rspb.Release, error) {
- // base64 decode string
- b, err := b64.DecodeString(data)
- if err != nil {
- return nil, err
- }
- // For backwards compatibility with releases that were stored before
- // compression was introduced we skip decompression if the
- // gzip magic header is not found
- if bytes.Equal(b[0:3], magicGzip) {
- r, err := gzip.NewReader(bytes.NewReader(b))
- if err != nil {
- return nil, err
- }
- defer r.Close()
- b2, err := ioutil.ReadAll(r)
- if err != nil {
- return nil, err
- }
- b = b2
- }
- var rls rspb.Release
- // unmarshal release object bytes
- if err := json.Unmarshal(b, &rls); err != nil {
- return nil, err
- }
- return &rls, nil
- }
- func contains(s []string, str string) bool {
- for _, v := range s {
- if v == str {
- return true
- }
- }
- return false
- }
- func parseSecretToHelmRelease(secret v1.Secret, chartList []string) (*rspb.Release, bool, error) {
- if secret.Type != "helm.sh/release.v1" {
- return nil, true, nil
- }
- releaseData, ok := secret.Data["release"]
- if !ok {
- return nil, true, fmt.Errorf("release field not found")
- }
- helm_object, err := decodeRelease(string(releaseData))
- if err != nil {
- return nil, true, err
- }
- if len(chartList) > 0 && !contains(chartList, helm_object.Name) {
- return nil, true, nil
- }
- return helm_object, false, nil
- }
- func (a *Agent) StreamHelmReleases(conn *websocket.Conn, namespace string, chartList []string, selectors string) error {
- run := func() error {
- tweakListOptionsFunc := func(options *metav1.ListOptions) {
- options.LabelSelector = selectors
- }
- factory := informers.NewSharedInformerFactoryWithOptions(
- a.Clientset,
- 0,
- informers.WithTweakListOptions(tweakListOptionsFunc),
- informers.WithNamespace(namespace),
- )
- informer := factory.Core().V1().Secrets().Informer()
- stopper := make(chan struct{})
- errorchan := make(chan error)
- defer close(stopper)
- informer.SetWatchErrorHandler(func(r *cache.Reflector, err error) {
- if strings.HasSuffix(err.Error(), ": Unauthorized") {
- errorchan <- &AuthError{}
- }
- })
- informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
- UpdateFunc: func(oldObj, newObj interface{}) {
- secretObj, ok := newObj.(*v1.Secret)
- if !ok {
- errorchan <- fmt.Errorf("could not cast to secret")
- return
- }
- helm_object, isNotHelmRelease, err := parseSecretToHelmRelease(*secretObj, chartList)
- if isNotHelmRelease && err == nil {
- return
- }
- if err != nil {
- errorchan <- err
- return
- }
- msg := Message{
- EventType: "UPDATE",
- Object: helm_object,
- }
- if writeErr := conn.WriteJSON(msg); writeErr != nil {
- errorchan <- writeErr
- return
- }
- },
- AddFunc: func(obj interface{}) {
- secretObj, ok := obj.(*v1.Secret)
- if !ok {
- errorchan <- fmt.Errorf("could not cast to secret")
- return
- }
- helm_object, isNotHelmRelease, err := parseSecretToHelmRelease(*secretObj, chartList)
- if isNotHelmRelease && err == nil {
- return
- }
- if err != nil {
- errorchan <- err
- return
- }
- msg := Message{
- EventType: "ADD",
- Object: helm_object,
- }
- if writeErr := conn.WriteJSON(msg); writeErr != nil {
- errorchan <- writeErr
- return
- }
- },
- DeleteFunc: func(obj interface{}) {
- secretObj, ok := obj.(*v1.Secret)
- if !ok {
- errorchan <- fmt.Errorf("could not cast to secret")
- return
- }
- helm_object, isNotHelmRelease, err := parseSecretToHelmRelease(*secretObj, chartList)
- if isNotHelmRelease && err == nil {
- return
- }
- if err != nil {
- errorchan <- err
- return
- }
- msg := Message{
- EventType: "DELETE",
- Object: helm_object,
- }
- if writeErr := conn.WriteJSON(msg); writeErr != nil {
- errorchan <- writeErr
- return
- }
- },
- })
- go func() {
- // listens for websocket closing handshake
- for {
- if _, _, err := conn.ReadMessage(); err != nil {
- conn.Close()
- errorchan <- nil
- return
- }
- }
- }()
- go informer.Run(stopper)
- for {
- select {
- case err := <-errorchan:
- return err
- }
- }
- }
- return a.RunWebsocketTask(run)
- }
- type SharedProvisionOpts struct {
- ProjectID uint
- Repo repository.Repository
- Infra *models.Infra
- Operation provisioner.ProvisionerOperation
- PGConf *env.DBConf
- RedisConf *env.RedisConf
- ProvImageTag string
- ProvImagePullSecret string
- }
- // ProvisionECR spawns a new provisioning pod that creates an ECR instance
- func (a *Agent) ProvisionECR(
- opts *SharedProvisionOpts,
- awsConf *integrations.AWSIntegration,
- ecrName string,
- ) (*batchv1.Job, error) {
- id := opts.Infra.GetUniqueName()
- prov := &provisioner.Conf{
- ID: id,
- Name: fmt.Sprintf("prov-%s-%s", id, string(opts.Operation)),
- Kind: provisioner.ECR,
- Operation: opts.Operation,
- Redis: opts.RedisConf,
- Postgres: opts.PGConf,
- ProvisionerImageTag: opts.ProvImageTag,
- ImagePullSecret: opts.ProvImagePullSecret,
- LastApplied: opts.Infra.LastApplied,
- AWS: &aws.Conf{
- AWSRegion: awsConf.AWSRegion,
- AWSAccessKeyID: string(awsConf.AWSAccessKeyID),
- AWSSecretAccessKey: string(awsConf.AWSSecretAccessKey),
- },
- ECR: &ecr.Conf{
- ECRName: ecrName,
- },
- }
- return a.provision(prov, opts.Infra, opts.Repo)
- }
- // ProvisionEKS spawns a new provisioning pod that creates an EKS instance
- func (a *Agent) ProvisionEKS(
- opts *SharedProvisionOpts,
- awsConf *integrations.AWSIntegration,
- eksName, machineType string,
- ) (*batchv1.Job, error) {
- id := opts.Infra.GetUniqueName()
- prov := &provisioner.Conf{
- ID: id,
- Name: fmt.Sprintf("prov-%s-%s", id, string(opts.Operation)),
- Kind: provisioner.EKS,
- Operation: opts.Operation,
- Redis: opts.RedisConf,
- Postgres: opts.PGConf,
- ProvisionerImageTag: opts.ProvImageTag,
- ImagePullSecret: opts.ProvImagePullSecret,
- LastApplied: opts.Infra.LastApplied,
- AWS: &aws.Conf{
- AWSRegion: awsConf.AWSRegion,
- AWSAccessKeyID: string(awsConf.AWSAccessKeyID),
- AWSSecretAccessKey: string(awsConf.AWSSecretAccessKey),
- },
- EKS: &eks.Conf{
- ClusterName: eksName,
- MachineType: machineType,
- },
- }
- return a.provision(prov, opts.Infra, opts.Repo)
- }
- // ProvisionGCR spawns a new provisioning pod that creates a GCR instance
- func (a *Agent) ProvisionGCR(
- opts *SharedProvisionOpts,
- gcpConf *integrations.GCPIntegration,
- ) (*batchv1.Job, error) {
- id := opts.Infra.GetUniqueName()
- prov := &provisioner.Conf{
- ID: id,
- Name: fmt.Sprintf("prov-%s-%s", id, string(opts.Operation)),
- Kind: provisioner.GCR,
- Operation: opts.Operation,
- Redis: opts.RedisConf,
- Postgres: opts.PGConf,
- ProvisionerImageTag: opts.ProvImageTag,
- ImagePullSecret: opts.ProvImagePullSecret,
- LastApplied: opts.Infra.LastApplied,
- GCP: &gcp.Conf{
- GCPRegion: gcpConf.GCPRegion,
- GCPProjectID: gcpConf.GCPProjectID,
- GCPKeyData: string(gcpConf.GCPKeyData),
- },
- }
- return a.provision(prov, opts.Infra, opts.Repo)
- }
- // ProvisionGKE spawns a new provisioning pod that creates a GKE instance
- func (a *Agent) ProvisionGKE(
- opts *SharedProvisionOpts,
- gcpConf *integrations.GCPIntegration,
- gkeName string,
- ) (*batchv1.Job, error) {
- id := opts.Infra.GetUniqueName()
- prov := &provisioner.Conf{
- ID: id,
- Name: fmt.Sprintf("prov-%s-%s", id, string(opts.Operation)),
- Kind: provisioner.GKE,
- Operation: opts.Operation,
- Redis: opts.RedisConf,
- Postgres: opts.PGConf,
- ProvisionerImageTag: opts.ProvImageTag,
- ImagePullSecret: opts.ProvImagePullSecret,
- LastApplied: opts.Infra.LastApplied,
- GCP: &gcp.Conf{
- GCPRegion: gcpConf.GCPRegion,
- GCPProjectID: gcpConf.GCPProjectID,
- GCPKeyData: string(gcpConf.GCPKeyData),
- },
- GKE: &gke.Conf{
- ClusterName: gkeName,
- },
- }
- return a.provision(prov, opts.Infra, opts.Repo)
- }
- // ProvisionDOCR spawns a new provisioning pod that creates a DOCR instance
- func (a *Agent) ProvisionDOCR(
- opts *SharedProvisionOpts,
- doConf *integrations.OAuthIntegration,
- doAuth *oauth2.Config,
- docrName, docrSubscriptionTier string,
- ) (*batchv1.Job, error) {
- // get the token
- oauthInt, err := opts.Repo.OAuthIntegration().ReadOAuthIntegration(
- opts.ProjectID,
- opts.Infra.DOIntegrationID,
- )
- if err != nil {
- return nil, err
- }
- tok, _, err := oauth.GetAccessToken(oauthInt.SharedOAuthModel, doAuth, oauth.MakeUpdateOAuthIntegrationTokenFunction(oauthInt, opts.Repo))
- if err != nil {
- return nil, err
- }
- id := opts.Infra.GetUniqueName()
- prov := &provisioner.Conf{
- ID: id,
- Name: fmt.Sprintf("prov-%s-%s", id, string(opts.Operation)),
- Kind: provisioner.DOCR,
- Operation: opts.Operation,
- Redis: opts.RedisConf,
- Postgres: opts.PGConf,
- ProvisionerImageTag: opts.ProvImageTag,
- ImagePullSecret: opts.ProvImagePullSecret,
- LastApplied: opts.Infra.LastApplied,
- DO: &do.Conf{
- DOToken: tok,
- },
- DOCR: &docr.Conf{
- DOCRName: docrName,
- DOCRSubscriptionTier: docrSubscriptionTier,
- },
- }
- return a.provision(prov, opts.Infra, opts.Repo)
- }
- // ProvisionDOKS spawns a new provisioning pod that creates a DOKS instance
- func (a *Agent) ProvisionDOKS(
- opts *SharedProvisionOpts,
- doConf *integrations.OAuthIntegration,
- doAuth *oauth2.Config,
- doRegion, doksClusterName string,
- ) (*batchv1.Job, error) {
- // get the token
- oauthInt, err := opts.Repo.OAuthIntegration().ReadOAuthIntegration(
- opts.ProjectID,
- opts.Infra.DOIntegrationID,
- )
- if err != nil {
- return nil, err
- }
- tok, _, err := oauth.GetAccessToken(oauthInt.SharedOAuthModel, doAuth, oauth.MakeUpdateOAuthIntegrationTokenFunction(oauthInt, opts.Repo))
- if err != nil {
- return nil, err
- }
- id := opts.Infra.GetUniqueName()
- prov := &provisioner.Conf{
- ID: id,
- Name: fmt.Sprintf("prov-%s-%s", id, string(opts.Operation)),
- Kind: provisioner.DOKS,
- Operation: opts.Operation,
- Redis: opts.RedisConf,
- Postgres: opts.PGConf,
- LastApplied: opts.Infra.LastApplied,
- ProvisionerImageTag: opts.ProvImageTag,
- ImagePullSecret: opts.ProvImagePullSecret,
- DO: &do.Conf{
- DOToken: tok,
- },
- DOKS: &doks.Conf{
- DORegion: doRegion,
- DOKSClusterName: doksClusterName,
- },
- }
- return a.provision(prov, opts.Infra, opts.Repo)
- }
- // ProvisionTest spawns a new provisioning pod that tests provisioning
- func (a *Agent) ProvisionTest(
- opts *SharedProvisionOpts,
- ) (*batchv1.Job, error) {
- id := opts.Infra.GetUniqueName()
- prov := &provisioner.Conf{
- ID: id,
- Name: fmt.Sprintf("prov-%s-%s", id, string(opts.Operation)),
- Operation: opts.Operation,
- Kind: provisioner.Test,
- Redis: opts.RedisConf,
- Postgres: opts.PGConf,
- ProvisionerImageTag: opts.ProvImageTag,
- ImagePullSecret: opts.ProvImagePullSecret,
- }
- return a.provision(prov, opts.Infra, opts.Repo)
- }
- func (a *Agent) provision(
- prov *provisioner.Conf,
- infra *models.Infra,
- repo repository.Repository,
- ) (*batchv1.Job, error) {
- prov.Namespace = "default"
- job, err := prov.GetProvisionerJobTemplate()
- if err != nil {
- return nil, err
- }
- job, err = a.Clientset.BatchV1().Jobs(prov.Namespace).Create(
- context.TODO(),
- job,
- metav1.CreateOptions{},
- )
- if err != nil {
- return nil, err
- }
- infra.LastApplied = prov.LastApplied
- infra, err = repo.Infra().UpdateInfra(infra)
- if err != nil {
- return nil, err
- }
- return job, nil
- }
- // CreateImagePullSecrets will create the required image pull secrets and
- // return a map from the registry name to the name of the secret.
- func (a *Agent) CreateImagePullSecrets(
- repo repository.Repository,
- namespace string,
- linkedRegs map[string]*models.Registry,
- doAuth *oauth2.Config,
- ) (map[string]string, error) {
- res := make(map[string]string)
- for key, val := range linkedRegs {
- _reg := registry.Registry(*val)
- data, err := _reg.GetDockerConfigJSON(repo, doAuth)
- if err != nil {
- return nil, err
- }
- secretName := fmt.Sprintf("porter-%s-%d", val.ToRegistryType().Service, val.ID)
- secret, err := a.Clientset.CoreV1().Secrets(namespace).Get(
- context.TODO(),
- secretName,
- metav1.GetOptions{},
- )
- // if not found, create the secret
- if err != nil && errors.IsNotFound(err) {
- _, err = a.Clientset.CoreV1().Secrets(namespace).Create(
- context.TODO(),
- &v1.Secret{
- ObjectMeta: metav1.ObjectMeta{
- Name: secretName,
- },
- Data: map[string][]byte{
- string(v1.DockerConfigJsonKey): data,
- },
- Type: v1.SecretTypeDockerConfigJson,
- },
- metav1.CreateOptions{},
- )
- if err != nil {
- return nil, err
- }
- // add secret name to the map
- res[key] = secretName
- continue
- } else if err != nil {
- return nil, err
- }
- // otherwise, check that the secret contains the correct data: if
- // if doesn't, update it
- if !bytes.Equal(secret.Data[v1.DockerConfigJsonKey], data) {
- _, err := a.Clientset.CoreV1().Secrets(namespace).Update(
- context.TODO(),
- &v1.Secret{
- ObjectMeta: metav1.ObjectMeta{
- Name: secretName,
- },
- Data: map[string][]byte{
- string(v1.DockerConfigJsonKey): data,
- },
- Type: v1.SecretTypeDockerConfigJson,
- },
- metav1.UpdateOptions{},
- )
- if err != nil {
- return nil, err
- }
- }
- // add secret name to the map
- res[key] = secretName
- }
- return res, nil
- }
|