Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 67 additions & 19 deletions hack/install-git-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,79 @@ set -o nounset
set -o pipefail

git_server() {
echo "Creating Git Server Knative service..."
cat << EOF | kubectl apply -f -
apiVersion: serving.knative.dev/v1
kind: Service
echo "Creating Git Server"

local name="func-git"

local namespace
namespace="$(kubectl config view --minify --output 'jsonpath={..namespace}')"
namespace="${namespace:-"default"}"


local ingress_class="contour-external"
local cluster_domain="localtest.me"
if kubectl api-versions | grep -q openshift.io; then
cluster_domain="$(kubectl get ingresses.config/cluster -o jsonpath='{.spec.domain}')"
ingress_class="openshift-default"
fi


local -r func_git_host="${name}.${namespace}.${cluster_domain}"

kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: func-git
name: "${name}"
namespace: "${namespace}"
labels:
app: git
app.kubernetes.io/name: "${name}"
spec:
template:
metadata:
annotations:
autoscaling.knative.dev/max-scale: "1"
autoscaling.knative.dev/min-scale: "1"
client.knative.dev/user-image: ghcr.io/matejvasek/func/gitserver:latest
spec:
containers:
- image: ghcr.io/matejvasek/func/gitserver:latest
ports:
containers:
- name: "${name}"
image: ghcr.io/matejvasek/func/gitserver:latest
ports:
- containerPort: 8080
resources: {}
status: {}
name: http
---
apiVersion: v1
kind: Service
metadata:
name: "${name}"
namespace: "${namespace}"
spec:
selector:
app.kubernetes.io/name: "${name}"
ports:
- name: http
protocol: TCP
port: 80
targetPort: http
type: ClusterIP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: "${name}"
namespace: "${namespace}"
spec:
ingressClassName: "${ingress_class}"
rules:
- host: "${func_git_host}"
http:
paths:
- backend:
service:
name: "${name}"
port:
number: 80
pathType: Prefix
path: /
EOF

kubectl wait ksvc --for=condition=RoutesReady --timeout=30s -l "app=git"
echo "starting func-git service at: ${func_git_host}"

kubectl wait --for=condition=Ready "pod/${name}" --timeout=30s
}

git_server
Expand Down
80 changes: 35 additions & 45 deletions test/common/gitserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package common

import (
"context"
"fmt"

"strings"
"testing"

"knative.dev/func/pkg/k8s"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

"knative.dev/func/pkg/k8s"
)

var DefaultGitServer GitProvider
Expand Down Expand Up @@ -39,71 +39,61 @@ type GitProvider interface {
// ------------------------------------------------------

type GitTestServerKnativeProvider struct {
PodName string
ServiceUrl string
Kubectl *TestExecCmd
t *testing.T
Kubectl *TestExecCmd
namespace string
externalHost string
t *testing.T
}

func (g *GitTestServerKnativeProvider) Init(T *testing.T) {

g.t = T
if g.PodName == "" {
config, err := k8s.GetClientConfig().ClientConfig()
if err != nil {
T.Fatal(err.Error())
}
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
T.Fatal(err.Error())
}
ctx := context.Background()

namespace, _, _ := k8s.GetClientConfig().Namespace()
podList, err := clientSet.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{
LabelSelector: "serving.knative.dev/service=func-git",
})
if err != nil {
T.Fatal(err.Error())
}
for _, pod := range podList.Items {
g.PodName = pod.Name
}
}

if g.ServiceUrl == "" {
// Get Route Name
_, g.ServiceUrl = GetKnativeServiceRevisionAndUrl(T, "func-git")
}
// name of the pod,svc and ingress
const podName = "func-git"
const svcName = podName
const ingressName = podName

func (g *GitTestServerKnativeProvider) Init(t *testing.T) {
g.t = t
if g.Kubectl == nil {
g.Kubectl = &TestExecCmd{
Binary: "kubectl",
ShouldDumpCmdLine: true,
ShouldDumpOnSuccess: true,
T: T,
T: t,
}
}
if g.namespace == "" {
g.namespace, _, _ = k8s.GetClientConfig().Namespace()
}

if g.externalHost == "" {
cli, err := k8s.NewKubernetesClientset()
if err != nil {
t.Fatal(err)
}
i, err := cli.NetworkingV1().Ingresses(g.namespace).Get(context.Background(), ingressName, metav1.GetOptions{})
if err != nil {
t.Fatal(err)
}
g.externalHost = i.Spec.Rules[0].Host
}
T.Logf("Initialized HTTP Func Git Server: Server URL = %v Pod Name = %v\n", g.ServiceUrl, g.PodName)

t.Logf("Initialized HTTP Func Git Server: Server URL = %s Pod Name = %s\n", g.externalHost, podName)
}

func (g *GitTestServerKnativeProvider) CreateRepository(repoName string) *GitRemoteRepo {
// kubectl exec $podname -c user-container -- git-repo create $reponame
cmdResult := g.Kubectl.Exec("exec", g.PodName, "-c", "user-container", "--", "git-repo", "create", repoName)
cmdResult := g.Kubectl.Exec("exec", podName, "--", "git-repo", "create", repoName)
if !strings.Contains(cmdResult.Out, "created") {
g.t.Fatal("unable to create git bare repository " + repoName)
}
namespace, _, _ := k8s.GetClientConfig().Namespace()
gitRepo := &GitRemoteRepo{
RepoName: repoName,
ExternalCloneURL: g.ServiceUrl + "/" + repoName + ".git",
ClusterCloneURL: "http://func-git." + namespace + ".svc.cluster.local/" + repoName + ".git",
ExternalCloneURL: fmt.Sprintf("http://%s/%s.git", g.externalHost, repoName),
ClusterCloneURL: fmt.Sprintf("http://%s.%s.svc.cluster.local/%s.git", svcName, g.namespace, repoName),
}
return gitRepo
}

func (g *GitTestServerKnativeProvider) DeleteRepository(repoName string) {
cmdResult := g.Kubectl.Exec("exec", g.PodName, "-c", "user-container", "--", "git-repo", "delete", repoName)
cmdResult := g.Kubectl.Exec("exec", podName, "--", "git-repo", "delete", repoName)
if !strings.Contains(cmdResult.Out, "deleted") {
g.t.Fatal("unable to delete git bare repository " + repoName)
}
Expand Down
Loading