Skip to content

Commit

Permalink
feat: support creating files with configmap in container (#479)
Browse files Browse the repository at this point in the history
* feat: support creating files with configmap in container

* fix: fix a lint error
  • Loading branch information
liu-hm19 authored Aug 29, 2023
1 parent 2d79171 commit 272419a
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 44 deletions.
20 changes: 18 additions & 2 deletions pkg/generator/appconfiguration/generator/workload/job_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ func (g *jobGenerator) Generate(spec *models.Spec) error {
spec.Resources = make(models.Resources, 0)
}

uniqueAppName := appconfiguration.UniqueAppName(g.project.Name, g.stack.Name, g.appName)

meta := metav1.ObjectMeta{
Namespace: g.project.Name,
Name: appconfiguration.UniqueAppName(g.project.Name, g.stack.Name, g.appName),
Name: uniqueAppName,
Labels: appconfiguration.MergeMaps(
appconfiguration.UniqueAppLabels(g.project.Name, g.appName),
g.job.Labels,
Expand All @@ -65,10 +67,23 @@ func (g *jobGenerator) Generate(spec *models.Spec) error {
),
}

containers, err := toOrderedContainers(job.Containers)
containers, volumes, configMaps, err := toOrderedContainers(job.Containers, uniqueAppName)
if err != nil {
return err
}

for _, cm := range configMaps {
cm.Namespace = g.project.Name
if err = appconfiguration.AppendToSpec(
models.Kubernetes,
appconfiguration.KubernetesResourceID(cm.TypeMeta, cm.ObjectMeta),
spec,
&cm,
); err != nil {
return err
}
}

jobSpec := batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -82,6 +97,7 @@ func (g *jobGenerator) Generate(spec *models.Spec) error {
},
Spec: corev1.PodSpec{
Containers: containers,
Volumes: volumes,
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,29 @@ func (g *workloadServiceGenerator) Generate(spec *models.Spec) error {
spec.Resources = make(models.Resources, 0)
}

uniqueAppName := appconfiguration.UniqueAppName(g.project.Name, g.stack.Name, g.appName)

// Create a slice of containers based on the app's
// containers.
containers, err := toOrderedContainers(service.Containers)
// containers along with related volumes and configMaps.
containers, volumes, configMaps, err := toOrderedContainers(service.Containers, uniqueAppName)
if err != nil {
return err
}

// Create ConfigMap objects based on the app's configuration.
for _, cm := range configMaps {
cmObj := cm
cmObj.Namespace = g.project.Name
if err = appconfiguration.AppendToSpec(
models.Kubernetes,
appconfiguration.KubernetesResourceID(cmObj.TypeMeta, cmObj.ObjectMeta),
spec,
&cmObj,
); err != nil {
return err
}
}

// Create a K8s workload object based on the app's configuration.
// common parts
objectMeta := metav1.ObjectMeta{
Expand All @@ -93,7 +109,7 @@ func (g *workloadServiceGenerator) Generate(spec *models.Spec) error {
Annotations: appconfiguration.MergeMaps(
g.service.Annotations,
),
Name: appconfiguration.UniqueAppName(g.project.Name, g.stack.Name, g.appName),
Name: uniqueAppName,
Namespace: g.project.Name,
}
podTemplateSpec := v1.PodTemplateSpec{
Expand All @@ -108,6 +124,7 @@ func (g *workloadServiceGenerator) Generate(spec *models.Spec) error {
},
Spec: v1.PodSpec{
Containers: containers,
Volumes: volumes,
},
}
selector := &metav1.LabelSelector{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

func Test_workloadServiceGenerator_Generate(t *testing.T) {
replica := int32(2)
mode := int32(511)
cs := &v1alpha1.CollaSet{
TypeMeta: metav1.TypeMeta{
Kind: "CollaSet",
Expand Down Expand Up @@ -49,6 +50,25 @@ func Test_workloadServiceGenerator_Generate(t *testing.T) {
{
Name: "nginx",
Image: "nginx:v1",
VolumeMounts: []corev1.VolumeMount{
{
Name: "default-dev-foo-nginx-0",
MountPath: "/tmp",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "default-dev-foo-nginx-0",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "default-dev-foo-nginx-0",
},
DefaultMode: &mode,
},
},
},
},
},
Expand All @@ -58,6 +78,22 @@ func Test_workloadServiceGenerator_Generate(t *testing.T) {
unstructured, err := runtime.DefaultUnstructuredConverter.ToUnstructured(cs)
assert.NoError(t, err)

cm := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: corev1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: "default-dev-foo-nginx-0",
Namespace: "default",
},
Data: map[string]string{
"example.txt": "some file contents",
},
}
unstructuredConfigMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(cm)
assert.NoError(t, err)

type fields struct {
project *projectstack.Project
stack *projectstack.Stack
Expand Down Expand Up @@ -96,6 +132,12 @@ func Test_workloadServiceGenerator_Generate(t *testing.T) {
Containers: map[string]container.Container{
"nginx": {
Image: "nginx:v1",
Files: map[string]container.FileSpec{
"/tmp/example.txt": {
Content: "some file contents",
Mode: "0777",
},
},
},
},
Replicas: 2,
Expand All @@ -121,7 +163,8 @@ func Test_workloadServiceGenerator_Generate(t *testing.T) {
if err := g.Generate(tt.args.spec); (err != nil) != tt.wantErr {
t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr)
}
assert.Equal(t, unstructured, tt.args.spec.Resources[0].Attributes)
assert.Equal(t, unstructuredConfigMap, tt.args.spec.Resources[0].Attributes)
assert.Equal(t, unstructured, tt.args.spec.Resources[1].Attributes)
})
}
}
Loading

0 comments on commit 272419a

Please sign in to comment.