|
| 1 | +import { Injectable, Optional } from '@angular/core' |
| 2 | +import { HttpClient } from '@angular/common/http' |
| 3 | +import { KubernetesUrlGeneratorService } from './kubernetes-url-generator.service' |
| 4 | +import { DataServiceConfig } from './config' |
| 5 | +import { KubeObject, Status } from '@ccremer/kubernetes-client/types/core' |
| 6 | +import { map, Observable } from 'rxjs' |
| 7 | +import { toURLSearchParams } from '@ccremer/kubernetes-client/api' |
| 8 | +import { |
| 9 | + DeleteOptions, |
| 10 | + GetOptions, |
| 11 | + ListOptions, |
| 12 | + MutationOptions, |
| 13 | + PatchOptions, |
| 14 | +} from '@ccremer/kubernetes-client/api/options' |
| 15 | +import { KubeList } from '@ccremer/kubernetes-client/types/core/KubeList' |
| 16 | + |
| 17 | +@Injectable() |
| 18 | +export class KubernetesClientService { |
| 19 | + constructor( |
| 20 | + protected client: HttpClient, |
| 21 | + protected urlGenerator: KubernetesUrlGeneratorService, |
| 22 | + @Optional() protected config?: DataServiceConfig |
| 23 | + ) {} |
| 24 | + |
| 25 | + protected hideManagedFields<T extends KubeObject>(stream: Observable<T>, hide?: boolean): Observable<T> { |
| 26 | + if (!hide) return stream |
| 27 | + return stream.pipe( |
| 28 | + map((entity) => { |
| 29 | + delete entity.metadata?.managedFields |
| 30 | + return entity |
| 31 | + }) |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + get<K extends KubeObject>( |
| 36 | + apiVersion: string, |
| 37 | + kind: string, |
| 38 | + name?: string, |
| 39 | + namespace?: string, |
| 40 | + options?: GetOptions |
| 41 | + ): Observable<K> { |
| 42 | + const endpoint = this.urlGenerator.buildEndpoint( |
| 43 | + 'GET', |
| 44 | + apiVersion, |
| 45 | + kind, |
| 46 | + namespace, |
| 47 | + name, |
| 48 | + toURLSearchParams(options) |
| 49 | + ) |
| 50 | + return this.hideManagedFields(this.client.get<K>(endpoint, { responseType: 'json' }), options?.hideManagedFields) |
| 51 | + } |
| 52 | + |
| 53 | + list<K extends KubeObject>( |
| 54 | + apiVersion: string, |
| 55 | + kind: string, |
| 56 | + namespace?: string, |
| 57 | + options?: ListOptions |
| 58 | + ): Observable<KubeList<K>> { |
| 59 | + const endpoint = this.urlGenerator.buildEndpoint( |
| 60 | + 'GET', |
| 61 | + apiVersion, |
| 62 | + kind, |
| 63 | + namespace, |
| 64 | + undefined, |
| 65 | + toURLSearchParams(options) |
| 66 | + ) |
| 67 | + return this.client.get<KubeList<K>>(endpoint, { responseType: 'json' }).pipe( |
| 68 | + map((list) => { |
| 69 | + if (!options?.hideManagedFields) return list |
| 70 | + list.items = list.items.map((item) => { |
| 71 | + delete item.metadata?.managedFields |
| 72 | + return item |
| 73 | + }) |
| 74 | + return list |
| 75 | + }) |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + create<K extends KubeObject>(body: K, options?: MutationOptions): Observable<K> { |
| 80 | + const endpoint = this.urlGenerator.buildEndpoint( |
| 81 | + 'POST', |
| 82 | + body.apiVersion, |
| 83 | + body.kind, |
| 84 | + body.metadata?.namespace, |
| 85 | + body.metadata?.name, |
| 86 | + toURLSearchParams(options) |
| 87 | + ) |
| 88 | + return this.hideManagedFields( |
| 89 | + this.client.post<K>(endpoint, body, { responseType: 'json' }), |
| 90 | + options?.hideManagedFields |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + update<K extends KubeObject>(body: K, options?: MutationOptions): Observable<K> { |
| 95 | + const endpoint = this.urlGenerator.buildEndpoint( |
| 96 | + 'POST', |
| 97 | + body.apiVersion, |
| 98 | + body.kind, |
| 99 | + body.metadata?.namespace, |
| 100 | + body.metadata?.name, |
| 101 | + toURLSearchParams(options) |
| 102 | + ) |
| 103 | + return this.hideManagedFields( |
| 104 | + this.client.put<K>(endpoint, body, { responseType: 'json' }), |
| 105 | + options?.hideManagedFields |
| 106 | + ) |
| 107 | + } |
| 108 | + |
| 109 | + patch<K extends KubeObject>(body: K, options?: PatchOptions): Observable<K> { |
| 110 | + const endpoint = this.urlGenerator.buildEndpoint( |
| 111 | + 'POST', |
| 112 | + body.apiVersion, |
| 113 | + body.kind, |
| 114 | + body.metadata?.namespace, |
| 115 | + body.metadata?.name, |
| 116 | + toURLSearchParams(options) |
| 117 | + ) |
| 118 | + return this.hideManagedFields( |
| 119 | + this.client.patch<K>(endpoint, body, { |
| 120 | + responseType: 'json', |
| 121 | + headers: { 'content-type': 'application/merge-patch+json' }, |
| 122 | + }), |
| 123 | + options?.hideManagedFields |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + delete( |
| 128 | + apiVersion: string, |
| 129 | + kind: string, |
| 130 | + name?: string, |
| 131 | + namespace?: string, |
| 132 | + options?: DeleteOptions |
| 133 | + ): Observable<Status> { |
| 134 | + const endpoint = this.urlGenerator.buildEndpoint( |
| 135 | + 'DELETE', |
| 136 | + apiVersion, |
| 137 | + kind, |
| 138 | + namespace, |
| 139 | + name, |
| 140 | + toURLSearchParams(options) |
| 141 | + ) |
| 142 | + return this.client.delete<Status>(endpoint, { responseType: 'json' }) |
| 143 | + } |
| 144 | +} |
0 commit comments