|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" |
| 8 | + appsv1 "k8s.io/api/apps/v1" |
| 9 | + "k8s.io/apimachinery/pkg/runtime" |
| 10 | + ctrl "sigs.k8s.io/controller-runtime" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 13 | +) |
| 14 | + |
| 15 | +type CozystackResourceDefinitionReconciler struct { |
| 16 | + client.Client |
| 17 | + Scheme *runtime.Scheme |
| 18 | +} |
| 19 | + |
| 20 | +func (r *CozystackResourceDefinitionReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 21 | + log := log.FromContext(ctx) |
| 22 | + |
| 23 | + deploy := &appsv1.Deployment{} |
| 24 | + err := r.Get(ctx, client.ObjectKey{ |
| 25 | + Namespace: "cozy-system", |
| 26 | + Name: "cozystack-api", |
| 27 | + }, deploy) |
| 28 | + if err != nil { |
| 29 | + log.Error(err, "cant get Deployment cozy-system/cozystack-api") |
| 30 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 31 | + } |
| 32 | + |
| 33 | + patch := client.MergeFrom(deploy.DeepCopy()) |
| 34 | + if deploy.Spec.Template.Annotations == nil { |
| 35 | + deploy.Spec.Template.Annotations = make(map[string]string) |
| 36 | + } |
| 37 | + deploy.Spec.Template.Annotations["kubectl.kubernetes.io/restartedAt"] = time.Now().Format(time.RFC3339) |
| 38 | + if err := r.Patch(ctx, deploy, patch); err != nil { |
| 39 | + log.Error(err, "cannot patch Deployment annotation") |
| 40 | + return ctrl.Result{}, err |
| 41 | + } |
| 42 | + |
| 43 | + log.Info("Deployment cozy-system/cozystack-api restarted") |
| 44 | + return ctrl.Result{}, nil |
| 45 | +} |
| 46 | + |
| 47 | +func (r *CozystackResourceDefinitionReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 48 | + return ctrl.NewControllerManagedBy(mgr). |
| 49 | + For(&cozyv1alpha1.CozystackResourceDefinition{}). |
| 50 | + Complete(r) |
| 51 | +} |
0 commit comments