Skip to content

Commit

Permalink
Merge pull request #462 from redhatrises/ds_reconciler_cleanup
Browse files Browse the repository at this point in the history
clean(node-sensor): remove some unnecessary functions
  • Loading branch information
redhatrises authored Dec 8, 2023
2 parents 0a8dbd0 + 4c347be commit b048b3d
Showing 1 changed file with 9 additions and 31 deletions.
40 changes: 9 additions & 31 deletions controllers/falcon_node/falconnodesensor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,12 @@ func (r *FalconNodeSensorReconciler) Reconcile(ctx context.Context, req ctrl.Req

err = r.Get(ctx, types.NamespacedName{Name: nodesensor.Name, Namespace: nodesensor.TargetNs()}, daemonset)
if err != nil && errors.IsNotFound(err) {
// Define a new daemonset
ds := r.nodeSensorDaemonset(nodesensor.Name, image, serviceAccount, nodesensor, logger)
ds := assets.Daemonset(nodesensor.Name, image, serviceAccount, nodesensor)

err := controllerutil.SetControllerReference(nodesensor, ds, r.Scheme)
if err != nil {
logger.Error(err, "Unable to assign Controller Reference to the DaemonSet")
}

if len(proxy.ReadProxyVarsFromEnv()) > 0 {
for i, container := range ds.Spec.Template.Spec.Containers {
Expand Down Expand Up @@ -457,15 +461,17 @@ func (r *FalconNodeSensorReconciler) handleConfigMaps(ctx context.Context, confi
var updated bool
cmName := nodesensor.Name + "-config"
confCm := &corev1.ConfigMap{}
configmap := assets.SensorConfigMap(cmName, nodesensor.TargetNs(), common.FalconKernelSensor, config.SensorEnvVars())

err := r.Get(ctx, types.NamespacedName{Name: cmName, Namespace: nodesensor.TargetNs()}, confCm)
if err != nil && errors.IsNotFound(err) {
// does not exist, create
configmap, err := r.nodeSensorConfigmap(cmName, config, nodesensor)
err = controllerutil.SetControllerReference(nodesensor, configmap, r.Scheme)
if err != nil {
logger.Error(err, "Failed to format new Configmap", "Configmap.Namespace", nodesensor.TargetNs(), "Configmap.Name", cmName)
return nil, updated, err
}

if err := r.Create(ctx, configmap); err != nil {
if errors.IsAlreadyExists(err) {
// We have got NotFound error during the Get(), but then we have got AlreadyExists error from Create(). Client cache is invalid.
Expand All @@ -488,11 +494,6 @@ func (r *FalconNodeSensorReconciler) handleConfigMaps(ctx context.Context, confi
return nil, updated, err
}

configmap, err := r.nodeSensorConfigmap(cmName, config, nodesensor)
if err != nil {
logger.Error(err, "Failed to format existing Configmap", "Configmap.Namespace", nodesensor.TargetNs(), "Configmap.Name", cmName)
return nil, updated, err
}
if !reflect.DeepEqual(confCm.Data, configmap.Data) {
err = r.Update(ctx, configmap)
if err != nil {
Expand Down Expand Up @@ -540,29 +541,6 @@ func (r *FalconNodeSensorReconciler) handleCrowdStrikeSecrets(ctx context.Contex
return nil
}

func (r *FalconNodeSensorReconciler) nodeSensorConfigmap(name string, config *node.ConfigCache, nodesensor *falconv1alpha1.FalconNodeSensor) (*corev1.ConfigMap, error) {
cm := assets.SensorConfigMap(name, nodesensor.TargetNs(), common.FalconKernelSensor, config.SensorEnvVars())

err := controllerutil.SetControllerReference(nodesensor, cm, r.Scheme)
if err != nil {
return nil, err
}
return cm, nil
}

func (r *FalconNodeSensorReconciler) nodeSensorDaemonset(name, image, serviceAccount string, nodesensor *falconv1alpha1.FalconNodeSensor, logger logr.Logger) *appsv1.DaemonSet {
ds := assets.Daemonset(name, image, serviceAccount, nodesensor)

// NOTE: calling SetControllerReference, and setting owner references in
// general, is important as it allows deleted objects to be garbage collected.
err := controllerutil.SetControllerReference(nodesensor, ds, r.Scheme)
if err != nil {
logger.Error(err, "unable to set controller reference")
}

return ds
}

func updateDaemonSetContainerProxy(ds *appsv1.DaemonSet, nodesensor *falconv1alpha1.FalconNodeSensor, logger logr.Logger) bool {
updated := false
if len(proxy.ReadProxyVarsFromEnv()) > 0 {
Expand Down

0 comments on commit b048b3d

Please sign in to comment.