RSDK-12900: use viam-defaults.json only when cloud config is unavailable (remove merging)#177
Conversation
utils/config.go
Outdated
| cacheBytes, err := os.ReadFile(cachePath) | ||
| if err != nil { | ||
| if errors.Is(err, fs.ErrNotExist) { | ||
| return StackConfigs(&pb.DeviceAgentConfigResponse{}) | ||
| return StackConfigs(nil) |
There was a problem hiding this comment.
LoadConfigFromCache is only called once at startup. It will apply viam-defaults.json if the the cache is unavailable, however, GetConfig will soon run and replace it with the cloud config (if possible) before the subsystems start up and begin running (after #170)
2077e51 to
21665dd
Compare
| errOut = errors.Join(errOut, err) | ||
| } else { | ||
| jsonBytes, err = json.Marshal(cloudCfg) | ||
| if !cloudCfgSuccess { |
There was a problem hiding this comment.
how soon will this trip if the connection is flaky? like one config fetch fails but the next succeeds? will we end up with the config constantly switching between two states?
There was a problem hiding this comment.
I think in practice it's unlikely to be an issue, and the risk of config flip flop is largely unchanged from what we have now:
Lines 692 to 710 in 5fa9720
If the DeviceAgentConfig grpc call fails with an error, this code doesn't run at all (we just continue with the existing config).
If the call succeeds but returns corrupted data, we run it through a few marshal/unmarshal round trips (ProtoToConfig) and only if everything is still good up to there, do we unmarshal to the final cfg. The one difference is currently we keep whatever the final unmarshal gives us, but in this PR we only keep it if it succeeds without error.
Lines 337 to 345 in 5fa9720
I'll see if any more ideas to make this more resilient come to mind, but I generally don't think this is a major cause for concern.
There was a problem hiding this comment.
will we fallback to viam-defaults if provisioning turns on? e.g. you move a previously connected pi to a new location.
we should do that because otherwise the hotspot name/password will be unset
There was a problem hiding this comment.
Good catch, no. IIUC the way it works now is when you first get online, it applies the cloud config over viam-defaults.json, so the resulting config still contains the hotspot settings (via viam-defaults), and it gets saved to disk. So next startup, if it needs to enter provisioning, it will read the cached config which still has the hotspot info.
In this case of moving a robot to a new location, do we still prefer using the cached config if it's available, or just revert to using viam-defaults.json only? I'd think the former because it's has more info & is closer to the user's desired state, but then we may have to go back to merging and selectively choose the fields to include (perhaps just the provisioning ones?).
There was a problem hiding this comment.
Lmk what you think about a91fb64. I think added complexity is unavoidable whether we go with this method or restore the merging in StackConfigs but selectively choose which fields to include.
There was a problem hiding this comment.
I think it's a good start - thinking about it some more, maybe a better course is not to stack the cloud/viam-defaults together, but use either the cloud or viam-defaults depending on whether the cloud config differs from the default config (so if there are no differences, we can safely assume the user doesn't care about the networking configuration, but if there are, we don't only use the cloud config and not stack them since there could be weird interactions).
Then we can log which config we're using and it should be more clear when we are using which config
There was a problem hiding this comment.
but use either the cloud or viam-defaults depending on whether the cloud config differs from the default config
I'm not sure this would help for the case we're trying to solve: if the hotspot settings are only in viam-defaults, but the cloud cfg has one additional option, e.g. wifi_power_save: false, this would opt to only use the cloud config and miss the hotspot settings. If the cloud config is either completely empty or perfectly matches viam-defaults, using only the latter is no different from the merge in a91fb64.
There was a problem hiding this comment.
ya, you're right, let's just keep it the current impl (merge cloud + defaults for provisioning)
There was a problem hiding this comment.
731572c changed to only log & apply when there are diffs (functionally same as before) + comments for clarity.
0577624 to
0ce998c
Compare
0ce998c to
5e59e35
Compare
05eaf0c to
a91fb64
Compare
| errOut = errors.Join(errOut, err) | ||
| } else { | ||
| jsonBytes, err = json.Marshal(cloudCfg) | ||
| if !cloudCfgSuccess { |
There was a problem hiding this comment.
I think it's a good start - thinking about it some more, maybe a better course is not to stack the cloud/viam-defaults together, but use either the cloud or viam-defaults depending on whether the cloud config differs from the default config (so if there are no differences, we can safely assume the user doesn't care about the networking configuration, but if there are, we don't only use the cloud config and not stack them since there could be weird interactions).
Then we can log which config we're using and it should be more clear when we are using which config
utils/config.go
Outdated
| err = json.Unmarshal(cacheBytes, &cfg) | ||
| if err != nil { | ||
| cfg, newErr := StackConfigs(&pb.DeviceAgentConfigResponse{}) | ||
| cfg, newErr := StackConfigs(nil) |
There was a problem hiding this comment.
seems like we use StackConfigs(nil) enough where we can make a specific function to only get the viam-defaults + default config
There was a problem hiding this comment.
724754d I refactored StackConfigs into StackProtoConfig(proto) and StackOfflineConfig() and the viam-provisioning.json & viam-defaults.json json into separate smaller functions.
Co-authored-by: Cheuk <90270663+cheukt@users.noreply.github.com>
dfaefa5 to
1f0cc86
Compare
1f0cc86 to
1cd9383
Compare
1cd9383 to
731572c
Compare
We currently apply the cloud config over
viam-defaults.json. Change to useviam-defaults.jsononly when the cloud config is either unavailable or broken.