Skip to content

Commit

Permalink
feat(web): add instance and service labels in ConfigMap from files
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Aug 8, 2022
1 parent c0127cf commit a3e8c3e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
10 changes: 5 additions & 5 deletions internal/pkg/rpaas/extra_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (m *k8sRpaasManager) getConfigMapByFileName(ctx context.Context, i *v1alpha
var cms corev1.ConfigMapList
if err := m.cli.List(ctx, &cms, &client.ListOptions{
Namespace: i.Namespace,
LabelSelector: labels.SelectorFromSet(labels.Set(labelsSelectorForFile(filename))),
LabelSelector: labels.SelectorFromSet(labels.Set(labelsSelectorForFile(i.Name, filename))),
}); err != nil {
return nil, err
}
Expand Down Expand Up @@ -215,7 +215,7 @@ func newConfigMapForFile(i *v1alpha1.RpaasInstance, f File) *corev1.ConfigMap {
ObjectMeta: metav1.ObjectMeta{
GenerateName: fmt.Sprintf("%s-extra-files-", i.Name),
Namespace: i.Namespace,
Labels: labelsSelectorForFile(f.Name),
Labels: labelsSelectorForFile(i.Name, f.Name),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(i, schema.GroupVersionKind{
Group: v1alpha1.GroupVersion.Group,
Expand All @@ -228,11 +228,11 @@ func newConfigMapForFile(i *v1alpha1.RpaasInstance, f File) *corev1.ConfigMap {
}
}

func labelsSelectorForFile(filename string) map[string]string {
return map[string]string{
func labelsSelectorForFile(instance, filename string) map[string]string {
return mergeMap(labelsForRpaasInstance(instance), map[string]string{
fmt.Sprintf("%s/is-file", defaultKeyLabelPrefix): "true",
fmt.Sprintf("%s/file-name", defaultKeyLabelPrefix): filename,
}
})
}

func validateFiles(fs []File) error {
Expand Down
56 changes: 42 additions & 14 deletions internal/pkg/rpaas/extra_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ func Test_k8sRpaasManager_GetExtraFiles(t *testing.T) {
Name: "my-instance-extra-file-123456",
Namespace: "rpaasv2",
Labels: map[string]string{
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "index.html",
"rpaas.extensions.tsuru.io/instance-name": "my-instance",
"rpaas.extensions.tsuru.io/service-name": "rpaasv2",
"rpaas_instance": "my-instance",
"rpaas_service": "rpaasv2",
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "index.html",
},
},
BinaryData: map[string][]byte{"index.html": []byte(`<h1>Hello world!</h1>`)},
Expand All @@ -57,8 +61,12 @@ func Test_k8sRpaasManager_GetExtraFiles(t *testing.T) {
Name: "my-instance-extra-file-abcde",
Namespace: "rpaasv2",
Labels: map[string]string{
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "binary.exe",
"rpaas.extensions.tsuru.io/instance-name": "my-instance",
"rpaas.extensions.tsuru.io/service-name": "rpaasv2",
"rpaas_instance": "my-instance",
"rpaas_service": "rpaasv2",
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "binary.exe",
},
},
BinaryData: map[string][]byte{"binary.exe": {66, 55, 10, 00, 20}},
Expand Down Expand Up @@ -131,8 +139,12 @@ func Test_k8sRpaasManager_DeleteExtraFiles(t *testing.T) {
Name: "my-instance-extra-files-1",
Namespace: "rpaasv2",
Labels: map[string]string{
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "index.html",
"rpaas.extensions.tsuru.io/instance-name": "my-instance",
"rpaas.extensions.tsuru.io/service-name": "rpaasv2",
"rpaas_instance": "my-instance",
"rpaas_service": "rpaasv2",
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "index.html",
},
},
BinaryData: map[string][]byte{"index.html": []byte("<h1>Hello world!</h1>")},
Expand All @@ -142,8 +154,12 @@ func Test_k8sRpaasManager_DeleteExtraFiles(t *testing.T) {
Name: "my-instance-extra-files-2",
Namespace: "rpaasv2",
Labels: map[string]string{
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "binary.exe",
"rpaas.extensions.tsuru.io/instance-name": "my-instance",
"rpaas.extensions.tsuru.io/service-name": "rpaasv2",
"rpaas_instance": "my-instance",
"rpaas_service": "rpaasv2",
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "binary.exe",
},
},
BinaryData: map[string][]byte{"binary.exe": {66, 55, 00, 10}},
Expand Down Expand Up @@ -222,8 +238,12 @@ func Test_k8sRpaasManager_CreateExtraFiles(t *testing.T) {

assert.Equal(t, "my-instance-extra-files-", cm.GenerateName)
assert.Equal(t, map[string]string{
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "index.html",
"rpaas.extensions.tsuru.io/instance-name": "my-instance",
"rpaas.extensions.tsuru.io/service-name": "rpaasv2",
"rpaas_instance": "my-instance",
"rpaas_service": "rpaasv2",
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "index.html",
}, cm.Labels)
assert.Equal(t, map[string][]byte{"index.html": []byte("<h1>Hello world!</h1>")}, cm.BinaryData)

Expand Down Expand Up @@ -291,8 +311,12 @@ func Test_k8sRpaasManager_UpdateExtraFiles(t *testing.T) {
Name: "my-instance-extra-files-abcde",
Namespace: "rpaasv2",
Labels: map[string]string{
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "index.html",
"rpaas.extensions.tsuru.io/instance-name": "my-instance",
"rpaas.extensions.tsuru.io/service-name": "rpaasv2",
"rpaas_instance": "my-instance",
"rpaas_service": "rpaasv2",
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "index.html",
},
},
BinaryData: map[string][]byte{"index.html": []byte("<h1>Hello world!</h1>")},
Expand All @@ -319,8 +343,12 @@ func Test_k8sRpaasManager_UpdateExtraFiles(t *testing.T) {
Name: "my-instance-extra-files-abcde",
Namespace: "rpaasv2",
Labels: map[string]string{
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "index.html",
"rpaas.extensions.tsuru.io/instance-name": "my-instance",
"rpaas.extensions.tsuru.io/service-name": "rpaasv2",
"rpaas_instance": "my-instance",
"rpaas_service": "rpaasv2",
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "index.html",
},
},
BinaryData: map[string][]byte{"index.html": []byte("<h1>Hello world!</h1>")},
Expand Down
16 changes: 12 additions & 4 deletions internal/pkg/rpaas/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4493,8 +4493,12 @@ func Test_k8sRpaasManager_GetInstanceInfo(t *testing.T) {
Name: "my-instance-extra-files-1",
Namespace: "rpaasv2",
Labels: map[string]string{
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "waf.cfg",
"rpaas.extensions.tsuru.io/instance-name": "my-instance",
"rpaas.extensions.tsuru.io/service-name": "rpaasv2",
"rpaas_instance": "my-instance",
"rpaas_service": "rpaasv2",
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "waf.cfg",
},
},
BinaryData: map[string][]byte{
Expand All @@ -4506,8 +4510,12 @@ func Test_k8sRpaasManager_GetInstanceInfo(t *testing.T) {
Name: "my-instance-extra-files-2",
Namespace: "rpaasv2",
Labels: map[string]string{
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "binary.exe",
"rpaas.extensions.tsuru.io/instance-name": "my-instance",
"rpaas.extensions.tsuru.io/service-name": "rpaasv2",
"rpaas_instance": "my-instance",
"rpaas_service": "rpaasv2",
"rpaas.extensions.tsuru.io/is-file": "true",
"rpaas.extensions.tsuru.io/file-name": "binary.exe",
},
},
BinaryData: map[string][]byte{
Expand Down

0 comments on commit a3e8c3e

Please sign in to comment.