-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Max Schmidt <[email protected]>
- Loading branch information
1 parent
4c2cfb6
commit d3332e5
Showing
7 changed files
with
192 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
resources: | ||
- manager.yaml | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
images: | ||
- name: controller | ||
newName: controller | ||
newTag: latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,21 +18,38 @@ package controllers | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/tools/record" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
|
||
"github.com/kwasm/kwasm-operator/api/v1beta1" | ||
runtimev1beta1 "github.com/kwasm/kwasm-operator/api/v1beta1" | ||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// ShimReconciler reconciles a Shim object | ||
type ShimReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
Scheme *runtime.Scheme | ||
Recorder record.EventRecorder | ||
} | ||
|
||
// Definitions to manage status conditions | ||
const ( | ||
// typeAvailableShim represents the status of the Deployment reconciliation | ||
typeAvailableShim = "Available" | ||
// typeDegradedShim represents the status used when the custom resource is deleted and the finalizer operations are must to occur. | ||
typeDegradedShim = "Degraded" | ||
) | ||
|
||
//+kubebuilder:rbac:groups=runtime.kwasm.sh,resources=shims,verbs=get;list;watch;create;update;patch;delete | ||
//+kubebuilder:rbac:groups=runtime.kwasm.sh,resources=shims/status,verbs=get;update;patch | ||
//+kubebuilder:rbac:groups=runtime.kwasm.sh,resources=shims/finalizers,verbs=update | ||
|
@@ -48,12 +65,64 @@ type ShimReconciler struct { | |
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile | ||
func (r *ShimReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
_ = log.FromContext(ctx) | ||
// Fetch the Shim custom resource | ||
shim := &v1beta1.Shim{} | ||
err := r.Get(ctx, req.NamespacedName, shim) | ||
if err != nil { | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
|
||
// Create a new Kubernetes Job based on the Shim custom resource | ||
job := r.buildJobForShim(shim) | ||
|
||
// Set the owner reference to the Shim custom resource | ||
if err := controllerutil.SetControllerReference(shim, job, r.Scheme); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
// TODO(user): your logic here | ||
// Check if the Job already exists, if not, create it | ||
found := &batchv1.Job{} | ||
err = r.Get(ctx, req.NamespacedName, found) | ||
if err != nil && client.IgnoreNotFound(err) != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
if err != nil { | ||
// Job does not exist, create it | ||
if err := r.Create(ctx, job); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
// Job already exists, do nothing | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
func (r *ShimReconciler) buildJobForShim(shim *v1beta1.Shim) *batchv1.Job { | ||
job := &batchv1.Job{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: fmt.Sprintf("shim-job-%s", shim.Name), | ||
Namespace: os.Getenv("CONTROLLER_NAMESPACE"), | ||
}, | ||
Spec: batchv1.JobSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: "my-shim-container", | ||
Image: "your-specific-image", // Set your specific image here | ||
// Add other container settings as needed | ||
}, | ||
}, | ||
RestartPolicy: "Never", | ||
}, | ||
}, | ||
}, | ||
} | ||
return job | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *ShimReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
apiVersion: runtime.kwasm.sh/v1beta1 | ||
kind: Shim | ||
metadata: | ||
# The Shim resource is a cluster wide resource, no namespace. | ||
name: my-shim-v0.1.2 | ||
spec: | ||
# optional: label selector for nodes to target with shim. | ||
# If not supplied, the shim should be installed on all nodes. | ||
nodeSelector: | ||
wasm: "true" | ||
|
||
# required: The method for fetching a shim. | ||
# This could be any number of strategies for fetching. For example, OCI. | ||
fetchStrategy: | ||
anonHttp: | ||
location: https://github.com/some-org/some-project/releases/v0.8.0/shims.tar.gz | ||
|
||
# required: The runtime class to be applied in the cluster for the shim. | ||
# | ||
# The validation for this structure should also validate the `handler` | ||
# will map to the name / path of the shim binary that is installed on the node. | ||
# | ||
# Upon installation of a shim to a node, a label should be added to the node | ||
# to indicate a specific shim is installed on the node. This label must be | ||
# used to inform the K8s scheduler where to schedule workloads for the given | ||
# runtime class. | ||
# | ||
# --- | ||
# apiVersion: node.k8s.io/v1 | ||
# kind: RuntimeClass | ||
# metadata: | ||
# name: myshim-v0.1.2 | ||
# handler: myshim_v0_1_2 | ||
# scheduling: | ||
# nodeSelector: | ||
# myshim_v0_1_2: "true" | ||
runtimeClass: | ||
name: my-shim-v0.1.2 | ||
|
||
# rolloutStrategy describes how a change to this shim will be applied to nodes. | ||
rolloutStrategy: | ||
type: rolling | ||
rolling: | ||
maxUpdate: 5 # could also be a percentage of nodes, like 10% of nodes. | ||
# conditions should provide the status of the resource and it's progression |