Skip to content

Commit 8cb2bcf

Browse files
fix(cli): avoid injecting unused provider secrets
1 parent 1204402 commit 8cb2bcf

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

go/core/cli/internal/cli/agent/install.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func InstallCmd(ctx context.Context, cfg *InstallCfg) *PortForward {
107107
return nil
108108
}
109109

110-
helmConfig := setupHelmConfig(modelProvider, apiKeyValue)
110+
helmConfig := setupHelmConfig(modelProvider, apiKeyValue, profiles.InstallsDefaultModelConfig(selectedProfile))
111111

112112
// setup profile if provided
113113
if selectedProfile != "" {
@@ -146,7 +146,7 @@ func InteractiveInstallCmd(ctx context.Context, c *ishell.Context) *PortForward
146146
return nil
147147
}
148148

149-
helmConfig := setupHelmConfig(modelProvider, apiKeyValue)
149+
helmConfig := setupHelmConfig(modelProvider, apiKeyValue, profiles.InstallsDefaultModelConfig(selectedProfile))
150150
helmConfig.inlineValues = profiles.GetProfileYaml(selectedProfile)
151151

152152
return install(ctx, cfg, helmConfig, modelProvider)
@@ -164,12 +164,14 @@ type helmConfig struct {
164164

165165
// setupHelmConfig sets up the helm config for the kagent chart
166166
// This sets up the general configuration for a helm installation without the profile, which is calculated later based on the installation type (interactive or non-interactive)
167-
func setupHelmConfig(modelProvider v1alpha2.ModelProvider, apiKeyValue string) helmConfig {
168-
// Build Helm values
169-
helmProviderKey := GetModelProviderHelmValuesKey(modelProvider)
170-
values := []string{
171-
fmt.Sprintf("providers.default=%s", helmProviderKey),
172-
fmt.Sprintf("providers.%s.apiKey=%s", helmProviderKey, apiKeyValue),
167+
func setupHelmConfig(modelProvider v1alpha2.ModelProvider, apiKeyValue string, installDefaultModelConfig bool) helmConfig {
168+
values := []string{}
169+
if installDefaultModelConfig {
170+
helmProviderKey := GetModelProviderHelmValuesKey(modelProvider)
171+
values = append(values, fmt.Sprintf("providers.default=%s", helmProviderKey))
172+
if apiKeyValue != "" {
173+
values = append(values, fmt.Sprintf("providers.%s.apiKey=%s", helmProviderKey, apiKeyValue))
174+
}
173175
}
174176

175177
// allow user to set the helm registry and version

go/core/cli/internal/cli/agent/install_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/kagent-dev/kagent/go/api/v1alpha2"
77
"github.com/kagent-dev/kagent/go/core/cli/internal/profiles"
8+
"github.com/kagent-dev/kagent/go/core/pkg/env"
89
"github.com/stretchr/testify/assert"
910
)
1011

@@ -61,3 +62,21 @@ func TestShouldRequireProviderCredentials(t *testing.T) {
6162
})
6263
}
6364
}
65+
66+
func TestSetupHelmConfig(t *testing.T) {
67+
t.Setenv(env.KagentHelmRepo.Name(), "")
68+
t.Setenv(env.KagentHelmVersion.Name(), "")
69+
t.Setenv(env.KagentHelmExtraArgs.Name(), "")
70+
71+
t.Run("includes provider values when default modelconfig is installed", func(t *testing.T) {
72+
cfg := setupHelmConfig(v1alpha2.ModelProviderOpenAI, "test-key", true)
73+
assert.Contains(t, cfg.values, "providers.default=openAI")
74+
assert.Contains(t, cfg.values, "providers.openAI.apiKey=test-key")
75+
})
76+
77+
t.Run("omits provider values when default modelconfig is disabled", func(t *testing.T) {
78+
cfg := setupHelmConfig(v1alpha2.ModelProviderOpenAI, "test-key", false)
79+
assert.NotContains(t, cfg.values, "providers.default=openAI")
80+
assert.NotContains(t, cfg.values, "providers.openAI.apiKey=test-key")
81+
})
82+
}

helm/kagent/templates/modelconfig-secret.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{{- if ne .Values.providers.createDefaultModelConfig false }}
22
{{- $dot := . }}
3-
{{- $model := index $dot.Values.providers $dot.Values.providers.default }}
3+
{{- $defaultProvider := $dot.Values.providers.default | default "openAI" }}
4+
{{- if hasKey $dot.Values.providers $defaultProvider | not }}
5+
{{- fail (printf "Provider key=%s is not found under .Values.providers" $defaultProvider) }}
6+
{{- end }}
7+
{{- $model := index $dot.Values.providers $defaultProvider }}
48
{{- if and $model.apiKeySecretRef $model.apiKey }}
59
---
610
apiVersion: v1

helm/kagent/tests/modelconfig-secret_test.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ tests:
4141
path: data.ANTHROPIC_API_KEY
4242
value: YW50aHJvcGljLXRlc3Qta2V5 # base64 of "anthropic-test-key"
4343

44+
- it: should fall back to openai secret when default provider is empty
45+
set:
46+
providers:
47+
default: ""
48+
openAI:
49+
apiKey: "fallback-openai-key"
50+
asserts:
51+
- equal:
52+
path: metadata.name
53+
value: kagent-openai
54+
- equal:
55+
path: data.OPENAI_API_KEY
56+
value: ZmFsbGJhY2stb3BlbmFpLWtleQ== # base64 of "fallback-openai-key"
57+
4458
- it: should render azure openai secret when azure provider is default
4559
set:
4660
providers:

0 commit comments

Comments
 (0)