Skip to content

Commit

Permalink
Merge pull request #12443 from aruiz14/rework-gitrepo-status-resource…
Browse files Browse the repository at this point in the history
…s-2.9

[2.9] Use BundleDeployment status to calculate GitRepo resources
  • Loading branch information
richard-cox authored Nov 21, 2024
2 parents e251654 + d78d06f commit 41ab2a8
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 128 deletions.
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;
});
}
},
};
</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() {
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 || {};

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

get targetClusters() {
Expand Down
94 changes: 46 additions & 48 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,29 @@ 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;

return res;
}, {});

const out = [];

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

resources.forEach((r, i) => {
let namespacedName = r.name;
if (!c) {
continue;
}

if (r.namespace) {
namespacedName = `${ r.namespace }:${ r.name }`;
}
const resources = FleetUtils.resourcesFromBundleDeploymentStatus(bd.status);

let state = r.state;
const perEntry = r.perClusterState?.find((x) => x.clusterId === c.id);
const tooMany = r.perClusterState?.length >= 10 || false;

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 +355,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,
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 +407,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'] },
},
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

0 comments on commit 41ab2a8

Please sign in to comment.