-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: impl mutator using KRM KCL spec.
- Loading branch information
Showing
8 changed files
with
1,304 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,4 @@ go.work | |
/vendor | ||
.tools | ||
.gopath | ||
.DS_store |
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,55 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"html" | ||
"io" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
m "kcl-lang.io/kcl-operator/pkg/mutation" | ||
) | ||
|
||
func handleRoot(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintf(w, "hello %q", html.EscapeString(r.URL.Path)) | ||
} | ||
|
||
func handleMutate(w http.ResponseWriter, r *http.Request) { | ||
// read the body / request | ||
body, err := io.ReadAll(r.Body) | ||
defer r.Body.Close() | ||
if err != nil { | ||
log.Println(err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
fmt.Fprintf(w, "%s", err) | ||
} | ||
|
||
// mutate the request | ||
mutator := &m.Mutator{} | ||
mutated, err := mutator.Mutate(body, true) | ||
if err != nil { | ||
log.Println(err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
fmt.Fprintf(w, "%s", err) | ||
} | ||
|
||
// and write it back | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(mutated) | ||
} | ||
|
||
func main() { | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", handleRoot) | ||
mux.HandleFunc("/mutate", handleMutate) | ||
|
||
s := &http.Server{ | ||
Addr: ":8443", | ||
Handler: mux, | ||
ReadTimeout: 10 * time.Second, | ||
WriteTimeout: 10 * time.Second, | ||
MaxHeaderBytes: 1 << 20, // 1048576 | ||
} | ||
log.Fatal(s.ListenAndServeTLS("./ssl/mutateme.pem", "./ssl/mutateme.key")) | ||
} |
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 |
---|---|---|
|
@@ -19,10 +19,11 @@ package controllers | |
import ( | ||
"context" | ||
|
||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
|
||
krmkcldevv1alpha1 "kcl-lang.io/kcl-operator/api/kclrun/v1alpha1" | ||
) | ||
|
@@ -47,16 +48,30 @@ type KCLRunReconciler struct { | |
// For more details, check Reconcile and its Result here: | ||
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile | ||
func (r *KCLRunReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
_ = log.FromContext(ctx) | ||
|
||
// TODO(user): your logic here | ||
|
||
log := ctrl.Log.WithValues("kclrun", req.NamespacedName) | ||
log.Info("KCLRun resource has changed.") | ||
latest := &krmkcldevv1alpha1.KCLRun{} | ||
err := r.Get(ctx, req.NamespacedName, latest) | ||
if err != nil { | ||
if errors.IsNotFound(err) { | ||
log.Info("KCLRun resource not found. Ignoring since object must be deleted") | ||
return ctrl.Result{}, nil | ||
} | ||
log.Error(err, "Failed to get KCLRun") | ||
return ctrl.Result{}, err | ||
} | ||
err = r.Status().Update(ctx, latest) | ||
if err != nil { | ||
log.Error(err, "Failed to update KCLRun status") | ||
return ctrl.Result{}, err | ||
} | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *KCLRunReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&krmkcldevv1alpha1.KCLRun{}). | ||
WithOptions(controller.Options{MaxConcurrentReconciles: 2}). | ||
Complete(r) | ||
} |
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
Oops, something went wrong.