Skip to content
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
11 changes: 8 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Config struct {
Provider ProviderConfig `envPrefix:"AGENT_"`
Health HealthConfig `envPrefix:"AGENT_"`
Agent AgentConfig `envPrefix:"AGENT_"`
SP SPConfig `envPrefix:""`
DCM DCMConfig `envPrefix:"DCM_"`
Heartbeat HeartbeatConfig `envPrefix:"AGENT_"`
Messaging MessagingConfig `envPrefix:"AGENT_"`
Expand Down Expand Up @@ -62,9 +63,13 @@ type AgentConfig struct {
Name string `env:"NAME"`
Environment string `env:"ENVIRONMENT"`
Cost string `env:"COST"`
// Kubeconfig is the default kubeconfig path for embedded SPs (AGENT_KUBECONFIG).
// When empty, SPs use in-cluster configuration unless overridden per SP.
Kubeconfig string `env:"KUBECONFIG"`
}

// SPConfig holds shared defaults for embedded service providers.
type SPConfig struct {
// DefaultKubeconfig is the kubeconfig path shared by all embedded SPs (SP_DEFAULT_KUBECONFIG).
// When empty, SPs use in-cluster configuration unless overridden per SP via SP_KUBECONFIG.
DefaultKubeconfig string `env:"SP_DEFAULT_KUBECONFIG"`
Comment thread
gciavarrini marked this conversation as resolved.
}

// DCMConfig holds DCM registration configuration.
Expand Down
12 changes: 12 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,18 @@ var _ = Describe("Topic 6 Config", Label("unit"), func() {
})
})

var _ = Describe("SP Config", Label("unit"), func() {
Describe("Load", func() {
It("parses SP_DEFAULT_KUBECONFIG from env", func() {
GinkgoT().Setenv("SP_DEFAULT_KUBECONFIG", "/etc/agent/kubeconfig")

cfg, err := config.Load()
Expect(err).NotTo(HaveOccurred())
Expect(cfg.SP.DefaultKubeconfig).To(Equal("/etc/agent/kubeconfig"))
})
})
})

// writeConfigFile writes a .env-style (KEY=VALUE per line) config file and
// points AGENT_CONFIG_FILE at it. This is REQ-XC-CFG-010's MAY-level
// file-based config support.
Expand Down
23 changes: 0 additions & 23 deletions internal/openshift/acmcluster/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,4 @@ var _ = Describe("Config", func() {
Expect(cfg).To(BeNil())
Expect(err.Error()).To(ContainSubstring("messaging URL is required"))
})

It("uses agent kubeconfig when SP_KUBECONFIG is unset", func() {
setAllRequired()

cfg, err := config.Load(shared.Agent{
MessagingURL: "nats://localhost:4222",
Kubeconfig: "/etc/agent/kubeconfig",
})
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Kubeconfig).To(Equal("/etc/agent/kubeconfig"))
})

It("prefers SP_KUBECONFIG over agent kubeconfig", func() {
setAllRequired()
GinkgoT().Setenv("SP_KUBECONFIG", "/sp/kubeconfig")

cfg, err := config.Load(shared.Agent{
MessagingURL: "nats://localhost:4222",
Kubeconfig: "/agent/kubeconfig",
})
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Kubeconfig).To(Equal("/sp/kubeconfig"))
})
})
2 changes: 1 addition & 1 deletion internal/openshift/container/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const defaultProviderName = "container-sp"
// Config is the root configuration for the embedded container service provider.
type Config struct {
shared.Config
Namespace string `env:"SP_K8S_NAMESPACE" envDefault:"default"`
Namespace string `env:"SP_CONTAINER_NAMESPACE" envDefault:"default"`
ExternalServiceType string `env:"SP_K8S_EXTERNAL_SVC_TYPE"`
DebounceMs int `env:"SP_MONITOR_DEBOUNCE_MS" envDefault:"500"`
ResyncPeriod time.Duration `env:"SP_MONITOR_RESYNC_PERIOD" envDefault:"10m"`
Expand Down
11 changes: 0 additions & 11 deletions internal/openshift/container/config/config_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,4 @@ var _ = Describe("Configuration", func() {
Expect(cfg).To(BeNil())
Expect(err.Error()).To(ContainSubstring("must be LoadBalancer or NodePort"))
})

It("uses agent kubeconfig when SP_KUBECONFIG is unset", func() {
_ = os.Setenv("SP_K8S_EXTERNAL_SVC_TYPE", "NodePort")

cfg, err := config.Load(shared.Agent{
MessagingURL: "nats://test:4222",
Kubeconfig: "/etc/agent/kubeconfig",
})
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Kubeconfig).To(Equal("/etc/agent/kubeconfig"))
})
})
2 changes: 1 addition & 1 deletion internal/openshift/kubevirtvm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const defaultProviderName = "kubevirt-vm-sp"
// Config is the root configuration for the embedded VM service provider.
type Config struct {
shared.Config
Namespace string `env:"KUBERNETES_NAMESPACE" envDefault:"default"`
Namespace string `env:"SP_VM_NAMESPACE" envDefault:"default"`
Comment thread
gciavarrini marked this conversation as resolved.
Timeout time.Duration `env:"KUBERNETES_TIMEOUT" envDefault:"60s"`
MaxRetries int `env:"KUBERNETES_MAX_RETRIES" envDefault:"3"`
NATSMaxReconnect int `env:"NATS_MAX_RECONNECT" envDefault:"-1"`
Expand Down
13 changes: 13 additions & 0 deletions internal/openshift/kubevirtvm/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package config_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Config Suite")
}
43 changes: 43 additions & 0 deletions internal/openshift/kubevirtvm/config/config_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package config_test

import (
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/dcm-project/environment-agent/internal/openshift/kubevirtvm/config"
"github.com/dcm-project/environment-agent/internal/openshift/shared"
)

var _ = Describe("Configuration", func() {
agentDefaults := shared.Agent{MessagingURL: "nats://test:4222"}

clearEnv := func() {
_ = os.Unsetenv("SP_NAME")
_ = os.Unsetenv("SP_VM_NAMESPACE")
_ = os.Unsetenv("SP_KUBECONFIG")
}

BeforeEach(func() {
clearEnv()
})

AfterEach(func() {
clearEnv()
})

It("uses SP_VM_NAMESPACE when set", func() {
_ = os.Setenv("SP_VM_NAMESPACE", "vms")

cfg, err := config.Load(agentDefaults)
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Namespace).To(Equal("vms"))
})

It("defaults namespace to default when SP_VM_NAMESPACE is unset", func() {
cfg, err := config.Load(agentDefaults)
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Namespace).To(Equal("default"))
})
})
2 changes: 1 addition & 1 deletion internal/openshift/shared/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func FromAgent(cfg *agentconfig.Config) Agent {
}
return Agent{
MessagingURL: cfg.Messaging.URL,
Kubeconfig: cfg.Agent.Kubeconfig,
Kubeconfig: cfg.SP.DefaultKubeconfig,
}
}

Expand Down
16 changes: 14 additions & 2 deletions internal/openshift/shared/config_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/dcm-project/environment-agent/internal/config"
"github.com/dcm-project/environment-agent/internal/openshift/shared"
)

Expand All @@ -21,7 +22,7 @@ var _ = Describe("Config", Label("unit"), func() {
Expect(err).To(MatchError("messaging URL is required"))
})

It("uses agent kubeconfig when SP_KUBECONFIG is unset", func() {
It("uses kubeconfig from shared.Agent when SP_KUBECONFIG is unset", func() {
cfg := shared.Config{}
err := shared.LoadInto(&cfg, shared.Agent{
MessagingURL: "nats://agent:4222",
Expand All @@ -33,7 +34,7 @@ var _ = Describe("Config", Label("unit"), func() {
Expect(cfg.Name).To(Equal("test-sp"))
})

It("prefers SP_KUBECONFIG over agent kubeconfig", func() {
It("prefers SP_KUBECONFIG over kubeconfig from shared.Agent", func() {
GinkgoT().Setenv("SP_KUBECONFIG", "/sp/kubeconfig")

cfg := shared.Config{}
Expand All @@ -53,4 +54,15 @@ var _ = Describe("Config", Label("unit"), func() {
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Name).To(Equal("custom-sp"))
})

It("maps SP_DEFAULT_KUBECONFIG from agent config via FromAgent", func() {
GinkgoT().Setenv("SP_DEFAULT_KUBECONFIG", "/etc/agent/kubeconfig")
GinkgoT().Setenv("AGENT_MESSAGING_URL", "nats://agent:4222")

cfg, err := config.Load()
Expect(err).NotTo(HaveOccurred())
agent := shared.FromAgent(cfg)
Expect(agent.Kubeconfig).To(Equal("/etc/agent/kubeconfig"))
Expect(agent.MessagingURL).To(Equal("nats://agent:4222"))
})
})
2 changes: 1 addition & 1 deletion internal/openshift/storage/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const defaultProviderName = "storage"
// Config is the root configuration for the embedded storage service provider.
type Config struct {
shared.Config
Namespace string `env:"SP_K8S_NAMESPACE" envDefault:"default"`
Namespace string `env:"SP_STORAGE_NAMESPACE" envDefault:"default"`
DefaultStorageClass string `env:"SP_K8S_DEFAULT_STORAGE_CLASS"`
DefaultAccessMode string `env:"SP_K8S_DEFAULT_ACCESS_MODE" envDefault:"ReadWriteOnce"`
DebounceMs int `env:"SP_MONITOR_DEBOUNCE_MS" envDefault:"500"`
Expand Down
13 changes: 13 additions & 0 deletions internal/openshift/storage/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package config_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Config Suite")
}
44 changes: 44 additions & 0 deletions internal/openshift/storage/config/config_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package config_test

import (
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/dcm-project/environment-agent/internal/openshift/shared"
"github.com/dcm-project/environment-agent/internal/openshift/storage/config"
)

var _ = Describe("Configuration", func() {
agentDefaults := shared.Agent{MessagingURL: "nats://test:4222"}

clearEnv := func() {
_ = os.Unsetenv("SP_NAME")
_ = os.Unsetenv("SP_STORAGE_NAMESPACE")
_ = os.Unsetenv("SP_KUBECONFIG")
_ = os.Unsetenv("SP_K8S_DEFAULT_ACCESS_MODE")
}

BeforeEach(func() {
clearEnv()
})

AfterEach(func() {
clearEnv()
})

It("uses SP_STORAGE_NAMESPACE when set", func() {
_ = os.Setenv("SP_STORAGE_NAMESPACE", "storage-ns")

cfg, err := config.Load(agentDefaults)
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Namespace).To(Equal("storage-ns"))
})

It("defaults namespace to default when SP_STORAGE_NAMESPACE is unset", func() {
cfg, err := config.Load(agentDefaults)
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Namespace).To(Equal("default"))
})
})