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

Use BundleDeployment status to calculate GitRepo resources #12521

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -173,7 +173,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 @@ -113,6 +113,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 @@ -82,7 +82,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
93 changes: 43 additions & 50 deletions shell/models/fleet.cattle.io.gitrepo.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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 @@ -325,35 +326,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 @@ -363,33 +353,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 @@ -410,9 +405,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 @@ -116,7 +116,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