From 8038cd93710c6c0fbb50f2593ee61345a8e603d3 Mon Sep 17 00:00:00 2001 From: Alex Lovell-Troy Date: Wed, 27 Nov 2024 12:29:29 -0500 Subject: [PATCH] refactor: integrate zerolog for enhanced logging and update group data handling in MemStore --- internal/memstore/ciMemStore.go | 48 ++++++++++++++++++---------- internal/memstore/ciMemStore_test.go | 16 ++++++++-- io.json | 10 ++++++ rsyslog.json | 10 ++++++ 4 files changed, 65 insertions(+), 19 deletions(-) create mode 100644 io.json create mode 100644 rsyslog.json diff --git a/internal/memstore/ciMemStore.go b/internal/memstore/ciMemStore.go index 3ef447a..853e36c 100644 --- a/internal/memstore/ciMemStore.go +++ b/internal/memstore/ciMemStore.go @@ -3,7 +3,8 @@ package memstore import ( "errors" "fmt" - "log" + + "github.com/rs/zerolog/log" "github.com/OpenCHAMI/cloud-init/pkg/citypes" "github.com/samber/lo" @@ -88,42 +89,55 @@ func (m *MemStore) Get(id string, groupLabels []string) (citypes.CI, error) { }, } + log.Info().Msgf("groups: %v", groupLabels) + // add matching group data stored with groups API to metadata for _, groupLabel := range groupLabels { + log.Debug().Msgf("groupLabel: %s", groupLabel) // check if the group is stored locally with the label obtained from SMD groupData, ok := m.groups[groupLabel] if !ok { // we didn't find the group in the memstore with the label, so // go on to the next one - log.Printf("failed to get '%s' from groups", groupLabel) + log.Debug().Msgf("failed to get '%s' from groups", groupLabel) } else { // found the group, so add it to the metadata + log.Debug().Msgf("found group '%s' in groups", groupLabel) + log.Debug().Msgf("groupData.Name: %v", groupData.Name) + log.Debug().Msgf("groupData.Data: %v", groupData.Data) + log.Debug().Msgf("groupData.Actions: %v", groupData.Actions) groups := ci.CIData.MetaData["groups"].(map[string][]citypes.MetaDataKV) groups[groupLabel] = groupData.Data + log.Debug().Msgf("Adding groups to MetaData: %v", groups) ci.CIData.MetaData["groups"] = groups } - // In user data, we cannot store things as groups. We store the write_files and runcmd lists directly. - // check if we already have a "write_files" section in user data - if writeFiles, ok := ci.CIData.UserData["write_files"].([]citypes.WriteFiles); ok { - // found the "write_files" section, so add the new group's write_files - if groupData.Actions != nil { - for _, wf := range groupData.Actions["write_files"].([]citypes.WriteFiles) { - wf.Group = groupLabel - writeFiles = append(writeFiles, wf) - } + // In user data, we cannot store things as groups. We store the write_files lists directly. + // First establish that the write_files section exists in the groupData from our storage + if _, ok := groupData.Actions["write_files"]; ok { + log.Debug().Msgf("write_files found in group '%s'", groupLabel) + log.Debug().Msgf("groupData.Actions[\"write_files\"]: %v", groupData.Actions["write_files"]) - ci.CIData.UserData["write_files"] = writeFiles + // Then ensure that the write_files section exists in the user data + if _, ok := ci.CIData.UserData["write_files"].([]citypes.WriteFiles); !ok { + // write_files does not exist, so add it with current group's write_files + ci.CIData.UserData["write_files"] = []citypes.WriteFiles{} } - } else { - // did not find "write_files", so add it with current group's write_files - if groupData.Actions != nil { - ci.CIData.UserData["write_files"] = groupData.Actions["write_files"] + // Now iterate through the entries and add them to the UserData + for _, wf := range groupData.Actions["write_files"].([]interface{}) { + writeFilesEntry := citypes.WriteFiles{ + Path: wf.(map[string]interface{})["path"].(string), + Content: wf.(map[string]interface{})["content"].(string), + Group: groupLabel, + } + log.Debug().Msgf("writeFilesEntry: %v", writeFilesEntry) + writeFilesEntry.Group = groupLabel + ci.CIData.UserData["write_files"] = append(ci.CIData.UserData["write_files"].([]citypes.WriteFiles), writeFilesEntry) } } - } + } fmt.Printf("ci: %v\n", ci) return ci, nil } diff --git a/internal/memstore/ciMemStore_test.go b/internal/memstore/ciMemStore_test.go index a61ba5d..a8c24c8 100644 --- a/internal/memstore/ciMemStore_test.go +++ b/internal/memstore/ciMemStore_test.go @@ -46,18 +46,30 @@ func TestMemStore_Get(t *testing.T) { {"os_version": "rocky9"}, {"cluster_name": "hill"}, {"admin": "groves"}, - }} + }, + Actions: map[string]any{ + "write_files": []citypes.WriteFiles{ + {Content: "OK COMPUTER", Path: "/etc/hello_computes"}, + }}, + } store.groups["row1"] = citypes.GroupData{ Data: []citypes.MetaDataKV{ {"rack": "rack1"}, {"syslog_aggregator": "syslog1"}, - }} + }, + Actions: map[string]any{ + "write_files": []citypes.WriteFiles{ + {Content: "OK COMPUTER", Path: "/etc/hello_row"}, + }}, + } ci, err := store.Get("test-id", []string{"computes", "row1"}) assert.NoError(t, err) assert.Equal(t, "test-id", ci.Name) assert.NotNil(t, ci.CIData.MetaData) assert.Contains(t, ci.CIData.MetaData["groups"].(map[string][]citypes.MetaDataKV), "row1") assert.Contains(t, ci.CIData.MetaData["groups"].(map[string][]citypes.MetaDataKV), "computes") + assert.Contains(t, ci.CIData.UserData, "write_files") + assert.Contains(t, ci.CIData.UserData["write_files"].([]citypes.WriteFiles), citypes.WriteFiles{Content: "OK COMPUTER", Path: "/etc/hello_computes"}) ciJSON, err := json.Marshal(ci) if err != nil { t.Logf("Cloud-init payload: %+v", ci) diff --git a/io.json b/io.json new file mode 100644 index 0000000..7703ea9 --- /dev/null +++ b/io.json @@ -0,0 +1,10 @@ +{ + "user-data": { + "write_files": [ + { + "content": "Hello io Nodes", + "path": "/etc/hello" + } + ] + } +} diff --git a/rsyslog.json b/rsyslog.json new file mode 100644 index 0000000..af50d89 --- /dev/null +++ b/rsyslog.json @@ -0,0 +1,10 @@ +{ + "user-data": { + "write_files": [ + { + "content": "{%- set ns = namespace(group=\"\", priority=99) -%}\n{%- for group,group_data in groups.items() -%}\n\t{%- if group_data.data.rsyslog_aggregator is defined -%}\n\t\t{%- set priority = group_data.data.priority | int -%}\n\t\t{%- if ( priority < ns.priority ) -%}\n\t\t\t{%- set ns.group = group -%}\n\t\t\t{%- set ns.priority = priority -%}\n\t\t{%- endif -%}\n\t{%- endif -%}\n{%- endfor -%}\n# Remote Logging\naction(type=\"omfwd\" target=\"{{ groups[ns.group].data.rsyslog_aggregator }}\" port=\"514\" protocol=\"tcp\")\n", + "path": "/etc/rsyslog.conf.test" + } + ] + } +}