Skip to content

feat: implement networkpolicy primitive#31

Open
sourcehawk wants to merge 24 commits intomainfrom
feature/networkpolicy-primitive
Open

feat: implement networkpolicy primitive#31
sourcehawk wants to merge 24 commits intomainfrom
feature/networkpolicy-primitive

Conversation

@sourcehawk
Copy link
Owner

@sourcehawk sourcehawk commented Mar 22, 2026

Implements the networkpolicy Kubernetes resource primitive following the pattern established by the existing ConfigMap and Deployment primitives.

Summary

  • Adds networkpolicy primitive package under pkg/primitives/networkpolicy/
  • Implements required lifecycle interfaces
  • Includes editors, mutator, flavors, and builder
  • Adds NetworkPolicySpecEditor in shared pkg/mutation/editors/ package
  • Updates shared docs/primitives.md with networkpolicy entry and adds docs/primitives/networkpolicy.md

Checklist

  • Compiles cleanly
  • Tests pass
  • Follows naming conventions in CONTEXT.md
  • Cross-cutting changes documented (shared editor, shared docs)

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new networkpolicy primitive to the operator-component-framework, providing a static resource wrapper + builder/mutator pipeline (with typed spec editor support), along with documentation and a runnable example demonstrating feature-gated composition of NetworkPolicy rules.

Changes:

  • Introduces pkg/primitives/networkpolicy with Resource, Builder, Mutator, and field-application flavors.
  • Adds a shared NetworkPolicySpecEditor under pkg/mutation/editors with unit tests.
  • Adds documentation (docs/primitives/networkpolicy.md) and a complete example (examples/networkpolicy-primitive/).

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pkg/primitives/networkpolicy/resource.go Static resource wrapper + default field applicator for NetworkPolicy.
pkg/primitives/networkpolicy/builder.go Fluent builder for configuring mutations, flavors, and extractors.
pkg/primitives/networkpolicy/mutator.go Plan-and-apply mutator with feature boundaries + typed edit hooks.
pkg/primitives/networkpolicy/flavors.go NetworkPolicy-specific wrappers around generic “preserve current” flavors.
pkg/primitives/networkpolicy/builder_test.go Builder validation and option-wiring tests.
pkg/primitives/networkpolicy/mutator_test.go Mutator behavior/order/error-propagation tests.
pkg/primitives/networkpolicy/flavors_test.go Flavor behavior + integration tests via Resource.Mutate.
pkg/mutation/editors/networkpolicyspec.go New shared typed editor for NetworkPolicySpec.
pkg/mutation/editors/networkpolicyspec_test.go Unit tests for NetworkPolicySpecEditor.
docs/primitives/networkpolicy.md New primitive documentation (builder usage, mutations, ordering, flavors).
examples/networkpolicy-primitive/app/controller.go Example controller wiring the primitive into a component.
examples/networkpolicy-primitive/features/mutations.go Example feature mutations for ingress/egress + version label.
examples/networkpolicy-primitive/resources/networkpolicy.go Example resource factory assembling the primitive configuration.
examples/networkpolicy-primitive/main.go Runnable demo using a fake client and multiple reconciliation passes.
examples/networkpolicy-primitive/README.md Example overview and run instructions.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated 3 comments.

@sourcehawk
Copy link
Owner Author

Claude Review Cycle 1 Complete

Addressed:

  • Fixed docs/primitives/networkpolicy.md line 44: Removed incorrect claim that DefaultFieldApplicator preserves ResourceVersion. Aligned wording with the configmap primitive docs and actual code behavior.

Intentionally ignored:

  • resource.go line 15 (preserve server-managed metadata in DefaultFieldApplicator): All primitives in this codebase (configmap, deployment, networkpolicy) use the identical *current = *desired.DeepCopy() pattern without preserving ResourceVersion/UID/etc. This is a deliberate codebase convention — server-managed metadata is handled at the framework/controller-runtime layer, not in the field applicator. Changing only networkpolicy would break consistency.
  • docs/primitives/networkpolicy.md line 13 (double pipes in markdown table): Inspected the table — it uses single | separators throughout. No double pipes exist; this comment appears to be a false positive.

Copilot AI review requested due to automatic review settings March 22, 2026 19:52
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 4 comments.

Copilot AI review requested due to automatic review settings March 22, 2026 21:59
@sourcehawk
Copy link
Owner Author

approved

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 4 comments.

Copilot AI review requested due to automatic review settings March 23, 2026 03:00
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 2 comments.

@sourcehawk
Copy link
Owner Author

Claude Review Cycle 1 Complete

Addressed:

  • resource.go:29 — Changed "workload or integration primitive" to "workload or task primitive" to align with configmap primitive wording.
  • networkpolicyspec.go:78 — Replaced "EgressRules" with ".Egress field (egress rules)" and prefixed type with networkingv1.PolicyTypeEgress for clarity.

Intentionally ignored:
None

<!-- claude-review-cycle -->

Copilot AI review requested due to automatic review settings March 23, 2026 16:08
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.

Copilot AI review requested due to automatic review settings March 23, 2026 16:58
@sourcehawk
Copy link
Owner Author

Claude Review Cycle 1 Complete

Addressed:

  • Mutator GoDoc (mutator.go:25): Added feature boundary documentation and editors.ObjectMutator interface mention, aligned with configmap/deployment mutator conventions.
  • Primitives index (docs/primitives.md): Added networkpolicy to the Built-in Primitives table and NetworkPolicySpecEditor to the Mutation Editors table for discoverability.

Intentionally ignored:

  • Markdown table formatting (networkpolicy.md:13): The table already uses single leading pipes and renders correctly. The suggested change is cosmetic whitespace only with no rendering difference.

<!-- claude-review-cycle -->

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.

sourcehawk and others added 4 commits March 23, 2026 20:16
Implements the NetworkPolicy static primitive following the configmap
reference pattern. Includes NetworkPolicySpecEditor tests, typed mutator
with plan-and-apply pattern, and PreserveCurrentLabels/Annotations flavors.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Demonstrates HTTP ingress, boolean-gated metrics ingress, DNS egress,
version labels, and PreserveCurrentLabels flavor. The DefaultFieldApplicator
preserves ResourceVersion across reconcile cycles.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The NetworkPolicySpecEditor was referenced by tests and the networkpolicy
primitive but was never committed, causing build failures in CI.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 17 out of 17 changed files in this pull request and generated no new comments.

@sourcehawk
Copy link
Owner Author

Claude Review Cycle 2 Complete

Addressed:

  • networkpolicyspec.go:49 (EnsureIngressRule naming) — Already renamed to AppendIngressRule in commit cda4560
  • networkpolicyspec.go:65 (EnsureEgressRule naming) — Already renamed to AppendEgressRule in commit cda4560
  • networkpolicy.md:15 (docs describing Ensure* methods) — Already updated to reflect Append* naming and append semantics in commit cda4560
  • primitives.md:160 (PR description claims "Does not modify shared files") — Updated PR description to accurately reflect cross-cutting changes (shared editor, shared docs)

Intentionally ignored:
None

<!-- claude-review-cycle -->

@sourcehawk sourcehawk requested review from Copilot and removed request for Copilot March 24, 2026 02:17
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 17 out of 17 changed files in this pull request and generated 1 comment.

Replace "avoids unnecessary version parsing" with "do not need
feature-gate evaluation" since version parsing does not occur
simply from using a non-nil feature.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@sourcehawk
Copy link
Owner Author

Claude Review Cycle 3 Complete

Addressed:

  • docs/primitives/networkpolicy.md:68 — Replaced "avoids unnecessary version parsing" with "do not need feature-gate evaluation" to accurately describe the nil Feature behavior.

Intentionally ignored:
None

<!-- claude-review-cycle -->

@sourcehawk sourcehawk requested review from Copilot and removed request for Copilot March 24, 2026 02:21
@sourcehawk sourcehawk requested review from Copilot and removed request for Copilot March 24, 2026 02:22
@sourcehawk sourcehawk requested review from Copilot and removed request for Copilot March 24, 2026 02:23
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 17 out of 17 changed files in this pull request and generated no new comments.

sourcehawk and others added 4 commits March 24, 2026 17:51
…ction

Align with the configmap and deployment mutators: NewMutator no longer
creates a default feature plan. BeginFeature must be called before
registering any mutations.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…mitive

Align with the framework's switch to Server-Side Apply (SSA). Remove
DefaultFieldApplicator, WithCustomFieldApplicator, WithFieldApplicationFlavor,
FieldApplicationFlavor, flavors.go, and flavors_test.go. Update builder to
drop the defaultApplicator parameter, update tests to use Object() output
instead of empty structs for Mutate(), and strip field applicator and flavor
sections from primitive docs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 25, 2026 14:07
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.

Comment on lines +48 to +50
// 4. Preserve labels added by external controllers.
builder.WithFieldApplicationFlavor(networkpolicy.PreserveCurrentLabels)

Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

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

Builder currently has no WithFieldApplicationFlavor method, and the networkpolicy package doesn't define PreserveCurrentLabels. As written, this example won't compile with go build ./examples/... (Makefile build-examples). Either remove this call or add the missing builder API and flavor implementation in pkg/primitives/networkpolicy (matching whatever mechanism other primitives use).

Copilot uses AI. Check for mistakes.
Comment on lines +9 to +10
- **Metadata Mutations**: Setting version labels on the NetworkPolicy via `EditObjectMetadata`.
- **Field Flavors**: Preserving labels managed by external controllers using `PreserveCurrentLabels`.
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

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

This README claims the example uses PreserveCurrentLabels, but there is no such symbol in pkg/primitives/networkpolicy, and the example code currently references a non-existent WithFieldApplicationFlavor API. Please update the README to match the actual, compilable example (or implement the missing flavor API/symbols).

Suggested change
- **Metadata Mutations**: Setting version labels on the NetworkPolicy via `EditObjectMetadata`.
- **Field Flavors**: Preserving labels managed by external controllers using `PreserveCurrentLabels`.
- **Metadata Mutations**: Setting version labels on the NetworkPolicy via metadata editors.
- **Label Coexistence**: Demonstrating how label updates from this component can coexist with labels managed by other controllers.

Copilot uses AI. Check for mistakes.
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.

2 participants