-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update
NetworkPolicyRule
type, add validation and registry
- Loading branch information
1 parent
0bd2a26
commit a5dff1d
Showing
15 changed files
with
519 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 2 additions & 11 deletions
13
client-go/applyconfigurations/core/v1alpha1/networkpolicytargetref.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and IronCore contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package validation | ||
|
||
import ( | ||
"github.com/ironcore-dev/ironcore-net/internal/apis/core" | ||
"k8s.io/apimachinery/pkg/api/validation" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
) | ||
|
||
func ValidateNetworkPolicyRule(networkPolicyRule *core.NetworkPolicyRule) field.ErrorList { | ||
var allErrs field.ErrorList | ||
|
||
allErrs = append(allErrs, validation.ValidateObjectMetaAccessor(networkPolicyRule, true, validation.NameIsDNSLabel, field.NewPath("metadata"))...) | ||
|
||
return allErrs | ||
} | ||
|
||
func ValidateNetworkPolicyRuleUpdate(newNetworkPolicyRule, oldNetworkPolicyRule *core.NetworkPolicyRule) field.ErrorList { | ||
var allErrs field.ErrorList | ||
|
||
allErrs = append(allErrs, validation.ValidateObjectMetaAccessorUpdate(newNetworkPolicyRule, oldNetworkPolicyRule, field.NewPath("metadata"))...) | ||
allErrs = append(allErrs, ValidateNetworkPolicyRule(newNetworkPolicyRule)...) | ||
|
||
return allErrs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and IronCore contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package networkpolicy | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ironcore-dev/ironcore-net/internal/apis/core" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apiserver/pkg/registry/generic" | ||
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry" | ||
"k8s.io/apiserver/pkg/registry/rest" | ||
"sigs.k8s.io/structured-merge-diff/v4/fieldpath" | ||
) | ||
|
||
type NetworkPolicyStorage struct { | ||
NetworkPolicy *REST | ||
Status *StatusREST | ||
} | ||
|
||
type REST struct { | ||
*genericregistry.Store | ||
} | ||
|
||
func (REST) ShortNames() []string { | ||
return []string{"netpol"} | ||
} | ||
|
||
func NewStorage(scheme *runtime.Scheme, optsGetter generic.RESTOptionsGetter) (NetworkPolicyStorage, error) { | ||
strategy := NewStrategy(scheme) | ||
statusStrategy := NewStatusStrategy(scheme) | ||
store := &genericregistry.Store{ | ||
NewFunc: func() runtime.Object { | ||
return &core.NetworkPolicy{} | ||
}, | ||
NewListFunc: func() runtime.Object { | ||
return &core.NetworkPolicyList{} | ||
}, | ||
PredicateFunc: MatchNetworkPolicy, | ||
DefaultQualifiedResource: core.Resource("networkpolicies"), | ||
SingularQualifiedResource: core.Resource("networkpolicy"), | ||
|
||
CreateStrategy: strategy, | ||
UpdateStrategy: strategy, | ||
DeleteStrategy: strategy, | ||
|
||
TableConvertor: newTableConvertor(), | ||
} | ||
|
||
options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: GetAttrs} | ||
if err := store.CompleteWithOptions(options); err != nil { | ||
return NetworkPolicyStorage{}, err | ||
} | ||
|
||
statusStore := *store | ||
statusStore.UpdateStrategy = statusStrategy | ||
statusStore.ResetFieldsStrategy = statusStrategy | ||
|
||
return NetworkPolicyStorage{ | ||
NetworkPolicy: &REST{store}, | ||
Status: &StatusREST{&statusStore}, | ||
}, nil | ||
} | ||
|
||
type StatusREST struct { | ||
store *genericregistry.Store | ||
} | ||
|
||
func (r *StatusREST) New() runtime.Object { | ||
return &core.NetworkPolicy{} | ||
} | ||
|
||
func (r *StatusREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) { | ||
return r.store.Get(ctx, name, options) | ||
} | ||
|
||
func (r *StatusREST) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) { | ||
return r.store.Update(ctx, name, objInfo, createValidation, updateValidation, false, options) | ||
} | ||
|
||
func (r *StatusREST) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { | ||
return r.store.GetResetFields() | ||
} | ||
|
||
func (r *StatusREST) Destroy() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and IronCore contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package networkpolicy | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ironcore-dev/ironcore-net/internal/apis/core" | ||
"github.com/ironcore-dev/ironcore-net/internal/apis/core/validation" | ||
"k8s.io/apimachinery/pkg/fields" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
"k8s.io/apiserver/pkg/registry/generic" | ||
apisrvstorage "k8s.io/apiserver/pkg/storage" | ||
"k8s.io/apiserver/pkg/storage/names" | ||
"sigs.k8s.io/structured-merge-diff/v4/fieldpath" | ||
) | ||
|
||
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) { | ||
networkPolicy, ok := obj.(*core.NetworkPolicy) | ||
if !ok { | ||
return nil, nil, fmt.Errorf("given object is not a NetworkPolicy") | ||
} | ||
return networkPolicy.Labels, SelectableFields(networkPolicy), nil | ||
} | ||
|
||
func MatchNetworkPolicy(label labels.Selector, field fields.Selector) apisrvstorage.SelectionPredicate { | ||
return apisrvstorage.SelectionPredicate{ | ||
Label: label, | ||
Field: field, | ||
GetAttrs: GetAttrs, | ||
} | ||
} | ||
|
||
func SelectableFields(networkPolicy *core.NetworkPolicy) fields.Set { | ||
return generic.ObjectMetaFieldsSet(&networkPolicy.ObjectMeta, true) | ||
} | ||
|
||
type networkPolicyStrategy struct { | ||
runtime.ObjectTyper | ||
names.NameGenerator | ||
} | ||
|
||
func NewStrategy(typer runtime.ObjectTyper) networkPolicyStrategy { | ||
return networkPolicyStrategy{typer, names.SimpleNameGenerator} | ||
} | ||
func (networkPolicyStrategy) NamespaceScoped() bool { | ||
return true | ||
} | ||
|
||
func (networkPolicyStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) { | ||
|
||
} | ||
|
||
func (networkPolicyStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { | ||
} | ||
|
||
func (networkPolicyStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList { | ||
networkPolicy := obj.(*core.NetworkPolicy) | ||
return validation.ValidateNetworkPolicy(networkPolicy) | ||
} | ||
|
||
func (networkPolicyStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { | ||
return nil | ||
} | ||
|
||
func (networkPolicyStrategy) AllowCreateOnUpdate() bool { | ||
return false | ||
} | ||
|
||
func (networkPolicyStrategy) AllowUnconditionalUpdate() bool { | ||
return false | ||
} | ||
|
||
func (networkPolicyStrategy) Canonicalize(obj runtime.Object) { | ||
} | ||
|
||
func (networkPolicyStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { | ||
newNetworkPolicy := obj.(*core.NetworkPolicy) | ||
oldNetworkPolicy := old.(*core.NetworkPolicy) | ||
return validation.ValidateNetworkPolicyUpdate(newNetworkPolicy, oldNetworkPolicy) | ||
} | ||
|
||
func (networkPolicyStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { | ||
return nil | ||
} | ||
|
||
type networkPolicyStatusStrategy struct { | ||
networkPolicyStrategy | ||
} | ||
|
||
func NewStatusStrategy(typer runtime.ObjectTyper) networkPolicyStatusStrategy { | ||
return networkPolicyStatusStrategy{NewStrategy(typer)} | ||
} | ||
func (networkPolicyStatusStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { | ||
return map[fieldpath.APIVersion]*fieldpath.Set{ | ||
"apinet.ironcore.dev/v1alpha1": fieldpath.NewSet( | ||
fieldpath.MakePathOrDie("spec"), | ||
), | ||
} | ||
} | ||
|
||
func (networkPolicyStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { | ||
} | ||
|
||
func (networkPolicyStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { | ||
newNetworkPolicy := obj.(*core.NetworkPolicy) | ||
oldNetworkPolicy := old.(*core.NetworkPolicy) | ||
return validation.ValidateNetworkPolicyUpdate(newNetworkPolicy, oldNetworkPolicy) | ||
} | ||
|
||
func (networkPolicyStatusStrategy) WarningsOnUpdate(cxt context.Context, obj, old runtime.Object) []string { | ||
return nil | ||
} |
Oops, something went wrong.