Skip to content

Commit

Permalink
refactor: integrate zerolog for enhanced logging and update group dat…
Browse files Browse the repository at this point in the history
…a handling in MemStore
  • Loading branch information
alexlovelltroy committed Nov 27, 2024
1 parent 8778406 commit 8038cd9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 19 deletions.
48 changes: 31 additions & 17 deletions internal/memstore/ciMemStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 14 additions & 2 deletions internal/memstore/ciMemStore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions io.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"user-data": {
"write_files": [
{
"content": "Hello io Nodes",
"path": "/etc/hello"
}
]
}
}
10 changes: 10 additions & 0 deletions rsyslog.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}

0 comments on commit 8038cd9

Please sign in to comment.