Skip to content

[networks]: add decoder/encoder to Attachment and NetworkStatus#1216

Open
saehejkang wants to merge 3 commits intoapple:mainfrom
saehejkang:add-init-decoder-for-attachment-and-network-config
Open

[networks]: add decoder/encoder to Attachment and NetworkStatus#1216
saehejkang wants to merge 3 commits intoapple:mainfrom
saehejkang:add-init-decoder-for-attachment-and-network-config

Conversation

@saehejkang
Copy link
Contributor

@saehejkang saehejkang commented Feb 14, 2026

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Motivation and Context

Adds backward-compatible JSON decoding/encoding fallback for Attachment and NetworkStatus to prevent keyNotFound errors when communicating with older container-apiserver versions.

Temporary fix but closes #1196

Testing

Tests ✅ based on comment here

Co-authored-by: Ed Saipetch <ed@twentybelow.com>
@saehejkang saehejkang force-pushed the add-init-decoder-for-attachment-and-network-config branch from 96a7b48 to 16eb09b Compare February 15, 2026 04:09

network = try container.decode(String.self, forKey: .network)
hostname = try container.decode(String.self, forKey: .hostname)
if let address = try? container.decode(CIDRv4.self, forKey: .ipv4Address) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: would be nice to add some tests for legacy payload decoding.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might warrant an issue (bug or request) to be added on later. I want to get these changes merged soon because they are blocking socktainer users from using the latest version of container.


network = try container.decode(String.self, forKey: .network)
hostname = try container.decode(String.self, forKey: .hostname)
if let address = try? container.decode(CIDRv4.self, forKey: .ipv4Address) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: I think optional try? for fallback approach will swallow decode errors. A better approach would be fallback only on keyNotFound and throw error for malformed payloads. You could use something akin to;

 private static func decodeOrFallback<T: Decodable>(
      _ type: T.Type,
      from container: KeyedDecodingContainer<CodingKeys>,
      primary: CodingKeys,
      fallback: CodingKeys
  ) throws -> T {
      do {
          // return primary
      } catch let DecodingError.keyNotFound(key, _) where key.stringValue ==
  primary.stringValue {
          // return fallback
      } catch {
          // throw error
      }
  }

// callsite will be much cleaner

ipv4Address = try Self.decodeOrFallback(
      CIDRv4.self,
      from: container,
      primary: .ipv4Address,
      fallback: .address
  )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the point about being more precise with error handling, but in this case I think it is a bit over-engineered for what we need right now. I also agree that swallowing all decode errors via try? isn’t ideal, but at the moment we don’t have meaningful error propagation wired up (managed resource work). Would prefer to just keep what we have here.

@saehejkang
Copy link
Contributor Author

saehejkang commented Feb 26, 2026

@jglogan bringing this to your inbox as it a blocker for socktainer and should get merged soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Attachment and NetworkStatus lack backward-compatible decoding (unlike NetworkConfiguration)

3 participants