Skip to content

Commit

Permalink
Merge pull request #82 from bytedance/add-owner-reference
Browse files Browse the repository at this point in the history
Add owner reference
  • Loading branch information
Danny-Wei authored Jun 3, 2024
2 parents 6000e69 + 8a1bd11 commit 5047149
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
21 changes: 11 additions & 10 deletions internal/policy/clusterpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,18 @@ func (c *ClusterPolicyController) handleDeleteVarmorClusterPolicy(name string) e
ap, err := c.varmorInterface.ArmorProfiles(varmorconfig.Namespace).Get(context.Background(), apName, metav1.GetOptions{})
if err != nil {
if k8errors.IsNotFound(err) {
return nil
logger.Info("ArmorProfile object not found", "namespace", varmorconfig.Namespace, "name", apName)
} else {
logger.Error(err, "c.varmorInterface.ArmorProfiles().Get()")
return err
}
} else {
logger.Info("delete ArmorProfile")
err = c.varmorInterface.ArmorProfiles(varmorconfig.Namespace).Delete(context.Background(), apName, metav1.DeleteOptions{})
if err != nil {
logger.Error(err, "ArmorProfile().Delete()")
return err
}
logger.Error(err, "c.varmorInterface.ArmorProfiles().Get()")
return err
}

logger.Info("delete ArmorProfile")
err = c.varmorInterface.ArmorProfiles(varmorconfig.Namespace).Delete(context.Background(), apName, metav1.DeleteOptions{})
if err != nil {
logger.Error(err, "ArmorProfile().Delete()")
return err
}

if c.restartExistWorkloads && ap.Spec.UpdateExistingWorkloads {
Expand Down
21 changes: 11 additions & 10 deletions internal/policy/policy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,18 @@ func (c *PolicyController) handleDeleteVarmorPolicy(namespace, name string) erro
ap, err := c.varmorInterface.ArmorProfiles(namespace).Get(context.Background(), apName, metav1.GetOptions{})
if err != nil {
if k8errors.IsNotFound(err) {
return nil
logger.Info("ArmorProfile object not found", "namespace", namespace, "name", apName)
} else {
logger.Error(err, "c.varmorInterface.ArmorProfiles().Get()")
return err
}
} else {
logger.Info("delete ArmorProfile")
err = c.varmorInterface.ArmorProfiles(namespace).Delete(context.Background(), apName, metav1.DeleteOptions{})
if err != nil {
logger.Error(err, "ArmorProfile().Delete()")
return err
}
logger.Error(err, "c.varmorInterface.ArmorProfiles().Get()")
return err
}

logger.Info("delete ArmorProfile")
err = c.varmorInterface.ArmorProfiles(namespace).Delete(context.Background(), apName, metav1.DeleteOptions{})
if err != nil {
logger.Error(err, "ArmorProfile().Delete()")
return err
}

if c.restartExistWorkloads && ap.Spec.UpdateExistingWorkloads {
Expand Down
19 changes: 19 additions & 0 deletions internal/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func GenerateProfile(policy varmor.Policy, name string, namespace string, varmor

func NewArmorProfile(obj interface{}, varmorInterface varmorinterface.CrdV1beta1Interface, clusterScope bool) (*varmor.ArmorProfile, error) {
ap := varmor.ArmorProfile{}
controller := true

if clusterScope {
vcp := obj.(*varmor.VarmorClusterPolicy)
Expand All @@ -199,6 +200,15 @@ func NewArmorProfile(obj interface{}, varmorInterface varmorinterface.CrdV1beta1
ap.Name = profileName
ap.Namespace = varmorconfig.Namespace
ap.Labels = vcp.ObjectMeta.DeepCopy().Labels
ap.OwnerReferences = []metav1.OwnerReference{
{
APIVersion: "crd.varmor.org/v1beta1",
Kind: "VarmorClusterPolicy",
Name: vcp.Name,
UID: vcp.UID,
Controller: &controller,
},
}

profile, err := GenerateProfile(vcp.Spec.Policy, ap.Name, ap.Namespace, varmorInterface, false)
if err != nil {
Expand All @@ -223,6 +233,15 @@ func NewArmorProfile(obj interface{}, varmorInterface varmorinterface.CrdV1beta1
ap.Name = profileName
ap.Namespace = vp.Namespace
ap.Labels = vp.ObjectMeta.DeepCopy().Labels
ap.OwnerReferences = []metav1.OwnerReference{
{
APIVersion: "crd.varmor.org/v1beta1",
Kind: "VarmorPolicy",
Name: vp.Name,
UID: vp.UID,
Controller: &controller,
},
}

profile, err := GenerateProfile(vp.Spec.Policy, ap.Name, ap.Namespace, varmorInterface, false)
if err != nil {
Expand Down

0 comments on commit 5047149

Please sign in to comment.