Skip to content

Commit 7ea8d29

Browse files
committed
feat: env variables for provider name and namespace
1 parent 157eaa7 commit 7ea8d29

File tree

8 files changed

+131
-8
lines changed

8 files changed

+131
-8
lines changed

api/constants/constants.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,11 @@ const (
2121
OnboardingNameLabel = OpenMCPGroupName + "/onboarding-name"
2222
// OnboardingNamespaceLabel is used to store the namespace on the onboarding cluster of a resource.
2323
OnboardingNamespaceLabel = OpenMCPGroupName + "/onboarding-namespace"
24+
25+
// EnvVariableProviderName is the name of an environment variable passed to providers.
26+
// It contains the name of the provider resource.
27+
EnvVariableProviderName = "OPENMCP_PROVIDER_NAME"
28+
// EnvVariablePlatformClusterNamespace is the name of an environment variable passed to providers.
29+
// It contains the namespace on the platform cluster in which the provider is running.
30+
EnvVariablePlatformClusterNamespace = "OPENMCP_PLATFORM_CLUSTER_NAMESPACE"
2431
)

docs/controller/deployment.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ run
2626
--verbosity <DEBUG|INFO|ERROR>
2727
```
2828

29+
### Env Variables
30+
31+
The environment variables below are available in the pods of the init job and the deployment of providers:
32+
33+
- the environment variables specified in the [provider resource](#provider-resource), in field `spec.env`;
34+
- the following predefined environment variables:
35+
- `OPENMCP_PROVIDER_NAME`: the name of the provider resource;
36+
- `OPENMCP_PLATFORM_CLUSTER_NAMESPACE`: the namespace on the platform cluster in which the provider is running.
37+
38+
2939
## Provider Resource
3040

3141
The provider resources specify how to deploy the providers. They are of the kind `ClusterProvider`, `ServiceProvider`, or `PlatformService`. They are cluster-scoped, and have the following common structure:

internal/controllers/provider/controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ var _ = Describe("Deployment Controller", func() {
6464
Expect(job.Spec.Template.Spec.Containers[0].Args).To(ContainElement("init"), "Job container args should contain the init command")
6565
Expect(job.Spec.Template.Spec.Containers[0].Args).To(ContainElement("--environment=test-environment"), "Job container args should contain the environment")
6666
Expect(job.Spec.Template.Spec.Containers[0].Args).To(ContainElement("--verbosity=DEBUG"), "Job container args should contain the verbosity")
67-
Expect(job.Spec.Template.Spec.Containers[0].Env).To(HaveLen(1), "Job container should have an environment variables")
67+
Expect(job.Spec.Template.Spec.Containers[0].Env).To(HaveLen(3), "Job container should have an environment variables")
6868
Expect(job.Spec.Template.Spec.Containers[0].Env[0].Name).To(Equal("NAME"), "Job container environment variable name should match the provider spec")
6969
Expect(job.Spec.Template.Spec.Containers[0].Env[0].Value).To(Equal("test-name"), "Job container environment variable value should match the provider spec")
7070

@@ -82,7 +82,7 @@ var _ = Describe("Deployment Controller", func() {
8282
Expect(deploy.Spec.Template.Spec.Containers[0].Args).To(ContainElement("run"), "Deployment container args should contain the run command")
8383
Expect(deploy.Spec.Template.Spec.Containers[0].Args).To(ContainElement("--environment=test-environment"), "Deployment container args should contain the environment")
8484
Expect(deploy.Spec.Template.Spec.Containers[0].Args).To(ContainElement("--verbosity=DEBUG"), "Deployment container args should contain the verbosity")
85-
Expect(deploy.Spec.Template.Spec.Containers[0].Env).To(HaveLen(1), "Deployment container should have an environment variables")
85+
Expect(deploy.Spec.Template.Spec.Containers[0].Env).To(HaveLen(3), "Deployment container should have an environment variables")
8686
Expect(deploy.Spec.Template.Spec.Containers[0].Env[0].Name).To(Equal("NAME"), "Deployment container environment variable name should match the provider spec")
8787
Expect(deploy.Spec.Template.Spec.Containers[0].Env[0].Value).To(Equal("test-name"), "Deployment container environment variable value should match the provider spec")
8888

internal/controllers/provider/install/deployment.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ func (m *deploymentMutator) Empty() *appsv1.Deployment {
4949
}
5050

5151
func (m *deploymentMutator) Mutate(d *appsv1.Deployment) error {
52+
env, err := m.values.EnvironmentVariables()
53+
if err != nil {
54+
return err
55+
}
56+
5257
d.Spec = appsv1.DeploymentSpec{
5358
Replicas: ptr.To[int32](1),
5459
Selector: &metav1.LabelSelector{
@@ -69,7 +74,7 @@ func (m *deploymentMutator) Mutate(d *appsv1.Deployment) error {
6974
"--environment=" + m.values.Environment(),
7075
"--verbosity=" + m.values.Verbosity(),
7176
},
72-
Env: m.values.EnvironmentVariables(),
77+
Env: env,
7378
},
7479
},
7580
ImagePullSecrets: m.values.ImagePullSecrets(),

internal/controllers/provider/install/job.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ func (m *jobMutator) Empty() *v1.Job {
5555
}
5656

5757
func (m *jobMutator) Mutate(j *v1.Job) error {
58+
env, err := m.values.EnvironmentVariables()
59+
if err != nil {
60+
return err
61+
}
62+
5863
j.Spec = v1.JobSpec{
5964
BackoffLimit: ptr.To[int32](4),
6065
Template: corev1.PodTemplateSpec{
@@ -72,7 +77,7 @@ func (m *jobMutator) Mutate(j *v1.Job) error {
7277
"--environment=" + m.values.Environment(),
7378
"--verbosity=" + m.values.Verbosity(),
7479
},
75-
Env: m.values.EnvironmentVariables(),
80+
Env: env,
7681
},
7782
},
7883
ServiceAccountName: m.values.NamespacedResourceName(initPrefix),
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package install
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestComponentUtils(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
13+
RunSpecs(t, "Installer Test Suite")
14+
}

internal/controllers/provider/install/values.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
corev1 "k8s.io/api/core/v1"
88
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
99

10+
"github.com/openmcp-project/openmcp-operator/api/constants"
1011
"github.com/openmcp-project/openmcp-operator/api/provider/v1alpha1"
1112
)
1213

@@ -35,11 +36,11 @@ func (v *Values) Environment() string {
3536
func determineNamespace(provider *unstructured.Unstructured) string {
3637
var namespacePrefix string
3738
switch provider.GroupVersionKind().Kind {
38-
case "ServiceProvider":
39+
case v1alpha1.ServiceProviderGKV().Kind:
3940
namespacePrefix = "sp"
40-
case "ClusterProvider":
41+
case v1alpha1.ClusterProviderGKV().Kind:
4142
namespacePrefix = "cp"
42-
case "PlatformService":
43+
case v1alpha1.PlatformServiceGKV().Kind:
4344
namespacePrefix = "ps"
4445
default:
4546
namespacePrefix = provider.GroupVersionKind().Kind
@@ -105,7 +106,23 @@ func (v *Values) Verbosity() string {
105106
return v.deploymentSpec.Verbosity
106107
}
107108

108-
func (v *Values) EnvironmentVariables() []corev1.EnvVar {
109+
// EnvironmentVariables returns the environment variables set in the provider resource, enriched by the following:
110+
// - OPENMCP_PROVIDER_NAME: the name of the provider resource,
111+
// - OPENMCP_PROVIDER_NAMESPACE: the namespace in which the provider will be deployed.
112+
func (v *Values) EnvironmentVariables() ([]corev1.EnvVar, error) {
113+
varList := append(
114+
v.providerEnvironmentVariables(),
115+
corev1.EnvVar{Name: constants.EnvVariableProviderName, Value: v.provider.GetName()},
116+
corev1.EnvVar{Name: constants.EnvVariablePlatformClusterNamespace, Value: v.namespace},
117+
)
118+
119+
if err := v.checkUniquenessOfVariableNames(varList); err != nil {
120+
return nil, err
121+
}
122+
return varList, nil
123+
}
124+
125+
func (v *Values) providerEnvironmentVariables() []corev1.EnvVar {
109126
env := make([]corev1.EnvVar, len(v.deploymentSpec.Env))
110127
for i, e := range v.deploymentSpec.Env {
111128
env[i] = corev1.EnvVar{
@@ -115,3 +132,14 @@ func (v *Values) EnvironmentVariables() []corev1.EnvVar {
115132
}
116133
return env
117134
}
135+
136+
func (v *Values) checkUniquenessOfVariableNames(varList []corev1.EnvVar) error {
137+
varMap := make(map[string]bool)
138+
for _, e := range varList {
139+
if varMap[e.Name] {
140+
return fmt.Errorf("environment variable is not unique: %s", e.Name)
141+
}
142+
varMap[e.Name] = true
143+
}
144+
return nil
145+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package install
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
corev1 "k8s.io/api/core/v1"
7+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
8+
9+
"github.com/openmcp-project/openmcp-operator/api/constants"
10+
"github.com/openmcp-project/openmcp-operator/api/provider/v1alpha1"
11+
)
12+
13+
var _ = Describe("Installer", func() {
14+
15+
Context("Env Variables", func() {
16+
17+
const (
18+
providerName = "test-provider"
19+
envName1 = "NAME_1"
20+
envValue1 = "VALUE_1"
21+
)
22+
23+
It("should contain provider variables and predefined openmcp variables", func() {
24+
provider := &unstructured.Unstructured{}
25+
provider.SetGroupVersionKind(v1alpha1.ClusterProviderGKV())
26+
provider.SetName(providerName)
27+
spec := &v1alpha1.DeploymentSpec{
28+
Env: []v1alpha1.EnvVar{
29+
{Name: envName1, Value: envValue1},
30+
},
31+
}
32+
v := NewValues(provider, spec, "test")
33+
env, err := v.EnvironmentVariables()
34+
Expect(err).NotTo(HaveOccurred())
35+
Expect(env).To(ContainElement(corev1.EnvVar{Name: envName1, Value: envValue1}))
36+
Expect(env).To(ContainElement(corev1.EnvVar{Name: constants.EnvVariableProviderName, Value: providerName}))
37+
Expect(env).To(ContainElement(corev1.EnvVar{Name: constants.EnvVariablePlatformClusterNamespace, Value: v.Namespace()}))
38+
})
39+
40+
It("should detect a name conflict", func() {
41+
provider := &unstructured.Unstructured{}
42+
provider.SetGroupVersionKind(v1alpha1.ClusterProviderGKV())
43+
provider.SetName(providerName)
44+
spec := &v1alpha1.DeploymentSpec{
45+
Env: []v1alpha1.EnvVar{
46+
{Name: constants.EnvVariableProviderName, Value: envValue1},
47+
},
48+
}
49+
v := NewValues(provider, spec, "test")
50+
_, err := v.EnvironmentVariables()
51+
Expect(err).To(HaveOccurred())
52+
})
53+
})
54+
})

0 commit comments

Comments
 (0)