|
| 1 | +package v20260301 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + |
| 10 | + "google.golang.org/protobuf/types/known/anypb" |
| 11 | + |
| 12 | + "github.com/Azure/AKSFlexNode/components/linux" |
| 13 | + "github.com/Azure/AKSFlexNode/components/services/actions" |
| 14 | + "github.com/Azure/AKSFlexNode/pkg/systemd" |
| 15 | + "github.com/Azure/AKSFlexNode/pkg/utils/utilio" |
| 16 | + "github.com/Azure/AKSFlexNode/pkg/utils/utilpb" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + dockerServiceUnit = "docker.service" |
| 21 | + dockerSocketUnit = "docker.socket" |
| 22 | + daemonConfigPath = "/etc/docker/daemon.json" |
| 23 | +) |
| 24 | + |
| 25 | +// disableDockerAction disables the docker service and configures the docker |
| 26 | +// daemon with "iptables": false. This prevents docker from manipulating |
| 27 | +// iptables rules, which would conflict with Kubernetes networking. |
| 28 | +type disableDockerAction struct { |
| 29 | + systemd systemd.Manager |
| 30 | +} |
| 31 | + |
| 32 | +func newDisableDockerAction() (actions.Server, error) { |
| 33 | + return &disableDockerAction{ |
| 34 | + systemd: systemd.New(), |
| 35 | + }, nil |
| 36 | +} |
| 37 | + |
| 38 | +var _ actions.Server = (*disableDockerAction)(nil) |
| 39 | + |
| 40 | +func (a *disableDockerAction) ApplyAction( |
| 41 | + ctx context.Context, |
| 42 | + req *actions.ApplyActionRequest, |
| 43 | +) (*actions.ApplyActionResponse, error) { |
| 44 | + settings, err := utilpb.AnyTo[*linux.DisableDocker](req.GetItem()) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + if err := a.ensureDockerDisabled(ctx); err != nil { |
| 50 | + return nil, fmt.Errorf("disabling docker: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + if err := a.ensureDaemonConfig(); err != nil { |
| 54 | + return nil, fmt.Errorf("configuring docker daemon: %w", err) |
| 55 | + } |
| 56 | + |
| 57 | + item, err := anypb.New(settings) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + |
| 62 | + return actions.ApplyActionResponse_builder{Item: item}.Build(), nil |
| 63 | +} |
| 64 | + |
| 65 | +// ensureDockerDisabled idempotently stops, disables, and masks the docker |
| 66 | +// service and socket units so docker cannot be started. |
| 67 | +func (a *disableDockerAction) ensureDockerDisabled(ctx context.Context) error { |
| 68 | + if err := systemd.EnsureUnitMasked(ctx, a.systemd, dockerSocketUnit); err != nil { |
| 69 | + return fmt.Errorf("masking %s: %w", dockerSocketUnit, err) |
| 70 | + } |
| 71 | + |
| 72 | + if err := systemd.EnsureUnitMasked(ctx, a.systemd, dockerServiceUnit); err != nil { |
| 73 | + return fmt.Errorf("masking %s: %w", dockerServiceUnit, err) |
| 74 | + } |
| 75 | + |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// ensureDaemonConfig ensures /etc/docker/daemon.json contains |
| 80 | +// "iptables": false. |
| 81 | +func (a *disableDockerAction) ensureDaemonConfig() error { |
| 82 | + return a.ensureDaemonConfigAt(daemonConfigPath) |
| 83 | +} |
| 84 | + |
| 85 | +// ensureDaemonConfigAt ensures the daemon.json at the given path |
| 86 | +// contains "iptables": false. If the file already exists, the existing |
| 87 | +// configuration is preserved and only the iptables key is set. If the file does |
| 88 | +// not exist, a new one is created. |
| 89 | +func (a *disableDockerAction) ensureDaemonConfigAt(path string) error { |
| 90 | + config := map[string]any{} |
| 91 | + |
| 92 | + existing, err := os.ReadFile(path) //#nosec G304 -- trusted path |
| 93 | + switch { |
| 94 | + case errors.Is(err, os.ErrNotExist): |
| 95 | + // file does not exist, will create with defaults |
| 96 | + case err != nil: |
| 97 | + return fmt.Errorf("reading %s: %w", path, err) |
| 98 | + default: |
| 99 | + if err := json.Unmarshal(existing, &config); err != nil { |
| 100 | + return fmt.Errorf("parsing %s: %w", path, err) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if val, ok := config["iptables"]; ok { |
| 105 | + if enabled, isBool := val.(bool); isBool && !enabled { |
| 106 | + // iptables is already set to false, nothing to do |
| 107 | + return nil |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + config["iptables"] = false |
| 112 | + |
| 113 | + content, err := marshalDaemonConfig(config) |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + return utilio.WriteFile(path, content, 0644) |
| 119 | +} |
| 120 | + |
| 121 | +// marshalDaemonConfig serializes a daemon config map to indented JSON with a |
| 122 | +// trailing newline, matching the conventional format for daemon.json. |
| 123 | +func marshalDaemonConfig(config map[string]any) ([]byte, error) { |
| 124 | + data, err := json.MarshalIndent(config, "", " ") |
| 125 | + if err != nil { |
| 126 | + return nil, fmt.Errorf("marshaling daemon config: %w", err) |
| 127 | + } |
| 128 | + data = append(data, '\n') |
| 129 | + return data, nil |
| 130 | +} |
0 commit comments