Skip to content

Commit

Permalink
feat: cool syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
yyewolf committed Oct 24, 2024
1 parent 9c5029d commit d1eaf14
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
48 changes: 30 additions & 18 deletions internal/controllers/k8s_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package controllers

import (
"context"
"encoding/json"
"fmt"
v1 "instancer/api/v1"
"instancer/internal/ctf"
"strconv"
Expand Down Expand Up @@ -66,6 +68,8 @@ func (r *InstancierReconciler) Reconcile(ctx context.Context, req ctrl.Request)
r.Reinit()
}

fmt.Println(req.Name)

res, err := r.Register(ctx, req)
if err != nil {
return res, err
Expand All @@ -79,11 +83,8 @@ func (r *InstancierReconciler) Register(ctx context.Context, req ctrl.Request) (
var challenge v1.Challenge
err := r.Get(ctx, req.NamespacedName, &challenge)
if err == nil {
if challenge.DeletionTimestamp != nil {
r.UnregisterChallenge(&challenge)
return ctrl.Result{}, nil
}
challenge.Status.Phase = "Syncing"
r.Status().Update(context.Background(), &challenge)
r.RegisterChallenge(&challenge)
return ctrl.Result{}, nil
}
Expand All @@ -92,35 +93,26 @@ func (r *InstancierReconciler) Register(ctx context.Context, req ctrl.Request) (
var instancedChallenge v1.InstancedChallenge
err = r.Get(ctx, req.NamespacedName, &instancedChallenge)
if err == nil {
if instancedChallenge.DeletionTimestamp != nil {
r.UnregisterChallenge(&instancedChallenge)
return ctrl.Result{}, nil
}
instancedChallenge.Status.Phase = "Syncing"
r.Status().Update(context.Background(), &instancedChallenge)
r.RegisterChallenge(&instancedChallenge)
return ctrl.Result{}, nil
}

var oracleInstancedChallenge v1.OracleInstancedChallenge
err = r.Get(ctx, req.NamespacedName, &oracleInstancedChallenge)
if err == nil {
if oracleInstancedChallenge.DeletionTimestamp != nil {
r.UnregisterChallenge(&oracleInstancedChallenge)
return ctrl.Result{}, nil
}
oracleInstancedChallenge.Status.Phase = "Syncing"
r.Status().Update(context.Background(), &oracleInstancedChallenge)
r.RegisterChallenge(&oracleInstancedChallenge)
return ctrl.Result{}, nil
}

var globallyInstancedChallenge v1.GloballyInstancedChallenge
err = r.Get(ctx, req.NamespacedName, &globallyInstancedChallenge)
if err == nil {
if globallyInstancedChallenge.DeletionTimestamp != nil {
r.UnregisterChallenge(&globallyInstancedChallenge)
return ctrl.Result{}, nil
}
globallyInstancedChallenge.Status.Phase = "Syncing"
r.Status().Update(context.Background(), &globallyInstancedChallenge)
r.RegisterChallenge(&globallyInstancedChallenge)
return ctrl.Result{}, nil
}
Expand Down Expand Up @@ -329,8 +321,28 @@ func predicates() predicate.Predicate {
GenericFunc: func(genericEvent event.GenericEvent) bool {
return false
},
UpdateFunc: func(updateEvent event.UpdateEvent) bool {
return true
UpdateFunc: func(e event.UpdateEvent) bool {
var oldSpec, newSpec []byte

switch newObject := e.ObjectNew.(type) {
case *v1.Challenge:
oldObject := e.ObjectOld.(*v1.Challenge)
oldSpec, _ = json.Marshal(oldObject.Spec)
newSpec, _ = json.Marshal(newObject.Spec)
case *v1.InstancedChallenge:
oldObject := e.ObjectOld.(*v1.InstancedChallenge)
oldSpec, _ = json.Marshal(oldObject.Spec)
newSpec, _ = json.Marshal(newObject.Spec)
case *v1.GloballyInstancedChallenge:
oldObject := e.ObjectOld.(*v1.GloballyInstancedChallenge)
oldSpec, _ = json.Marshal(oldObject.Spec)
newSpec, _ = json.Marshal(newObject.Spec)
case *v1.OracleInstancedChallenge:
oldObject := e.ObjectOld.(*v1.OracleInstancedChallenge)
oldSpec, _ = json.Marshal(oldObject.Spec)
newSpec, _ = json.Marshal(newObject.Spec)
}
return string(oldSpec) != string(newSpec)
},
}
}
2 changes: 1 addition & 1 deletion internal/files/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func GetFiles(repo string, files []string) ([][]byte, error) {

file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to get file %s: %w", fileName, err)
return nil, fmt.Errorf("failed to get file %s from git", fileName)
}

b, err := io.ReadAll(file)
Expand Down

0 comments on commit d1eaf14

Please sign in to comment.