@@ -22,6 +22,7 @@ import (
22
22
"net/http"
23
23
"os"
24
24
"path/filepath"
25
+ "strings"
25
26
"testing"
26
27
"time"
27
28
@@ -32,9 +33,11 @@ import (
32
33
"github.com/go-git/go-git/v5/plumbing"
33
34
"github.com/go-git/go-git/v5/plumbing/object"
34
35
"gopkg.in/yaml.v2"
36
+ appsv1 "k8s.io/api/apps/v1"
35
37
coreapi "k8s.io/api/core/v1"
36
38
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37
39
"k8s.io/apimachinery/pkg/runtime"
40
+ "k8s.io/apimachinery/pkg/util/intstr"
38
41
"k8s.io/client-go/rest"
39
42
aggregatorv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
40
43
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -153,8 +156,7 @@ func (t *TestSuite) CreateGitRepo() GitConfig {
153
156
return createLocalGitServer (t .T )
154
157
} else {
155
158
// Deploy Git server via k8s client.
156
- t .Fatal ("Creatig git server on k8s not yet supported." )
157
- return GitConfig {}
159
+ return t .createInClusterGitServer ()
158
160
}
159
161
}
160
162
@@ -247,6 +249,7 @@ func createClientScheme(t *testing.T) *runtime.Scheme {
247
249
configapi .AddToScheme ,
248
250
coreapi .AddToScheme ,
249
251
aggregatorv1 .AddToScheme ,
252
+ appsv1 .AddToScheme ,
250
253
}) {
251
254
if err := api (scheme ); err != nil {
252
255
t .Fatalf ("Failed to initialize test k8s api client" )
@@ -356,3 +359,187 @@ func createInitialCommit(t *testing.T, repo *gogit.Repository) {
356
359
t .Fatalf ("Failed to set refs/heads/main to commit sha %s" , commitHash )
357
360
}
358
361
}
362
+
363
+ func inferGitServerImage (porchImage string ) string {
364
+ slash := strings .LastIndex (porchImage , "/" )
365
+ repo := porchImage [:slash + 1 ]
366
+ image := porchImage [slash + 1 :]
367
+ colon := strings .LastIndex (image , ":" )
368
+ tag := image [colon + 1 :]
369
+
370
+ return repo + "git-server:" + tag
371
+ }
372
+
373
+ func (t * TestSuite ) createInClusterGitServer () GitConfig {
374
+ ctx := context .TODO ()
375
+
376
+ // Determine git-server image name. Use the same container registry and tag as the Porch server,
377
+ // replacing base image name with `git-server`. TODO: Make configurable?
378
+
379
+ var porch appsv1.Deployment
380
+ t .GetF (ctx , client.ObjectKey {
381
+ Namespace : "porch-system" ,
382
+ Name : "porch-server" ,
383
+ }, & porch )
384
+
385
+ gitImage := inferGitServerImage (porch .Spec .Template .Spec .Containers [0 ].Image )
386
+
387
+ var replicas int32 = 1
388
+ var selector = strings .ReplaceAll (t .Name (), "/" , "_" )
389
+
390
+ t .CreateF (ctx , & appsv1.Deployment {
391
+ ObjectMeta : metav1.ObjectMeta {
392
+ Name : "git-server" ,
393
+ Namespace : t .namespace ,
394
+ Annotations : map [string ]string {
395
+ "kpt.dev/porch-test" : t .Name (),
396
+ },
397
+ },
398
+ Spec : appsv1.DeploymentSpec {
399
+ Replicas : & replicas ,
400
+ Selector : & metav1.LabelSelector {
401
+ MatchLabels : map [string ]string {
402
+ "git-server" : selector ,
403
+ },
404
+ },
405
+ Template : coreapi.PodTemplateSpec {
406
+ ObjectMeta : metav1.ObjectMeta {
407
+ Labels : map [string ]string {
408
+ "git-server" : selector ,
409
+ },
410
+ },
411
+ Spec : coreapi.PodSpec {
412
+ Containers : []coreapi.Container {
413
+ {
414
+ Name : "git-server" ,
415
+ Image : gitImage ,
416
+ Args : []string {},
417
+ Ports : []coreapi.ContainerPort {
418
+ {
419
+ ContainerPort : 8080 ,
420
+ Protocol : coreapi .ProtocolTCP ,
421
+ },
422
+ },
423
+ ImagePullPolicy : coreapi .PullIfNotPresent ,
424
+ },
425
+ },
426
+ },
427
+ },
428
+ },
429
+ })
430
+
431
+ t .Cleanup (func () {
432
+ t .DeleteE (ctx , & appsv1.Deployment {
433
+ ObjectMeta : metav1.ObjectMeta {
434
+ Name : "git-server" ,
435
+ Namespace : t .namespace ,
436
+ },
437
+ })
438
+ })
439
+
440
+ t .CreateF (ctx , & coreapi.Service {
441
+ ObjectMeta : metav1.ObjectMeta {
442
+ Name : "git-server-service" ,
443
+ Namespace : t .namespace ,
444
+ Annotations : map [string ]string {
445
+ "kpt.dev/porch-test" : t .Name (),
446
+ },
447
+ },
448
+ Spec : coreapi.ServiceSpec {
449
+ Ports : []coreapi.ServicePort {
450
+ {
451
+ Protocol : coreapi .ProtocolTCP ,
452
+ Port : 8080 ,
453
+ TargetPort : intstr.IntOrString {
454
+ Type : intstr .Int ,
455
+ IntVal : 8080 ,
456
+ },
457
+ },
458
+ },
459
+ Selector : map [string ]string {
460
+ "git-server" : selector ,
461
+ },
462
+ },
463
+ })
464
+
465
+ t .Cleanup (func () {
466
+ t .DeleteE (ctx , & coreapi.Service {
467
+ ObjectMeta : metav1.ObjectMeta {
468
+ Name : "git-server-service" ,
469
+ Namespace : t .namespace ,
470
+ },
471
+ })
472
+ })
473
+
474
+ t .Logf ("Waiting for git-server to start ..." )
475
+
476
+ // Wait a minute for git server to start up.
477
+ giveUp := time .Now ().Add (time .Minute )
478
+
479
+ for {
480
+ time .Sleep (5 * time .Second )
481
+
482
+ var server appsv1.Deployment
483
+ t .GetF (ctx , client.ObjectKey {
484
+ Namespace : t .namespace ,
485
+ Name : "git-server" ,
486
+ }, & server )
487
+ if server .Status .AvailableReplicas > 0 {
488
+ t .Logf ("git server is up" )
489
+ break
490
+ }
491
+
492
+ if time .Now ().After (giveUp ) {
493
+ t .Fatalf ("git server failed to start: %s" , & server )
494
+ return GitConfig {}
495
+ }
496
+ }
497
+
498
+ t .Logf ("Waiting for git-serever-service to be ready ..." )
499
+
500
+ // Check the Endpoint resource for readiness
501
+ giveUp = time .Now ().Add (time .Minute )
502
+
503
+ for {
504
+ time .Sleep (5 * time .Second )
505
+
506
+ var endpoint coreapi.Endpoints
507
+ err := t .client .Get (ctx , client.ObjectKey {
508
+ Namespace : t .namespace ,
509
+ Name : "git-server-service" ,
510
+ }, & endpoint )
511
+
512
+ if err == nil && endpointIsReady (& endpoint ) {
513
+ t .Logf ("git-server-service is ready" )
514
+ break
515
+ }
516
+
517
+ if time .Now ().After (giveUp ) {
518
+ t .Fatalf ("git-server0-service not ready on time: %s" , & endpoint )
519
+ return GitConfig {}
520
+ }
521
+ }
522
+
523
+ return GitConfig {
524
+ Repo : fmt .Sprintf ("http://git-server-service.%s.svc.cluster.local:8080" , t .namespace ),
525
+ Branch : "main" ,
526
+ Directory : "/" ,
527
+ }
528
+ }
529
+
530
+ func endpointIsReady (endpoints * coreapi.Endpoints ) bool {
531
+ if len (endpoints .Subsets ) == 0 {
532
+ return false
533
+ }
534
+ for _ , s := range endpoints .Subsets {
535
+ if len (s .Addresses ) == 0 {
536
+ return false
537
+ }
538
+ for _ , a := range s .Addresses {
539
+ if a .IP == "" {
540
+ return false
541
+ }
542
+ }
543
+ }
544
+ return true
545
+ }
0 commit comments