import type { StateService } from '@uirouter/angularjs' ; import type { IController , IQService , IScope } from 'angular' ; import { module } from 'angular' ; import type { IModalService } from 'angular-ui-bootstrap' ; import type { Application , IManifest , IOwnerOption , IServerGroup } from '@spinnaker/core' ; import { ClusterTargetBuilder , ConfirmationModalService , ManifestReader , SERVER_GROUP_WRITER , ServerGroupReader , SETTINGS , } from '@spinnaker/core' ; import type { IKubernetesServerGroup } from '../../interfaces' ; import { KubernetesManifestCommandBuilder } from '../../manifest/manifestCommandBuilder.service' ; import { ManifestTrafficService } from '../../manifest/traffic/ManifestTrafficService' ; import { ManifestWizard } from '../../manifest/wizard/ManifestWizard' ; interface IServerGroupFromStateParams { accountId : string ; region : string ; name : string ; } class KubernetesServerGroupDetailsController implements IController { public state = { loading : true } ; public serverGroup : IKubernetesServerGroup ; public manifest : IManifest ; public entityTagTargets : IOwnerOption [ ] ; public static $inject = [ 'serverGroup' , 'app' , '$uibModal' , '$scope' , '$state' , '$q' ] ; constructor ( serverGroup , public app , private $uibModal , private $scope , private $state , private $q ) { const dataSource = this . app . getDataSource ( 'serverGroups' ) ; dataSource . ready ( ) . then ( ( ) => { this . extractServerGroup ( serverGroup ) ; this . $scope . isDisabled = ! SETTINGS . kubernetesAdHocInfraWritesEnabled ; dataSource . onRefresh ( this . $scope , ( ) => this . extractServerGroup ( serverGroup ) ) ; } ) . catch ( ( ) => this . autoClose ( ) ) ; } private ownerReferences ( ) { const manifest = this . manifest . manifest ; if ( manifest != null && manifest . hasOwnProperty ( 'metadata' ) && manifest . metadata . hasOwnProperty ( 'ownerReferences' ) && Array . isArray ( manifest . metadata . ownerReferences ) ) { return manifest . metadata . ownerReferences ; } else { return [ ] as any [ ] ; } } private ownerIsController ( ownerReference ) { return ownerReference . hasOwnProperty ( 'controller' ) && ownerReference . controller === true ; } private lowerCaseFirstLetter ( s ) { return s . charAt ( 0 ) . toLowerCase ( ) + s . slice ( 1 ) ; } public manifestController ( ) { const controller = this . ownerReferences ( ) . find ( this . ownerIsController ) ; if ( typeof controller === 'undefined' ) { return null ; } else { return this . lowerCaseFirstLetter ( controller . kind ) + ' ' + controller . name ; } } public canScaleServerGroup ( ) { return this . serverGroup . kind !== 'DaemonSet' && this . manifestController ( ) === null ; } public scaleServerGroup ( ) { this . $uibModal . open ( { templateUrl : require ( '../../manifest/scale/scale.html' ) , controller : 'kubernetesV2ManifestScaleCtrl' , controllerAs : 'ctrl' , resolve : { coordinates : { name : this . serverGroup . name , namespace : this . serverGroup . namespace , account : this . serverGroup . account , } , currentReplicas : this . manifest . manifest . spec . replicas , application : this . app , } , } ) ; } public canEditServerGroup ( ) { return this . manifestController ( ) === null ; } public editServerGroup ( ) { KubernetesManifestCommandBuilder . buildNewManifestCommand ( this . app , this . manifest . manifest , this . serverGroup . moniker , this . serverGroup . account ) . then ( ( builtCommand ) => { ManifestWizard . show ( { title : 'Edit Manifest' , application : this . app , command : builtCommand } ) ; } ) ; } public deleteServerGroup ( ) { this . $uibModal . open ( { templateUrl : require ( '../../manifest/delete/delete.html' ) , controller : 'kubernetesV2ManifestDeleteCtrl' , controllerAs : 'ctrl' , resolve : { coordinates : { name : this . serverGroup . name , namespace : this . serverGroup . namespace , account : this . serverGroup . account , } , manifestController : ( ) => this . manifestController ( ) , application : this . app , } , } ) ; } public canDisable = ( ) => ManifestTrafficService . canDisableServerGroup ( this . serverGroup ) ; public disableServerGroup = ( ) : void => { ConfirmationModalService . confirm ( { header : `Really disable ${ this . manifest . name }?` , buttonText : 'Disable' , askForReason : true , submitJustWithReason : true , submitMethod : ( { reason } ) => ManifestTrafficService . disable ( this . manifest , this . app , reason ) , taskMonitorConfig : { application : this . app , title : `Disabling ${ this . manifest . name }` , onTaskComplete : ( ) => this . app . getDataSource ( 'serverGroups' ) . refresh ( ) , } , } ) ; } ; public canEnable = ( ) => ManifestTrafficService . canEnableServerGroup ( this . serverGroup ) ; public enableServerGroup = ( ) : void => { ConfirmationModalService . confirm ( { header : `Really enable ${ this . manifest . name }?` , buttonText : 'Enable' , askForReason : true , submitJustWithReason : true , submitMethod : ( { reason } ) => ManifestTrafficService . enable ( this . manifest , this . app , reason ) , taskMonitorConfig : { application : this . app , title : `Enabling ${ this . manifest . name }` , onTaskComplete : ( ) => this . app . getDataSource ( 'serverGroups' ) . refresh ( ) , } , } ) ; } ; private autoClose ( ) { if ( this . $scope . $$destroyed ) { return ; } else { this . $state . params . allowModalToStayOpen = true ; this . $state . go ( '^' , null , { location : 'replace' } ) ; } } private extractServerGroup ( { accountId , name , region } ) { this . $q . all ( [ ServerGroupReader . getServerGroup ( this . app . name , accountId , region , name ) , ManifestReader . getManifest ( accountId , region , name ) , ] ) . then ( ( [ serverGroupDetails , manifest ] ) => { if ( ! serverGroupDetails ) { return this . autoClose ( ) ; } this . serverGroup = serverGroupDetails as IKubernetesServerGroup ; this . entityTagTargets = this . configureEntityTagTargets ( ) ; this . manifest = manifest ; this . state . loading = false ; } ) ; } private configureEntityTagTargets ( ) { return ClusterTargetBuilder . buildClusterTargets ( this . serverGroup ) ; } } export const KUBERNETES_SERVER_GROUP_DETAILS_CTRL = 'spinnaker.kubernetes.serverGroup.details.controller' ; module ( KUBERNETES_SERVER_GROUP_DETAILS_CTRL , [ SERVER_GROUP_WRITER ] ) . controller ( 'kubernetesV2ServerGroupDetailsCtrl' , KubernetesServerGroupDetailsController ) ;