mirror of
https://github.com/argoproj/argo-cd
synced 2026-04-21 17:07:16 +00:00
chore: cr comments
Signed-off-by: Blake Pettersson <blake.pettersson@gmail.com>
This commit is contained in:
parent
9b20e04440
commit
80fe3c596a
2 changed files with 32 additions and 37 deletions
|
|
@ -175,20 +175,12 @@ func streamApplicationListJSON(w io.Writer, appList *v1alpha1.ApplicationList, f
|
|||
// Respects omitempty: fields are only written when non-empty.
|
||||
func writeInlineTypeMeta(w io.Writer, tm *metav1.TypeMeta) error {
|
||||
if tm.Kind != "" {
|
||||
data, err := json.Marshal(tm.Kind)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := fmt.Fprintf(w, `"kind":%s,`, data); err != nil {
|
||||
if _, err := fmt.Fprintf(w, `"kind":"%s",`, tm.Kind); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if tm.APIVersion != "" {
|
||||
data, err := json.Marshal(tm.APIVersion)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := fmt.Fprintf(w, `"apiVersion":%s,`, data); err != nil {
|
||||
if _, err := fmt.Fprintf(w, `"apiVersion":"%s",`, tm.APIVersion); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
@ -212,6 +204,24 @@ func parseFieldSelection(req *gohttp.Request) (fields map[string]any, exclude bo
|
|||
return fields, exclude
|
||||
}
|
||||
|
||||
func processApplicationListField(v any, fields map[string]any, exclude bool) (any, error) {
|
||||
if appList, ok := v.(*v1alpha1.ApplicationList); ok {
|
||||
var items []map[string]any
|
||||
for i := range appList.Items {
|
||||
converted, err := processAppFields(&appList.Items[i], fields, exclude)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, converted)
|
||||
}
|
||||
return map[string]any{
|
||||
"items": items,
|
||||
"metadata": appList.ListMeta,
|
||||
}, nil
|
||||
}
|
||||
return nil, errors.New("not an application list")
|
||||
}
|
||||
|
||||
func init() {
|
||||
logsForwarder := func(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w gohttp.ResponseWriter, req *gohttp.Request, recv func() (proto.Message, error), opts ...func(context.Context, gohttp.ResponseWriter, proto.Message) error) {
|
||||
if req.URL.Query().Get("download") == "true" {
|
||||
|
|
@ -260,6 +270,12 @@ func init() {
|
|||
return
|
||||
}
|
||||
|
||||
if req.Header.Get("Accept") == "text/event-stream" {
|
||||
// Use old non-streaming processor
|
||||
http.UnaryForwarderWithFieldProcessor(processApplicationListField)(ctx, mux, marshaler, w, req, resp, opts...)
|
||||
return
|
||||
}
|
||||
|
||||
// Replicate grpc-gateway ForwardResponseMessage header handling.
|
||||
md, ok := runtime.ServerMetadataFromContext(ctx)
|
||||
if ok {
|
||||
|
|
|
|||
|
|
@ -25,25 +25,6 @@ import (
|
|||
"github.com/argoproj/argo-cd/v3/test"
|
||||
)
|
||||
|
||||
func processApplicationListField(t *testing.T, v any, fields map[string]any, exclude bool) (any, error) {
|
||||
t.Helper()
|
||||
if appList, ok := v.(*v1alpha1.ApplicationList); ok {
|
||||
var items []map[string]any
|
||||
for i := range appList.Items {
|
||||
converted, err := processAppFields(&appList.Items[i], fields, exclude)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, converted)
|
||||
}
|
||||
return map[string]any{
|
||||
"items": items,
|
||||
"metadata": appList.ListMeta,
|
||||
}, nil
|
||||
}
|
||||
return nil, errors.New("not an application list")
|
||||
}
|
||||
|
||||
func TestProcessApplicationListField_SyncOperation(t *testing.T) {
|
||||
list := v1alpha1.ApplicationList{
|
||||
Items: []v1alpha1.Application{{Operation: &v1alpha1.Operation{Sync: &v1alpha1.SyncOperation{
|
||||
|
|
@ -51,7 +32,7 @@ func TestProcessApplicationListField_SyncOperation(t *testing.T) {
|
|||
}}}},
|
||||
}
|
||||
|
||||
res, err := processApplicationListField(t, &list, map[string]any{"items.operation.sync": true}, false)
|
||||
res, err := processApplicationListField(&list, map[string]any{"items.operation.sync": true}, false)
|
||||
require.NoError(t, err)
|
||||
resMap, ok := res.(map[string]any)
|
||||
require.True(t, ok)
|
||||
|
|
@ -72,7 +53,7 @@ func TestProcessApplicationListField_SyncOperationMissing(t *testing.T) {
|
|||
Items: []v1alpha1.Application{{Operation: nil}},
|
||||
}
|
||||
|
||||
res, err := processApplicationListField(t, &list, map[string]any{"items.operation.sync": true}, false)
|
||||
res, err := processApplicationListField(&list, map[string]any{"items.operation.sync": true}, false)
|
||||
require.NoError(t, err)
|
||||
resMap, ok := res.(map[string]any)
|
||||
require.True(t, ok)
|
||||
|
|
@ -243,7 +224,7 @@ func TestStreamApplicationListJSON_MatchesProcessApplicationListField(t *testing
|
|||
}
|
||||
|
||||
// Get result from the batch processApplicationListField
|
||||
batchResult, err := processApplicationListField(t, list, fields, false)
|
||||
batchResult, err := processApplicationListField(list, fields, false)
|
||||
require.NoError(t, err)
|
||||
batchJSON, err := json.Marshal(batchResult)
|
||||
require.NoError(t, err)
|
||||
|
|
@ -375,7 +356,7 @@ func TestStreamApplicationListJSON_MatchesFieldFilter_AllFields(t *testing.T) {
|
|||
}
|
||||
|
||||
// Batch (old path)
|
||||
batchResult, err := processApplicationListField(t, list, fields, false)
|
||||
batchResult, err := processApplicationListField(list, fields, false)
|
||||
require.NoError(t, err)
|
||||
batchJSON, err := json.Marshal(batchResult)
|
||||
require.NoError(t, err)
|
||||
|
|
@ -410,7 +391,7 @@ func TestStreamApplicationListJSON_MatchesFieldFilter_Exclude(t *testing.T) {
|
|||
// Exclude spec
|
||||
fields := map[string]any{"items.spec": true}
|
||||
|
||||
batchResult, err := processApplicationListField(t, list, fields, true)
|
||||
batchResult, err := processApplicationListField(list, fields, true)
|
||||
require.NoError(t, err)
|
||||
batchJSON, err := json.Marshal(batchResult)
|
||||
require.NoError(t, err)
|
||||
|
|
@ -458,9 +439,7 @@ func TestForwarder_HeadersMatchForwardResponseMessage(t *testing.T) {
|
|||
}
|
||||
|
||||
// --- Old path: UnaryForwarderWithFieldProcessor via ForwardResponseMessage ---
|
||||
oldForwarder := argohttp.UnaryForwarderWithFieldProcessor(func(val any, fields map[string]any, exclude bool) (any, error) {
|
||||
return processApplicationListField(t, val, fields, exclude)
|
||||
})
|
||||
oldForwarder := argohttp.UnaryForwarderWithFieldProcessor(processApplicationListField)
|
||||
oldRec := httptest.NewRecorder()
|
||||
oldReq := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/v1/applications?fields=items.metadata.name", http.NoBody)
|
||||
oldForwarder(ctx, mux, nil, oldRec, oldReq, list, testOpt)
|
||||
|
|
|
|||
Loading…
Reference in a new issue