Skip to content

Commit

Permalink
#1311. Built the initial SelectPackages route, passing init=?true loo…
Browse files Browse the repository at this point in the history
…ks only for init package in cwd.
  • Loading branch information
mike-winberry committed Apr 5, 2023
1 parent 3a92cf6 commit a7d1788
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 21 deletions.
21 changes: 15 additions & 6 deletions src/internal/api/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,36 @@ func Summary(w http.ResponseWriter, _ *http.Request) {
var distro string
var hasZarf bool
var host string
var k8sRevision string

c, err := cluster.NewClusterWithWait(5 * time.Second)
reachable = err == nil
if reachable {
distro, _ = c.Kube.DetectDistro()
host = getClusterName(c.Kube)
host = c.Kube.RestConfig.Host
state, _ = c.LoadZarfState()
hasZarf = state.Distro != ""
k8sRevision = getServerVersion(c.Kube)
}

data := types.ClusterSummary{
Reachable: reachable,
HasZarf: hasZarf,
Distro: distro,
ZarfState: state,
Host: host,
Reachable: reachable,
HasZarf: hasZarf,
Distro: distro,
ZarfState: state,
Host: host,
K8sRevision: k8sRevision,
}

common.WriteJSONResponse(w, data, http.StatusOK)
}

func getServerVersion(k *k8s.K8s) string {
info, _ := k.Clientset.DiscoveryClient.ServerVersion()

return info.String()
}

func getClusterName(k *k8s.K8s) string {

// Get all nodes
Expand Down
38 changes: 37 additions & 1 deletion src/internal/api/packages/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package packages

import (
"fmt"
"io/fs"
"net/http"
"os"
"path/filepath"
"regexp"

"github.com/defenseunicorns/zarf/src/internal/api/common"
Expand All @@ -31,7 +33,7 @@ func FindInHome(w http.ResponseWriter, _ *http.Request) {
findPackage(packagePattern, w, os.UserHomeDir)
}

// FindInitPackage returns all init packages anywhere down the directory tree of the working directory.
// FindInitPackage returns all init packages anywhere down the directory tree of the users home directory.
func FindInitPackage(w http.ResponseWriter, _ *http.Request) {
message.Debug("packages.FindInitPackage()")
findPackage(initPattern, w, os.Getwd)
Expand All @@ -52,3 +54,37 @@ func findPackage(pattern *regexp.Regexp, w http.ResponseWriter, setDir func() (s
}
common.WriteJSONResponse(w, files, http.StatusOK)
}

// RecursiveFileList walks a path with an optional regex pattern and returns a slice of file paths.
// Slow but actually crawls all directories ignoring any file/directory it doesn't have permission to open
func recursiveFileListThatWorks(dir string, pattern *regexp.Regexp) (files []string, err error) {
const dotcharater = 46
err = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
// Return errors
if err != nil {
// skip permission errors
if os.IsPermission(err) {
return nil
}
return err
}

// Skip hidden directories
if d.IsDir() && d.Name()[0] == dotcharater {
return filepath.SkipDir
}

if !d.IsDir() {
if pattern != nil {
if len(pattern.FindStringIndex(path)) > 0 {
files = append(files, path)
}
} else {
files = append(files, path)
}
}

return nil
})
return files, err
}
11 changes: 6 additions & 5 deletions src/types/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ type RestAPI struct {

// ClusterSummary contains the summary of a cluster for the API.
type ClusterSummary struct {
Reachable bool `json:"reachable"`
HasZarf bool `json:"hasZarf"`
Distro string `json:"distro"`
ZarfState ZarfState `json:"zarfState"`
Host string `json:"host"`
Reachable bool `json:"reachable"`
HasZarf bool `json:"hasZarf"`
Distro string `json:"distro"`
ZarfState ZarfState `json:"zarfState"`
Host string `json:"host"`
K8sRevision string `json:"k8sRevision"`
}

// APIZarfPackage represents a ZarfPackage and its path for the API.
Expand Down
12 changes: 7 additions & 5 deletions src/ui/lib/api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -815,11 +815,12 @@ export interface ZarfPackageVariable {
}

export interface ClusterSummary {
distro: string;
hasZarf: boolean;
host: string;
reachable: boolean;
zarfState: ZarfState;
distro: string;
hasZarf: boolean;
host: string;
k8sRevision: string;
reachable: boolean;
zarfState: ZarfState;
}

export interface ZarfState {
Expand Down Expand Up @@ -1325,6 +1326,7 @@ const typeMap: any = {
{ json: "distro", js: "distro", typ: "" },
{ json: "hasZarf", js: "hasZarf", typ: true },
{ json: "host", js: "host", typ: "" },
{ json: "k8sRevision", js: "k8sRevision", typ: "" },
{ json: "reachable", js: "reachable", typ: true },
{ json: "zarfState", js: "zarfState", typ: r("ZarfState") },
], false),
Expand Down
2 changes: 1 addition & 1 deletion src/ui/lib/components/cluster-info-table.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<Typography element="div" variant="caption" class="label-values" style="font-weight:500;">
<Typography variant="inherit">Ready?</Typography>
<Typography variant="inherit">User?</Typography>
<Typography variant="inherit">K8s version?</Typography>
<Typography variant="inherit">{$clusterStore.k8sRevision}</Typography>
</Typography>
</Box>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/ui/lib/components/connect-cluster-dialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
cancel
</Button>
<Button
href="/packages"
href="/packages?init=true"
variant="raised"
backgroundColor="grey-300"
textColor="text-primary-on-light"
Expand Down
163 changes: 163 additions & 0 deletions src/ui/lib/components/local-package-table.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<script lang="ts">
import { Packages } from '$lib/api';
import { Paper, Typography, Box, type SSX } from '@ui';
import Tooltip from './tooltip.svelte';
import ZarfChip from './zarf-chip.svelte';
import { page } from '$app/stores';
import type { APIZarfPackage } from '$lib/api-types';
const initPkg = $page.url.searchParams.get('init');
async function readPackages(): Promise<APIZarfPackage[]> {
const paths = initPkg ? await Packages.findInit() : await Packages.findInHome();
const packages = paths.map((p) => Packages.read(p));
return Promise.all(packages);
}
const ssx: SSX = {
$self: {
display: 'flex',
flexDirection: 'column',
maxHeight: '288px',
'& .local-package-list-header': {
height: '56px',
padding: '16px',
display: 'flex',
alignItems: 'center',
gap: '20px',
borderBottomLeftRadius: '0px',
borderBottomRightRadius: '0px',
'& .tooltip-trigger': {
display: 'flex',
alignItems: 'end',
color: 'var(--action-active-56p)',
},
},
'& .local-package-list-body': {
height: '100px',
boxShadow: '0px -1px 0px 0px rgba(255, 255, 255, 0.12) inset',
overflowX: 'hidden',
overflowY: 'scroll',
'& .no-packages': {
width: '100%',
height: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
},
},
'& .local-package-list-footer': {
height: '48px',
borderTopLeftRadius: '0px',
borderTopRightRadius: '0px',
},
'& .package-table-head-row': {
'& .package-table-td.name': {
paddingLeft: '48px',
},
},
'& .package-table-row': {
display: 'flex',
alignItems: 'center',
boxShadow: 'inset 0px -1px 0px rgba(255,255,255,0.12)',
'& .package-table-td': {
padding: '8px 16px',
'&.name': {
minWidth: '224px',
width: '20%',
display: 'flex',
alignItems: 'center',
gap: '10.67px',
flexWrap: 'wrap',
wordBreak: 'break-all',
},
'&.version': {
minWidth: '134px',
width: '12%',
},
'&.tags': {
minWidth: '276px',
width: '24.8%',
display: 'flex',
flexWrap: 'wrap',
gap: '4px',
},
'&.description': {
minWidth: '240px',
width: '21.5%',
},
},
},
},
};
const tableLabels = ['name', 'version', 'tags', 'description'];
$: initString = (initPkg && 'Init') || '';
</script>

<Box {ssx} class="local-package-list-container">
<Paper class="local-package-list-header" elevation={1}>
<Typography variant="th">Local Directory</Typography>
<Tooltip>Something Something Local Directory</Tooltip>
</Paper>
<Paper class="package-table-head-row package-table-row" square elevation={1}>
{#each tableLabels as l}
<Typography
class="package-table-td {l.split(' ').join('-')}"
variant="overline"
color="text-secondary-on-dark">{l}</Typography
>
{/each}
</Paper>
<Paper class="local-package-list-body" square elevation={1}>
{#await readPackages()}
<div class="no-packages">
<Typography color="primary" variant="body1"
>Searching for local Zarf{initString} Packages</Typography
>
</div>
{:then packages}
{#if !packages.length}
<div class="no-packages">
<Typography color="blue-200" variant="body1"
>No Zarf{initString} Packages found on local system</Typography
>
</div>
{:else}
{#each packages as pkg}
<Paper class="package-table-row" square elevation={1}>
<Typography variant="body2" class="package-table-td name" element="span">
<span class="material-symbols-outlined" style="color:var(--success);">
check_circle
</span>
<span>{pkg.zarfPackage.metadata?.name}</span>
</Typography>

<Typography variant="body2" class="package-table-td version">
<ZarfChip>
{pkg.zarfPackage.metadata?.version}
</ZarfChip>
</Typography>

<Typography variant="body2" class="package-table-td tags">
<ZarfChip>
{pkg.zarfPackage?.build?.architecture}
</ZarfChip>
<ZarfChip>
{pkg.zarfPackage.kind}
</ZarfChip>
</Typography>
<Typography variant="body2" class="package-table-td description">
{pkg.zarfPackage.metadata?.description}
</Typography>
</Paper>
{/each}
{/if}
{:catch err}
<div class="no-packages">
<Typography color="error" variant="body1">{err.message}</Typography>
</div>
{/await}
</Paper>
<Paper class="local-package-list-footer" elevation={1} />
</Box>
2 changes: 1 addition & 1 deletion src/ui/lib/components/package-table.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@

<Typography variant="body2" class="package-table-td version">
<ZarfChip>
{pkg.data?.build?.version}
{pkg.data.metadata?.version}
</ZarfChip>
</Typography>

Expand Down
6 changes: 6 additions & 0 deletions src/ui/lib/palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export const ZarfPalettes: Palettes = {
selected: {
onDark: 'rgba(255, 255, 255, 0.16)',
},
active: {
'56p': 'rgba(255, 255, 255, 0.56)',
},
},
chip: {
color: 'var(--on-surface)',
Expand Down Expand Up @@ -69,6 +72,9 @@ export const ZarfPalettes: Palettes = {
selected: {
onDark: 'rgba(255, 255, 255, 0.16)',
},
active: {
'56p': 'rgba(255, 255, 255, 0.56)',
},
},
chip: {
color: 'var(--on-surface)',
Expand Down
5 changes: 4 additions & 1 deletion src/ui/routes/packages/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<script lang="ts">
import LocalPackageTable from '$lib/components/local-package-table.svelte';
import { Typography } from '@ui';
</script>

<div>Packages</div>
<Typography variant="h5" color="on-background">Select Packages</Typography>
<LocalPackageTable />

0 comments on commit a7d1788

Please sign in to comment.