|
| 1 | +import type { IObjectMeta } from "@kubernetes-models/apimachinery/apis/meta/v1/ObjectMeta" |
| 2 | +import { Model as BaseModel } from "@kubernetes-models/base" |
| 3 | + |
| 4 | +type Condition = Array<{ |
| 5 | + type: string |
| 6 | + status: string |
| 7 | + lastTransitionTime: string |
| 8 | + message?: string |
| 9 | + reason: string |
| 10 | +}> |
| 11 | +type Status = { |
| 12 | + conditions?: Condition |
| 13 | + [key: string]: any |
| 14 | +} |
| 15 | + |
| 16 | +export class Model<T> extends BaseModel<T> { |
| 17 | + getMetadata(): IObjectMeta { |
| 18 | + const self = this as any |
| 19 | + if (!self.metadatata) { |
| 20 | + throw new Error("No metadata found") |
| 21 | + } |
| 22 | + return self.metadata |
| 23 | + } |
| 24 | + |
| 25 | + getStatus(): Status { |
| 26 | + const self = this as any |
| 27 | + return self.status || {} |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Get the claim namespace from annotations (legacy method) |
| 32 | + * @returns The claim namespace |
| 33 | + * @throws Error if the resource wasn't created via a claim |
| 34 | + */ |
| 35 | + getClaimNamespace(): string { |
| 36 | + const { name } = this.getMetadata() |
| 37 | + const claimNamespace = this.getMetadata().labels?.["crossplane.io/claim-namespace"] |
| 38 | + |
| 39 | + if (!claimNamespace) { |
| 40 | + throw new Error(`Resource ${name} wasn't created via a claim`) |
| 41 | + } |
| 42 | + |
| 43 | + return claimNamespace |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Check if this resource is ready based on its status conditions |
| 48 | + * @returns true if the resource is ready, false otherwise |
| 49 | + */ |
| 50 | + isReady(): boolean { |
| 51 | + try { |
| 52 | + // ProviderConfigs don't have status conditions, we assume they're always ready |
| 53 | + if ((this as any).kind === "ProviderConfig") { |
| 54 | + return true |
| 55 | + } |
| 56 | + |
| 57 | + // Check for Ready condition in status.conditions |
| 58 | + const conditions = this.getStatus()?.conditions |
| 59 | + if (!conditions || !Array.isArray(conditions)) { |
| 60 | + return false |
| 61 | + } |
| 62 | + |
| 63 | + const readyCondition = conditions.find((condition: any) => condition.type === "Ready") |
| 64 | + return readyCondition?.status === "True" |
| 65 | + } catch (_error) { |
| 66 | + return false |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Get a specific condition from the resource status |
| 72 | + * @param conditionType - The type of condition to find |
| 73 | + * @returns The condition object or undefined if not found |
| 74 | + */ |
| 75 | + getCondition( |
| 76 | + conditionType: string |
| 77 | + ): |
| 78 | + | { type: string; status: string; lastTransitionTime: string; message?: string; reason: string } |
| 79 | + | undefined { |
| 80 | + return this.getStatus()?.conditions?.find(condition => condition.type === conditionType) |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Check if a specific condition is true |
| 85 | + * @param conditionType - The type of condition to check |
| 86 | + * @returns true if the condition exists and its status is "True" |
| 87 | + */ |
| 88 | + hasCondition(conditionType: string): boolean { |
| 89 | + const condition = this.getCondition(conditionType) |
| 90 | + return condition?.status === "True" |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get the resource name |
| 95 | + * @returns The resource name |
| 96 | + */ |
| 97 | + getName(): string | undefined { |
| 98 | + return this.getMetadata().name |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Get the resource namespace |
| 103 | + * @returns The resource namespace or undefined if not set |
| 104 | + */ |
| 105 | + getNamespace(): string | undefined { |
| 106 | + return this.getMetadata().namespace |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Get an annotation value |
| 111 | + * @param key - The annotation key |
| 112 | + * @returns The annotation value or undefined if not found |
| 113 | + */ |
| 114 | + getAnnotation(key: string): string | undefined { |
| 115 | + return this.getMetadata().annotations?.[key] |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Get a label value |
| 120 | + * @param key - The label key |
| 121 | + * @returns The label value or undefined if not found |
| 122 | + */ |
| 123 | + getLabel(key: string): string | undefined { |
| 124 | + return this.getMetadata().labels?.[key] |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Check if the resource is paused |
| 129 | + * @returns true if the resource has the pause annotation set to "true" |
| 130 | + */ |
| 131 | + isPaused(): boolean { |
| 132 | + return this.getAnnotation("crossplane.io/paused") === "true" |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Create a Usage resource to establish dependency relationships between resources |
| 137 | + * @param byResource - Optional resource that uses this resource |
| 138 | + * @returns Usage resource object |
| 139 | + */ |
| 140 | + makeUsage(byResource?: Model<any>): any { |
| 141 | + const usageName = byResource |
| 142 | + ? `${byResource.getMetadata().name}-uses-${this.getMetadata().name}` |
| 143 | + : `protect-${this.getMetadata().name}` |
| 144 | + |
| 145 | + const usage: any = { |
| 146 | + apiVersion: "apiextensions.crossplane.io/v1alpha1", |
| 147 | + kind: "Usage", |
| 148 | + getMetadata() { |
| 149 | + return usageName |
| 150 | + }, |
| 151 | + spec: { |
| 152 | + replayDeletion: true, |
| 153 | + of: { |
| 154 | + apiVersion: (this as any).apiVersion, |
| 155 | + kind: (this as any).kind, |
| 156 | + resourceRef: { name: this.getMetadata().name }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + } |
| 160 | + |
| 161 | + if (byResource) { |
| 162 | + usage.spec.by = { |
| 163 | + apiVersion: (byResource as any).apiVersion, |
| 164 | + kind: (byResource as any).kind, |
| 165 | + resourceRef: { name: byResource.getMetadata().name }, |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return usage |
| 170 | + } |
| 171 | +} |
0 commit comments