github.com/cilium/cilium@v1.16.2/pkg/datapath/linux/sysctl/ops.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package sysctl
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"log/slog"
    10  	"strings"
    11  
    12  	"github.com/spf13/afero"
    13  
    14  	"github.com/cilium/statedb"
    15  	"github.com/cilium/statedb/reconciler"
    16  
    17  	"github.com/cilium/cilium/pkg/datapath/tables"
    18  	"github.com/cilium/cilium/pkg/logging/logfields"
    19  )
    20  
    21  func newOps(log *slog.Logger, fs afero.Fs, cfg Config) reconciler.Operations[*tables.Sysctl] {
    22  	return &ops{log: log, fs: fs, procFs: cfg.ProcFs}
    23  }
    24  
    25  type ops struct {
    26  	log    *slog.Logger
    27  	fs     afero.Fs
    28  	procFs string
    29  }
    30  
    31  func (ops *ops) Update(ctx context.Context, txn statedb.ReadTxn, s *tables.Sysctl) error {
    32  	log := ops.log.With(
    33  		logfields.SysParamName, strings.Join(s.Name, "."),
    34  		logfields.SysParamValue, s.Val,
    35  	)
    36  
    37  	path, err := parameterPath(ops.procFs, s.Name)
    38  	if err != nil {
    39  		if s.IgnoreErr {
    40  			return nil
    41  		}
    42  		return fmt.Errorf("failed to get full path of sysctl setting %s: %w", s.Name, err)
    43  	}
    44  
    45  	val, err := readSysctl(ops.fs, path)
    46  	if err != nil {
    47  		if s.IgnoreErr {
    48  			return nil
    49  		}
    50  		return err
    51  	}
    52  	if val == s.Val {
    53  		return nil
    54  	}
    55  
    56  	if err := writeSysctl(ops.fs, path, s.Val); err != nil {
    57  		if s.IgnoreErr {
    58  			warn := "Failed to write sysctl setting"
    59  			if s.Warn != "" {
    60  				warn = s.Warn
    61  			}
    62  			log.Warn(warn)
    63  			return nil
    64  		}
    65  		return fmt.Errorf("failed to write sysctl setting %s: %w", path, err)
    66  	}
    67  	return nil
    68  }
    69  
    70  func (ops *ops) Delete(context.Context, statedb.ReadTxn, *tables.Sysctl) error {
    71  	// sysctl settings will never be deleted, just ignored
    72  	return nil
    73  }
    74  
    75  func (ops *ops) Prune(context.Context, statedb.ReadTxn, statedb.Iterator[*tables.Sysctl]) error {
    76  	// sysctl settings not in the table will never be pruned, just ignored
    77  	return nil
    78  }