2023-12-19 00:17:56 -05:00
|
|
|
package scheduler_k3s
|
|
|
|
|
|
|
|
|
|
import (
|
2024-01-17 21:48:06 -05:00
|
|
|
"bufio"
|
2024-01-17 18:06:18 -05:00
|
|
|
"context"
|
2024-01-23 01:29:50 -05:00
|
|
|
"crypto/rand"
|
2024-01-17 18:06:18 -05:00
|
|
|
"encoding/base64"
|
2024-01-18 16:55:09 -05:00
|
|
|
"encoding/json"
|
2024-01-18 05:28:07 -05:00
|
|
|
"errors"
|
2023-12-19 00:17:56 -05:00
|
|
|
"fmt"
|
2024-01-17 21:48:06 -05:00
|
|
|
"io"
|
2024-01-12 23:55:42 -05:00
|
|
|
"os"
|
2024-01-17 20:56:30 -05:00
|
|
|
"os/signal"
|
2024-01-17 18:06:18 -05:00
|
|
|
"path/filepath"
|
2024-01-22 06:42:03 -05:00
|
|
|
"sort"
|
2024-01-17 18:06:18 -05:00
|
|
|
"strconv"
|
2024-01-17 20:56:30 -05:00
|
|
|
"strings"
|
|
|
|
|
"syscall"
|
2024-01-17 18:06:18 -05:00
|
|
|
"time"
|
2023-12-19 00:17:56 -05:00
|
|
|
|
2024-01-18 18:42:55 -05:00
|
|
|
appjson "github.com/dokku/dokku/plugins/app-json"
|
2023-12-19 00:17:56 -05:00
|
|
|
"github.com/dokku/dokku/plugins/common"
|
2024-01-17 18:06:18 -05:00
|
|
|
"github.com/dokku/dokku/plugins/config"
|
2024-01-18 06:08:18 -05:00
|
|
|
"github.com/dokku/dokku/plugins/cron"
|
2024-01-17 21:48:06 -05:00
|
|
|
"github.com/fatih/color"
|
2024-01-18 06:08:18 -05:00
|
|
|
"github.com/kballard/go-shellquote"
|
2024-01-18 16:55:09 -05:00
|
|
|
"github.com/ryanuber/columnize"
|
2024-01-17 20:56:30 -05:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2024-01-17 21:48:06 -05:00
|
|
|
v1 "k8s.io/api/core/v1"
|
2024-01-18 05:28:07 -05:00
|
|
|
"k8s.io/kubernetes/pkg/client/conditions"
|
2024-01-17 21:48:06 -05:00
|
|
|
"k8s.io/utils/ptr"
|
2023-12-19 00:17:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TriggerInstall runs the install step for the scheduler-k3s plugin
|
|
|
|
|
func TriggerInstall() error {
|
|
|
|
|
if err := common.PropertySetup("scheduler-k3s"); err != nil {
|
|
|
|
|
return fmt.Errorf("Unable to install the scheduler-k3s plugin: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TriggerPostAppCloneSetup creates new scheduler-k3s files
|
|
|
|
|
func TriggerPostAppCloneSetup(oldAppName string, newAppName string) error {
|
|
|
|
|
err := common.PropertyClone("scheduler-k3s", oldAppName, newAppName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TriggerPostAppRenameSetup renames scheduler-k3s files
|
|
|
|
|
func TriggerPostAppRenameSetup(oldAppName string, newAppName string) error {
|
|
|
|
|
if err := common.PropertyClone("scheduler-k3s", oldAppName, newAppName); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := common.PropertyDestroy("scheduler-k3s", oldAppName); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 06:38:12 -05:00
|
|
|
// TriggerPostDelete destroys the scheduler-k3s data for a given app container
|
|
|
|
|
func TriggerPostDelete(appName string) error {
|
2023-12-19 00:17:56 -05:00
|
|
|
dataErr := common.RemoveAppDataDirectory("scheduler-k3s", appName)
|
|
|
|
|
propertyErr := common.PropertyDestroy("scheduler-k3s", appName)
|
|
|
|
|
|
|
|
|
|
if dataErr != nil {
|
|
|
|
|
return dataErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return propertyErr
|
|
|
|
|
}
|
2024-01-12 02:39:51 -05:00
|
|
|
|
2024-01-17 18:06:18 -05:00
|
|
|
// TriggerSchedulerDeploy deploys an image tag for a given application
|
|
|
|
|
func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) error {
|
|
|
|
|
if scheduler != "k3s" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
s, err := common.PlugnTriggerOutput("ps-current-scale", []string{appName}...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
processes, err := common.ParseScaleOutput(s)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 05:34:41 -05:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
signals := make(chan os.Signal, 1)
|
|
|
|
|
signal.Notify(signals, os.Interrupt, syscall.SIGHUP,
|
|
|
|
|
syscall.SIGINT,
|
|
|
|
|
syscall.SIGQUIT,
|
|
|
|
|
syscall.SIGTERM)
|
2024-01-22 05:12:49 -05:00
|
|
|
go func() {
|
2024-01-22 05:34:41 -05:00
|
|
|
<-signals
|
2024-01-22 05:12:49 -05:00
|
|
|
common.LogWarn(fmt.Sprintf("Deployment of %s has been cancelled", appName))
|
|
|
|
|
cancel()
|
|
|
|
|
}()
|
|
|
|
|
|
2024-01-22 08:01:11 -05:00
|
|
|
namespace := getComputedNamespace(appName)
|
2024-01-17 18:06:18 -05:00
|
|
|
if err := createKubernetesNamespace(ctx, namespace); err != nil {
|
|
|
|
|
return fmt.Errorf("Error creating kubernetes namespace for deployment: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
image, err := common.GetDeployingAppImageName(appName, imageTag, "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting deploying app image name: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 08:01:11 -05:00
|
|
|
deployTimeout := getComputedDeployTimeout(appName)
|
2024-01-17 18:06:18 -05:00
|
|
|
if _, err := strconv.Atoi(deployTimeout); err == nil {
|
|
|
|
|
deployTimeout = fmt.Sprintf("%ss", deployTimeout)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 08:01:11 -05:00
|
|
|
deployRollback := getComputedRollbackOnFailure(appName)
|
2024-01-17 18:06:18 -05:00
|
|
|
allowRollbacks, err := strconv.ParseBool(deployRollback)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error parsing rollback-on-failure value as boolean: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
imageSourceType := "dockerfile"
|
|
|
|
|
if common.IsImageCnbBased(image) {
|
|
|
|
|
imageSourceType = "pack"
|
|
|
|
|
} else if common.IsImageHerokuishBased(image, appName) {
|
|
|
|
|
imageSourceType = "herokuish"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
env, err := config.LoadMergedAppEnv(appName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error loading environment for deployment: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-24 02:19:21 -05:00
|
|
|
issuerName := "letsencrypt-stag"
|
|
|
|
|
server := getComputedLetsencryptServer(appName)
|
|
|
|
|
if server == "prod" || server == "production" {
|
|
|
|
|
issuerName = "letsencrypt-prod"
|
|
|
|
|
} else if server != "stag" && server != "staging" {
|
|
|
|
|
return fmt.Errorf("Invalid letsencrypt server config: %s", server)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tlsEnabled := false
|
|
|
|
|
letsencryptEmailStag := getGlobalLetsencryptEmailStag()
|
|
|
|
|
letsencryptEmailProd := getGlobalLetsencryptEmailProd()
|
|
|
|
|
if issuerName == "letsencrypt-stag" {
|
|
|
|
|
tlsEnabled = letsencryptEmailStag != ""
|
|
|
|
|
}
|
|
|
|
|
if issuerName == "letsencrypt-prod" {
|
|
|
|
|
tlsEnabled = letsencryptEmailProd != ""
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 18:06:18 -05:00
|
|
|
chartDir, err := os.MkdirTemp("", "dokku-chart-")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error creating chart directory: %w", err)
|
|
|
|
|
}
|
2024-01-23 07:25:42 -05:00
|
|
|
defer os.RemoveAll(chartDir)
|
2024-01-17 18:06:18 -05:00
|
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Join(chartDir, "templates"), os.FileMode(0755)); err != nil {
|
|
|
|
|
return fmt.Errorf("Error creating chart templates directory: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deploymentId := time.Now().Unix()
|
2024-02-12 17:15:37 -05:00
|
|
|
pullSecretBase64 := base64.StdEncoding.EncodeToString([]byte(""))
|
|
|
|
|
imagePullSecrets := getComputedImagePullSecrets(appName)
|
|
|
|
|
if imagePullSecrets == "" {
|
|
|
|
|
dockerConfigPath := filepath.Join(os.Getenv("DOKKU_ROOT"), ".docker/config.json")
|
|
|
|
|
if fi, err := os.Stat(dockerConfigPath); err == nil && !fi.IsDir() {
|
|
|
|
|
b, err := os.ReadFile(dockerConfigPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error reading docker config: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 18:25:16 -05:00
|
|
|
imagePullSecrets = fmt.Sprintf("ims-%s.%d", appName, deploymentId)
|
2024-02-12 17:15:37 -05:00
|
|
|
pullSecretBase64 = base64.StdEncoding.EncodeToString(b)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
globalTemplateFiles := []string{"service-account", "secret", "image-pull-secret"}
|
2024-02-05 16:44:20 -05:00
|
|
|
for _, templateName := range globalTemplateFiles {
|
|
|
|
|
b, err := templates.ReadFile(fmt.Sprintf("templates/chart/%s.yaml", templateName))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error reading %s template: %w", templateName, err)
|
|
|
|
|
}
|
2024-01-23 01:29:50 -05:00
|
|
|
|
2024-02-05 16:44:20 -05:00
|
|
|
filename := filepath.Join(chartDir, "templates", fmt.Sprintf("%s.yaml", templateName))
|
|
|
|
|
err = os.WriteFile(filename, b, os.FileMode(0644))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error writing %s template: %w", templateName, err)
|
|
|
|
|
}
|
2024-01-23 01:29:50 -05:00
|
|
|
|
2024-02-05 16:44:20 -05:00
|
|
|
if os.Getenv("DOKKU_TRACE") == "1" {
|
|
|
|
|
common.CatFile(filename)
|
|
|
|
|
}
|
2024-01-17 18:06:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
portMaps, err := getPortMaps(appName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting port mappings for deployment: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
primaryPort := int32(5000)
|
|
|
|
|
for _, portMap := range portMaps {
|
|
|
|
|
primaryPort = portMap.ContainerPort
|
|
|
|
|
if primaryPort != 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 18:42:55 -05:00
|
|
|
appJSON, err := appjson.GetAppJSON(appName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting app.json for deployment: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 06:08:18 -05:00
|
|
|
workingDir := common.GetWorkingDir(appName, image)
|
2024-01-17 18:06:18 -05:00
|
|
|
|
2024-01-18 06:08:18 -05:00
|
|
|
cronEntries, err := cron.FetchCronEntries(appName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error fetching cron entries: %w", err)
|
|
|
|
|
}
|
2024-01-18 16:51:33 -05:00
|
|
|
|
2024-01-22 06:42:03 -05:00
|
|
|
domains := []string{}
|
2024-01-23 01:29:50 -05:00
|
|
|
if _, ok := processes["web"]; ok {
|
2024-01-22 06:42:03 -05:00
|
|
|
err = common.PlugnTrigger("domains-vhost-enabled", []string{appName}...)
|
|
|
|
|
if err == nil {
|
|
|
|
|
b, err := common.PlugnTriggerOutput("domains-list", []string{appName}...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting domains for deployment: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, domain := range strings.Split(string(b), "\n") {
|
|
|
|
|
domain = strings.TrimSpace(domain)
|
|
|
|
|
if domain != "" {
|
|
|
|
|
domains = append(domains, domain)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-17 18:06:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chart := &Chart{
|
|
|
|
|
ApiVersion: "v2",
|
|
|
|
|
AppVersion: "1.0.0",
|
|
|
|
|
Name: appName,
|
2024-01-26 06:55:46 -05:00
|
|
|
Icon: "https://dokku.com/assets/dokku-logo.svg",
|
2024-01-17 18:06:18 -05:00
|
|
|
Version: fmt.Sprintf("0.0.%d", deploymentId),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = writeYaml(WriteYamlInput{
|
|
|
|
|
Object: chart,
|
|
|
|
|
Path: filepath.Join(chartDir, "Chart.yaml"),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error writing chart: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-06 05:16:20 -05:00
|
|
|
globalAnnotations, err := getGlobalAnnotations(appName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting global annotations: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 07:25:03 -05:00
|
|
|
values := &AppValues{
|
2024-01-23 01:29:50 -05:00
|
|
|
Global: GlobalValues{
|
2024-02-06 05:16:20 -05:00
|
|
|
Annotations: globalAnnotations,
|
2024-01-23 01:29:50 -05:00
|
|
|
AppName: appName,
|
|
|
|
|
DeploymentID: fmt.Sprint(deploymentId),
|
|
|
|
|
Image: GlobalImage{
|
|
|
|
|
ImagePullSecrets: imagePullSecrets,
|
2024-02-12 17:15:37 -05:00
|
|
|
PullSecretBase64: pullSecretBase64,
|
2024-01-23 01:29:50 -05:00
|
|
|
Name: image,
|
|
|
|
|
Type: imageSourceType,
|
|
|
|
|
WorkingDir: workingDir,
|
|
|
|
|
},
|
2024-01-26 03:23:34 -05:00
|
|
|
Namespace: namespace,
|
|
|
|
|
Network: GlobalNetwork{
|
2024-02-12 01:30:17 -05:00
|
|
|
IngressClass: getGlobalIngressClass(),
|
2024-01-26 03:23:34 -05:00
|
|
|
PrimaryPort: primaryPort,
|
|
|
|
|
},
|
|
|
|
|
Secrets: map[string]string{},
|
2024-01-23 01:29:50 -05:00
|
|
|
},
|
|
|
|
|
Processes: map[string]ProcessValues{},
|
2024-01-17 18:06:18 -05:00
|
|
|
}
|
2024-01-23 01:29:50 -05:00
|
|
|
|
2024-01-17 18:06:18 -05:00
|
|
|
for processType, processCount := range processes {
|
2024-01-23 01:29:50 -05:00
|
|
|
// todo: implement deployment annotations
|
|
|
|
|
// todo: implement pod annotations
|
|
|
|
|
// todo: implement volumes
|
|
|
|
|
|
|
|
|
|
healthchecks, ok := appJSON.Healthchecks[processType]
|
|
|
|
|
if !ok {
|
|
|
|
|
healthchecks = []appjson.Healthcheck{}
|
|
|
|
|
}
|
|
|
|
|
processHealthchecks := getProcessHealtchecks(healthchecks, primaryPort)
|
|
|
|
|
|
|
|
|
|
startCommand, err := getStartCommand(StartCommandInput{
|
|
|
|
|
AppName: appName,
|
|
|
|
|
ProcessType: processType,
|
|
|
|
|
ImageSourceType: imageSourceType,
|
|
|
|
|
Port: primaryPort,
|
|
|
|
|
Env: env.Map(),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting start command for deployment: %w", err)
|
|
|
|
|
}
|
|
|
|
|
args := startCommand.Command
|
|
|
|
|
|
|
|
|
|
processResources, err := getProcessResources(appName, processType)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting process resources: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-06 05:16:20 -05:00
|
|
|
annotations, err := getAnnotations(appName, processType)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting process annotations: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 06:42:03 -05:00
|
|
|
processValues := ProcessValues{
|
2024-02-06 05:16:20 -05:00
|
|
|
Annotations: annotations,
|
2024-01-23 01:29:50 -05:00
|
|
|
Args: args,
|
|
|
|
|
Healthchecks: processHealthchecks,
|
|
|
|
|
ProcessType: ProcessType_Worker,
|
|
|
|
|
Replicas: int32(processCount),
|
|
|
|
|
Resources: processResources,
|
2024-01-17 18:06:18 -05:00
|
|
|
}
|
2024-01-22 06:42:03 -05:00
|
|
|
if processType == "web" {
|
2024-01-23 01:29:50 -05:00
|
|
|
processValues.Web = ProcessWeb{
|
|
|
|
|
Domains: domains,
|
|
|
|
|
PortMaps: []ProcessPortMap{},
|
2024-01-23 06:18:25 -05:00
|
|
|
TLS: ProcessTls{
|
2024-01-23 07:25:03 -05:00
|
|
|
Enabled: tlsEnabled,
|
2024-01-23 06:18:25 -05:00
|
|
|
IssuerName: issuerName,
|
|
|
|
|
},
|
2024-01-23 01:29:50 -05:00
|
|
|
}
|
2024-01-23 09:45:20 -05:00
|
|
|
|
2024-01-23 01:29:50 -05:00
|
|
|
processValues.ProcessType = ProcessType_Web
|
|
|
|
|
for _, portMap := range portMaps {
|
|
|
|
|
protocol := PortmapProtocol_TCP
|
|
|
|
|
if portMap.Scheme == "udp" {
|
|
|
|
|
protocol = PortmapProtocol_UDP
|
|
|
|
|
}
|
2024-01-23 09:45:20 -05:00
|
|
|
|
2024-01-23 01:29:50 -05:00
|
|
|
processValues.Web.PortMaps = append(processValues.Web.PortMaps, ProcessPortMap{
|
2024-02-11 07:37:47 -05:00
|
|
|
ContainerPort: portMap.ContainerPort,
|
|
|
|
|
HostPort: portMap.HostPort,
|
|
|
|
|
Name: portMap.String(),
|
|
|
|
|
Protocol: protocol,
|
|
|
|
|
Scheme: portMap.Scheme,
|
2024-01-23 01:29:50 -05:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-11 07:38:42 -05:00
|
|
|
for _, portMap := range processValues.Web.PortMaps {
|
|
|
|
|
_, httpOk := portMaps[fmt.Sprintf("http-80-%d", portMap.ContainerPort)]
|
|
|
|
|
_, httpsOk := portMaps[fmt.Sprintf("https-443-%d", portMap.ContainerPort)]
|
|
|
|
|
if portMap.Scheme == "http" && !httpsOk && tlsEnabled {
|
|
|
|
|
processValues.Web.PortMaps = append(processValues.Web.PortMaps, ProcessPortMap{
|
|
|
|
|
ContainerPort: portMap.ContainerPort,
|
|
|
|
|
HostPort: 443,
|
|
|
|
|
Name: fmt.Sprintf("https-443-%d", portMap.ContainerPort),
|
|
|
|
|
Protocol: PortmapProtocol_TCP,
|
|
|
|
|
Scheme: "https",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if portMap.Scheme == "https" && !httpOk {
|
|
|
|
|
processValues.Web.PortMaps = append(processValues.Web.PortMaps, ProcessPortMap{
|
|
|
|
|
ContainerPort: portMap.ContainerPort,
|
|
|
|
|
HostPort: 80,
|
|
|
|
|
Name: fmt.Sprintf("http-80-%d", portMap.ContainerPort),
|
|
|
|
|
Protocol: PortmapProtocol_TCP,
|
|
|
|
|
Scheme: "http",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 01:29:50 -05:00
|
|
|
sort.Sort(NameSorter(processValues.Web.PortMaps))
|
|
|
|
|
sort.Strings(processValues.Web.Domains)
|
2024-01-22 06:42:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
values.Processes[processType] = processValues
|
2024-01-23 01:29:50 -05:00
|
|
|
|
2024-01-23 09:45:20 -05:00
|
|
|
templateFiles := []string{"deployment"}
|
|
|
|
|
if processType == "web" {
|
2024-01-30 10:57:24 -05:00
|
|
|
templateFiles = append(templateFiles, "service", "certificate", "ingress", "ingress-route", "https-redirect-middleware")
|
2024-01-23 09:45:20 -05:00
|
|
|
}
|
|
|
|
|
for _, templateName := range templateFiles {
|
2024-01-23 01:29:50 -05:00
|
|
|
b, err := templates.ReadFile(fmt.Sprintf("templates/chart/%s.yaml", templateName))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error reading %s template: %w", templateName, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filename := filepath.Join(chartDir, "templates", fmt.Sprintf("%s-%s.yaml", templateName, processType))
|
|
|
|
|
contents := strings.ReplaceAll(string(b), "PROCESS_NAME", processType)
|
|
|
|
|
err = os.WriteFile(filename, []byte(contents), os.FileMode(0644))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error writing %s template: %w", templateName, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if os.Getenv("DOKKU_TRACE") == "1" {
|
|
|
|
|
common.CatFile(filename)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clientset, err := NewKubernetesClient()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error creating kubernetes client: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 03:50:15 -05:00
|
|
|
if err := clientset.Ping(); err != nil {
|
|
|
|
|
return fmt.Errorf("kubernetes api not available: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 01:29:50 -05:00
|
|
|
cronJobs, err := clientset.ListCronJobs(ctx, ListCronJobsInput{
|
|
|
|
|
LabelSelector: fmt.Sprintf("app.kubernetes.io/part-of=%s", appName),
|
|
|
|
|
Namespace: namespace,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error listing cron jobs: %w", err)
|
|
|
|
|
}
|
|
|
|
|
for _, cronEntry := range cronEntries {
|
|
|
|
|
// todo: implement deployment annotations
|
|
|
|
|
// todo: implement pod annotations
|
|
|
|
|
// todo: implement volumes
|
|
|
|
|
suffix := ""
|
|
|
|
|
for _, cronJob := range cronJobs {
|
|
|
|
|
if cronJob.Labels["dokku.com/cron-id"] == cronEntry.ID {
|
|
|
|
|
var ok bool
|
|
|
|
|
suffix, ok = cronJob.Annotations["dokku.com/job-suffix"]
|
|
|
|
|
if !ok {
|
|
|
|
|
suffix = ""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if suffix == "" {
|
|
|
|
|
n := 5
|
|
|
|
|
b := make([]byte, n)
|
|
|
|
|
if _, err := rand.Read(b); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
suffix = strings.ToLower(fmt.Sprintf("%X", b))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
words, err := shellquote.Split(cronEntry.Command)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error parsing cron command: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
processResources, err := getProcessResources(appName, cronEntry.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting process resources: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-06 05:16:20 -05:00
|
|
|
annotations, err := getAnnotations(appName, cronEntry.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting process annotations: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 01:29:50 -05:00
|
|
|
processValues := ProcessValues{
|
2024-02-06 05:16:20 -05:00
|
|
|
Args: words,
|
|
|
|
|
Annotations: annotations,
|
2024-01-23 01:29:50 -05:00
|
|
|
Cron: ProcessCron{
|
|
|
|
|
ID: cronEntry.ID,
|
|
|
|
|
Schedule: cronEntry.Schedule,
|
|
|
|
|
Suffix: suffix,
|
|
|
|
|
},
|
|
|
|
|
ProcessType: ProcessType_Cron,
|
|
|
|
|
Replicas: 1,
|
|
|
|
|
Resources: processResources,
|
|
|
|
|
}
|
|
|
|
|
values.Processes[cronEntry.ID] = processValues
|
|
|
|
|
|
|
|
|
|
b, err := templates.ReadFile("templates/chart/cron-job.yaml")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error reading cron job template: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cronFile := filepath.Join(chartDir, "templates", fmt.Sprintf("cron-job-%s.yaml", cronEntry.ID))
|
|
|
|
|
contents := strings.ReplaceAll(string(b), "CRON_ID", cronEntry.ID)
|
|
|
|
|
err = os.WriteFile(cronFile, []byte(contents), os.FileMode(0644))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error writing cron job template: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if os.Getenv("DOKKU_TRACE") == "1" {
|
|
|
|
|
common.CatFile(cronFile)
|
|
|
|
|
}
|
2024-01-17 18:06:18 -05:00
|
|
|
}
|
2024-01-22 06:42:03 -05:00
|
|
|
|
2024-01-17 18:06:18 -05:00
|
|
|
for key, value := range env.Map() {
|
2024-01-23 01:29:50 -05:00
|
|
|
values.Global.Secrets[key] = base64.StdEncoding.EncodeToString([]byte(value))
|
2024-01-17 18:06:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = writeYaml(WriteYamlInput{
|
|
|
|
|
Object: values,
|
|
|
|
|
Path: filepath.Join(chartDir, "values.yaml"),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error writing chart: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
helmAgent, err := NewHelmAgent(namespace, DeployLogPrinter)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error creating helm agent: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chartPath, err := filepath.Abs(chartDir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting chart path: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timeoutDuration, err := time.ParseDuration(deployTimeout)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error parsing deploy timeout duration: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 07:25:18 -05:00
|
|
|
common.LogExclaim(fmt.Sprintf("Installing %s", appName))
|
2024-01-22 05:12:49 -05:00
|
|
|
err = helmAgent.InstallOrUpgradeChart(ctx, ChartInput{
|
2024-01-17 18:06:18 -05:00
|
|
|
ChartPath: chartPath,
|
|
|
|
|
Namespace: namespace,
|
2024-01-23 07:25:28 -05:00
|
|
|
ReleaseName: appName,
|
2024-01-17 18:06:18 -05:00
|
|
|
RollbackOnFailure: allowRollbacks,
|
|
|
|
|
Timeout: timeoutDuration,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 17:09:00 -05:00
|
|
|
common.LogInfo1("Running post-deploy")
|
|
|
|
|
_, err = common.CallPlugnTrigger(common.PlugnTriggerInput{
|
2024-02-12 20:07:43 -05:00
|
|
|
Args: []string{appName, "", "", imageTag},
|
|
|
|
|
StreamStdio: true,
|
|
|
|
|
Trigger: "core-post-deploy",
|
2024-01-18 17:09:00 -05:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error running core-post-deploy: %w", err)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
_, err = common.CallPlugnTrigger(common.PlugnTriggerInput{
|
2024-02-12 20:07:43 -05:00
|
|
|
Args: []string{appName, "", "", imageTag},
|
|
|
|
|
StreamStdio: true,
|
|
|
|
|
Trigger: "post-deploy",
|
2024-01-18 17:09:00 -05:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error running post-deploy: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 18:06:18 -05:00
|
|
|
return nil
|
|
|
|
|
}
|
2024-01-17 20:56:30 -05:00
|
|
|
|
|
|
|
|
// TriggerSchedulerEnter enters a container for a given application
|
|
|
|
|
func TriggerSchedulerEnter(scheduler string, appName string, processType string, podName string, args []string) error {
|
|
|
|
|
if scheduler != "k3s" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 23:28:10 -05:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
signals := make(chan os.Signal, 1)
|
|
|
|
|
signal.Notify(signals, os.Interrupt, syscall.SIGHUP,
|
|
|
|
|
syscall.SIGINT,
|
|
|
|
|
syscall.SIGQUIT,
|
|
|
|
|
syscall.SIGTERM)
|
2024-01-17 20:56:30 -05:00
|
|
|
go func() {
|
2024-01-17 23:28:10 -05:00
|
|
|
<-signals
|
2024-01-17 20:56:30 -05:00
|
|
|
cancel()
|
|
|
|
|
}()
|
|
|
|
|
|
2024-01-22 05:34:41 -05:00
|
|
|
clientset, err := NewKubernetesClient()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error creating kubernetes client: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 03:50:15 -05:00
|
|
|
if err := clientset.Ping(); err != nil {
|
|
|
|
|
return fmt.Errorf("kubernetes api not available: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 08:01:11 -05:00
|
|
|
namespace := getComputedNamespace(appName)
|
2024-01-19 19:59:44 -05:00
|
|
|
labelSelector := []string{fmt.Sprintf("app.kubernetes.io/part-of=%s", appName)}
|
2024-01-17 20:56:30 -05:00
|
|
|
processIndex := 1
|
|
|
|
|
if processType != "" {
|
|
|
|
|
parts := strings.SplitN(processType, ".", 2)
|
|
|
|
|
if len(parts) == 2 {
|
|
|
|
|
processType = parts[0]
|
|
|
|
|
processIndex, err = strconv.Atoi(parts[1])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error parsing process index: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-19 19:59:44 -05:00
|
|
|
labelSelector = append(labelSelector, fmt.Sprintf("app.kubernetes.io/name=%s", processType))
|
2024-01-17 20:56:30 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-18 16:14:33 -05:00
|
|
|
pods, err := clientset.ListPods(ctx, ListPodsInput{
|
|
|
|
|
Namespace: namespace,
|
2024-01-17 20:56:30 -05:00
|
|
|
LabelSelector: strings.Join(labelSelector, ","),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error listing pods: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 16:14:33 -05:00
|
|
|
if len(pods) == 0 {
|
2024-01-17 20:56:30 -05:00
|
|
|
return fmt.Errorf("No pods found for app %s", appName)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
processIndex--
|
2024-01-18 16:14:33 -05:00
|
|
|
if processIndex > len(pods) {
|
2024-01-17 20:56:30 -05:00
|
|
|
return fmt.Errorf("Process index %d out of range for app %s", processIndex, appName)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var selectedPod corev1.Pod
|
|
|
|
|
if podName != "" {
|
2024-01-18 16:14:33 -05:00
|
|
|
for _, pod := range pods {
|
2024-01-17 20:56:30 -05:00
|
|
|
if pod.Name == podName {
|
|
|
|
|
selectedPod = pod
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-01-18 16:14:33 -05:00
|
|
|
selectedPod = pods[processIndex]
|
2024-01-17 20:56:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
command := args
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
command = []string{"/bin/bash"}
|
|
|
|
|
if globalShell, err := common.PlugnTriggerOutputAsString("config-get-global", []string{"DOKKU_APP_SHELL"}...); err == nil && globalShell != "" {
|
|
|
|
|
command = []string{globalShell}
|
|
|
|
|
}
|
|
|
|
|
if appShell, err := common.PlugnTriggerOutputAsString("config-get", []string{appName, "DOKKU_APP_SHELL"}...); err == nil && appShell != "" {
|
|
|
|
|
command = []string{appShell}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 05:28:07 -05:00
|
|
|
entrypoint := ""
|
|
|
|
|
if selectedPod.Annotations["dokku.com/builder-type"] == "herokuish" {
|
|
|
|
|
entrypoint = "/exec"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return enterPod(ctx, EnterPodInput{
|
|
|
|
|
Clientset: clientset,
|
|
|
|
|
Command: command,
|
|
|
|
|
Entrypoint: entrypoint,
|
|
|
|
|
SelectedPod: selectedPod,
|
2024-01-19 19:26:06 -05:00
|
|
|
WaitTimeout: 10,
|
2024-01-18 05:28:07 -05:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 21:48:06 -05:00
|
|
|
// TriggerSchedulerLogs displays logs for a given application
|
|
|
|
|
func TriggerSchedulerLogs(scheduler string, appName string, processType string, tail bool, quiet bool, numLines int64) error {
|
|
|
|
|
if scheduler != "k3s" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 23:28:10 -05:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
signals := make(chan os.Signal, 1)
|
|
|
|
|
signal.Notify(signals, os.Interrupt, syscall.SIGHUP,
|
|
|
|
|
syscall.SIGINT,
|
|
|
|
|
syscall.SIGQUIT,
|
|
|
|
|
syscall.SIGTERM)
|
2024-01-17 21:48:06 -05:00
|
|
|
go func() {
|
2024-01-17 23:28:10 -05:00
|
|
|
<-signals
|
2024-01-17 21:48:06 -05:00
|
|
|
cancel()
|
|
|
|
|
}()
|
|
|
|
|
|
2024-01-22 05:34:41 -05:00
|
|
|
clientset, err := NewKubernetesClient()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error creating kubernetes client: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 03:50:15 -05:00
|
|
|
if err := clientset.Ping(); err != nil {
|
|
|
|
|
return fmt.Errorf("kubernetes api not available: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-19 19:59:44 -05:00
|
|
|
labelSelector := []string{fmt.Sprintf("app.kubernetes.io/part-of=%s", appName)}
|
2024-01-17 21:48:06 -05:00
|
|
|
processIndex := 0
|
|
|
|
|
if processType != "" {
|
|
|
|
|
parts := strings.SplitN(processType, ".", 2)
|
|
|
|
|
if len(parts) == 2 {
|
|
|
|
|
processType = parts[0]
|
|
|
|
|
processIndex, err = strconv.Atoi(parts[1])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error parsing process index: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-19 19:59:44 -05:00
|
|
|
labelSelector = append(labelSelector, fmt.Sprintf("app.kubernetes.io/name=%s", processType))
|
2024-01-17 21:48:06 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-22 08:01:11 -05:00
|
|
|
namespace := getComputedNamespace(appName)
|
2024-01-18 16:14:33 -05:00
|
|
|
pods, err := clientset.ListPods(ctx, ListPodsInput{
|
|
|
|
|
Namespace: namespace,
|
2024-01-17 21:48:06 -05:00
|
|
|
LabelSelector: strings.Join(labelSelector, ","),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error listing pods: %w", err)
|
|
|
|
|
}
|
2024-01-18 16:14:33 -05:00
|
|
|
if len(pods) == 0 {
|
2024-01-17 21:48:06 -05:00
|
|
|
return fmt.Errorf("No pods found for app %s", appName)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ch := make(chan bool)
|
|
|
|
|
|
2024-01-18 05:28:07 -05:00
|
|
|
if os.Getenv("FORCE_TTY") == "1" {
|
2024-01-17 23:28:10 -05:00
|
|
|
color.NoColor = false
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 21:48:06 -05:00
|
|
|
colors := []color.Attribute{
|
|
|
|
|
color.FgRed,
|
|
|
|
|
color.FgYellow,
|
|
|
|
|
color.FgGreen,
|
|
|
|
|
color.FgCyan,
|
|
|
|
|
color.FgBlue,
|
|
|
|
|
color.FgMagenta,
|
|
|
|
|
}
|
|
|
|
|
// colorIndex := 0
|
2024-01-18 16:14:33 -05:00
|
|
|
for i := 0; i < len(pods); i++ {
|
2024-01-17 21:48:06 -05:00
|
|
|
if processIndex > 0 && i != (processIndex-1) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logOptions := v1.PodLogOptions{
|
|
|
|
|
Follow: tail,
|
|
|
|
|
}
|
|
|
|
|
if numLines > 0 {
|
|
|
|
|
logOptions.TailLines = ptr.To(numLines)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
podColor := colors[i%len(colors)]
|
|
|
|
|
dynoText := color.New(podColor).SprintFunc()
|
2024-01-18 16:14:33 -05:00
|
|
|
podName := pods[i].Name
|
2024-01-17 21:48:06 -05:00
|
|
|
podLogs, err := clientset.Client.CoreV1().Pods(namespace).GetLogs(podName, &logOptions).Stream(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
buffer := bufio.NewReader(podLogs)
|
2024-01-17 23:28:10 -05:00
|
|
|
go func(ctx context.Context, buffer *bufio.Reader, prettyText func(a ...interface{}) string, ch chan bool) {
|
2024-01-17 21:48:06 -05:00
|
|
|
defer func() {
|
|
|
|
|
ch <- true
|
|
|
|
|
}()
|
|
|
|
|
for {
|
2024-01-17 23:28:10 -05:00
|
|
|
select {
|
|
|
|
|
case <-ctx.Done(): // if cancel() execute
|
|
|
|
|
ch <- true
|
2024-01-17 21:48:06 -05:00
|
|
|
return
|
2024-01-17 23:28:10 -05:00
|
|
|
default:
|
|
|
|
|
str, readErr := buffer.ReadString('\n')
|
|
|
|
|
if readErr == io.EOF {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if str == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !quiet {
|
|
|
|
|
str = fmt.Sprintf("%s %s", dynoText(fmt.Sprintf("app[%s]:", podName)), str)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := fmt.Print(str)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-01-17 21:48:06 -05:00
|
|
|
}
|
|
|
|
|
}
|
2024-01-17 23:28:10 -05:00
|
|
|
}(ctx, buffer, dynoText, ch)
|
2024-01-17 21:48:06 -05:00
|
|
|
}
|
|
|
|
|
<-ch
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-01-18 00:19:20 -05:00
|
|
|
|
2024-01-18 05:28:07 -05:00
|
|
|
// TriggerSchedulerRun runs a command in an ephemeral container
|
|
|
|
|
func TriggerSchedulerRun(scheduler string, appName string, envCount int, args []string) error {
|
|
|
|
|
if scheduler != "k3s" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extraEnv := map[string]string{}
|
|
|
|
|
if envCount > 0 {
|
|
|
|
|
var envPairs []string
|
|
|
|
|
envPairs, args = args[0:envCount], args[envCount:]
|
|
|
|
|
for _, envPair := range envPairs {
|
|
|
|
|
parts := strings.SplitN(envPair, "=", 2)
|
|
|
|
|
if len(parts) != 2 {
|
|
|
|
|
return fmt.Errorf("Invalid environment variable pair: %s", envPair)
|
|
|
|
|
}
|
|
|
|
|
extraEnv[parts[0]] = parts[1]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
imageTag, err := common.GetRunningImageTag(appName, "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting running image tag: %w", err)
|
|
|
|
|
}
|
2024-01-19 06:28:04 -05:00
|
|
|
image, err := common.GetDeployingAppImageName(appName, imageTag, "")
|
2024-01-18 05:28:07 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting deploying app image name: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
imageStage, err := common.DockerInspect(image, "{{ index .Config.Labels \"com.dokku.image-stage\" }}")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting image stage: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if imageStage != "release" {
|
|
|
|
|
common.LogWarn("Invalid image stage detected: expected 'release', got '$IMAGE_STAGE'")
|
|
|
|
|
return fmt.Errorf("Successfully deploy your app to fix dokku run calls")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dokkuRmContainer := os.Getenv("DOKKU_RM_CONTAINER")
|
|
|
|
|
if dokkuRmContainer == "" {
|
2024-01-20 04:58:02 -05:00
|
|
|
resp, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
|
2024-02-12 20:28:31 -05:00
|
|
|
Trigger: "config-get",
|
|
|
|
|
Args: []string{appName, "DOKKU_RM_CONTAINER"},
|
2024-01-18 05:28:07 -05:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
2024-01-20 04:58:02 -05:00
|
|
|
resp, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
|
2024-02-12 20:28:31 -05:00
|
|
|
Trigger: "config-get-global",
|
|
|
|
|
Args: []string{"DOKKU_RM_CONTAINER"},
|
2024-01-18 05:28:07 -05:00
|
|
|
})
|
|
|
|
|
if err == nil {
|
2024-01-20 04:58:02 -05:00
|
|
|
dokkuRmContainer = strings.TrimSpace(resp.Stdout)
|
2024-01-18 05:28:07 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-01-20 04:58:02 -05:00
|
|
|
dokkuRmContainer = strings.TrimSpace(resp.Stdout)
|
2024-01-18 05:28:07 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if dokkuRmContainer == "" {
|
|
|
|
|
dokkuRmContainer = "true"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rmContainer, err := strconv.ParseBool(dokkuRmContainer)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error parsing DOKKU_RM_CONTAINER value as boolean: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
labels := map[string]string{
|
2024-01-19 19:59:44 -05:00
|
|
|
"app.kubernetes.io/part-of": appName,
|
2024-01-18 05:28:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if os.Getenv("DOKKU_TRACE") == "1" {
|
|
|
|
|
extraEnv["TRACE"] = "true"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
processType := "run"
|
|
|
|
|
if os.Getenv("DOKKU_CRON_ID") != "" {
|
|
|
|
|
processType = "cron"
|
|
|
|
|
labels["dokku.com/cron-id"] = os.Getenv("DOKKU_CRON_ID")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
imageSourceType, err := common.DockerInspect(image, "{{ index .Config.Labels \"com.dokku.builder-type\" }}")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting image builder type: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// todo: do something with docker args
|
|
|
|
|
command := args
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
command = []string{"/bin/bash"}
|
|
|
|
|
if globalShell, err := common.PlugnTriggerOutputAsString("config-get-global", []string{"DOKKU_APP_SHELL"}...); err == nil && globalShell != "" {
|
|
|
|
|
command = []string{globalShell}
|
|
|
|
|
}
|
|
|
|
|
if appShell, err := common.PlugnTriggerOutputAsString("config-get", []string{appName, "DOKKU_APP_SHELL"}...); err == nil && appShell != "" {
|
|
|
|
|
command = []string{appShell}
|
|
|
|
|
}
|
|
|
|
|
} else if len(args) == 1 {
|
|
|
|
|
resp, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
|
2024-02-12 20:28:31 -05:00
|
|
|
Trigger: "procfile-get-command",
|
|
|
|
|
Args: []string{appName, args[0], "5000"},
|
2024-01-18 05:28:07 -05:00
|
|
|
})
|
|
|
|
|
if err == nil && resp.Stdout != "" {
|
|
|
|
|
common.LogInfo1Quiet(fmt.Sprintf("Found '%s' in Procfile, running that command", args[0]))
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
// todo: run command in procfile
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entrypoint := ""
|
|
|
|
|
if imageSourceType == "herokuish" {
|
|
|
|
|
entrypoint = "/exec"
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 08:01:11 -05:00
|
|
|
namespace := getComputedNamespace(appName)
|
2024-01-18 05:28:07 -05:00
|
|
|
helmAgent, err := NewHelmAgent(namespace, DevNullPrinter)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error creating helm agent: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
values, err := helmAgent.GetValues(fmt.Sprintf("dokku-%s", appName))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting helm values: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deploymentIDValue, ok := values["deploment_id"].(string)
|
|
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("Deployment ID is not a string")
|
|
|
|
|
}
|
|
|
|
|
if len(deploymentIDValue) == 0 {
|
|
|
|
|
return fmt.Errorf("Deployment ID is empty")
|
|
|
|
|
}
|
|
|
|
|
deploymentID, err := strconv.ParseInt(deploymentIDValue, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error parsing deployment ID: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attachToPod := os.Getenv("DOKKU_DETACH_CONTAINER") != "1"
|
2024-01-22 08:01:11 -05:00
|
|
|
imagePullSecrets := getComputedImagePullSecrets(appName)
|
2024-01-18 05:28:07 -05:00
|
|
|
workingDir := common.GetWorkingDir(appName, image)
|
|
|
|
|
job, err := templateKubernetesJob(Job{
|
|
|
|
|
AppName: appName,
|
|
|
|
|
Command: command,
|
|
|
|
|
DeploymentID: deploymentID,
|
|
|
|
|
Entrypoint: entrypoint,
|
|
|
|
|
Env: extraEnv,
|
|
|
|
|
Image: image,
|
|
|
|
|
ImagePullSecrets: imagePullSecrets,
|
|
|
|
|
ImageSourceType: imageSourceType,
|
|
|
|
|
Interactive: attachToPod,
|
|
|
|
|
Labels: labels,
|
|
|
|
|
Namespace: namespace,
|
|
|
|
|
ProcessType: processType,
|
|
|
|
|
RemoveContainer: rmContainer,
|
|
|
|
|
WorkingDir: workingDir,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error templating job: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if os.Getenv("FORCE_TTY") == "1" {
|
|
|
|
|
color.NoColor = false
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 16:14:33 -05:00
|
|
|
clientset, err := NewKubernetesClient()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error creating kubernetes client: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 03:50:15 -05:00
|
|
|
if err := clientset.Ping(); err != nil {
|
|
|
|
|
return fmt.Errorf("kubernetes api not available: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 05:28:07 -05:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
signals := make(chan os.Signal, 1)
|
|
|
|
|
signal.Notify(signals, os.Interrupt, syscall.SIGHUP,
|
|
|
|
|
syscall.SIGINT,
|
|
|
|
|
syscall.SIGQUIT,
|
|
|
|
|
syscall.SIGTERM)
|
|
|
|
|
go func() {
|
|
|
|
|
<-signals
|
2024-01-18 16:14:33 -05:00
|
|
|
if attachToPod {
|
|
|
|
|
clientset.DeleteJob(ctx, DeleteJobInput{ // nolint: errcheck
|
|
|
|
|
Name: job.Name,
|
|
|
|
|
Namespace: namespace,
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-01-18 05:28:07 -05:00
|
|
|
cancel()
|
|
|
|
|
}()
|
|
|
|
|
|
2024-01-18 16:14:33 -05:00
|
|
|
createdJob, err := clientset.CreateJob(ctx, CreateJobInput{
|
|
|
|
|
Job: job,
|
|
|
|
|
Namespace: namespace,
|
|
|
|
|
})
|
2024-01-18 05:28:07 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error creating job: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 16:14:33 -05:00
|
|
|
if attachToPod {
|
|
|
|
|
defer func() {
|
|
|
|
|
clientset.DeleteJob(ctx, DeleteJobInput{ // nolint: errcheck
|
|
|
|
|
Name: job.Name,
|
|
|
|
|
Namespace: namespace,
|
|
|
|
|
})
|
|
|
|
|
}()
|
2024-01-18 05:28:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
batchJobSelector := fmt.Sprintf("batch.kubernetes.io/job-name=%s", createdJob.Name)
|
2024-01-18 16:14:33 -05:00
|
|
|
pods, err := waitForPodToExist(ctx, WaitForPodToExistInput{
|
2024-01-19 19:26:06 -05:00
|
|
|
Clientset: clientset,
|
|
|
|
|
Namespace: namespace,
|
|
|
|
|
RetryCount: 3,
|
|
|
|
|
LabelSelector: batchJobSelector,
|
2024-01-18 05:28:07 -05:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error waiting for pod to exist: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 16:14:33 -05:00
|
|
|
for _, pod := range pods {
|
2024-01-18 05:28:07 -05:00
|
|
|
common.LogQuiet(pod.Name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !attachToPod {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 15:11:51 -05:00
|
|
|
err = waitForPodBySelectorRunning(ctx, WaitForPodBySelectorRunningInput{
|
2024-01-19 19:26:06 -05:00
|
|
|
Clientset: clientset,
|
|
|
|
|
Namespace: namespace,
|
|
|
|
|
LabelSelector: batchJobSelector,
|
|
|
|
|
Timeout: 300,
|
|
|
|
|
Waiter: isPodReady,
|
2024-01-18 05:28:07 -05:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, conditions.ErrPodCompleted) {
|
2024-01-18 16:14:33 -05:00
|
|
|
pods, podErr := clientset.ListPods(ctx, ListPodsInput{
|
|
|
|
|
Namespace: namespace,
|
|
|
|
|
LabelSelector: batchJobSelector,
|
2024-01-18 05:28:07 -05:00
|
|
|
})
|
|
|
|
|
if podErr != nil {
|
|
|
|
|
return fmt.Errorf("Error completed pod: %w", err)
|
|
|
|
|
}
|
2024-01-18 16:14:33 -05:00
|
|
|
selectedPod := pods[0]
|
2024-01-18 05:28:07 -05:00
|
|
|
if selectedPod.Status.Phase == v1.PodFailed {
|
|
|
|
|
for _, status := range selectedPod.Status.ContainerStatuses {
|
|
|
|
|
if status.Name != fmt.Sprintf("%s-%s", appName, processType) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if status.State.Terminated == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return fmt.Errorf("Unable to attach as the pod has already exited with a failed exit code: %s", status.State.Terminated.Message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt.Errorf("Unable to attach as the pod has already exited with a failed exit code")
|
|
|
|
|
} else if selectedPod.Status.Phase == v1.PodSucceeded {
|
|
|
|
|
return errors.New("Unable to attach as the pod has already exited with a successful exit code")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fmt.Errorf("Error waiting for pod to be running: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 16:14:33 -05:00
|
|
|
pods, err = clientset.ListPods(ctx, ListPodsInput{
|
|
|
|
|
Namespace: namespace,
|
|
|
|
|
LabelSelector: batchJobSelector,
|
2024-01-18 05:28:07 -05:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting pod: %w", err)
|
|
|
|
|
}
|
2024-01-18 16:14:33 -05:00
|
|
|
selectedPod := pods[0]
|
2024-01-18 05:28:07 -05:00
|
|
|
|
|
|
|
|
switch selectedPod.Status.Phase {
|
|
|
|
|
case v1.PodFailed, v1.PodSucceeded:
|
|
|
|
|
if selectedPod.Status.Phase == v1.PodFailed {
|
|
|
|
|
for _, status := range selectedPod.Status.ContainerStatuses {
|
|
|
|
|
if status.Name != fmt.Sprintf("%s-%s", appName, processType) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if status.State.Terminated == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return fmt.Errorf("Unable to attach as the pod has already exited with a failed exit code: %s", status.State.Terminated.Message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt.Errorf("Unable to attach as the pod has already exited with a failed exit code")
|
|
|
|
|
} else if selectedPod.Status.Phase == v1.PodSucceeded {
|
|
|
|
|
return errors.New("Unable to attach as the pod has already exited with a successful exit code")
|
|
|
|
|
}
|
|
|
|
|
case v1.PodRunning:
|
2024-01-18 16:14:33 -05:00
|
|
|
return enterPod(ctx, EnterPodInput{
|
2024-01-18 05:28:07 -05:00
|
|
|
Clientset: clientset,
|
|
|
|
|
Command: command,
|
|
|
|
|
Entrypoint: entrypoint,
|
|
|
|
|
SelectedPod: selectedPod,
|
|
|
|
|
})
|
|
|
|
|
default:
|
|
|
|
|
return fmt.Errorf("Unable to attach as the pod is in an unknown state: %s", selectedPod.Status.Phase)
|
|
|
|
|
}
|
|
|
|
|
// todo: support scheduler-post-run
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 16:55:09 -05:00
|
|
|
// TriggerSchedulerRunList lists one-off run pods for a given application
|
|
|
|
|
func TriggerSchedulerRunList(scheduler string, appName string, format string) error {
|
|
|
|
|
if scheduler != "k3s" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 05:34:41 -05:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
signals := make(chan os.Signal, 1)
|
|
|
|
|
signal.Notify(signals, os.Interrupt, syscall.SIGHUP,
|
|
|
|
|
syscall.SIGINT,
|
|
|
|
|
syscall.SIGQUIT,
|
|
|
|
|
syscall.SIGTERM)
|
|
|
|
|
go func() {
|
|
|
|
|
<-signals
|
|
|
|
|
cancel()
|
|
|
|
|
}()
|
|
|
|
|
|
2024-01-18 16:55:09 -05:00
|
|
|
clientset, err := NewKubernetesClient()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error creating kubernetes client: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 03:50:15 -05:00
|
|
|
if err := clientset.Ping(); err != nil {
|
|
|
|
|
return fmt.Errorf("kubernetes api not available: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 08:01:11 -05:00
|
|
|
namespace := getComputedNamespace(appName)
|
2024-01-18 16:55:09 -05:00
|
|
|
cronJobs, err := clientset.ListCronJobs(ctx, ListCronJobsInput{
|
2024-01-19 19:59:44 -05:00
|
|
|
LabelSelector: fmt.Sprintf("app.kubernetes.io/part-of=%s", appName),
|
2024-01-18 16:55:09 -05:00
|
|
|
Namespace: namespace,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error getting cron jobs: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type CronJobEntry struct {
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
AppName string `json:"app"`
|
|
|
|
|
Command string `json:"command"`
|
|
|
|
|
Schedule string `json:"schedule"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data := []CronJobEntry{}
|
|
|
|
|
lines := []string{"ID | Schedule | Command"}
|
|
|
|
|
for _, cronJob := range cronJobs {
|
|
|
|
|
command := ""
|
|
|
|
|
for _, container := range cronJob.Spec.JobTemplate.Spec.Template.Spec.Containers {
|
|
|
|
|
if container.Name == fmt.Sprintf("%s-cron", appName) {
|
|
|
|
|
command = strings.Join(container.Args, " ")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cronID, ok := cronJob.Labels["dokku.com/cron-id"]
|
|
|
|
|
if !ok {
|
|
|
|
|
common.LogWarn(fmt.Sprintf("Cron job %s does not have a cron ID label", cronJob.Name))
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lines = append(lines, fmt.Sprintf("%s | %s | %s", cronID, cronJob.Spec.Schedule, command))
|
|
|
|
|
data = append(data, CronJobEntry{
|
|
|
|
|
ID: cronID,
|
|
|
|
|
AppName: appName,
|
|
|
|
|
Command: command,
|
|
|
|
|
Schedule: cronJob.Spec.Schedule,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if format == "stdout" {
|
|
|
|
|
result := columnize.SimpleFormat(lines)
|
|
|
|
|
fmt.Println(result)
|
|
|
|
|
} else {
|
|
|
|
|
b, err := json.Marshal(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error marshalling cron jobs: %w", err)
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(string(b))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 15:11:51 -05:00
|
|
|
// TriggerSchedulerPostDelete destroys the scheduler-k3s data for a given app container
|
|
|
|
|
func TriggerSchedulerPostDelete(scheduler string, appName string) error {
|
|
|
|
|
if scheduler != "k3s" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-02-06 04:38:04 -05:00
|
|
|
|
2024-02-20 15:37:52 -05:00
|
|
|
dataErr := common.RemoveAppDataDirectory("logs", appName)
|
|
|
|
|
propertyErr := common.PropertyDestroy("logs", appName)
|
|
|
|
|
|
|
|
|
|
if dataErr != nil {
|
|
|
|
|
return dataErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if propertyErr != nil {
|
|
|
|
|
return propertyErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if isK3sKubernetes() {
|
|
|
|
|
if err := isK3sInstalled(); err != nil {
|
|
|
|
|
common.LogWarn("k3s is not installed, skipping")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 03:50:15 -05:00
|
|
|
if err := isKubernetesAvailable(); err != nil {
|
|
|
|
|
return fmt.Errorf("kubernetes api not available: %w", err)
|
2024-02-06 04:38:04 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-22 08:01:11 -05:00
|
|
|
namespace := getComputedNamespace(appName)
|
2024-01-18 15:11:51 -05:00
|
|
|
helmAgent, err := NewHelmAgent(namespace, DeployLogPrinter)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error creating helm agent: %w", err)
|
2024-01-18 05:28:07 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-18 15:11:51 -05:00
|
|
|
err = helmAgent.UninstallChart(fmt.Sprintf("dokku-%s", appName))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error uninstalling chart: %w", err)
|
|
|
|
|
}
|
2024-01-18 05:28:07 -05:00
|
|
|
|
2024-01-18 15:11:51 -05:00
|
|
|
return nil
|
2024-01-18 05:28:07 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-18 15:11:51 -05:00
|
|
|
// TriggerSchedulerStop stops an application
|
|
|
|
|
func TriggerSchedulerStop(scheduler string, appName string) error {
|
|
|
|
|
if scheduler != "k3s" {
|
|
|
|
|
return nil
|
2024-01-18 05:28:07 -05:00
|
|
|
}
|
2024-01-18 15:11:51 -05:00
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
signals := make(chan os.Signal, 1)
|
|
|
|
|
signal.Notify(signals, os.Interrupt, syscall.SIGHUP,
|
|
|
|
|
syscall.SIGINT,
|
|
|
|
|
syscall.SIGQUIT,
|
|
|
|
|
syscall.SIGTERM)
|
|
|
|
|
go func() {
|
|
|
|
|
<-signals
|
|
|
|
|
cancel()
|
|
|
|
|
}()
|
2024-01-18 05:28:07 -05:00
|
|
|
|
2024-01-22 05:34:41 -05:00
|
|
|
clientset, err := NewKubernetesClient()
|
|
|
|
|
if err != nil {
|
2024-02-22 07:54:32 -05:00
|
|
|
if isK3sKubernetes() {
|
|
|
|
|
if err := isK3sInstalled(); err != nil {
|
|
|
|
|
common.LogWarn("k3s is not installed, skipping")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-22 05:34:41 -05:00
|
|
|
return fmt.Errorf("Error creating kubernetes client: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 03:50:15 -05:00
|
|
|
if err := clientset.Ping(); err != nil {
|
|
|
|
|
return fmt.Errorf("kubernetes api not available: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 08:01:11 -05:00
|
|
|
namespace := getComputedNamespace(appName)
|
2024-01-18 16:14:33 -05:00
|
|
|
deployments, err := clientset.ListDeployments(ctx, ListDeploymentsInput{
|
|
|
|
|
Namespace: namespace,
|
2024-01-19 19:59:44 -05:00
|
|
|
LabelSelector: fmt.Sprintf("app.kubernetes.io/part-of=%s", appName),
|
2024-01-18 05:28:07 -05:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
2024-01-18 15:11:51 -05:00
|
|
|
return fmt.Errorf("Error listing deployments: %w", err)
|
2024-01-18 05:28:07 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-18 16:14:33 -05:00
|
|
|
for _, deployment := range deployments {
|
2024-02-06 05:16:20 -05:00
|
|
|
processType, ok := deployment.Labels["app.kubernetes.io/name"]
|
2024-01-18 15:11:51 -05:00
|
|
|
if !ok {
|
2024-02-06 05:16:20 -05:00
|
|
|
return fmt.Errorf("Deployment %s does not have a process type label", deployment.Name)
|
2024-01-18 15:11:51 -05:00
|
|
|
}
|
|
|
|
|
common.LogVerboseQuiet(fmt.Sprintf("Stopping %s process", processType))
|
2024-01-18 16:14:33 -05:00
|
|
|
err := clientset.ScaleDeployment(ctx, ScaleDeploymentInput{
|
|
|
|
|
Name: deployment.Name,
|
|
|
|
|
Namespace: namespace,
|
|
|
|
|
Replicas: 0,
|
|
|
|
|
})
|
2024-01-18 15:11:51 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Error updating deployment scale: %w", err)
|
2024-01-18 05:28:07 -05:00
|
|
|
}
|
|
|
|
|
}
|
2024-01-18 15:11:51 -05:00
|
|
|
|
2024-01-18 05:28:07 -05:00
|
|
|
return nil
|
|
|
|
|
}
|