Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for BaselineAdminNetworkPolicy. #9464

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,22 @@ image:
# using a local kind cluster.
###############################################################################
E2E_FOCUS ?= "sig-network.*Conformance"
ADMINPOLICY_SUPPORTED_FEATURES ?= "AdminNetworkPolicy"
ADMINPOLICY_UNSUPPORTED_FEATURES ?= "BaselineAdminNetworkPolicy"
ADMINPOLICY_SUPPORTED_FEATURES ?= "AdminNetworkPolicy,BaselineAdminNetworkPolicy"
ADMINPOLICY_UNSUPPORTED_FEATURES ?= ""
e2e-test:
$(MAKE) -C e2e build
$(MAKE) -C node kind-k8st-setup
KUBECONFIG=$(KIND_KUBECONFIG) ./e2e/bin/k8s/e2e.test -ginkgo.focus=$(E2E_FOCUS)
KUBECONFIG=$(KIND_KUBECONFIG) ./e2e/bin/adminpolicy/e2e.test -exempt-features=$(ADMINPOLICY_UNSUPPORTED_FEATURES) -supported-features=$(ADMINPOLICY_SUPPORTED_FEATURES)
KUBECONFIG=$(KIND_KUBECONFIG) ./e2e/bin/adminpolicy/e2e.test \
-exempt-features=$(ADMINPOLICY_UNSUPPORTED_FEATURES) \
-supported-features=$(ADMINPOLICY_SUPPORTED_FEATURES)

e2e-test-adminpolicy:
$(MAKE) -C e2e build
$(MAKE) -C node kind-k8st-setup
KUBECONFIG=$(KIND_KUBECONFIG) ./e2e/bin/adminpolicy/e2e.test \
-exempt-features=$(ADMINPOLICY_UNSUPPORTED_FEATURES) \
-supported-features=$(ADMINPOLICY_SUPPORTED_FEATURES)

###############################################################################
# Release logic below
Expand Down
5 changes: 3 additions & 2 deletions api/pkg/apis/projectcalico/v3/tier.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ type Tier struct {
}

const (
DefaultTierOrder = float64(1_000_000) // 1Million
AdminNetworkPolicyTierOrder = float64(1_000) // 1K
AdminNetworkPolicyTierOrder = float64(1_000) // 1K
DefaultTierOrder = float64(1_000_000) // 1Million
BaselineAdminNetworkPolicyTierOrder = float64(10_000_000) // 10Million
)

// TierSpec contains the specification for a security policy tier resource.
Expand Down
20 changes: 19 additions & 1 deletion apiserver/pkg/storage/calico/tier_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,12 @@ func TestTierList(t *testing.T) {
t.Fatalf("Get failed: %v", err)
}

banpTier := makeTier(names.BaselineAdminNetworkPolicyTierName, "", v3.BaselineAdminNetworkPolicyTierOrder)
err = store.Get(ctx, "projectcalico.org/tiers/baselineadminnetworkpolicy", opts, banpTier)
if err != nil {
t.Fatalf("Get failed: %v", err)
}

tests := []struct {
prefix string
pred storage.SelectionPredicate
Expand All @@ -552,7 +558,8 @@ func TestTierList(t *testing.T) {
return nil, fields.Set{"metadata.name": tier.Name}, nil
},
},
expectedOut: []*v3.Tier{anpTier, preset[1].storedObj, defaultTier},
// Tiers are returned in name order.
expectedOut: []*v3.Tier{anpTier, preset[1].storedObj, banpTier, defaultTier},
}}

for i, tt := range tests {
Expand All @@ -566,6 +573,17 @@ func TestTierList(t *testing.T) {
t.Errorf("#%d: length of list want=%d, get=%d", i, len(tt.expectedOut), len(out.Items))
continue
}
var wantNames, gotNames []string
for _, wantTier := range tt.expectedOut {
wantNames = append(wantNames, wantTier.Name)
}
for _, getTier := range out.Items {
gotNames = append(gotNames, getTier.Name)
}
if !reflect.DeepEqual(wantNames, gotNames) {
t.Errorf("#%d: tier names want=%v, get=%v", i, wantNames, gotNames)
}

for j, wantTier := range tt.expectedOut {
getTier := &out.Items[j]
if !reflect.DeepEqual(wantTier, getTier) {
Expand Down
7 changes: 5 additions & 2 deletions calicoctl/calicoctl/commands/datastore/migrate/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,12 @@ Description:
if !ok {
return fmt.Errorf("Unable to convert Calico gloabal network policy for inspection")
}
if !strings.HasPrefix(metaObj.GetObjectMeta().GetName(), names.K8sAdminNetworkPolicyNamePrefix) {
filtered = append(filtered, obj)
if strings.HasPrefix(metaObj.GetObjectMeta().GetName(), names.K8sAdminNetworkPolicyNamePrefix) ||
strings.HasPrefix(metaObj.GetObjectMeta().GetName(), names.K8sBaselineAdminNetworkPolicyNamePrefix) {
continue
}
filtered = append(filtered, obj)

}

err = meta.SetList(resource, filtered)
Expand Down
11 changes: 8 additions & 3 deletions calicoctl/calicoctl/resourcemgr/globalnetworkpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func init() {
},
func(ctx context.Context, client client.Interface, resource ResourceObject) (ResourceObject, error) {
r := resource.(*api.GlobalNetworkPolicy)
if strings.HasPrefix(r.Name, names.K8sAdminNetworkPolicyNamePrefix) {
if policyIsANP(r) {
return nil, cerrors.ErrorOperationNotSupported{
Operation: "create or apply",
Identifier: resource,
Expand All @@ -54,7 +54,7 @@ func init() {
},
func(ctx context.Context, client client.Interface, resource ResourceObject) (ResourceObject, error) {
r := resource.(*api.GlobalNetworkPolicy)
if strings.HasPrefix(r.Name, names.K8sAdminNetworkPolicyNamePrefix) {
if policyIsANP(r) {
return nil, cerrors.ErrorOperationNotSupported{
Operation: "create or apply",
Identifier: resource,
Expand All @@ -65,7 +65,7 @@ func init() {
},
func(ctx context.Context, client client.Interface, resource ResourceObject) (ResourceObject, error) {
r := resource.(*api.GlobalNetworkPolicy)
if strings.HasPrefix(r.Name, names.K8sAdminNetworkPolicyNamePrefix) {
if policyIsANP(r) {
return nil, cerrors.ErrorOperationNotSupported{
Operation: "create or apply",
Identifier: resource,
Expand All @@ -85,6 +85,11 @@ func init() {
)
}

func policyIsANP(r *api.GlobalNetworkPolicy) bool {
return strings.HasPrefix(r.Name, names.K8sAdminNetworkPolicyNamePrefix) ||
strings.HasPrefix(r.Name, names.K8sBaselineAdminNetworkPolicyNamePrefix)
}

// newGlobalNetworkPolicyList creates a new (zeroed) GlobalNetworkPolicyList struct with the TypeMetadata initialised to the current
// version.
func newGlobalNetworkPolicyList() *api.GlobalNetworkPolicyList {
Expand Down
3 changes: 2 additions & 1 deletion charts/calico/templates/calico-node-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ rules:
verbs:
- watch
- list
# Watch for changes to Kubernetes AdminNetworkPolicies.
# Watch for changes to Kubernetes (Baseline)AdminNetworkPolicies.
- apiGroups: ["policy.networking.k8s.io"]
resources:
- adminnetworkpolicies
- baselineadminnetworkpolicies
verbs:
- watch
- list
Expand Down
2 changes: 2 additions & 0 deletions libcalico-go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ KIND_CONFIG = $(KIND_DIR)/kind-single.config
NETPOL_TAG = v0.1.5
NETPOL_CRD_URL = https://raw.githubusercontent.com/kubernetes-sigs/network-policy-api/refs/tags/$(NETPOL_TAG)/config/crd/experimental
NETPOL_ANP_CRD = policy.networking.k8s.io_adminnetworkpolicies.yaml
NETPOL_BANP_CRD = policy.networking.k8s.io_baselineadminnetworkpolicies.yaml

###############################################################################
# Download and include ../lib.Makefile
Expand Down Expand Up @@ -59,6 +60,7 @@ gen-crds:
$(DOCKER_GO_BUILD) sh -c 'find ./config/crd -name "*.yaml" | xargs sed -i -e 1,2d'
# Add K8S AdminNetworkPolicy CRD
curl $(NETPOL_CRD_URL)/$(NETPOL_ANP_CRD) -o ./config/crd/$(NETPOL_ANP_CRD)
curl $(NETPOL_CRD_URL)/$(NETPOL_BANP_CRD) -o ./config/crd/$(NETPOL_BANP_CRD)

./lib/upgrade/migrator/clients/v1/k8s/custom/zz_generated.deepcopy.go: $(UPGRADE_SRCS)
$(DOCKER_GO_BUILD) sh -c 'deepcopy-gen \
Expand Down
Loading