@@ -18,21 +18,38 @@ package controllers
1818
1919import (
2020 "context"
21+ "fmt"
22+ "os"
2123
2224 "k8s.io/apimachinery/pkg/runtime"
25+ "k8s.io/client-go/tools/record"
2326 ctrl "sigs.k8s.io/controller-runtime"
2427 "sigs.k8s.io/controller-runtime/pkg/client"
28+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2529 "sigs.k8s.io/controller-runtime/pkg/log"
2630
31+ "github.com/kwasm/kwasm-operator/api/v1beta1"
2732 runtimev1beta1 "github.com/kwasm/kwasm-operator/api/v1beta1"
33+ batchv1 "k8s.io/api/batch/v1"
34+ corev1 "k8s.io/api/core/v1"
35+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2836)
2937
3038// ShimReconciler reconciles a Shim object
3139type ShimReconciler struct {
3240 client.Client
33- Scheme * runtime.Scheme
41+ Scheme * runtime.Scheme
42+ Recorder record.EventRecorder
3443}
3544
45+ // Definitions to manage status conditions
46+ const (
47+ // typeAvailableShim represents the status of the Deployment reconciliation
48+ typeAvailableShim = "Available"
49+ // typeDegradedShim represents the status used when the custom resource is deleted and the finalizer operations are must to occur.
50+ typeDegradedShim = "Degraded"
51+ )
52+
3653//+kubebuilder:rbac:groups=runtime.kwasm.sh,resources=shims,verbs=get;list;watch;create;update;patch;delete
3754//+kubebuilder:rbac:groups=runtime.kwasm.sh,resources=shims/status,verbs=get;update;patch
3855//+kubebuilder:rbac:groups=runtime.kwasm.sh,resources=shims/finalizers,verbs=update
@@ -48,12 +65,64 @@ type ShimReconciler struct {
4865// - https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/reconcile 4966func (r * ShimReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
5067 _ = log .FromContext (ctx )
68+ // Fetch the Shim custom resource
69+ shim := & v1beta1.Shim {}
70+ err := r .Get (ctx , req .NamespacedName , shim )
71+ if err != nil {
72+ return ctrl.Result {}, client .IgnoreNotFound (err )
73+ }
74+
75+ // Create a new Kubernetes Job based on the Shim custom resource
76+ job := r .buildJobForShim (shim )
77+
78+ // Set the owner reference to the Shim custom resource
79+ if err := controllerutil .SetControllerReference (shim , job , r .Scheme ); err != nil {
80+ return ctrl.Result {}, err
81+ }
5182
52- // TODO(user): your logic here
83+ // Check if the Job already exists, if not, create it
84+ found := & batchv1.Job {}
85+ err = r .Get (ctx , req .NamespacedName , found )
86+ if err != nil && client .IgnoreNotFound (err ) != nil {
87+ return ctrl.Result {}, err
88+ }
5389
90+ if err != nil {
91+ // Job does not exist, create it
92+ if err := r .Create (ctx , job ); err != nil {
93+ return ctrl.Result {}, err
94+ }
95+ return ctrl.Result {}, nil
96+ }
97+
98+ // Job already exists, do nothing
5499 return ctrl.Result {}, nil
55100}
56101
102+ func (r * ShimReconciler ) buildJobForShim (shim * v1beta1.Shim ) * batchv1.Job {
103+ job := & batchv1.Job {
104+ ObjectMeta : metav1.ObjectMeta {
105+ Name : fmt .Sprintf ("shim-job-%s" , shim .Name ),
106+ Namespace : os .Getenv ("CONTROLLER_NAMESPACE" ),
107+ },
108+ Spec : batchv1.JobSpec {
109+ Template : corev1.PodTemplateSpec {
110+ Spec : corev1.PodSpec {
111+ Containers : []corev1.Container {
112+ {
113+ Name : "my-shim-container" ,
114+ Image : "your-specific-image" , // Set your specific image here
115+ // Add other container settings as needed
116+ },
117+ },
118+ RestartPolicy : "Never" ,
119+ },
120+ },
121+ },
122+ }
123+ return job
124+ }
125+
57126// SetupWithManager sets up the controller with the Manager.
58127func (r * ShimReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
59128 return ctrl .NewControllerManagedBy (mgr ).
0 commit comments