forked from cloudfoundry/stratos
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #384 from SUSE/kube-terminal
Add support for a Kubernetes Terminal
- Loading branch information
Showing
36 changed files
with
1,278 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Colours | ||
CYAN="\033[96m" | ||
YELLOW="\033[93m" | ||
RED="\033[91m" | ||
RESET="\033[0m" | ||
BOLD="\033[1m" | ||
|
||
# Program Paths: | ||
PROG=$(basename ${BASH_SOURCE[0]}) | ||
PROG_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
STRATOS_DIR="$( cd "${PROG_DIR}/../.." && pwd )" | ||
|
||
echo "Creating Service Account" | ||
SRC="${STRATOS_DIR}/deploy/kubernetes/console/templates/service-account.yaml" | ||
|
||
TEMPFILE=$(mktemp) | ||
cp $SRC $TEMPFILE | ||
sed -i.bak '/\s*helm/d' $TEMPFILE | ||
sed -i.bak '/\s*app\.kubernetes\.io\/version/d' $TEMPFILE | ||
sed -i.bak '/\s*app\.kubernetes\.io\/instance/d' $TEMPFILE | ||
sed -i.bak '/\s*{{-/d' $TEMPFILE | ||
|
||
# Create a namespace | ||
NS="stratos-dev" | ||
kubectl get ns $NS > /dev/null 2>&1 | ||
if [ $? -ne 0 ]; then | ||
kubectl create ns $NS | ||
fi | ||
|
||
kubectl apply -n $NS -f $TEMPFILE | ||
USER=stratos-dev-admin-user | ||
USER=stratos | ||
|
||
# Service account should be created - now need to get token | ||
SECRET=$(kubectl get -n $NS sa $USER -o json | jq -r '.secrets[0].name') | ||
TOKEN=$(kubectl get -n $NS secret $SECRET -o json | jq -r '.data.token') | ||
echo "Token secret: $SECRET" | ||
TOKEN=$(echo $TOKEN | base64 -d -) | ||
echo "Token $TOKEN" | ||
|
||
rm -f $TEMPFILE | ||
rm -f $TEMPFILE.bak | ||
|
||
CFG=${STRATOS_DIR}/src/jetstream/config.properties | ||
touch $CFG | ||
|
||
echo -e "\n# Kubernetes Terminal Config for dev" >> $CFG | ||
echo "STRATOS_KUBERNETES_NAMESPACE=stratos-dev" >> $CFG | ||
echo "STRATOS_KUBERNETES_TERMINAL_IMAGE=splatform/stratos-kube-terminal:dev" >> $CFG | ||
echo "KUBE_TERMINAL_SERVICE_ACCOUNT_TOKEN=$TOKEN" >> $CFG | ||
|
||
MKUBE=$(minikube ip) | ||
if [ $? -eq 0 ]; then | ||
echo "KUBERNETES_SERVICE_HOST=$MKUBE" >> $CFG | ||
echo "KUBERNETES_SERVICE_PORT=8443" >> $CFG | ||
else | ||
echo "KUBERNETES_SERVICE_HOST=" >> $CFG | ||
echo "KUBERNETES_SERVICE_PORT=8443" >> $CFG | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
stratos-kube-terminal:_VERSION_ |
18 changes: 18 additions & 0 deletions
18
custom-src/frontend/app/custom/kubernetes/kube-terminal/kube-console.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<app-page-header [breadcrumbs]="breadcrumbs$ | async"> | ||
<h1>Kubernetes Terminal</h1> | ||
<div class="page-header-right"> | ||
<span> | ||
<button mat-icon-button matTooltip="Disconnect" name="stop" *ngIf="sshViewer.isConnected" (click)="sshViewer.disconnect()" [disabled]="sshViewer.attemptingConnection"> | ||
<mat-icon>stop</mat-icon> | ||
</button> | ||
<button mat-icon-button matTooltip="Connect" name="start" *ngIf="!sshViewer.isConnected" (click)="sshViewer.reconnect()" [disabled]="sshViewer.attemptingConnection"> | ||
<mat-icon>play_arrow</mat-icon> | ||
</button> | ||
<button mat-icon-button matTooltip="Back" [routerLink]="kubeSummaryLink"> | ||
<mat-icon>close</mat-icon> | ||
</button> | ||
</span> | ||
</div> | ||
</app-page-header> | ||
|
||
<app-ssh-viewer #sshViewer [errorMessage]="errorMessage" [sshStream]="messages" [sshInput]="sshInput" [connectionStatus]="connectionStatus"></app-ssh-viewer> |
Empty file.
42 changes: 42 additions & 0 deletions
42
custom-src/frontend/app/custom/kubernetes/kube-terminal/kube-console.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { createBasicStoreModule } from '@stratosui/store/testing'; | ||
|
||
import { ApplicationService } from '../../../../../cloud-foundry/src/features/applications/application.service'; | ||
import { ApplicationServiceMock } from '../../../../../cloud-foundry/test-framework/application-service-helper'; | ||
import { TabNavService } from '../../../../tab-nav.service'; | ||
import { CoreModule } from '../../../core/core.module'; | ||
import { SharedModule } from '../../../shared/shared.module'; | ||
import { KubeConsoleComponent } from './kube-console.component'; | ||
|
||
describe('KubeConsoleComponent', () => { | ||
let component: KubeConsoleComponent; | ||
let fixture: ComponentFixture<KubeConsoleComponent>; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [KubeConsoleComponent], | ||
imports: [ | ||
CoreModule, | ||
SharedModule, | ||
RouterTestingModule, | ||
createBasicStoreModule() | ||
], | ||
providers: [ | ||
{ provide: ApplicationService, useClass: ApplicationServiceMock }, | ||
TabNavService | ||
], | ||
}) | ||
.compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(KubeConsoleComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
95 changes: 95 additions & 0 deletions
95
custom-src/frontend/app/custom/kubernetes/kube-terminal/kube-console.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { Component, OnInit, ViewChild } from '@angular/core'; | ||
import { ActivatedRoute } from '@angular/router'; | ||
import { NEVER, Observable, Subject, Subscription } from 'rxjs'; | ||
import websocketConnect, { normalClosureMessage } from 'rxjs-websockets'; | ||
import { catchError, tap, switchMap, map } from 'rxjs/operators'; | ||
|
||
import { IHeaderBreadcrumb } from '../../../shared/components/page-header/page-header.types'; | ||
import { SshViewerComponent } from '../../../shared/components/ssh-viewer/ssh-viewer.component'; | ||
import { KubernetesEndpointService } from '../services/kubernetes-endpoint.service'; | ||
import { BaseKubeGuid } from '../kubernetes-page.types'; | ||
import { KubernetesService } from '../services/kubernetes.service'; | ||
|
||
|
||
@Component({ | ||
selector: 'app-kube-console', | ||
templateUrl: './kube-console.component.html', | ||
styleUrls: ['./kube-console.component.scss'], | ||
providers: [ | ||
{ | ||
provide: BaseKubeGuid, | ||
useFactory: (activatedRoute: ActivatedRoute) => { | ||
return { | ||
guid: activatedRoute.snapshot.params.endpointId | ||
}; | ||
}, | ||
deps: [ | ||
ActivatedRoute | ||
] | ||
}, | ||
KubernetesService, | ||
KubernetesEndpointService, | ||
] | ||
}) | ||
export class KubeConsoleComponent implements OnInit { | ||
|
||
public messages: Observable<string>; | ||
|
||
public connectionStatus = new Subject<number>(); | ||
|
||
public sshInput: Subject<string>; | ||
|
||
public errorMessage: string; | ||
|
||
public connected: boolean; | ||
|
||
public kubeSummaryLink: string; | ||
|
||
public breadcrumbs$: Observable<IHeaderBreadcrumb[]>; | ||
|
||
@ViewChild('sshViewer', { static: false }) sshViewer: SshViewerComponent; | ||
|
||
constructor( | ||
public kubeEndpointService: KubernetesEndpointService, | ||
) { } | ||
|
||
ngOnInit() { | ||
this.connectionStatus.next(0); | ||
const guid = this.kubeEndpointService.baseKube.guid; | ||
this.kubeSummaryLink = `/kubernetes/${guid}/summary`; | ||
|
||
if (!guid) { | ||
this.messages = NEVER; | ||
this.connectionStatus.next(0); | ||
this.errorMessage = 'No Endpoint ID available'; | ||
} else { | ||
const host = window.location.host; | ||
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'; | ||
const streamUrl = ( | ||
`${protocol}://${host}/pp/v1/kubeterminal/${guid}` | ||
); | ||
this.sshInput = new Subject<string>(); | ||
const connection = websocketConnect(streamUrl); | ||
|
||
this.messages = connection.pipe( | ||
tap(() => this.connectionStatus.next(1)), | ||
switchMap(getResponse => getResponse(this.sshInput)), | ||
catchError((e: Error) => { | ||
if (e.message !== normalClosureMessage && !this.sshViewer.isConnected) { | ||
this.errorMessage = 'Error launching Kubernetes Terminal'; | ||
} | ||
return []; | ||
})); | ||
|
||
// Breadcrumbs | ||
this.breadcrumbs$ = this.kubeEndpointService.endpoint$.pipe( | ||
map(endpoint => ([{ | ||
breadcrumbs: [ | ||
{ value: endpoint.entity.name, routerLink: `/kubernetes/${endpoint.entity.guid}` }, | ||
] | ||
}]) | ||
) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.