-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean access policies if server is not protected (#231)
- Loading branch information
1 parent
bb19970
commit 385dc07
Showing
8 changed files
with
585 additions
and
3 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
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
15 changes: 15 additions & 0 deletions
15
src/operator/controllers/protected_service_reconcilers/mocks/ext_netpol_handler_mock.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
src/operator/controllers/protected_service_reconcilers/policy_cleaner.go
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,80 @@ | ||
package protected_service_reconcilers | ||
|
||
import ( | ||
"context" | ||
otterizev1alpha2 "github.com/otterize/intents-operator/src/operator/api/v1alpha2" | ||
"github.com/otterize/intents-operator/src/operator/controllers/intents_reconcilers" | ||
"github.com/otterize/intents-operator/src/shared/injectablerecorder" | ||
v1 "k8s.io/api/networking/v1" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// PolicyCleanerReconciler reconciles a ProtectedService object | ||
type PolicyCleanerReconciler struct { | ||
client.Client | ||
injectablerecorder.InjectableRecorder | ||
extNetpolHandler ExternalNepolHandler | ||
} | ||
|
||
func NewPolicyCleanerReconciler(client client.Client, extNetpolHandler ExternalNepolHandler) *PolicyCleanerReconciler { | ||
return &PolicyCleanerReconciler{ | ||
Client: client, | ||
extNetpolHandler: extNetpolHandler, | ||
} | ||
} | ||
|
||
func (r *PolicyCleanerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
selector, err := intents_reconcilers.MatchAccessNetworkPolicy() | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
namespace := req.Namespace | ||
policies := &v1.NetworkPolicyList{} | ||
err = r.List(ctx, policies, &client.ListOptions{Namespace: namespace, LabelSelector: selector}) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
if len(policies.Items) == 0 { | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
var protectedServicesResources otterizev1alpha2.ProtectedServiceList | ||
err = r.List(ctx, &protectedServicesResources, &client.ListOptions{Namespace: namespace}) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
protectedServersByNamespace := sets.Set[string]{} | ||
for _, protectedService := range protectedServicesResources.Items { | ||
serverName := otterizev1alpha2.GetFormattedOtterizeIdentity(protectedService.Spec.Name, namespace) | ||
protectedServersByNamespace.Insert(serverName) | ||
} | ||
|
||
for _, networkPolicy := range policies.Items { | ||
serverName := networkPolicy.Labels[otterizev1alpha2.OtterizeNetworkPolicy] | ||
if !protectedServersByNamespace.Has(serverName) { | ||
err = r.removeNetworkPolicy(ctx, networkPolicy) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
func (r *PolicyCleanerReconciler) removeNetworkPolicy(ctx context.Context, networkPolicy v1.NetworkPolicy) error { | ||
err := r.extNetpolHandler.HandleBeforeAccessPolicyRemoval(ctx, &networkPolicy) | ||
if err != nil { | ||
return err | ||
} | ||
err = r.Delete(ctx, &networkPolicy) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.