feat: initial gitea-k3s-deployer service, web UI, tests, and K3s/Depot manifests
Build and Push Image / build (push) Failing after 5s
Build and Push Image / build (push) Failing after 5s
This commit is contained in:
commit
bf229aeed2
23 files changed
+2702
No files matched your search
@@ -0,0 +1,423 @@
|
||||
package manifest
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type ResourceRequirements struct {
|
||||
Requests ResourceList `yaml:"requests" json:"requests"`
|
||||
Limits ResourceList `yaml:"limits" json:"limits"`
|
||||
}
|
||||
|
||||
type ResourceList struct {
|
||||
CPU string `yaml:"cpu" json:"cpu"`
|
||||
Memory string `yaml:"memory" json:"memory"`
|
||||
}
|
||||
|
||||
type DeploySpec struct {
|
||||
Name string `yaml:"name" json:"name"`
|
||||
Namespace string `yaml:"namespace" json:"namespace"`
|
||||
Branch string `yaml:"branch" json:"branch"`
|
||||
Image string `yaml:"image" json:"image"`
|
||||
TagStrategy string `yaml:"tagStrategy" json:"tagStrategy"` // sha, latest, release, ref
|
||||
Port int `yaml:"port" json:"port"`
|
||||
Host string `yaml:"host" json:"host"`
|
||||
Replicas int `yaml:"replicas" json:"replicas"`
|
||||
HealthPath string `yaml:"healthPath" json:"healthPath"`
|
||||
Env map[string]string `yaml:"env" json:"env"`
|
||||
SecretRefs []string `yaml:"secretRefs" json:"secretRefs"`
|
||||
ImagePullSecrets []string `yaml:"imagePullSecrets" json:"imagePullSecrets"`
|
||||
Resources ResourceRequirements `yaml:"resources" json:"resources"`
|
||||
ManifestsDir string `yaml:"manifestsDir" json:"manifestsDir"`
|
||||
}
|
||||
|
||||
type RenderContext struct {
|
||||
Owner string
|
||||
RepoName string
|
||||
Ref string
|
||||
CommitSHA string
|
||||
ImageTag string
|
||||
DefaultImagePullSecret string
|
||||
DomainSuffix string
|
||||
}
|
||||
|
||||
type K8sObject struct {
|
||||
APIVersion string
|
||||
Kind string
|
||||
Namespace string
|
||||
Name string
|
||||
RawYAML []byte
|
||||
}
|
||||
|
||||
var nonAlnumDash = regexp.MustCompile(`[^a-z0-9-]+`)
|
||||
|
||||
func SanitizeName(s string) string {
|
||||
s = strings.ToLower(s)
|
||||
s = nonAlnumDash.ReplaceAllString(s, "-")
|
||||
s = strings.Trim(s, "-")
|
||||
if len(s) > 63 {
|
||||
s = strings.TrimRight(s[:63], "-")
|
||||
}
|
||||
if s == "" {
|
||||
return "app"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func ParseSpec(raw []byte, ctx RenderContext) (DeploySpec, error) {
|
||||
var spec DeploySpec
|
||||
if err := yaml.Unmarshal(raw, &spec); err != nil {
|
||||
return spec, fmt.Errorf("parse .latha-deploy.yaml: %w", err)
|
||||
}
|
||||
if spec.Name == "" {
|
||||
spec.Name = SanitizeName(ctx.RepoName)
|
||||
} else {
|
||||
spec.Name = SanitizeName(spec.Name)
|
||||
}
|
||||
if spec.Namespace == "" {
|
||||
spec.Namespace = "default"
|
||||
}
|
||||
if spec.Branch == "" {
|
||||
spec.Branch = "main"
|
||||
}
|
||||
if spec.Image == "" {
|
||||
spec.Image = fmt.Sprintf("git.site.latha.org/%s/%s", ctx.Owner, ctx.RepoName)
|
||||
}
|
||||
if spec.Port <= 0 {
|
||||
spec.Port = 8080
|
||||
}
|
||||
if spec.Replicas <= 0 {
|
||||
spec.Replicas = 1
|
||||
}
|
||||
if len(spec.ImagePullSecrets) == 0 && ctx.DefaultImagePullSecret != "" {
|
||||
spec.ImagePullSecrets = []string{ctx.DefaultImagePullSecret}
|
||||
}
|
||||
// Enforce non-BestEffort resource defaults for the 7.75 GiB K3s node
|
||||
if spec.Resources.Requests.CPU == "" {
|
||||
spec.Resources.Requests.CPU = "50m"
|
||||
}
|
||||
if spec.Resources.Requests.Memory == "" {
|
||||
spec.Resources.Requests.Memory = "64Mi"
|
||||
}
|
||||
if spec.Resources.Limits.CPU == "" {
|
||||
spec.Resources.Limits.CPU = "500m"
|
||||
}
|
||||
if spec.Resources.Limits.Memory == "" {
|
||||
spec.Resources.Limits.Memory = "256Mi"
|
||||
}
|
||||
return spec, nil
|
||||
}
|
||||
|
||||
func ResolveImageTag(spec DeploySpec, ctx RenderContext) string {
|
||||
if ctx.ImageTag != "" {
|
||||
return ctx.ImageTag
|
||||
}
|
||||
switch spec.TagStrategy {
|
||||
case "latest":
|
||||
return "latest"
|
||||
case "release", "ref":
|
||||
if ctx.Ref != "" {
|
||||
return SanitizeName(ctx.Ref)
|
||||
}
|
||||
return "latest"
|
||||
case "sha", "":
|
||||
if len(ctx.CommitSHA) >= 8 {
|
||||
return ctx.CommitSHA[:8]
|
||||
}
|
||||
if ctx.CommitSHA != "" {
|
||||
return ctx.CommitSHA
|
||||
}
|
||||
return "latest"
|
||||
default:
|
||||
return "latest"
|
||||
}
|
||||
}
|
||||
|
||||
func RenderFromSpec(spec DeploySpec, ctx RenderContext) ([]byte, error) {
|
||||
tag := ResolveImageTag(spec, ctx)
|
||||
fullImage := fmt.Sprintf("%s:%s", spec.Image, tag)
|
||||
|
||||
var docs [][]byte
|
||||
|
||||
if spec.Namespace != "default" && spec.Namespace != "kube-system" {
|
||||
nsObj := map[string]any{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Namespace",
|
||||
"metadata": map[string]any{
|
||||
"name": spec.Namespace,
|
||||
"labels": map[string]any{
|
||||
"app.kubernetes.io/managed-by": "gitea-k3s-deployer",
|
||||
},
|
||||
},
|
||||
}
|
||||
b, err := yaml.Marshal(nsObj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
docs = append(docs, b)
|
||||
}
|
||||
|
||||
container := map[string]any{
|
||||
"name": spec.Name,
|
||||
"image": fullImage,
|
||||
"imagePullPolicy": "Always",
|
||||
"ports": []map[string]any{
|
||||
{"containerPort": spec.Port, "name": "http"},
|
||||
},
|
||||
"resources": map[string]any{
|
||||
"requests": map[string]any{
|
||||
"cpu": spec.Resources.Requests.CPU,
|
||||
"memory": spec.Resources.Requests.Memory,
|
||||
},
|
||||
"limits": map[string]any{
|
||||
"cpu": spec.Resources.Limits.CPU,
|
||||
"memory": spec.Resources.Limits.Memory,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if len(spec.Env) > 0 {
|
||||
var envList []map[string]any
|
||||
for k, v := range spec.Env {
|
||||
envList = append(envList, map[string]any{"name": k, "value": v})
|
||||
}
|
||||
container["env"] = envList
|
||||
}
|
||||
|
||||
if len(spec.SecretRefs) > 0 {
|
||||
var envFrom []map[string]any
|
||||
for _, s := range spec.SecretRefs {
|
||||
envFrom = append(envFrom, map[string]any{
|
||||
"secretRef": map[string]any{"name": s},
|
||||
})
|
||||
}
|
||||
container["envFrom"] = envFrom
|
||||
}
|
||||
|
||||
if spec.HealthPath != "" {
|
||||
probe := map[string]any{
|
||||
"httpGet": map[string]any{
|
||||
"path": spec.HealthPath,
|
||||
"port": "http",
|
||||
},
|
||||
"initialDelaySeconds": 3,
|
||||
"periodSeconds": 10,
|
||||
}
|
||||
container["readinessProbe"] = probe
|
||||
container["livenessProbe"] = map[string]any{
|
||||
"httpGet": map[string]any{
|
||||
"path": spec.HealthPath,
|
||||
"port": "http",
|
||||
},
|
||||
"initialDelaySeconds": 10,
|
||||
"periodSeconds": 15,
|
||||
}
|
||||
}
|
||||
|
||||
podSpec := map[string]any{
|
||||
"containers": []map[string]any{container},
|
||||
}
|
||||
if len(spec.ImagePullSecrets) > 0 {
|
||||
var ips []map[string]any
|
||||
for _, s := range spec.ImagePullSecrets {
|
||||
ips = append(ips, map[string]any{"name": s})
|
||||
}
|
||||
podSpec["imagePullSecrets"] = ips
|
||||
}
|
||||
|
||||
labels := map[string]any{
|
||||
"app": spec.Name,
|
||||
"app.kubernetes.io/managed-by": "gitea-k3s-deployer",
|
||||
}
|
||||
|
||||
deployObj := map[string]any{
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
"metadata": map[string]any{
|
||||
"name": spec.Name,
|
||||
"namespace": spec.Namespace,
|
||||
"labels": labels,
|
||||
},
|
||||
"spec": map[string]any{
|
||||
"replicas": spec.Replicas,
|
||||
"selector": map[string]any{
|
||||
"matchLabels": map[string]any{"app": spec.Name},
|
||||
},
|
||||
"template": map[string]any{
|
||||
"metadata": map[string]any{
|
||||
"labels": labels,
|
||||
"annotations": map[string]any{
|
||||
"latha.org/commit": ctx.CommitSHA,
|
||||
"latha.org/ref": ctx.Ref,
|
||||
},
|
||||
},
|
||||
"spec": podSpec,
|
||||
},
|
||||
},
|
||||
}
|
||||
b, err := yaml.Marshal(deployObj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
docs = append(docs, b)
|
||||
|
||||
svcObj := map[string]any{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Service",
|
||||
"metadata": map[string]any{
|
||||
"name": spec.Name,
|
||||
"namespace": spec.Namespace,
|
||||
"labels": labels,
|
||||
},
|
||||
"spec": map[string]any{
|
||||
"selector": map[string]any{"app": spec.Name},
|
||||
"ports": []map[string]any{
|
||||
{
|
||||
"port": 80,
|
||||
"targetPort": "http",
|
||||
"protocol": "TCP",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
b, err = yaml.Marshal(svcObj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
docs = append(docs, b)
|
||||
|
||||
if spec.Host != "" {
|
||||
ingObj := map[string]any{
|
||||
"apiVersion": "networking.k8s.io/v1",
|
||||
"kind": "Ingress",
|
||||
"metadata": map[string]any{
|
||||
"name": spec.Name,
|
||||
"namespace": spec.Namespace,
|
||||
"labels": labels,
|
||||
"annotations": map[string]any{
|
||||
"traefik.ingress.kubernetes.io/router.entrypoints": "websecure",
|
||||
"traefik.ingress.kubernetes.io/router.tls.certresolver": "letsencrypt",
|
||||
"traefik.ingress.kubernetes.io/router.tls.domains.0.main": spec.Host,
|
||||
},
|
||||
},
|
||||
"spec": map[string]any{
|
||||
"ingressClassName": "traefik",
|
||||
"rules": []map[string]any{
|
||||
{
|
||||
"host": spec.Host,
|
||||
"http": map[string]any{
|
||||
"paths": []map[string]any{
|
||||
{
|
||||
"path": "/",
|
||||
"pathType": "Prefix",
|
||||
"backend": map[string]any{
|
||||
"service": map[string]any{
|
||||
"name": spec.Name,
|
||||
"port": map[string]any{"number": 80},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
b, err = yaml.Marshal(ingObj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
docs = append(docs, b)
|
||||
}
|
||||
|
||||
return bytes.Join(docs, []byte("---\n")), nil
|
||||
}
|
||||
|
||||
func SubstituteVars(raw []byte, ctx RenderContext) []byte {
|
||||
shortSHA := ctx.CommitSHA
|
||||
if len(shortSHA) > 8 {
|
||||
shortSHA = shortSHA[:8]
|
||||
}
|
||||
tag := ctx.ImageTag
|
||||
if tag == "" {
|
||||
if shortSHA != "" {
|
||||
tag = shortSHA
|
||||
} else {
|
||||
tag = "latest"
|
||||
}
|
||||
}
|
||||
replacer := strings.NewReplacer(
|
||||
"${IMAGE_TAG}", tag,
|
||||
"{{ .Tag }}", tag,
|
||||
"${COMMIT_SHA}", ctx.CommitSHA,
|
||||
"${SHORT_SHA}", shortSHA,
|
||||
"${APP_NAME}", SanitizeName(ctx.RepoName),
|
||||
"${REF}", ctx.Ref,
|
||||
)
|
||||
return []byte(replacer.Replace(string(raw)))
|
||||
}
|
||||
|
||||
func SplitYAMLObjects(raw []byte, defaultNamespace string) ([]K8sObject, error) {
|
||||
dec := yaml.NewDecoder(bytes.NewReader(raw))
|
||||
var objs []K8sObject
|
||||
for {
|
||||
var doc map[string]any
|
||||
err := dec.Decode(&doc)
|
||||
if errors.Is(err, io.EOF) {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode yaml manifest: %w", err)
|
||||
}
|
||||
if len(doc) == 0 {
|
||||
continue
|
||||
}
|
||||
apiVersion, _ := doc["apiVersion"].(string)
|
||||
kind, _ := doc["kind"].(string)
|
||||
if apiVersion == "" || kind == "" {
|
||||
return nil, fmt.Errorf("manifest missing apiVersion or kind")
|
||||
}
|
||||
meta, _ := doc["metadata"].(map[string]any)
|
||||
name, _ := meta["name"].(string)
|
||||
ns, _ := meta["namespace"].(string)
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("manifest %s/%s missing metadata.name", apiVersion, kind)
|
||||
}
|
||||
if ns == "" && !isClusterScoped(kind) {
|
||||
if defaultNamespace != "" {
|
||||
ns = defaultNamespace
|
||||
} else {
|
||||
ns = "default"
|
||||
}
|
||||
meta["namespace"] = ns
|
||||
}
|
||||
encoded, err := yaml.Marshal(doc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
objs = append(objs, K8sObject{
|
||||
APIVersion: apiVersion,
|
||||
Kind: kind,
|
||||
Namespace: ns,
|
||||
Name: name,
|
||||
RawYAML: encoded,
|
||||
})
|
||||
}
|
||||
return objs, nil
|
||||
}
|
||||
|
||||
func isClusterScoped(kind string) bool {
|
||||
switch kind {
|
||||
case "Namespace", "ClusterRole", "ClusterRoleBinding", "PersistentVolume", "StorageClass", "CustomResourceDefinition", "IngressClass":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user