Repository: moby/moby
File: daemon/internal/nri/nri.go (Go)

Code before:
		Mounts:        nil,
		Pid:           uint32(ctr.Pid),
		Rlimits:       nil,
		CreatedAt:     0,
		StartedAt:     0,
		FinishedAt:    0,
		ExitCode:      0,
		StatusReason:  "",
		StatusMessage: "",
		CDIDevices:    nil,
	}
	return nriPod, nriCtr, nil
}

func stateToNRI(state *container.State) adaptation.ContainerState {
	switch {
	case state.Paused, state.Restarting:
		return adaptation.ContainerState_CONTAINER_PAUSED
	case state.Running:
		return adaptation.ContainerState_CONTAINER_RUNNING
	case !state.FinishedAt.IsZero():
		return adaptation.ContainerState_CONTAINER_STOPPED
	}
	// CONTAINER_CREATED will be used before the container is started, including for the
	// CreateContainer hook (during container creation).
	return adaptation.ContainerState_CONTAINER_CREATED
}

func applyAdjustments(ctx context.Context, ctr *container.Container, adj *adaptation.ContainerAdjustment) error {
	if adj == nil {
		return nil
	}
	if err := applyEnvVars(ctx, ctr, adj.Env); err != nil {
		return fmt.Errorf("applying environment variable adjustments: %w", err)
	}
	return nil
}

func applyEnvVars(ctx context.Context, ctr *container.Container, envVars []*adaptation.KeyValue) error {
	if len(envVars) == 0 {
		return nil
	}
	existing := make(map[string]int, len(ctr.Config.Env))
	for i, e := range ctr.Config.Env {
		k, _, _ := strings.Cut(e, "=")
		existing[k] = i
	}
	for _, kv := range envVars {
		if kv.Key == "" {
			return errors.New("empty environment variable key")
		}

Review comment (suggestion):
Perhaps add this in the switch as `default` ?


Would it make sense to use `state.State()` for this? That would keep our logic consistent, and the switch could be made exhaustive (we have a linter for that), so that we don't miss states not being included; https://github.com/moby/moby/blob/1fd87e9fdf5da56e33dac97fcb1a8a3caad0114d/.golangci.yml#L94-L99 


https://github.com/moby/moby/blob/1fd87e9fdf5da56e33dac97fcb1a8a3caad0114d/daemon/container/state.go#L117-L146


Code after:
			CgroupsPath:    "",
			IoPriority:     nil,
			SeccompProfile: nil,
			SeccompPolicy:  nil,
		},
		Mounts:        nil,
		Pid:           uint32(ctr.Pid),
		Rlimits:       nil,
		CreatedAt:     0,
		StartedAt:     0,
		FinishedAt:    0,
		ExitCode:      0,
		StatusReason:  "",
		StatusMessage: "",
		CDIDevices:    nil,
	}
	return nriPod, nriCtr, nil
}

func stateToNRI(state *container.State) adaptation.ContainerState {
	log.G(context.TODO()).Errorf("Mapping container state %q to NRI", state.State())
	switch state.State() {
	case containertypes.StateCreated:
		// CONTAINER_CREATED will be used before the container is started, including for the
		// CreateContainer hook (during container creation).
		return adaptation.ContainerState_CONTAINER_CREATED
	case containertypes.StateRunning:
		return adaptation.ContainerState_CONTAINER_RUNNING
	case containertypes.StatePaused, containertypes.StateRestarting:
		return adaptation.ContainerState_CONTAINER_PAUSED
	case containertypes.StateRemoving, containertypes.StateExited, containertypes.StateDead:
		return adaptation.ContainerState_CONTAINER_STOPPED
	}
	return adaptation.ContainerState_CONTAINER_UNKNOWN
}

func applyAdjustments(ctx context.Context, ctr *container.Container, adj *adaptation.ContainerAdjustment) error {
	if adj == nil {
		return nil
	}
	if err := applyEnvVars(ctx, ctr, adj.Env); err != nil {
		return fmt.Errorf("applying environment variable adjustments: %w", err)
	}
	if err := applyMounts(ctx, ctr, adj.Mounts); err != nil {
		return fmt.Errorf("applying mount adjustments: %w", err)
	}
	return nil
}

func applyEnvVars(ctx context.Context, ctr *container.Container, envVars []*adaptation.KeyValue) error {
	if len(envVars) == 0 {