feat: add cluster context support in Compose with x-context extension

This commit is contained in:
Pasha Sviderski
2026-03-13 18:37:36 +10:00
parent 8ae38cfd57
commit 1525d182b5
8 changed files with 121 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
package compose
import (
"github.com/compose-spec/compose-go/v2/types"
)
// ContextExtensionKey is the top-level Compose extension key for specifying the cluster context.
const ContextExtensionKey = "x-context"
// ClusterContext extracts the x-context value from the project's top-level extensions.
// Returns an empty string if x-context is not set.
func ClusterContext(project *types.Project) string {
v, ok := project.Extensions[ContextExtensionKey]
if !ok {
return ""
}
s, ok := v.(string)
if !ok {
return ""
}
return s
}
+45
View File
@@ -0,0 +1,45 @@
package compose
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClusterContext(t *testing.T) {
tests := []struct {
name string
content string
want string
}{
{
name: "no x-context",
content: `
services:
web:
image: nginx
`,
want: "",
},
{
name: "x-context set",
content: `
x-context: prod
services:
web:
image: nginx
`,
want: "prod",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
project, err := LoadProjectFromContent(context.Background(), tt.content)
require.NoError(t, err)
assert.Equal(t, tt.want, ClusterContext(project))
})
}
}