-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(adapter): Add applier and modify package name
- Loading branch information
Showing
6 changed files
with
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package applier | ||
|
||
import ( | ||
"context" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
|
||
kubearmorv1 "github.com/kubearmor/KubeArmor/pkg/KubeArmorController/api/security.kubearmor.com/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// Applier manages the enforcement of policies. | ||
type Applier struct { | ||
Client client.Client | ||
} | ||
|
||
// NewApplier creates a new Applier. | ||
func NewApplier(client client.Client) *Applier { | ||
return &Applier{Client: client} | ||
} | ||
|
||
// ApplyPolicy applies or updates a given KubeArmorPolicy. | ||
func (e *Applier) ApplyPolicy(ctx context.Context, kubeArmorPolicy *kubearmorv1.KubeArmorPolicy) error { | ||
log := log.FromContext(ctx) | ||
|
||
// Check if the policy already exists | ||
existingPolicy := &kubearmorv1.KubeArmorPolicy{} | ||
err := e.Client.Get(ctx, types.NamespacedName{Name: kubeArmorPolicy.Name, Namespace: kubeArmorPolicy.Namespace}, existingPolicy) | ||
if err != nil && !errors.IsNotFound(err) { | ||
log.Error(err, "Existing KubeArmorPolicy lookup failed", "PolicyName", kubeArmorPolicy.Name) | ||
return err | ||
} | ||
|
||
// Update if exists, create otherwise | ||
if errors.IsNotFound(err) { | ||
log.Info("Apply a new KubeArmorPolicy", "PolicyName", kubeArmorPolicy.Name, "Policy", kubeArmorPolicy) | ||
return e.Client.Create(ctx, kubeArmorPolicy) | ||
} else { | ||
log.Info("Update existing KubeArmorPolicy", "PolicyName", kubeArmorPolicy.Name) | ||
existingPolicy.Spec = kubeArmorPolicy.Spec | ||
return e.Client.Update(ctx, existingPolicy) | ||
} | ||
} |
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
Binary file not shown.
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