feat: add support for global properties
This commit is contained in:
@@ -2,7 +2,6 @@ package scheduler_k3s
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@@ -255,48 +254,105 @@ func extractStartCommand(input StartCommandInput) string {
|
|||||||
return command
|
return command
|
||||||
}
|
}
|
||||||
|
|
||||||
type PortMap struct {
|
func getDeployTimeout(appName string) string {
|
||||||
ContainerPort int32 `json:"container_port"`
|
return common.PropertyGetDefault("scheduler-k3s", appName, "deploy-timeout", "")
|
||||||
HostPort int32 `json:"host_port"`
|
|
||||||
Scheme string `json:"scheme"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PortMap) String() string {
|
func getGlobalDeployTimeout() string {
|
||||||
return fmt.Sprintf("%s-%d-%d", p.Scheme, p.HostPort, p.ContainerPort)
|
return common.PropertyGetDefault("scheduler-k3s", "--global", "deploy-timeout", "300s")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PortMap) IsAllowedHttp() bool {
|
func getComputedDeployTimeout(appName string) string {
|
||||||
return p.Scheme == "http" || p.ContainerPort == 80
|
deployTimeout := getDeployTimeout(appName)
|
||||||
}
|
if deployTimeout == "" {
|
||||||
|
deployTimeout = getGlobalDeployTimeout()
|
||||||
func (p PortMap) IsAllowedHttps() bool {
|
|
||||||
return p.Scheme == "https" || p.ContainerPort == 443
|
|
||||||
}
|
|
||||||
|
|
||||||
func getPortMaps(appName string) ([]PortMap, error) {
|
|
||||||
portMaps := []PortMap{}
|
|
||||||
|
|
||||||
output, err := common.PlugnTriggerOutputAsString("ports-get", []string{appName, "json"}...)
|
|
||||||
if err != nil {
|
|
||||||
return portMaps, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal([]byte(output), &portMaps)
|
return deployTimeout
|
||||||
if err != nil {
|
}
|
||||||
return portMaps, err
|
|
||||||
|
func getImagePullSecrets(appName string) string {
|
||||||
|
return common.PropertyGetDefault("scheduler-k3s", appName, "image-pull-secrets", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getGlobalImagePullSecrets() string {
|
||||||
|
return common.PropertyGetDefault("scheduler-k3s", "--global", "image-pull-secrets", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getComputedImagePullSecrets(appName string) string {
|
||||||
|
imagePullSecrets := getImagePullSecrets(appName)
|
||||||
|
if imagePullSecrets == "" {
|
||||||
|
imagePullSecrets = getGlobalImagePullSecrets()
|
||||||
}
|
}
|
||||||
|
|
||||||
allowedMappings := []PortMap{}
|
return imagePullSecrets
|
||||||
for _, portMap := range portMaps {
|
}
|
||||||
if !portMap.IsAllowedHttp() && !portMap.IsAllowedHttps() {
|
|
||||||
// todo: log warning
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
allowedMappings = append(allowedMappings, portMap)
|
func getLetsencryptServer(appName string) string {
|
||||||
|
return common.PropertyGetDefault("scheduler-k3s", appName, "letsencrypt-server", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getGlobalLetsencryptServer(appName string) string {
|
||||||
|
return common.PropertyGetDefault("scheduler-k3s", appName, "letsencrypt-server", "prod")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getComputedLetsencryptServer(appName string) string {
|
||||||
|
letsencryptServer := getLetsencryptServer(appName)
|
||||||
|
if letsencryptServer == "" {
|
||||||
|
letsencryptServer = getGlobalLetsencryptServer(appName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return allowedMappings, nil
|
return letsencryptServer
|
||||||
|
}
|
||||||
|
|
||||||
|
func getGlobalLetsencryptEmailProd() string {
|
||||||
|
return common.PropertyGetDefault("scheduler-k3s", "--global", "letsencrypt-email-prod", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getGlobalLetsencryptEmailStag() string {
|
||||||
|
return common.PropertyGetDefault("scheduler-k3s", "--global", "letsencrypt-email-stag", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNamespace(appName string) string {
|
||||||
|
return common.PropertyGetDefault("scheduler-k3s", appName, "namespace", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getGlobalNamespace() string {
|
||||||
|
return common.PropertyGetDefault("scheduler-k3s", "--global", "namespace", "default")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getComputedNamespace(appName string) string {
|
||||||
|
namespace := getNamespace(appName)
|
||||||
|
if namespace == "" {
|
||||||
|
namespace = getGlobalNamespace()
|
||||||
|
}
|
||||||
|
|
||||||
|
return namespace
|
||||||
|
}
|
||||||
|
|
||||||
|
func getGlobalNetworkInterface() string {
|
||||||
|
return common.PropertyGetDefault("scheduler-k3s", "--global", "network-interface", "eth0")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRollbackOnFailure(appName string) string {
|
||||||
|
return common.PropertyGetDefault("scheduler-k3s", appName, "rollback-on-failure", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getGlobalRollbackOnFailure() string {
|
||||||
|
return common.PropertyGetDefault("scheduler-k3s", "--global", "rollback-on-failure", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getComputedRollbackOnFailure(appName string) string {
|
||||||
|
rollbackOnFailure := getRollbackOnFailure(appName)
|
||||||
|
if rollbackOnFailure == "" {
|
||||||
|
rollbackOnFailure = getGlobalRollbackOnFailure()
|
||||||
|
}
|
||||||
|
|
||||||
|
return rollbackOnFailure
|
||||||
|
}
|
||||||
|
|
||||||
|
func getGlobalGlobalToken() string {
|
||||||
|
return common.PropertyGet("scheduler-k3s", "--global", "token")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getStartCommand(input StartCommandInput) (StartCommandOutput, error) {
|
func getStartCommand(input StartCommandInput) (StartCommandOutput, error) {
|
||||||
|
|||||||
52
plugins/scheduler-k3s/portmap.go
Normal file
52
plugins/scheduler-k3s/portmap.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package scheduler_k3s
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/dokku/dokku/plugins/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PortMap struct {
|
||||||
|
ContainerPort int32 `json:"container_port"`
|
||||||
|
HostPort int32 `json:"host_port"`
|
||||||
|
Scheme string `json:"scheme"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PortMap) String() string {
|
||||||
|
return fmt.Sprintf("%s-%d-%d", p.Scheme, p.HostPort, p.ContainerPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PortMap) IsAllowedHttp() bool {
|
||||||
|
return p.Scheme == "http" || p.ContainerPort == 80
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PortMap) IsAllowedHttps() bool {
|
||||||
|
return p.Scheme == "https" || p.ContainerPort == 443
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPortMaps(appName string) ([]PortMap, error) {
|
||||||
|
portMaps := []PortMap{}
|
||||||
|
|
||||||
|
output, err := common.PlugnTriggerOutputAsString("ports-get", []string{appName, "json"}...)
|
||||||
|
if err != nil {
|
||||||
|
return portMaps, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal([]byte(output), &portMaps)
|
||||||
|
if err != nil {
|
||||||
|
return portMaps, err
|
||||||
|
}
|
||||||
|
|
||||||
|
allowedMappings := []PortMap{}
|
||||||
|
for _, portMap := range portMaps {
|
||||||
|
if !portMap.IsAllowedHttp() && !portMap.IsAllowedHttps() {
|
||||||
|
// todo: log warning
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
allowedMappings = append(allowedMappings, portMap)
|
||||||
|
}
|
||||||
|
|
||||||
|
return allowedMappings, nil
|
||||||
|
}
|
||||||
@@ -17,18 +17,23 @@ import (
|
|||||||
var (
|
var (
|
||||||
// DefaultProperties is a map of all valid k3s properties with corresponding default property values
|
// DefaultProperties is a map of all valid k3s properties with corresponding default property values
|
||||||
DefaultProperties = map[string]string{
|
DefaultProperties = map[string]string{
|
||||||
|
"deploy-timeout": "",
|
||||||
|
"letsencrypt-server": "",
|
||||||
"image-pull-secrets": "",
|
"image-pull-secrets": "",
|
||||||
|
"namespace": "",
|
||||||
"rollback-on-failure": "",
|
"rollback-on-failure": "",
|
||||||
"namespace": "default",
|
|
||||||
"timeout": "300",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GlobalProperties is a map of all valid global k3s properties
|
// GlobalProperties is a map of all valid global k3s properties
|
||||||
GlobalProperties = map[string]bool{
|
GlobalProperties = map[string]bool{
|
||||||
"network-interface": true,
|
"deploy-timeout": true,
|
||||||
"rollback-on-failure": true,
|
"image-pull-secrets": true,
|
||||||
"token": true,
|
"letsencrypt-email-prod": true,
|
||||||
"timeout": true,
|
"letsencrypt-email-stag": true,
|
||||||
|
"namespace": true,
|
||||||
|
"network-interface": true,
|
||||||
|
"rollback-on-failure": true,
|
||||||
|
"token": true,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ func CommandInitialize(taintScheduling bool) error {
|
|||||||
cancel()
|
cancel()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
networkInterface := common.PropertyGetDefault("scheduler-k3s", "--global", "network-interface", "eth0")
|
networkInterface := getGlobalNetworkInterface()
|
||||||
ifaces, err := net.Interfaces()
|
ifaces, err := net.Interfaces()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Unable to get network interfaces: %w", err)
|
return fmt.Errorf("Unable to get network interfaces: %w", err)
|
||||||
@@ -142,7 +142,7 @@ func CommandInitialize(taintScheduling bool) error {
|
|||||||
return fmt.Errorf("Invalid k3s installer filesize")
|
return fmt.Errorf("Invalid k3s installer filesize")
|
||||||
}
|
}
|
||||||
|
|
||||||
token := common.PropertyGet("scheduler-k3s", "--global", "token")
|
token := getGlobalGlobalToken()
|
||||||
if len(token) == 0 {
|
if len(token) == 0 {
|
||||||
n := 5
|
n := 5
|
||||||
b := make([]byte, n)
|
b := make([]byte, n)
|
||||||
@@ -289,7 +289,7 @@ func CommandClusterAdd(role string, remoteHost string, allowUknownHosts bool, ta
|
|||||||
return fmt.Errorf("Invalid server-type: %s", role)
|
return fmt.Errorf("Invalid server-type: %s", role)
|
||||||
}
|
}
|
||||||
|
|
||||||
token := common.PropertyGet("scheduler-k3s", "--global", "token")
|
token := getGlobalGlobalToken()
|
||||||
if len(token) == 0 {
|
if len(token) == 0 {
|
||||||
return fmt.Errorf("Missing k3s token")
|
return fmt.Errorf("Missing k3s token")
|
||||||
}
|
}
|
||||||
@@ -298,7 +298,7 @@ func CommandClusterAdd(role string, remoteHost string, allowUknownHosts bool, ta
|
|||||||
return fmt.Errorf("Taint scheduling can only be used on the server role")
|
return fmt.Errorf("Taint scheduling can only be used on the server role")
|
||||||
}
|
}
|
||||||
|
|
||||||
networkInterface := common.PropertyGetDefault("scheduler-k3s", "--global", "network-interface", "eth0")
|
networkInterface := getGlobalNetworkInterface()
|
||||||
ifaces, err := net.Interfaces()
|
ifaces, err := net.Interfaces()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Unable to get network interfaces: %w", err)
|
return fmt.Errorf("Unable to get network interfaces: %w", err)
|
||||||
|
|||||||
@@ -188,7 +188,7 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
|
|||||||
cancel()
|
cancel()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
namespace := common.PropertyGetDefault("scheduler-k3s", appName, "namespace", "default")
|
namespace := getComputedNamespace(appName)
|
||||||
if err := createKubernetesNamespace(ctx, namespace); err != nil {
|
if err := createKubernetesNamespace(ctx, namespace); err != nil {
|
||||||
return fmt.Errorf("Error creating kubernetes namespace for deployment: %w", err)
|
return fmt.Errorf("Error creating kubernetes namespace for deployment: %w", err)
|
||||||
}
|
}
|
||||||
@@ -198,18 +198,18 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
|
|||||||
return fmt.Errorf("Error getting deploying app image name: %w", err)
|
return fmt.Errorf("Error getting deploying app image name: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
deployTimeout := common.PropertyGetDefault("scheduler-k3s", appName, "deploy-timeout", "300s")
|
deployTimeout := getComputedDeployTimeout(appName)
|
||||||
if _, err := strconv.Atoi(deployTimeout); err == nil {
|
if _, err := strconv.Atoi(deployTimeout); err == nil {
|
||||||
deployTimeout = fmt.Sprintf("%ss", deployTimeout)
|
deployTimeout = fmt.Sprintf("%ss", deployTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
deployRollback := common.PropertyGetDefault("scheduler-k3s", appName, "rollback-on-failure", "false")
|
deployRollback := getComputedRollbackOnFailure(appName)
|
||||||
allowRollbacks, err := strconv.ParseBool(deployRollback)
|
allowRollbacks, err := strconv.ParseBool(deployRollback)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error parsing rollback-on-failure value as boolean: %w", err)
|
return fmt.Errorf("Error parsing rollback-on-failure value as boolean: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
imagePullSecrets := common.PropertyGetDefault("scheduler-k3s", appName, "image-pull-secrets", "")
|
imagePullSecrets := getComputedImagePullSecrets(appName)
|
||||||
|
|
||||||
imageSourceType := "dockerfile"
|
imageSourceType := "dockerfile"
|
||||||
if common.IsImageCnbBased(image) {
|
if common.IsImageCnbBased(image) {
|
||||||
@@ -576,8 +576,7 @@ func TriggerSchedulerEnter(scheduler string, appName string, processType string,
|
|||||||
return fmt.Errorf("Error creating kubernetes client: %w", err)
|
return fmt.Errorf("Error creating kubernetes client: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace := common.PropertyGetDefault("scheduler-k3s", appName, "namespace", "default")
|
namespace := getComputedNamespace(appName)
|
||||||
|
|
||||||
labelSelector := []string{fmt.Sprintf("app.kubernetes.io/part-of=%s", appName)}
|
labelSelector := []string{fmt.Sprintf("app.kubernetes.io/part-of=%s", appName)}
|
||||||
processIndex := 1
|
processIndex := 1
|
||||||
if processType != "" {
|
if processType != "" {
|
||||||
@@ -682,7 +681,7 @@ func TriggerSchedulerLogs(scheduler string, appName string, processType string,
|
|||||||
labelSelector = append(labelSelector, fmt.Sprintf("app.kubernetes.io/name=%s", processType))
|
labelSelector = append(labelSelector, fmt.Sprintf("app.kubernetes.io/name=%s", processType))
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace := common.PropertyGetDefault("scheduler-k3s", appName, "namespace", "default")
|
namespace := getComputedNamespace(appName)
|
||||||
pods, err := clientset.ListPods(ctx, ListPodsInput{
|
pods, err := clientset.ListPods(ctx, ListPodsInput{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
LabelSelector: strings.Join(labelSelector, ","),
|
LabelSelector: strings.Join(labelSelector, ","),
|
||||||
@@ -881,7 +880,7 @@ func TriggerSchedulerRun(scheduler string, appName string, envCount int, args []
|
|||||||
entrypoint = "/exec"
|
entrypoint = "/exec"
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace := common.PropertyGetDefault("scheduler-k3s", appName, "namespace", "default")
|
namespace := getComputedNamespace(appName)
|
||||||
helmAgent, err := NewHelmAgent(namespace, DevNullPrinter)
|
helmAgent, err := NewHelmAgent(namespace, DevNullPrinter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error creating helm agent: %w", err)
|
return fmt.Errorf("Error creating helm agent: %w", err)
|
||||||
@@ -905,7 +904,7 @@ func TriggerSchedulerRun(scheduler string, appName string, envCount int, args []
|
|||||||
}
|
}
|
||||||
|
|
||||||
attachToPod := os.Getenv("DOKKU_DETACH_CONTAINER") != "1"
|
attachToPod := os.Getenv("DOKKU_DETACH_CONTAINER") != "1"
|
||||||
imagePullSecrets := common.PropertyGetDefault("scheduler-k3s", appName, "image-pull-secrets", "")
|
imagePullSecrets := getComputedImagePullSecrets(appName)
|
||||||
workingDir := common.GetWorkingDir(appName, image)
|
workingDir := common.GetWorkingDir(appName, image)
|
||||||
job, err := templateKubernetesJob(Job{
|
job, err := templateKubernetesJob(Job{
|
||||||
AppName: appName,
|
AppName: appName,
|
||||||
@@ -1088,8 +1087,7 @@ func TriggerSchedulerRunList(scheduler string, appName string, format string) er
|
|||||||
return fmt.Errorf("Error creating kubernetes client: %w", err)
|
return fmt.Errorf("Error creating kubernetes client: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace := common.PropertyGetDefault("scheduler-k3s", appName, "namespace", "default")
|
namespace := getComputedNamespace(appName)
|
||||||
|
|
||||||
cronJobs, err := clientset.ListCronJobs(ctx, ListCronJobsInput{
|
cronJobs, err := clientset.ListCronJobs(ctx, ListCronJobsInput{
|
||||||
LabelSelector: fmt.Sprintf("app.kubernetes.io/part-of=%s", appName),
|
LabelSelector: fmt.Sprintf("app.kubernetes.io/part-of=%s", appName),
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
@@ -1149,7 +1147,7 @@ func TriggerSchedulerPostDelete(scheduler string, appName string) error {
|
|||||||
if scheduler != "k3s" {
|
if scheduler != "k3s" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
namespace := common.PropertyGetDefault("scheduler-k3s", appName, "namespace", "default")
|
namespace := getComputedNamespace(appName)
|
||||||
helmAgent, err := NewHelmAgent(namespace, DeployLogPrinter)
|
helmAgent, err := NewHelmAgent(namespace, DeployLogPrinter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error creating helm agent: %w", err)
|
return fmt.Errorf("Error creating helm agent: %w", err)
|
||||||
@@ -1185,7 +1183,7 @@ func TriggerSchedulerStop(scheduler string, appName string) error {
|
|||||||
return fmt.Errorf("Error creating kubernetes client: %w", err)
|
return fmt.Errorf("Error creating kubernetes client: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace := common.PropertyGetDefault("scheduler-k3s", appName, "namespace", "default")
|
namespace := getComputedNamespace(appName)
|
||||||
deployments, err := clientset.ListDeployments(ctx, ListDeploymentsInput{
|
deployments, err := clientset.ListDeployments(ctx, ListDeploymentsInput{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
LabelSelector: fmt.Sprintf("app.kubernetes.io/part-of=%s", appName),
|
LabelSelector: fmt.Sprintf("app.kubernetes.io/part-of=%s", appName),
|
||||||
|
|||||||
Reference in New Issue
Block a user