Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
github.com/nyaruka/phonenumbers v1.1.7
github.com/pkg/errors v0.9.1
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.70.0
github.com/rigdev/rig-go-api v0.0.0-20240419130241-0d1e7be1236b
github.com/rigdev/rig-go-api v0.0.0-20240426160443-55ccf311c714
github.com/rigdev/rig-go-sdk v0.0.0-20240417114812-ecae62a90e9b
github.com/rivo/tview v0.0.0-20240225120200-5605142ca62e
github.com/robfig/cron v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lne
github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/rigdev/rig-go-api v0.0.0-20240419130241-0d1e7be1236b h1:PGy7QXThVTINJaDLdS9yzgeIIxOmDCN/xATkOFHNzug=
github.com/rigdev/rig-go-api v0.0.0-20240419130241-0d1e7be1236b/go.mod h1:0GWZXcPZXpGDoB7FhQAj1CWizEIUjELZenmeGpCnuU4=
github.com/rigdev/rig-go-api v0.0.0-20240426160443-55ccf311c714 h1:xSb0X+taXeO25y4tUuYaiF2bia3WDMDpes9ajUkibzg=
github.com/rigdev/rig-go-api v0.0.0-20240426160443-55ccf311c714/go.mod h1:0GWZXcPZXpGDoB7FhQAj1CWizEIUjELZenmeGpCnuU4=
github.com/rigdev/rig-go-sdk v0.0.0-20240417114812-ecae62a90e9b h1:owpf6cgWMmO31ReecaGvqlmtgj8ozyhxIK1lhTCDUCw=
github.com/rigdev/rig-go-sdk v0.0.0-20240417114812-ecae62a90e9b/go.mod h1:RSzrgNlAsBiUgcSORU0YKw3e97G7GFfQCcM3Sj1pznc=
github.com/rivo/tview v0.0.0-20240225120200-5605142ca62e h1:7ubTieBkl4KCz5ABZzh0zPkBYWPguSOHUundUsorIzQ=
Expand Down
9 changes: 5 additions & 4 deletions pkg/api/platform/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,17 @@ type CapsuleSpecExtension struct {
// Args is a list of arguments either passed to the Command or if Command
// is left empty the arguments will be passed to the ENTRYPOINT of the
// docker image.
Args []string `json:"args,omitempty" protobuf:"5"`
Args []string `json:"args,omitempty" protobuf:"5" patchStrategy:"replace"`

// Interfaces specifies the list of interfaces the the container should
// have. Specifying interfaces will create the corresponding kubernetes
// Services and Ingresses depending on how the interface is configured.
Interfaces []v1alpha2.CapsuleInterface `json:"interfaces,omitempty" protobuf:"6"`
// nolint:lll
Interfaces []v1alpha2.CapsuleInterface `json:"interfaces,omitempty" protobuf:"6" patchMergeKey:"port" patchStrategy:"merge"`

// Files is a list of files to mount in the container. These can either be
// based on ConfigMaps or Secrets.
ConfigFiles []ConfigFile `json:"configFiles" protobuf:"7"`
ConfigFiles []ConfigFile `json:"configFiles" protobuf:"7" patchMergeKey:"path" patchStrategy:"merge"`

EnvironmentVariables map[string]string `json:"environmentVariables,omitempty" protobuf:"12"`

Expand All @@ -118,7 +119,7 @@ type CapsuleSpecExtension struct {
// NodeSelector is a selector for what nodes the Capsule should live on.
NodeSelector map[string]string `json:"nodeSelector,omitempty" protobuf:"9"`

CronJobs []v1alpha2.CronJob `json:"cronJobs,omitempty" protobuf:"10"`
CronJobs []v1alpha2.CronJob `json:"cronJobs,omitempty" protobuf:"10" patchMergeKey:"name" patchStrategy:"replace"`

Annotations map[string]string `json:"annotations" protobuf:"11"`
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/v1alpha2/capsule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ type HorizontalScale struct {
CPUTarget *CPUTarget `json:"cpuTarget,omitempty" protobuf:"2"`
// CustomMetrics specifies custom metrics emitted by the custom.metrics.k8s.io API
// which the autoscaler should scale on
CustomMetrics []CustomMetric `json:"customMetrics,omitempty" protobuf:"3"`
CustomMetrics []CustomMetric `json:"customMetrics,omitempty" protobuf:"3" patchStrategy:"replace"`
}

// Instances specifies the minimum and maximum amount of capsule
Expand Down
45 changes: 45 additions & 0 deletions pkg/obj/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package obj

import (
"bytes"
"encoding/json"
"fmt"

platformv1 "github.com/rigdev/rig-go-api/platform/v1"
v1 "github.com/rigdev/rig/pkg/api/platform/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/util/strategicpatch"
Expand Down Expand Up @@ -97,3 +100,45 @@ func convert[T any](obj any) (T, error) {
}
return objT, nil
}

// nolint:lll
// MergeProjectEnv merges a ProjEnvCapsuleBase into a CapsuleSpecExtension and returns a new object with the merged result
// It uses StrategicMergePatch (https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/)
func MergeProjectEnv(patch *platformv1.ProjEnvCapsuleBase, into *platformv1.CapsuleSpecExtension) (*platformv1.CapsuleSpecExtension, error) {
return mergeCapsuleSpec(patch, into)
}

// nolint:lll
// MergeCapsuleSpecExtension merges a CapsuleSpecExtension into another CapsuleSpecExtension and returns a new object with the merged result
// It uses StrategicMergePatch (https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/)
func MergeCapsuleSpecExtensions(patch, into *platformv1.CapsuleSpecExtension) (*platformv1.CapsuleSpecExtension, error) {
return mergeCapsuleSpec(patch, into)
}

func mergeCapsuleSpec(patch any, into *platformv1.CapsuleSpecExtension) (*platformv1.CapsuleSpecExtension, error) {
// It would be possible to do much faster merging by manualling overwriting protobuf fields.
// This is tedius to maintain so until it becomes an issue, we use json marshalling to leverage StrategicMergePatch
patchBytes, err := json.Marshal(patch)
Comment thread
MatiasFrank marked this conversation as resolved.
if err != nil {
return nil, err
}

intoBytes, err := json.Marshal(into)
if err != nil {
return nil, err
}

outBytes, err := strategicpatch.StrategicMergePatch(intoBytes, patchBytes, &v1.CapsuleSpecExtension{})
if err != nil {
return nil, err
}

out := &platformv1.CapsuleSpecExtension{}
if err := json.Unmarshal(outBytes, out); err != nil {
return nil, err
}
out.Kind = into.GetKind()
out.ApiVersion = into.GetApiVersion()

return out, nil
}
Loading