Skip to content

Commit

Permalink
Activate NRI for cri-o optionally
Browse files Browse the repository at this point in the history
Signed-off-by: Feruzjon Muyassarov <[email protected]>
  • Loading branch information
fmuyassarov committed Sep 29, 2023
1 parent 90dc2e5 commit 996e2d4
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 29 deletions.
2 changes: 1 addition & 1 deletion cmd/config-manager/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ RUN go build -tags osusergo,netgo -ldflags "-extldflags=-static" -o config-manag
# Final Image
FROM gcr.io/distroless/static
COPY --from=build /go/builder/config-manager /bin/config-manager
ENTRYPOINT ["/bin/config-manager"]
CMD ["/bin/config-manager"]
97 changes: 71 additions & 26 deletions cmd/config-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,63 @@ import (
)

const (
tomlFilePath = "/etc/containerd/config.toml"
nriPluginKey = "io.containerd.nri.v1.nri"
disableKey = "disable"
replaceMode = "replace"
resultDone = "done"
unit = "containerd.service"
containerdConfig = "/etc/containerd/config.toml"
crioConfig = "/etc/crio/crio.conf"
containerdNriTable = "io.containerd.nri.v1.nri"
containerdNriKey = "disable"
crioNriKey = "enable_nri"
containerdUnit = "containerd.service"
crioUnit = "crio.service"
replaceMode = "replace"
resultDone = "done"
)

func main() {
tomlMap, err := readConfig(tomlFilePath)
if err != nil {
log.Fatalf("Error reading TOML file: %v", err)
if len(os.Args) < 2 || (os.Args[1] != "cri-o" && os.Args[1] != "containerd") {
log.Fatalf("You must pass a container runtime. Valid options: containerd, cri-o")
}

updatedTomlMap := updateNRIPlugin(tomlMap)

err = writeConfig(tomlFilePath, updatedTomlMap)
if err != nil {
log.Fatalf("failed to write updated config into a file %q:, %v", tomlFilePath, err)
}

err = restartSystemdUnit(unit)
if err != nil {
log.Fatalf("failed to restart containerd: %v", err)
if runtime := os.Args[1]; runtime == "containerd" {
tomlMap, err := readConfig(containerdConfig)
if err != nil {
log.Fatalf("Error reading TOML file: %v", err)
}
updatedTomlMap := patchContainerdConfig(tomlMap)

err = writeConfig(containerdConfig, updatedTomlMap, true)
if err != nil {
log.Fatalf("failed to write updated config into a file %q:, %v", containerdConfig, err)
}

err = restartSystemdUnit(containerdUnit)
if err != nil {
log.Fatalf("failed to restart containerd: %v", err)
}

} else if runtime == "cri-o" {
tomlMap, err := readConfig(crioConfig)
if err != nil {
log.Fatalf("Error reading TOML file: %v", err)
}

updatedTomlMap := patchCrioConfig(tomlMap)

err = writeConfig(crioConfig, updatedTomlMap, false)
if err != nil {
log.Fatalf("failed to write updated config into a file %q:, %v", crioConfig, err)
}

err = restartSystemdUnit(crioUnit)
if err != nil {
log.Fatalf("failed to restart CRI-O: %v", err)
}
}
}
func writeConfig(file string, config map[string]interface{}) error {

func writeConfig(file string, config map[string]interface{}, setIndent bool) error {
var buf bytes.Buffer
enc := tomlv2.NewEncoder(&buf)
enc.SetIndentTables(true)
enc.SetIndentTables(setIndent)
if err := enc.Encode(config); err != nil {
return fmt.Errorf("error encoding file: %w", err)
}
Expand Down Expand Up @@ -90,23 +117,41 @@ func readConfig(file string) (map[string]interface{}, error) {
return tomlMap, nil
}

func updateNRIPlugin(config map[string]interface{}) map[string]interface{} {
func patchContainerdConfig(config map[string]interface{}) map[string]interface{} {
plugins, exists := config["plugins"].(map[string]interface{})
if !exists {
log.Println("Top level plugins section not found, adding it to enable NRI...")
plugins = make(map[string]interface{})
config["plugins"] = plugins
}

nri, exists := plugins[nriPluginKey].(map[string]interface{})
nri, exists := plugins[containerdNriTable].(map[string]interface{})
if !exists {
log.Println("NRI plugin section not found, adding it to enable NRI...")
nri = make(map[string]interface{})
plugins[nriPluginKey] = nri
plugins[containerdNriTable] = nri
}

nri[containerdNriKey] = false
log.Println("Enabled NRI in containerd...")
return config
}

func patchCrioConfig(config map[string]interface{}) map[string]interface{} {
crio, exists := config["crio"].(map[string]interface{})
if !exists {
config["crio"] = crio
}

// If NRI table is not found, add it.
nri, exists := crio["nri"].(map[string]interface{})
if !exists {
nri = make(map[string]interface{})
crio["nri"] = nri
}

nri[disableKey] = false
log.Println("Enabled NRI...")
nri[crioNriKey] = true
log.Println("Enabled NRI in CRI-O...")
return config
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ spec:
kubernetes.io/os: "linux"
{{- if .Values.nri.patchContainerdConfig }}
initContainers:
- name: patch-containerd
- name: patch-{{ .Values.nri.runtime }}
image: {{ .Values.initContainerImage.name }}:{{ .Values.initContainerImage.tag | default .Chart.AppVersion }}
imagePullPolicy: {{ .Values.initContainerImage.pullPolicy }}
command: ["/bin/config-manager"]
args: [{{ .Values.nri.runtime }}]
volumeMounts:
- name: containerd-config
mountPath: /etc/containerd/config.toml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ resources:

nri:
patchContainerdConfig: false
runtime: containerd

initContainerImage:
name: ghcr.io/containers/nri-plugins/nri-resource-policy-config-manager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ spec:
kubernetes.io/os: "linux"
{{- if .Values.nri.patchContainerdConfig }}
initContainers:
- name: patch-containerd
- name: patch-{{ .Values.nri.runtime }}
image: {{ .Values.initContainerImage.name }}:{{ .Values.initContainerImage.tag | default .Chart.AppVersion }}
imagePullPolicy: {{ .Values.initContainerImage.pullPolicy }}
command: ["/bin/config-manager"]
args: [{{ .Values.nri.runtime }}]
volumeMounts:
- name: containerd-config
mountPath: /etc/containerd/config.toml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ resources:

nri:
patchContainerdConfig: false
runtime: containerd

initContainerImage:
name: ghcr.io/containers/nri-plugins/nri-resource-policy-config-manager
Expand Down
2 changes: 2 additions & 0 deletions docs/resource-policy/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ along with the default values, for the Topology-aware and Balloons plugins Helm
| `hostPort` | 8891 | metrics port to expose on the host |
| `config` | <pre><code>ReservedResources:</code><br><code> cpu: 750m</code></pre> | plugin configuration data |
| `nri.patchContainerdConfig` | false | enable/disable NRI in containerd. |
| `nri.runtime` | containerd | container runtime in use. Valid options: containerd, cri-o |
| `initImage.name` | [ghcr.io/containers/nri-plugins/config-manager](ghcr.io/containers/nri-plugins/config-manager) | init container image name |
| `initImage.tag` | unstable | init container image tag |
| `initImage.pullPolicy` | Always | init container image pull policy |
Expand All @@ -107,6 +108,7 @@ along with the default values, for the Topology-aware and Balloons plugins Helm
| `hostPort` | 8891 | metrics port to expose on the host |
| `config` | <pre><code>ReservedResources:</code><br><code> cpu: 750m</code></pre> | plugin configuration data |
| `nri.patchContainerdConfig` | false | enable/disable NRI in containerd. |
| `nri.runtime` | containerd | container runtime in use. Valid options: containerd, cri-o |
| `initImage.name` | [ghcr.io/containers/nri-plugins/config-manager](ghcr.io/containers/nri-plugins/config-manager) | init container image name |
| `initImage.tag` | unstable | init container image tag |
| `initImage.pullPolicy` | Always | init container image pull policy |
Expand Down

0 comments on commit 996e2d4

Please sign in to comment.