mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 05:19:39 +02:00
Revert "feat(helm): filter on chart versions at API level [R8S-324]" (#753)
This commit is contained in:
parent
a80b185e10
commit
32ef208278
6 changed files with 51 additions and 153 deletions
|
@ -7,7 +7,6 @@ import (
|
|||
|
||||
"github.com/portainer/portainer/pkg/libhelm/options"
|
||||
httperror "github.com/portainer/portainer/pkg/libhttp/error"
|
||||
"github.com/portainer/portainer/pkg/libhttp/request"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
@ -33,18 +32,13 @@ func (handler *Handler) helmRepoSearch(w http.ResponseWriter, r *http.Request) *
|
|||
return httperror.BadRequest("Bad request", errors.New("missing `repo` query parameter"))
|
||||
}
|
||||
|
||||
chart, _ := request.RetrieveQueryParameter(r, "chart", false)
|
||||
useCache, _ := request.RetrieveBooleanQueryParameter(r, "useCache", false)
|
||||
|
||||
_, err := url.ParseRequestURI(repo)
|
||||
if err != nil {
|
||||
return httperror.BadRequest("Bad request", errors.Wrap(err, fmt.Sprintf("provided URL %q is not valid", repo)))
|
||||
}
|
||||
|
||||
searchOpts := options.SearchRepoOptions{
|
||||
Repo: repo,
|
||||
Chart: chart,
|
||||
UseCache: useCache,
|
||||
Repo: repo,
|
||||
}
|
||||
|
||||
result, err := handler.helmPackageManager.SearchRepo(searchOpts)
|
||||
|
|
|
@ -33,8 +33,6 @@ vi.mock('../queries/useHelmRepositories', () => ({
|
|||
],
|
||||
isInitialLoading: false,
|
||||
isError: false,
|
||||
isFetching: false,
|
||||
refetch: vi.fn(() => Promise.resolve([])),
|
||||
})),
|
||||
useHelmRepositories: vi.fn(() => ({
|
||||
data: ['repo1', 'repo2'],
|
||||
|
@ -83,8 +81,6 @@ describe('UpgradeButton', () => {
|
|||
data,
|
||||
isInitialLoading: false,
|
||||
isError: false,
|
||||
isFetching: false,
|
||||
refetch: vi.fn(() => Promise.resolve([])),
|
||||
});
|
||||
|
||||
renderButton();
|
||||
|
@ -98,8 +94,6 @@ describe('UpgradeButton', () => {
|
|||
data: [],
|
||||
isInitialLoading: true,
|
||||
isError: false,
|
||||
isFetching: false,
|
||||
refetch: vi.fn(() => Promise.resolve([])),
|
||||
});
|
||||
|
||||
renderButton();
|
||||
|
@ -115,8 +109,6 @@ describe('UpgradeButton', () => {
|
|||
data,
|
||||
isInitialLoading: false,
|
||||
isError: false,
|
||||
isFetching: false,
|
||||
refetch: vi.fn(() => Promise.resolve([])),
|
||||
});
|
||||
|
||||
renderButton();
|
||||
|
@ -147,8 +139,6 @@ describe('UpgradeButton', () => {
|
|||
],
|
||||
isInitialLoading: false,
|
||||
isError: false,
|
||||
isFetching: false,
|
||||
refetch: vi.fn(() => Promise.resolve([])),
|
||||
});
|
||||
|
||||
renderButton({ release: mockRelease });
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import { ArrowUp } from 'lucide-react';
|
||||
import { useRouter } from '@uirouter/react';
|
||||
import { useState } from 'react';
|
||||
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
import { notifySuccess } from '@/portainer/services/notifications';
|
||||
|
@ -42,19 +41,16 @@ export function UpgradeButton({
|
|||
const updateHelmReleaseMutation = useUpdateHelmReleaseMutation(environmentId);
|
||||
|
||||
const repositoriesQuery = useHelmRepositories();
|
||||
const [useCache, setUseCache] = useState(true);
|
||||
const helmRepoVersionsQuery = useHelmRepoVersions(
|
||||
release?.chart.metadata?.name || '',
|
||||
60 * 60 * 1000, // 1 hour
|
||||
repositoriesQuery.data,
|
||||
useCache
|
||||
repositoriesQuery.data
|
||||
);
|
||||
const versions = helmRepoVersionsQuery.data;
|
||||
|
||||
// Combined loading state
|
||||
const isInitialLoading =
|
||||
repositoriesQuery.isInitialLoading ||
|
||||
helmRepoVersionsQuery.isFetching ||
|
||||
helmRepoVersionsQuery.isInitialLoading;
|
||||
const isError = repositoriesQuery.isError || helmRepoVersionsQuery.isError;
|
||||
|
||||
|
@ -62,10 +58,9 @@ export function UpgradeButton({
|
|||
select: (data) => data.chart.metadata?.version,
|
||||
});
|
||||
const latestVersionAvailable = versions[0]?.Version ?? '';
|
||||
const isNewVersionAvailable = Boolean(
|
||||
const isNewVersionAvailable =
|
||||
latestVersion?.data &&
|
||||
semverCompare(latestVersionAvailable, latestVersion?.data) === 1
|
||||
);
|
||||
semverCompare(latestVersionAvailable, latestVersion?.data) === 1;
|
||||
|
||||
const editableHelmRelease: UpdateHelmReleasePayload = {
|
||||
name: releaseName,
|
||||
|
@ -75,14 +70,6 @@ export function UpgradeButton({
|
|||
version: release?.chart.metadata?.version,
|
||||
};
|
||||
|
||||
function handleRefreshVersions() {
|
||||
if (useCache === false) {
|
||||
helmRepoVersionsQuery.refetch();
|
||||
} else {
|
||||
setUseCache(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="relative">
|
||||
<LoadingButton
|
||||
|
@ -110,38 +97,30 @@ export function UpgradeButton({
|
|||
Checking for new versions...
|
||||
</InlineLoader>
|
||||
)}
|
||||
{!isInitialLoading && !isError && (
|
||||
{versions.length === 0 && !isInitialLoading && !isError && (
|
||||
<span className="absolute flex items-center -bottom-5 left-0 right-0 text-xs text-muted text-center whitespace-nowrap">
|
||||
{getStatusMessage(
|
||||
versions.length === 0,
|
||||
latestVersionAvailable,
|
||||
isNewVersionAvailable
|
||||
)}
|
||||
{versions.length === 0 && (
|
||||
<Tooltip
|
||||
message={
|
||||
<div>
|
||||
Portainer is unable to find any versions for this chart in the
|
||||
repositories saved. Try adding a new repository which contains
|
||||
the chart in the{' '}
|
||||
<Link
|
||||
to="portainer.account"
|
||||
params={{ '#': 'helm-repositories' }}
|
||||
data-cy="user-settings-link"
|
||||
>
|
||||
Helm repositories settings
|
||||
</Link>
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
)}
|
||||
<button
|
||||
onClick={handleRefreshVersions}
|
||||
className="text-primary hover:text-primary-light cursor-pointer bg-transparent border-0 pl-1 p-0"
|
||||
type="button"
|
||||
>
|
||||
Refresh versions
|
||||
</button>
|
||||
No versions available
|
||||
<Tooltip
|
||||
message={
|
||||
<div>
|
||||
Portainer is unable to find any versions for this chart in the
|
||||
repositories saved. Try adding a new repository which contains
|
||||
the chart in the{' '}
|
||||
<Link
|
||||
to="portainer.account"
|
||||
params={{ '#': 'helm-repositories' }}
|
||||
data-cy="user-settings-link"
|
||||
>
|
||||
Helm repositories settings
|
||||
</Link>
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
</span>
|
||||
)}
|
||||
{isNewVersionAvailable && (
|
||||
<span className="absolute -bottom-5 left-0 right-0 text-xs text-muted text-center whitespace-nowrap">
|
||||
New version available ({latestVersionAvailable})
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
@ -185,18 +164,4 @@ export function UpgradeButton({
|
|||
},
|
||||
});
|
||||
}
|
||||
|
||||
function getStatusMessage(
|
||||
hasNoAvailableVersions: boolean,
|
||||
latestVersionAvailable: string,
|
||||
isNewVersionAvailable: boolean
|
||||
): string {
|
||||
if (hasNoAvailableVersions) {
|
||||
return 'No versions available ';
|
||||
}
|
||||
if (isNewVersionAvailable) {
|
||||
return `New version available (${latestVersionAvailable}) `;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,21 +45,20 @@ export function useHelmRepositories() {
|
|||
export function useHelmRepoVersions(
|
||||
chart: string,
|
||||
staleTime: number,
|
||||
repositories: string[] = [],
|
||||
useCache: boolean = true
|
||||
repositories: string[] = []
|
||||
) {
|
||||
// Fetch versions from each repository in parallel as separate queries
|
||||
const versionQueries = useQueries({
|
||||
queries: useMemo(
|
||||
() =>
|
||||
repositories.map((repo) => ({
|
||||
queryKey: ['helm', 'repositories', chart, repo, useCache],
|
||||
queryFn: () => getSearchHelmRepo(repo, chart, useCache),
|
||||
queryKey: ['helm', 'repositories', chart, repo],
|
||||
queryFn: () => getSearchHelmRepo(repo, chart),
|
||||
enabled: !!chart && repositories.length > 0,
|
||||
staleTime,
|
||||
...withGlobalError(`Unable to retrieve versions from ${repo}`),
|
||||
})),
|
||||
[repositories, chart, staleTime, useCache]
|
||||
[repositories, chart, staleTime]
|
||||
),
|
||||
});
|
||||
|
||||
|
@ -73,8 +72,6 @@ export function useHelmRepoVersions(
|
|||
data: allVersions,
|
||||
isInitialLoading: versionQueries.some((q) => q.isLoading),
|
||||
isError: versionQueries.some((q) => q.isError),
|
||||
isFetching: versionQueries.some((q) => q.isFetching),
|
||||
refetch: () => Promise.all(versionQueries.map((q) => q.refetch())),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -83,12 +80,11 @@ export function useHelmRepoVersions(
|
|||
*/
|
||||
async function getSearchHelmRepo(
|
||||
repo: string,
|
||||
chart: string,
|
||||
useCache: boolean = true
|
||||
chart: string
|
||||
): Promise<ChartVersion[]> {
|
||||
try {
|
||||
const { data } = await axios.get<HelmSearch>(`templates/helm`, {
|
||||
params: { repo, chart, useCache },
|
||||
params: { repo, chart },
|
||||
});
|
||||
const versions = data.entries[chart];
|
||||
return (
|
||||
|
|
|
@ -3,8 +3,6 @@ package options
|
|||
import "net/http"
|
||||
|
||||
type SearchRepoOptions struct {
|
||||
Repo string `example:"https://charts.gitlab.io/"`
|
||||
Client *http.Client `example:"&http.Client{Timeout: time.Second * 10}"`
|
||||
Chart string `example:"my-chart"`
|
||||
UseCache bool `example:"false"`
|
||||
Repo string `example:"https://charts.gitlab.io/"`
|
||||
Client *http.Client `example:"&http.Client{Timeout: time.Second * 10}"`
|
||||
}
|
||||
|
|
|
@ -4,8 +4,6 @@ import (
|
|||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/portainer/portainer/pkg/libhelm/options"
|
||||
|
@ -27,17 +25,6 @@ type RepoIndex struct {
|
|||
Generated string `json:"generated"`
|
||||
}
|
||||
|
||||
type RepoIndexCache struct {
|
||||
Index *repo.IndexFile
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
var (
|
||||
indexCache = make(map[string]RepoIndexCache)
|
||||
cacheMutex sync.RWMutex
|
||||
cacheDuration = 60 * time.Minute
|
||||
)
|
||||
|
||||
// SearchRepo downloads the `index.yaml` file for specified repo, parses it and returns JSON to caller.
|
||||
func (hspm *HelmSDKPackageManager) SearchRepo(searchRepoOpts options.SearchRepoOptions) ([]byte, error) {
|
||||
// Validate input options
|
||||
|
@ -66,18 +53,6 @@ func (hspm *HelmSDKPackageManager) SearchRepo(searchRepoOpts options.SearchRepoO
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// Check cache first
|
||||
if searchRepoOpts.UseCache {
|
||||
cacheMutex.RLock()
|
||||
if cached, exists := indexCache[repoURL.String()]; exists {
|
||||
if time.Since(cached.Timestamp) < cacheDuration {
|
||||
cacheMutex.RUnlock()
|
||||
return convertAndMarshalIndex(cached.Index, searchRepoOpts.Chart)
|
||||
}
|
||||
}
|
||||
cacheMutex.RUnlock()
|
||||
}
|
||||
|
||||
// Set up Helm CLI environment
|
||||
repoSettings := cli.New()
|
||||
|
||||
|
@ -117,21 +92,23 @@ func (hspm *HelmSDKPackageManager) SearchRepo(searchRepoOpts options.SearchRepoO
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// Update cache and remove old entries
|
||||
cacheMutex.Lock()
|
||||
indexCache[searchRepoOpts.Repo] = RepoIndexCache{
|
||||
Index: indexFile,
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
for key, index := range indexCache {
|
||||
if time.Since(index.Timestamp) > cacheDuration {
|
||||
delete(indexCache, key)
|
||||
}
|
||||
// Convert the index file to our response format
|
||||
result, err := convertIndexToResponse(indexFile)
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Str("context", "HelmClient").
|
||||
Err(err).
|
||||
Msg("Failed to convert index to response format")
|
||||
return nil, errors.Wrap(err, "failed to convert index to response format")
|
||||
}
|
||||
|
||||
cacheMutex.Unlock()
|
||||
log.Debug().
|
||||
Str("context", "HelmClient").
|
||||
Str("repo", searchRepoOpts.Repo).
|
||||
Int("entries_count", len(indexFile.Entries)).
|
||||
Msg("Successfully searched repository")
|
||||
|
||||
return convertAndMarshalIndex(indexFile, searchRepoOpts.Chart)
|
||||
return json.Marshal(result)
|
||||
}
|
||||
|
||||
// validateSearchRepoOptions validates the required search repository options.
|
||||
|
@ -239,7 +216,7 @@ func loadIndexFile(indexPath string) (*repo.IndexFile, error) {
|
|||
}
|
||||
|
||||
// convertIndexToResponse converts the Helm index file to our response format.
|
||||
func convertIndexToResponse(indexFile *repo.IndexFile, chartName string) (RepoIndex, error) {
|
||||
func convertIndexToResponse(indexFile *repo.IndexFile) (RepoIndex, error) {
|
||||
result := RepoIndex{
|
||||
APIVersion: indexFile.APIVersion,
|
||||
Entries: make(map[string][]ChartInfo),
|
||||
|
@ -248,9 +225,7 @@ func convertIndexToResponse(indexFile *repo.IndexFile, chartName string) (RepoIn
|
|||
|
||||
// Convert Helm SDK types to our response types
|
||||
for name, charts := range indexFile.Entries {
|
||||
if chartName == "" || name == chartName {
|
||||
result.Entries[name] = convertChartsToChartInfo(charts)
|
||||
}
|
||||
result.Entries[name] = convertChartsToChartInfo(charts)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
@ -374,23 +349,3 @@ func ensureHelmDirectoriesExist(settings *cli.EnvSettings) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func convertAndMarshalIndex(indexFile *repo.IndexFile, chartName string) ([]byte, error) {
|
||||
// Convert the index file to our response format
|
||||
result, err := convertIndexToResponse(indexFile, chartName)
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Str("context", "HelmClient").
|
||||
Err(err).
|
||||
Msg("Failed to convert index to response format")
|
||||
return nil, errors.Wrap(err, "failed to convert index to response format")
|
||||
}
|
||||
|
||||
log.Debug().
|
||||
Str("context", "HelmClient").
|
||||
Str("repo", chartName).
|
||||
Int("entries_count", len(indexFile.Entries)).
|
||||
Msg("Successfully searched repository")
|
||||
|
||||
return json.Marshal(result)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue