import { Errorable , failed } from '../../errorable' ; interface CompatibilityGuaranteed { readonly guaranteed : true ; } interface CompatibilityNotGuaranteed { readonly guaranteed : false ; readonly didCheck : boolean ; readonly clientVersion : string ; readonly serverVersion : string ; } export type Compatibility = CompatibilityGuaranteed | CompatibilityNotGuaranteed ; export function isGuaranteedCompatible ( c ) { return c . guaranteed ; } export interface Version { readonly major : string ; readonly minor : string ; readonly gitVersion : string ; } export async function check ( kubectlLoadJSON ) { const version = await kubectlLoadJSON ( 'version -o json' ) ; if ( failed ( version ) ) { return { guaranteed : false , didCheck : false , clientVersion : '' , serverVersion : '' } ; } const clientVersion = version . result . clientVersion ; const serverVersion = version . result . serverVersion ; if ( isCompatible ( clientVersion , serverVersion ) ) { return { guaranteed : true } ; } return { guaranteed : false , didCheck : true , clientVersion : clientVersion . gitVersion , serverVersion : serverVersion . gitVersion } ; } function isCompatible ( clientVersion , serverVersion ) { if ( clientVersion . major === serverVersion . major ) { const clientMinor = Number . parseInt ( clientVersion . minor ) ; const serverMinor = Number . parseInt ( serverVersion . minor ) ; if ( Number . isInteger ( clientMinor ) && Number . isInteger ( serverMinor ) && Math . abs ( clientMinor - serverMinor ) <= 1 ) { return true ; } } return false ; }