Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add runtime classes hook and runtimes chart #7382

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions charts/chart_versions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ charts:
- version: 1.9.001
filename: /charts/rke2-snapshot-validation-webhook.yaml
bootstrap: false
- version: 0.1.000
filename: /charts/rke2-runtimeclasses.yaml
bootstrap: false
85 changes: 85 additions & 0 deletions pkg/rke2/rc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package rke2

import (
"context"
"sync"

"github.com/k3s-io/k3s/pkg/cli/cmds"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

const runtimeClassesChart = "rke2-runtimeclasses"

var runtimes = map[string]bool{
"nvidia": true,
"nvidia-experimental": true,
"crun": true,
}

func setRuntimes() cmds.StartupHook {
return func(ctx context.Context, wg *sync.WaitGroup, args cmds.StartupHookArgs) error {
go func() {
defer wg.Done()
<-args.APIServerReady
logrus.Info("Setting runtimes")

config, err := clientcmd.BuildConfigFromFlags("", args.KubeConfigSupervisor)
if err != nil {
logrus.Fatalf("runtimes: new k8s restConfig: %v", err)
}

client, err := kubernetes.NewForConfig(config)
if err != nil {
logrus.Fatalf("runtimes: new k8s client: %v", err)
}
vitorsavian marked this conversation as resolved.
Show resolved Hide resolved

rcClient := client.NodeV1().RuntimeClasses()

classes, err := rcClient.List(context.Background(), metav1.ListOptions{})
if err != nil {
logrus.Fatalf("runtimes: failed to get runtime classes")
}

for _, c := range classes.Items {

// verify if the runtime class is the runtime class supported by rke2
if _, ok := runtimes[c.Name]; !ok {
continue
}

if c.Labels == nil {
labels := make(map[string]string)
c.SetLabels(labels)
vitorsavian marked this conversation as resolved.
Show resolved Hide resolved
}

if managedBy, ok := c.Labels["app.kubernetes.io/managed-by"]; !ok || managedBy != "Helm" {
c.Labels["app.kubernetes.io/managed-by"] = "Helm"
}

if c.Annotations == nil {
annotations := make(map[string]string)
c.SetAnnotations(annotations)
vitorsavian marked this conversation as resolved.
Show resolved Hide resolved
}

if releaseName, ok := c.Annotations["meta.helm.sh/release-name"]; !ok || releaseName != runtimeClassesChart {
c.Annotations["meta.helm.sh/release-name"] = runtimeClassesChart
vitorsavian marked this conversation as resolved.
Show resolved Hide resolved
}

if namespace, ok := c.Annotations["meta.helm.sh/release-namespace"]; !ok || namespace != "kube-system" {
c.Annotations["meta.helm.sh/release-namespace"] = "kube-system"
}

_, err = rcClient.Update(context.Background(), &c, metav1.UpdateOptions{})
if err != nil {
logrus.Fatalf("runtimes: failed to update runtime classes")
}

}
}()

return nil
}
}
1 change: 1 addition & 0 deletions pkg/rke2/rke2.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func Server(clx *cli.Context, cfg Config) error {
restrictServiceAccounts(cisMode, defaultNamespaces),
setKubeProxyDisabled(),
cleanupStaticPodsOnSelfDelete(dataDir),
setRuntimes(),
)

var leaderControllers rawServer.CustomControllers
Expand Down
Loading