add redis queue to install helmcharts in background
This commit is contained in:
Submodule application updated: 1fd625e22d...282c3e52d0
6
go.mod
6
go.mod
@@ -6,12 +6,15 @@ require (
|
||||
github.com/argoproj/argo-cd/v2 v2.13.3
|
||||
github.com/golang-jwt/jwt/v4 v4.5.1
|
||||
github.com/gorilla/mux v1.7.3
|
||||
github.com/hibiken/asynq v0.25.1
|
||||
github.com/rs/cors v1.11.0
|
||||
golang.org/x/crypto v0.32.0
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
k8s.io/api v0.31.0
|
||||
k8s.io/apimachinery v0.31.0
|
||||
k8s.io/client-go v0.31.0
|
||||
k8s.io/kubectl v0.31.2
|
||||
sigs.k8s.io/yaml v1.4.0
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -59,6 +62,7 @@ require (
|
||||
github.com/redis/go-redis/v9 v9.7.0 // indirect
|
||||
github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||
github.com/sirupsen/logrus v1.9.3 // indirect
|
||||
github.com/spf13/cast v1.7.0 // indirect
|
||||
github.com/spf13/cobra v1.8.1 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/x448/float16 v0.8.4 // indirect
|
||||
@@ -87,7 +91,6 @@ require (
|
||||
google.golang.org/grpc v1.69.4 // indirect
|
||||
google.golang.org/protobuf v1.36.3 // indirect
|
||||
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
k8s.io/apiextensions-apiserver v0.31.2 // indirect
|
||||
k8s.io/apiserver v0.31.0 // indirect
|
||||
@@ -96,7 +99,6 @@ require (
|
||||
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
|
||||
oras.land/oras-go/v2 v2.5.0 // indirect
|
||||
sigs.k8s.io/structured-merge-diff/v4 v4.4.4-0.20241211184406-7bf59b3d70ee // indirect
|
||||
sigs.k8s.io/yaml v1.4.0 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
6
go.sum
6
go.sum
@@ -112,6 +112,8 @@ github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4Nij
|
||||
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
|
||||
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
|
||||
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
||||
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
|
||||
@@ -243,6 +245,8 @@ github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB1
|
||||
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
|
||||
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
|
||||
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
|
||||
github.com/hibiken/asynq v0.25.1 h1:phj028N0nm15n8O2ims+IvJ2gz4k2auvermngh9JhTw=
|
||||
github.com/hibiken/asynq v0.25.1/go.mod h1:pazWNOLBu0FEynQRBvHA26qdIKRSmfdIfUm4HdsLmXg=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20240312041847-bd984b5ce465/go.mod h1:gx7rwoVhcfuVKG5uya9Hs3Sxj7EIvldVofAWIUtGouw=
|
||||
@@ -428,6 +432,8 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
|
||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/skeema/knownhosts v1.3.0 h1:AM+y0rI04VksttfwjkSTNQorvGqmwATnvnAHpSgc0LY=
|
||||
github.com/skeema/knownhosts v1.3.0/go.mod h1:sPINvnADmT/qYH1kfv+ePMmOBTH6Tbl7b5LvTDjFK7M=
|
||||
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
|
||||
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
|
||||
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
|
||||
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
|
||||
|
||||
54
jobs/helminstall.go
Normal file
54
jobs/helminstall.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
const (
|
||||
TypeInstallChart = "chart:install"
|
||||
)
|
||||
|
||||
type InstallChartPayload struct {
|
||||
ChartName string
|
||||
Version string
|
||||
Namespace string
|
||||
UserID string
|
||||
}
|
||||
|
||||
func NewInstallCahrtTask(chartname, version, ns, userID string) *asynq.Task {
|
||||
payload, _ := json.Marshal(InstallChartPayload{
|
||||
ChartName: chartname,
|
||||
Version: version,
|
||||
Namespace: ns,
|
||||
UserID: userID,
|
||||
})
|
||||
|
||||
return asynq.NewTask(TypeInstallChart, payload)
|
||||
}
|
||||
|
||||
func HandleInstallCahrt(ctx context.Context, t *asynq.Task) error {
|
||||
var payload InstallChartPayload
|
||||
if err := json.Unmarshal(t.Payload(), &payload); err != nil {
|
||||
return fmt.Errorf("Faild to parse payload: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("[Job] Installing chart %s in namespace %s", payload.ChartName, payload.Namespace)
|
||||
log.Printf("[Job] Validating Chart ... ")
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
log.Printf("[Job] Creating resources ...")
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
log.Printf("[Job] Wating for pods ...")
|
||||
time.Sleep(2 * time.Second)
|
||||
log.Printf("[Job] Finilizing installation ...")
|
||||
log.Printf("[Job] chart %s installed successfully", payload.ChartName)
|
||||
return nil
|
||||
|
||||
}
|
||||
21
main.go
21
main.go
@@ -8,12 +8,14 @@ import (
|
||||
|
||||
"main/db"
|
||||
"main/handler"
|
||||
"main/jobs"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/rs/cors"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
@@ -121,8 +123,25 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func main() {
|
||||
|
||||
db.InitDB()
|
||||
router := mux.NewRouter()
|
||||
var redisClient = asynq.NewClient(asynq.RedisClientOpt{Addr: "130.185.77.247:30828", Password: "xwy8ahx46F"})
|
||||
chart := jobs.InstallChartPayload{
|
||||
ChartName: "redis",
|
||||
Version: "2.1.0",
|
||||
Namespace: "default",
|
||||
UserID: "razaghi",
|
||||
}
|
||||
if _, err := json.Marshal(chart); err != nil {
|
||||
fmt.Printf("Could not json ")
|
||||
}
|
||||
task := jobs.NewInstallCahrtTask(chart.ChartName, chart.Namespace, chart.Namespace, chart.UserID)
|
||||
info, err := redisClient.Enqueue(task)
|
||||
if err != nil {
|
||||
fmt.Printf("Error in connecting redis")
|
||||
}
|
||||
fmt.Printf("This is issued task %v", info.ID)
|
||||
go startWorker()
|
||||
|
||||
router := mux.NewRouter()
|
||||
router.HandleFunc("/register", registerHnadler)
|
||||
router.HandleFunc("/login", loginHandler)
|
||||
router.HandleFunc("/createcluster", handler.CreateClusterHandler)
|
||||
|
||||
22
worker.go
Normal file
22
worker.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"main/jobs"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
func startWorker() {
|
||||
srv := asynq.NewServer(
|
||||
asynq.RedisClientOpt{Addr: "130.185.77.247:30828", Password: "xwy8ahx46F"},
|
||||
asynq.Config{Concurrency: 5},
|
||||
)
|
||||
|
||||
mux := asynq.NewServeMux()
|
||||
mux.HandleFunc(jobs.TypeInstallChart, jobs.HandleInstallCahrt)
|
||||
|
||||
if err := srv.Run(mux); err != nil {
|
||||
log.Fatalf("Could not run worker: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user