Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.9] Use BundleDeployment status to calculate GitRepo resources #12443

Open
wants to merge 28 commits into
base: release-2.9
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
28c097b
Use BundleDeployment status to calculate GitRepo resources
aruiz14 Oct 31, 2024
8824bc9
Exclude spec.resources when fetching bundles
aruiz14 Oct 31, 2024
8636de0
Avoid using spec for getting resources in Bundle details page
aruiz14 Oct 31, 2024
016f2c7
Remove unused imports and component
aruiz14 Oct 31, 2024
ec268d2
Make creating resources map readable, thanks Richard
aruiz14 Nov 4, 2024
6e2bc59
Fix missing state
aruiz14 Nov 4, 2024
d765009
Avoid find over clusters
aruiz14 Nov 4, 2024
ea6c3b4
Rework resourcesStatuses based on feedback and lack of type and id fi…
aruiz14 Nov 4, 2024
8fa0474
Move new functions to typescript
aruiz14 Nov 4, 2024
3a426be
Fix bundle resource count
aruiz14 Nov 4, 2024
ec0e185
Fix for bundles without repo-name label
aruiz14 Nov 4, 2024
ed0f602
Fix missing state from Bundle details
aruiz14 Nov 4, 2024
93b259e
Omit spec.resources everywhere else fetching bundles
aruiz14 Nov 4, 2024
256c137
BundleDeploymentResource and ResourceKey are not exactly the same
aruiz14 Nov 5, 2024
111ceab
Extract resourceKey so it can be reused
aruiz14 Nov 5, 2024
3ad0bc6
Count resource states instead of using find
aruiz14 Nov 5, 2024
85cd930
Use jsdoc for TS comments
aruiz14 Nov 5, 2024
9445621
Fix most TS warnings
aruiz14 Nov 5, 2024
c522d2e
Remove state type and just use a string
aruiz14 Nov 5, 2024
7a33747
Rename new interfaces to use a prefix
aruiz14 Nov 5, 2024
2c0c1e8
Move more types to types file
aruiz14 Nov 5, 2024
41b4964
Extract counter type and "contructor" out of the function that uses it
aruiz14 Nov 5, 2024
cd9bbb1
Take nonReadyStatus into account
aruiz14 Nov 5, 2024
3b3e80f
Refactor to use a Class and export a default instance
aruiz14 Nov 6, 2024
d9a429f
rename functions to be more descriptive
aruiz14 Nov 6, 2024
2cbe667
make linter happy
aruiz14 Nov 6, 2024
f560a2e
Fix advanced/bundles test spec
aruiz14 Nov 6, 2024
9d69978
Merge branch 'release-2.9' into rework-gitrepo-status-resources-2.9
aruiz14 Nov 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cypress/e2e/tests/pages/fleet/advanced/bundles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Bundles', { testIsolation: 'off', tags: ['@fleet', '@adminUser'] }, ()
fleetBundleeDetailsPage.waitForPage();

// check table headers
const expectedHeadersDetailsViewEvents = ['Name'];
const expectedHeadersDetailsViewEvents = ['State', 'API Version', 'Kind', 'Name', 'Namespace'];

fleetBundleeDetailsPage.list().resourceTable().sortableTable()
.tableHeaderRow()
Expand Down
3 changes: 2 additions & 1 deletion shell/components/ResourceDetail/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ export default {
},
bundle: {
inStoreType: 'management',
type: FLEET.BUNDLE
type: FLEET.BUNDLE,
opt: { excludeFields: ['metadata.managedFields', 'spec.resources'] },
},

bundleDeployment: {
Expand Down
7 changes: 0 additions & 7 deletions shell/components/fleet/FleetRepos.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
FLEET_REPO_PER_CLUSTER_STATE

} from '@shell/config/table-headers';
import { FLEET } from '@shell/config/labels-annotations';
import { STATES_ENUM } from '@shell/plugins/dashboard-store/resource-class';

// i18n-ignore repoDisplay
Expand Down Expand Up @@ -118,12 +117,6 @@ export default {
parseTargetMode(row) {
return row.targetInfo?.mode === 'clusterGroup' ? this.t('fleet.gitRepo.warningTooltip.clusterGroup') : this.t('fleet.gitRepo.warningTooltip.cluster');
},

clusterViewResourceStatus(row) {
return row.clusterResourceStatus.find((c) => {
return c.metadata?.labels[FLEET.CLUSTER_NAME] === this.clusterId;
aruiz14 marked this conversation as resolved.
Show resolved Hide resolved
});
}
},
};
</script>
Expand Down
1 change: 1 addition & 0 deletions shell/config/labels-annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export const FLEET = {
CLUSTER_NAME: 'management.cattle.io/cluster-name',
BUNDLE_ID: 'fleet.cattle.io/bundle-id',
MANAGED: 'fleet.cattle.io/managed',
CLUSTER_NAMESPACE: 'fleet.cattle.io/cluster-namespace',
CLUSTER: 'fleet.cattle.io/cluster'
};

Expand Down
73 changes: 5 additions & 68 deletions shell/detail/fleet.cattle.io.bundle.vue
Original file line number Diff line number Diff line change
@@ -1,77 +1,25 @@
<script>
import { FLEET } from '@shell/config/types';
import FleetBundleResources from '@shell/components/fleet/FleetBundleResources.vue';
import SortableTable from '@shell/components/SortableTable';
import FleetUtils from '@shell/utils/fleet';

export default {
name: 'FleetBundleDetail',

components: {
FleetBundleResources,
SortableTable,
},
props: {
components: { FleetBundleResources },
props: {
value: {
type: Object,
required: true,
}
},

data() {
return { repo: null };
},

async fetch() {
const { namespace, labels } = this.value.metadata;
const repoName = `${ namespace }/${ labels['fleet.cattle.io/repo-name'] }`;

if (this.hasRepoLabel) {
this.repo = await this.$store.dispatch('management/find', { type: FLEET.GIT_REPO, id: repoName });
}
},

computed: {
hasRepoLabel() {
richard-cox marked this conversation as resolved.
Show resolved Hide resolved
return !!(this.value?.metadata?.labels && this.value?.metadata?.labels['fleet.cattle.io/repo-name']);
},
bundleResources() {
if (this.hasRepoLabel) {
const bundleResourceIds = this.bundleResourceIds;

return this.repo?.status?.resources?.filter((resource) => {
return bundleResourceIds.includes(resource.name);
});
} else if (this.value?.spec?.resources?.length) {
return this.value?.spec?.resources.map((item) => {
return {
content: item.content,
name: item.name.includes('.') ? item.name.split('.')[0] : item.name
};
});
}

return [];
},
resourceHeaders() {
return [
{
name: 'name',
value: 'name',
sort: ['name'],
labelKey: 'tableHeaders.name',
},
];
return FleetUtils.resourcesFromBundleStatus(this.value?.status);
},
resourceCount() {
return (this.bundleResources && this.bundleResources.length) || this.value?.spec?.resources?.length;
return this.bundleResources.length;
},
bundleResourceIds() {
if (this.value.status?.resourceKey) {
return this.value?.status?.resourceKey.map((item) => item.name);
}

return [];
}
}
};

Expand All @@ -84,19 +32,8 @@ export default {
<span>{{ resourceCount }}</span>
</div>
<FleetBundleResources
v-if="hasRepoLabel"
:value="bundleResources"
/>
<SortableTable
v-else
:rows="bundleResources"
:headers="resourceHeaders"
:table-actions="false"
:row-actions="false"
key-field="tableKey"
default-sort-by="state"
:paged="true"
/>
</div>
</template>

Expand Down
3 changes: 2 additions & 1 deletion shell/detail/fleet.cattle.io.gitrepo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ export default {
const allDispatches = await checkSchemasForFindAllHash({
allBundles: {
inStoreType: 'management',
type: FLEET.BUNDLE
type: FLEET.BUNDLE,
opt: { excludeFields: ['metadata.managedFields', 'spec.resources'] },
},

allBundleDeployments: {
Expand Down
4 changes: 3 additions & 1 deletion shell/models/fleet.cattle.io.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export default class FleetBundle extends SteveModel {
}

get repoName() {
return this.metadata.labels['fleet.cattle.io/repo-name'];
const labels = this.metadata?.labels || {};
moio marked this conversation as resolved.
Show resolved Hide resolved

return labels['fleet.cattle.io/repo-name'];
}

get targetClusters() {
Expand Down
93 changes: 43 additions & 50 deletions shell/models/fleet.cattle.io.gitrepo.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import Vue from 'vue';
import { convert, matching, convertSelectorObj } from '@shell/utils/selector';
import jsyaml from 'js-yaml';
import { escapeHtml, randomStr } from '@shell/utils/string';
import { escapeHtml } from '@shell/utils/string';
import { FLEET } from '@shell/config/types';
import { FLEET as FLEET_ANNOTATIONS } from '@shell/config/labels-annotations';
import { addObject, addObjects, findBy, insertAt } from '@shell/utils/array';
import { set } from '@shell/utils/object';
import SteveModel from '@shell/plugins/steve/steve-class';
import {
STATES_ENUM, colorForState, mapStateToEnum, primaryDisplayStatusFromCount, stateDisplay, stateSort
colorForState, mapStateToEnum, primaryDisplayStatusFromCount, stateDisplay, stateSort
} from '@shell/plugins/dashboard-store/resource-class';
import { NAME } from '@shell/config/product/explorer';
import FleetUtils from '@shell/utils/fleet';

function quacksLikeAHash(str) {
if (str.match(/^[a-f0-9]{40,}$/i)) {
Expand Down Expand Up @@ -322,35 +323,24 @@ export default class GitRepo extends SteveModel {
}

get resourcesStatuses() {
const clusters = this.targetClusters || [];
const resources = this.status?.resources || [];
const conditions = this.status?.conditions || [];
const bundleDeployments = this.bundleDeployments || [];
const clusters = (this.targetClusters || []).reduce((res, c) => {
res[c.id] = c;

const out = [];

for (const c of clusters) {
const clusterBundleDeploymentResources = this.bundleDeployments
.find((bd) => bd.metadata?.labels?.[FLEET_ANNOTATIONS.CLUSTER] === c.metadata.name)
?.status?.resources || [];

resources.forEach((r, i) => {
let namespacedName = r.name;
return res;
}, {});

if (r.namespace) {
namespacedName = `${ r.namespace }:${ r.name }`;
}
const out = [];

let state = r.state;
const perEntry = r.perClusterState?.find((x) => x.clusterId === c.id);
const tooMany = r.perClusterState?.length >= 10 || false;
for (const bd of bundleDeployments) {
const clusterId = FleetUtils.clusterIdFromBundleDeploymentLabels(bd.metadata?.labels);
const c = clusters[clusterId];
const resources = FleetUtils.resourcesFromBundleDeploymentStatus(bd.status);

if (perEntry) {
state = perEntry.state;
} else if (tooMany) {
state = STATES_ENUM.UNKNOWN;
} else {
state = STATES_ENUM.READY;
}
resources.forEach((r) => {
const id = FleetUtils.resourceId(r);
const type = FleetUtils.resourceType(r);
const state = r.state;

const color = colorForState(state).replace('text-', 'bg-');
const display = stateDisplay(state);
Expand All @@ -360,33 +350,38 @@ export default class GitRepo extends SteveModel {
params: {
product: NAME,
cluster: c.metadata.labels[FLEET_ANNOTATIONS.CLUSTER_NAME],
resource: r.type,
resource: type,
namespace: r.namespace,
id: r.name,
}
};

const key = `${ c.id }-${ type }-${ r.namespace }-${ r.name }`;

out.push({
key: `${ r.id }-${ c.id }-${ r.type }-${ r.namespace }-${ r.name }`,
tableKey: `${ r.id }-${ c.id }-${ r.type }-${ r.namespace }-${ r.name }-${ randomStr(8) }`,
kind: r.kind,
apiVersion: r.apiVersion,
type: r.type,
aruiz14 marked this conversation as resolved.
Show resolved Hide resolved
id: r.id,
namespace: r.namespace,
name: r.name,
clusterId: c.id,
clusterLabel: c.metadata.labels[FLEET_ANNOTATIONS.CLUSTER_NAME],
clusterName: c.nameDisplay,
state: mapStateToEnum(state),
stateBackground: color,
stateDisplay: display,
stateSort: stateSort(color, display),
namespacedName,
key,
tableKey: key,

// Needed?
id,
type,
clusterId: c.id,

// columns, see FleetResources.vue
state: mapStateToEnum(state),
clusterName: c.nameDisplay,
apiVersion: r.apiVersion,
kind: r.kind,
name: r.name,
namespace: r.namespace,
creationTimestamp: r.createdAt,

// other properties
clusterLabel: c.metadata.labels[FLEET_ANNOTATIONS.CLUSTER_NAME],
stateBackground: color,
stateDisplay: display,
stateSort: stateSort(color, display),
detailLocation,
conditions: conditions[i],
bundleDeploymentStatus: clusterBundleDeploymentResources?.[i],
creationTimestamp: clusterBundleDeploymentResources?.[i]?.createdAt
});
});
}
Expand All @@ -407,9 +402,7 @@ export default class GitRepo extends SteveModel {

get clusterResourceStatus() {
const clusterStatuses = this.resourcesStatuses.reduce((prev, curr) => {
const { clusterId, clusterLabel } = curr;

const state = curr.state;
const { clusterId, clusterLabel, state } = curr;

if (!prev[clusterId]) {
prev[clusterId] = {
Expand Down
1 change: 1 addition & 0 deletions shell/pages/c/_cluster/fleet/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default {
allBundles: {
inStoreType: 'management',
type: FLEET.BUNDLE,
opt: { excludeFields: ['metadata.managedFields', 'spec.resources'] },
richard-cox marked this conversation as resolved.
Show resolved Hide resolved
},
gitRepos: {
inStoreType: 'management',
Expand Down
40 changes: 40 additions & 0 deletions shell/types/resources/fleet.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export interface BundleResourceKey {
kind: string,
apiVersion: string,
namespace?: string,
name: string,
}

export interface BundleDeploymentResource extends BundleResourceKey {
createdAt?: string,
}

export interface BundleModifiedResource extends BundleResourceKey {
missing?: boolean,
delete?: boolean,
patch: string,
}

export interface BundleNonReadyResource extends BundleResourceKey {
summary: { [state: string]: string }
}

export interface BundleNonReadyBundle {
modifiedStatus: BundleModifiedResource[],
nonReadyStatus: BundleNonReadyResource[],
}

export interface BundleDeploymentStatus {
resources?: BundleDeploymentResource[],
modifiedStatus?: BundleModifiedResource[],
nonReadyStatus?: BundleNonReadyResource[],
}

export interface BundleStatusSummary {
nonReadyResources?: BundleNonReadyBundle[],
}

export interface BundleStatus {
resourceKey?: BundleResourceKey[],
summary?: BundleStatusSummary,
}
2 changes: 1 addition & 1 deletion shell/utils/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const checkSchemasForFindAllHash = (types, store) => {
const validSchema = value.schemaValidator ? value.schemaValidator(schema) : !!schema;

if (validSchema) {
hash[key] = store.dispatch(`${ value.inStoreType }/findAll`, { type: value.type } );
hash[key] = store.dispatch(`${ value.inStoreType }/findAll`, { type: value.type, opt: value.opt } );
}
}

Expand Down
Loading
Loading