Skip to content

Commit

Permalink
Merge pull request #87 from vshn/partof
Browse files Browse the repository at this point in the history
Partof
  • Loading branch information
davidgubler authored Sep 14, 2023
2 parents 4c19672 + 267792e commit 4818461
Show file tree
Hide file tree
Showing 25 changed files with 755 additions and 248 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/docker-compose-*
/env
/k8ify
/.idea

# Goreleaser
/dist/
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Service Labels
| `k8ify.converter: $script` | Call `$script` to convert this service into a K8s object, expecting YAML on `$script`'s stdout. Used for plugging additional functionality into k8ify. The first argument sent to `$script` is the name of the resource, after that all the parameters follow (next row) |
| `k8ify.converter.$key: $value` | Call `$script` with parameter `--$key $value` |
| `k8ify.serviceAccountName: $name` | Set this service's pod(s) spec.serviceAccountName to `$name`, which tells the pod(s) to use ServiceAccount `$name` for accessing the K8s API. This does not set up the ServiceAcccount itself. |
| `k8ify.partOf: $name` | This compose service will be combined with another compose service (resulting in a deployment or statefulSet with multiple containers). Useful e.g. for sidecars or closely coupled services like nginx & php-fpm. |

Volume Labels

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/vshn/k8ify

go 1.18
go 1.21

require (
github.com/compose-spec/compose-go v1.18.4
Expand Down
61 changes: 44 additions & 17 deletions internal/prechecks.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,46 @@
package internal

import (
"github.com/vshn/k8ify/pkg/util"
"os"

composeTypes "github.com/compose-spec/compose-go/types"
"github.com/sirupsen/logrus"
"github.com/vshn/k8ify/pkg/ir"
)

const HLINE = "--------------------------------------------------------------------------------"

func ComposeServicePrecheck(composeService composeTypes.ServiceConfig) {
if composeService.Deploy == nil || composeService.Deploy.Resources.Reservations == nil {
logrus.Error(HLINE)
logrus.Errorf(" Service '%s' does not have any CPU/memory reservations defined.", composeService.Name)
logrus.Error(" k8ify can generate K8s manifests regardless, but your service will be")
logrus.Error(" unreliable or not work at all: It may not start at all, be slow to react")
logrus.Error(" due to insufficient CPU time or get OOM killed due to insufficient memory.")
logrus.Error(" Please specify CPU and memory reservations like this:")
logrus.Error(" services:")
logrus.Errorf(" %s:", composeService.Name)
logrus.Error(" deploy:")
logrus.Error(" resources:")
logrus.Error(" reservations: # Minimum guaranteed by K8s to be always available")
logrus.Error(` cpus: "0.2" # Number of CPU cores. Quotes are required!`)
logrus.Error(" memory: 256M")
logrus.Error(HLINE)
func ComposeServicePrecheck(inputs *ir.Inputs) {
for _, service := range inputs.Services {
composeService := service.AsCompose()
if composeService.Deploy == nil || composeService.Deploy.Resources.Reservations == nil {
logrus.Error(HLINE)
logrus.Errorf(" Service '%s' does not have any CPU/memory reservations defined.", composeService.Name)
logrus.Error(" k8ify can generate K8s manifests regardless, but your service will be")
logrus.Error(" unreliable or not work at all: It may not start at all, be slow to react")
logrus.Error(" due to insufficient CPU time or get OOM killed due to insufficient memory.")
logrus.Error(" Please specify CPU and memory reservations like this:")
logrus.Error(" services:")
logrus.Errorf(" %s:", composeService.Name)
logrus.Error(" deploy:")
logrus.Error(" resources:")
logrus.Error(" reservations: # Minimum guaranteed by K8s to be always available")
logrus.Error(` cpus: "0.2" # Number of CPU cores. Quotes are required!`)
logrus.Error(" memory: 256M")
logrus.Error(HLINE)
}
parentSingleton := util.IsSingleton(composeService.Labels)
for _, part := range service.GetParts() {
partSingleton := util.IsSingleton(part.AsCompose().Labels)
if partSingleton && !parentSingleton {
logrus.Errorf("Singleton compose service %s can't be part of non-singleton compose service %s", part.Name, service.Name)
os.Exit(1)
}
if !partSingleton && parentSingleton {
logrus.Errorf("Non-singleton compose service %s can't be part of singleton compose service %s", part.Name, service.Name)
os.Exit(1)
}
}
}
}

Expand All @@ -34,7 +49,19 @@ func VolumesPrecheck(inputs *ir.Inputs) {
references := make(map[string][]string)

for _, service := range inputs.Services {

// conditions must be met not only for volumes of the parent but also all volumes of the parts
allVolumes := make(map[string]bool) // set semantics (eliminate duplicates)
for _, volumeName := range service.VolumeNames() {
allVolumes[volumeName] = true
}
for _, part := range service.GetParts() {
for _, volumeName := range part.VolumeNames() {
allVolumes[volumeName] = true
}
}

for volumeName := range allVolumes {
volume, ok := inputs.Volumes[volumeName]

// CHECK: Volume does not exist
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ func Main(args []string) int {
}

inputs := ir.FromCompose(project)
internal.ComposeServicePrecheck(inputs)
internal.VolumesPrecheck(inputs)

objects := converter.Objects{}

for _, service := range inputs.Services {
internal.ComposeServicePrecheck(service.AsCompose())
objects = objects.Append(converter.ComposeServiceToK8s(config.Ref, &service, inputs.Volumes))
objects = objects.Append(converter.ComposeServiceToK8s(config.Ref, service, inputs.Volumes))
}

converter.PatchIngresses(objects.Ingresses, config.IngressPatch)
Expand Down
Loading

0 comments on commit 4818461

Please sign in to comment.