Skip to content

Commit

Permalink
instancer
Browse files Browse the repository at this point in the history
  • Loading branch information
argonaut0 committed Sep 29, 2023
1 parent d71ad3a commit 3a76fc6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ test:
rm -r tmp

build:
GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o ./out/instanced
GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o ./out/instanced

docker:
docker build . --tag us.gcr.io/maplectf/instanced:latest && docker push us.gcr.io/maplectf/instanced:latest
10 changes: 5 additions & 5 deletions src/instancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ func (in *Instancer) LoadCRDs() {
log := in.log
// Test CRDs
log.Debug().Msg("querying CRDs")
crdChallObjs, err := in.QueryInstancedChallenges("challenges")
var err error
in.challengeTmpls, err = in.QueryInstancedChallenges("challenges")
if err != nil {
log.Debug().Err(err).Msg("error retrieving challenge definitions from CRDs")
}
for k := range crdChallObjs {
for k := range in.challengeTmpls {
log.Info().Str("challenge", k).Msg("parsed challenge template")
}
log.Info().Int("count", len(crdChallObjs)).Msg("parsed challenges")
in.challengeTmpls = crdChallObjs
log.Info().Int("count", len(in.challengeTmpls)).Msg("parsed challenges")
}

func (in *Instancer) DestoryExpiredInstances() {
Expand Down Expand Up @@ -222,7 +222,7 @@ func (in *Instancer) GetTeamChallengeStates(teamID string) ([]InstanceRecord, er
return nil, err
}
//for k := range in.challengeObjs {
for k := range in.config.Challenges {
for k := range in.challengeTmpls {
active := false
for _, v := range instances {
if v.Challenge == k {
Expand Down
5 changes: 5 additions & 0 deletions src/k8sclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ func (in *Instancer) QueryInstancedChallenges(namespace string) (map[string]*tem
ret := make(map[string]*template.Template)

for _, c := range chalList.Items {
hidden, found, err := unstructured.NestedBool(c.Object, "spec", "hidden")
if err != nil || found && hidden {
in.log.Info().Err(err).Str("challenge", c.GetName()).Msg("skipping hidden challenge")
continue
}
tmplStr, found, err := unstructured.NestedString(c.Object, "spec", "challengeTemplate")
if err != nil || !found {
fmt.Printf("template not found for challenge crd %v: error=%v", c.GetName(), err)
Expand Down
7 changes: 7 additions & 0 deletions src/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func (in *Instancer) registerEndpoints() {
in.echo.DELETE("/instances", in.handleInstanceDelete)

in.echo.GET("/challenges", in.handleInstanceListTeam)

in.echo.POST("/reload", in.handleCRDReload)
}

type InstancesResponse struct {
Expand Down Expand Up @@ -133,3 +135,8 @@ func (in *Instancer) handleInstanceListTeam(c echo.Context) error {
// todo: properly marshal records
return c.JSON(http.StatusOK, records)
}

func (in *Instancer) handleCRDReload(c echo.Context) error {
go in.LoadCRDs()
return c.JSON(http.StatusAccepted, "accepted")
}

0 comments on commit 3a76fc6

Please sign in to comment.