@@ -18,21 +18,38 @@ package controllers
18
18
19
19
import (
20
20
"context"
21
+ "fmt"
22
+ "os"
21
23
22
24
"k8s.io/apimachinery/pkg/runtime"
25
+ "k8s.io/client-go/tools/record"
23
26
ctrl "sigs.k8s.io/controller-runtime"
24
27
"sigs.k8s.io/controller-runtime/pkg/client"
28
+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
25
29
"sigs.k8s.io/controller-runtime/pkg/log"
26
30
31
+ "github.com/kwasm/kwasm-operator/api/v1beta1"
27
32
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"
28
36
)
29
37
30
38
// ShimReconciler reconciles a Shim object
31
39
type ShimReconciler struct {
32
40
client.Client
33
- Scheme * runtime.Scheme
41
+ Scheme * runtime.Scheme
42
+ Recorder record.EventRecorder
34
43
}
35
44
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
+
36
53
//+kubebuilder:rbac:groups=runtime.kwasm.sh,resources=shims,verbs=get;list;watch;create;update;patch;delete
37
54
//+kubebuilder:rbac:groups=runtime.kwasm.sh,resources=shims/status,verbs=get;update;patch
38
55
//+kubebuilder:rbac:groups=runtime.kwasm.sh,resources=shims/finalizers,verbs=update
@@ -48,12 +65,64 @@ type ShimReconciler struct {
48
65
// - https://pkg.go.dev/sigs.k8s.io/[email protected] /pkg/reconcile
49
66
func (r * ShimReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
50
67
_ = 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
+ }
51
82
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
+ }
53
89
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
54
99
return ctrl.Result {}, nil
55
100
}
56
101
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
+
57
126
// SetupWithManager sets up the controller with the Manager.
58
127
func (r * ShimReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
59
128
return ctrl .NewControllerManagedBy (mgr ).
0 commit comments