This commit is contained in:
Tomer Zait
2024-01-16 23:44:49 +02:00
parent 770da91eec
commit cd6a318896
11 changed files with 255 additions and 30 deletions
+63
View File
@@ -33,6 +33,7 @@ import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
batchv1 "k8s.io/api/batch/v1"
api "k8s.io/api/core/v1"
)
@@ -693,6 +694,42 @@ func parseEnvironment(composeServiceConfig *types.ServiceConfig, serviceConfig *
}
}
func handleCronJobConcurrencyPolicy(policy string) (batchv1.ConcurrencyPolicy, error) {
switch policy {
case "Allow":
return batchv1.AllowConcurrent, nil
case "Forbid":
return batchv1.ForbidConcurrent, nil
case "Replace":
return batchv1.ReplaceConcurrent, nil
case "":
return "", nil
default:
return "", fmt.Errorf("invalid cronjob concurrency policy: %s", policy)
}
}
func handleCronJobBackoffLimit(backoffLimit string) (*int32, error) {
if backoffLimit == "" {
return nil, nil
}
limit, err := cast.ToInt32E(backoffLimit)
if err != nil {
return nil, fmt.Errorf("invalid cronjob backoff limit: %s", backoffLimit)
}
return &limit, nil
}
func handleCronJobSchedule(schedule string) (string, error) {
if schedule == "" {
return "", fmt.Errorf("cronjob schedule cannot be empty")
}
return schedule, nil
}
// parseKomposeLabels parse kompose labels, also do some validation
func parseKomposeLabels(labels map[string]string, serviceConfig *kobject.ServiceConfig) error {
// Label handler
@@ -734,6 +771,27 @@ func parseKomposeLabels(labels map[string]string, serviceConfig *kobject.Service
serviceConfig.ImagePullPolicy = value
case LabelContainerVolumeSubpath:
serviceConfig.VolumeMountSubPath = value
case LabelCronJobSchedule:
cronJobSchedule, err := handleCronJobSchedule(value)
if err != nil {
return errors.Wrap(err, "handleCronJobSchedule failed")
}
serviceConfig.CronJobSchedule = cronJobSchedule
case LabelCronJobConcurrencyPolicy:
cronJobConcurrencyPolicy, err := handleCronJobConcurrencyPolicy(value)
if err != nil {
return errors.Wrap(err, "handleCronJobConcurrencyPolicy failed")
}
serviceConfig.CronJobConcurrencyPolicy = cronJobConcurrencyPolicy
case LabelCronJobBackoffLimit:
cronJobBackoffLimit, err := handleCronJobBackoffLimit(value)
if err != nil {
return errors.Wrap(err, "handleCronJobBackoffLimit failed")
}
serviceConfig.CronJobBackoffLimit = cronJobBackoffLimit
default:
serviceConfig.Labels[key] = value
}
@@ -755,6 +813,11 @@ func parseKomposeLabels(labels map[string]string, serviceConfig *kobject.Service
return errors.New("cannot set kompose.service.nodeport.port when service has multiple ports")
}
if serviceConfig.Restart == "always" && serviceConfig.CronJobConcurrencyPolicy != "" {
log.Infof("cronjob restart policy will be converted from '%s' to 'on-failure'", serviceConfig.Restart)
serviceConfig.Restart = "on-failure"
}
return nil
}
+6 -2
View File
@@ -76,14 +76,18 @@ const (
HealthCheckLivenessHTTPGetPort = "kompose.service.healthcheck.liveness.http_get_port"
// HealthCheckLivenessTCPPort defines liveness health check tcp port
HealthCheckLivenessTCPPort = "kompose.service.healthcheck.liveness.tcp_port"
// ServiceTypeHeadless ...
ServiceTypeHeadless = "Headless"
// LabelSecurityContextFsGroup defines the pod FsGroup
LabelSecurityContextFsGroup = "kompose.security-context.fsgroup"
// LabelContainerVolumeSubpath defines the volume mount subpath inside container
LabelContainerVolumeSubpath = "kompose.volume.subpath"
// LabelCronJobSchedule defines the cron job schedule
LabelCronJobSchedule = "kompose.cronjob.schedule"
// LabelCronJobConcurrencyPolicy defines the cron job concurrency policy
LabelCronJobConcurrencyPolicy = "kompose.cronjob.concurrency_policy"
// LabelCronJobBackoffLimit defines the job backoff limit
LabelCronJobBackoffLimit = "kompose.cronjob.backoff_limit"
)
// load environment variables from compose file