Skip to content

Commit

Permalink
fix: Add missing KluctlDeployments to staticdata
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock committed Aug 23, 2023
1 parent dafcc4e commit b0e8cdb
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cmd/kluctl/commands/cmd_webui_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (cmd *webuiBuildCmd) Run(ctx context.Context) error {
collector := results.NewResultsCollector(ctx, stores)
collector.Start()

st := status.Start(ctx, "Collecting results")
st := status.Start(ctx, "Collecting summaries")
defer st.Failed()
err = collector.WaitForResults(time.Second, time.Second*30)
if err != nil {
Expand All @@ -42,5 +42,5 @@ func (cmd *webuiBuildCmd) Run(ctx context.Context) error {
st.Success()

sbw := webui.NewStaticWebuiBuilder(collector)
return sbw.Build(cmd.Path)
return sbw.Build(ctx, cmd.Path)
}
14 changes: 11 additions & 3 deletions pkg/results/result-store-secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func (s *ResultStoreSecrets) cleanupOrphanedResults() error {

deploymentsMap := map[result.KluctlDeploymentInfo]bool{}
for _, d := range deployments {
deploymentsMap[result.KluctlDeploymentInfo{Name: d.Name, Namespace: d.Namespace, ClusterId: s.clusterId}] = true
deploymentsMap[result.KluctlDeploymentInfo{Name: d.Deployment.Name, Namespace: d.Deployment.Namespace, ClusterId: s.clusterId}] = true
}

tryDeleteResult := func(secretKey client.ObjectKey, deployment *result.KluctlDeploymentInfo, id string, t string) {
Expand Down Expand Up @@ -785,13 +785,21 @@ func (s *ResultStoreSecrets) GetValidateResult(options GetValidateResultOptions)
return &vr, nil
}

func (s *ResultStoreSecrets) ListKluctlDeployments() ([]kluctlv1.KluctlDeployment, error) {
func (s *ResultStoreSecrets) ListKluctlDeployments() ([]WatchKluctlDeploymentEvent, error) {
var l kluctlv1.KluctlDeploymentList
err := s.cache.List(s.ctx, &l)
if err != nil {
return nil, err
}
return l.Items, nil
ret := make([]WatchKluctlDeploymentEvent, 0, len(l.Items))
for _, x := range l.Items {
x := x
ret = append(ret, WatchKluctlDeploymentEvent{
ClusterId: s.clusterId,
Deployment: &x,
})
}
return ret, nil
}

func (s *ResultStoreSecrets) WatchKluctlDeployments() (<-chan WatchKluctlDeploymentEvent, context.CancelFunc, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/results/result-store.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type ResultStore interface {
WatchValidateResultSummaries(options ListResultSummariesOptions) (<-chan WatchValidateResultSummaryEvent, context.CancelFunc, error)
GetValidateResult(options GetValidateResultOptions) (*result.ValidateResult, error)

ListKluctlDeployments() ([]kluctlv1.KluctlDeployment, error)
ListKluctlDeployments() ([]WatchKluctlDeploymentEvent, error)
WatchKluctlDeployments() (<-chan WatchKluctlDeploymentEvent, context.CancelFunc, error)
GetKluctlDeployment(clusterId string, name string, namespace string) (*kluctlv1.KluctlDeployment, error)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/results/results-collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,12 @@ func (rc *ResultsCollector) GetValidateResult(options GetValidateResultOptions)
return se.store.GetValidateResult(options)
}

func (rc *ResultsCollector) ListKluctlDeployments() ([]kluctlv1.KluctlDeployment, error) {
func (rc *ResultsCollector) ListKluctlDeployments() ([]WatchKluctlDeploymentEvent, error) {
rc.mutex.Lock()
defer rc.mutex.Unlock()
ret := make([]kluctlv1.KluctlDeployment, 0, len(rc.commandResultSummaries))
ret := make([]WatchKluctlDeploymentEvent, 0, len(rc.kluctlDeployments))
for _, x := range rc.kluctlDeployments {
ret = append(ret, *x.event.Deployment)
ret = append(ret, x.event)
}
return ret, nil
}
Expand Down
43 changes: 41 additions & 2 deletions pkg/webui/staticbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"
"fmt"
"github.com/kluctl/kluctl/v2/pkg/results"
"github.com/kluctl/kluctl/v2/pkg/status"
"github.com/kluctl/kluctl/v2/pkg/utils"
"github.com/kluctl/kluctl/v2/pkg/yaml"
cp "github.com/otiai10/copy"
"os"
"path/filepath"
"sync/atomic"
)

type StaticWebuiBuilder struct {
Expand All @@ -22,7 +24,7 @@ func NewStaticWebuiBuilder(store results.ResultStore) *StaticWebuiBuilder {
return ret
}

func (swb *StaticWebuiBuilder) Build(path string) error {
func (swb *StaticWebuiBuilder) Build(ctx context.Context, path string) error {
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return err
Expand All @@ -34,12 +36,24 @@ func (swb *StaticWebuiBuilder) Build(path string) error {
return err
}

kds, err := swb.store.ListKluctlDeployments()
if err != nil {
return err
}

err = os.MkdirAll(filepath.Join(tmpDir, "staticdata"), 0o700)
if err != nil {
return err
}

gh := utils.NewGoHelper(context.Background(), 8)
doneCnt := atomic.Int32{}
buildStatusStr := func() string {
return fmt.Sprintf("Retrieved %d of %d results", doneCnt.Load(), len(summaries))
}
st := status.Start(ctx, buildStatusStr())
defer st.Failed()

gh := utils.NewGoHelper(ctx, 4)
for _, rs := range summaries {
rs := rs
gh.RunE(func() error {
Expand Down Expand Up @@ -71,11 +85,19 @@ func (swb *StaticWebuiBuilder) Build(path string) error {
if err != nil {
return err
}
doneCnt.Add(1)
st.Update(buildStatusStr())
return nil
})
}
gh.Wait()

err = gh.ErrorOrNil()
if err != nil {
return err
}
st.Success()

j, err := yaml.WriteJsonString(summaries)
if err != nil {
return err
Expand All @@ -86,6 +108,23 @@ func (swb *StaticWebuiBuilder) Build(path string) error {
return err
}

var kds2 []map[string]any
for _, kd := range kds {
kds2 = append(kds2, map[string]any{
"clusterId": kd.ClusterId,
"deployment": kd.Deployment,
})
}
j, err = yaml.WriteJsonString(kds2)
if err != nil {
return err
}
j = `const staticKluctlDeployments=` + j
err = os.WriteFile(filepath.Join(tmpDir, "staticdata/kluctldeployments.js"), []byte(j), 0o600)
if err != nil {
return err
}

j, err = yaml.WriteJsonString(GetShortNames())
if err != nil {
return err
Expand Down
35 changes: 30 additions & 5 deletions pkg/webui/ui/src/api.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { AuthInfo, CommandResult, ObjectRef, ResultObject, ShortName, ValidateResult } from "./models";
import {
AuthInfo,
CommandResult, CommandResultSummary,
ObjectRef,
ResultObject,
ShortName,
ValidateResult
} from "./models";
import _ from "lodash";
import React from "react";
import { Box, Typography } from "@mui/material";
Expand All @@ -7,6 +14,7 @@ import "./staticbuild.d.ts"
import { loadScript } from "./loadscript";
import { GitRef } from "./models-static";
import { sleep } from "./utils/misc";
import { KluctlDeploymentWithClusterId } from "./components/App";

console.log(window.location)

Expand Down Expand Up @@ -299,16 +307,33 @@ export class StaticApi implements Api {

async listenEvents(filterProject: string | undefined, filterSubDir: string | undefined, handle: (msg: any) => void): Promise<() => void> {
await loadScript(staticPath + "/summaries.js")
await loadScript(staticPath + "/kluctldeployments.js")

staticSummaries.forEach(rs => {
if (filterProject && filterProject !== rs.project.normalizedGitUrl) {
staticKluctlDeployments.forEach(kd_ => {
const kd = kd_ as KluctlDeploymentWithClusterId
if (filterProject && filterProject !== kd.deployment.status?.projectKey?.gitRepoKey) {
return
}
if (filterSubDir && filterSubDir !== rs.project.subDir) {
if (filterSubDir && filterSubDir !== kd.deployment.status?.projectKey?.subDir) {
return
}
handle({
"type": "update_summary",
"type": "update_kluctl_deployment",
"deployment": kd.deployment,
"clusterId": kd.clusterId,
})
})

staticSummaries.forEach(rs_ => {
const rs = rs_ as CommandResultSummary
if (filterProject && filterProject !== rs.projectKey.gitRepoKey) {
return
}
if (filterSubDir && filterSubDir !== rs.projectKey.subDir) {
return
}
handle({
"type": "update_command_result_summary",
"summary": rs,
})
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/webui/ui/src/staticbuild.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare const staticResults: Map<string, any>;

declare const staticShortNames: any[];
declare const staticProjects: any[];
declare const staticKluctlDeployments: any[];
declare const staticSummaries: any[];

0 comments on commit b0e8cdb

Please sign in to comment.