From a595f4ca2df6cc812c8d7e021bcfb6e2502eb10b Mon Sep 17 00:00:00 2001 From: Claudio Netto Date: Mon, 25 Jul 2022 16:13:03 -0300 Subject: [PATCH] feat(web): return extra files on /info endpoint --- internal/pkg/rpaas/k8s.go | 20 ++++++++++++++++++++ internal/pkg/rpaas/k8s_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/internal/pkg/rpaas/k8s.go b/internal/pkg/rpaas/k8s.go index 23e948a14..b01390b96 100644 --- a/internal/pkg/rpaas/k8s.go +++ b/internal/pkg/rpaas/k8s.go @@ -850,6 +850,7 @@ func (m *k8sRpaasManager) GetExtraFiles(ctx context.Context, instanceName string if err != nil { return nil, err } + extraFiles, err := m.getExtraFiles(ctx, *instance) if err != nil && IsNotFoundError(err) { return []File{}, nil @@ -857,6 +858,7 @@ func (m *k8sRpaasManager) GetExtraFiles(ctx context.Context, instanceName string if err != nil { return nil, err } + files := []File{} for key, path := range instance.Spec.ExtraFiles.Files { files = append(files, File{ @@ -864,6 +866,11 @@ func (m *k8sRpaasManager) GetExtraFiles(ctx context.Context, instanceName string Content: extraFiles.BinaryData[key], }) } + + sort.Slice(files, func(i, j int) bool { + return files[i].Name < files[j].Name + }) + return files, nil } @@ -1184,6 +1191,7 @@ func (m *k8sRpaasManager) getExtraFiles(ctx context.Context, instance v1alpha1.R if instance.Spec.ExtraFiles == nil { return nil, &NotFoundError{Msg: "there are no extra files"} } + configMapName := types.NamespacedName{ Name: instance.Spec.ExtraFiles.Name, Namespace: instance.Namespace, @@ -1672,6 +1680,18 @@ func (m *k8sRpaasManager) GetInstanceInfo(ctx context.Context, instanceName stri } } + extraFiles, err := m.GetExtraFiles(ctx, instanceName) + if err != nil { + return nil, err + } + + for _, f := range extraFiles { + info.ExtraFiles = append(info.ExtraFiles, clientTypes.RpaasFile{ + Name: f.Name, + Content: f.Content, + }) + } + routes, err := m.GetRoutes(ctx, instanceName) if err != nil { return nil, err diff --git a/internal/pkg/rpaas/k8s_test.go b/internal/pkg/rpaas/k8s_test.go index 8ee734115..e7d2968ac 100644 --- a/internal/pkg/rpaas/k8s_test.go +++ b/internal/pkg/rpaas/k8s_test.go @@ -4726,6 +4726,38 @@ func Test_k8sRpaasManager_GetInstanceInfo(t *testing.T) { return info }, }, + + "w/ extra files": { + resources: []runtime.Object{ + &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-instance-extra-files", + Namespace: "rpaasv2", + }, + BinaryData: map[string][]byte{ + "waf.cfg": []byte("My WAF rules :P"), + "binary.exe": {66, 55, 10, 0}, + }, + }, + }, + instance: func(i v1alpha1.RpaasInstance) v1alpha1.RpaasInstance { + i.Spec.ExtraFiles = &nginxv1alpha1.FilesRef{ + Name: "my-instance-extra-files", + Files: map[string]string{ + "waf.cfg": "waf.cfg", + "binary.exe": "binary.exe", + }, + } + return i + }, + expected: func(info clientTypes.InstanceInfo) clientTypes.InstanceInfo { + info.ExtraFiles = []clientTypes.RpaasFile{ + {Name: "binary.exe", Content: []byte{66, 55, 10, 0}}, + {Name: "waf.cfg", Content: []byte("My WAF rules :P")}, + } + return info + }, + }, } for name, tt := range tests {