Skip to content

Commit

Permalink
Add Hosts for Ingress and refractor
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisht13 committed Jul 15, 2023
1 parent c003a7b commit 71e6b45
Show file tree
Hide file tree
Showing 26 changed files with 202 additions and 219 deletions.
12 changes: 3 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ GOBIN := $(PROJECTROOT)/bin
UTILDIR := $(PROJECTROOT)/scripts/utils
SPINNER := $(UTILDIR)/spinner.sh
BUILDIR := $(PROJECTROOT)/scripts/build
CONTROLLER_MANIFEST:= $(PROJECTROOT)/manifests/dev/expose-controller.yml
HELM_MANIFEST:= $(PROJECTROOT)/manifests/templates/helm-values.yml
OPENVPN_MANIFEST:= $(PROJECTROOT)/manifests/templates/helm-values.yml
MANIFEST:= $(PROJECTROOT)/kubernetes/manifests

KEY_NAME := team

Expand Down Expand Up @@ -83,20 +81,16 @@ gen-certificates:
kubectl --namespace $(OPENVPN_NAMESPACE) exec -it $(POD_NAME) cat "/etc/openvpn/certs/pki/$(KEY_NAME)-$$n.ovpn" > $(KEY_NAME)-$$n.ovpn; \
done

gen-vpn: set-env
helm install openvpn -f $(HELM_MANIFEST) stable/openvpn --namespace openvpn
minikube tunnel

set-env: build
minikube start --driver=docker && \
minikube addons enable ingress && \
kubectl apply -f $(CONTROLLER_MANIFEST) && \
kubectl apply -f $(MANIFEST) && \
sudo -- sh -c "echo \"$(minikube service nginx-ingress-controller --url -n kube-system | awk '{print substr($0,8)}' | awk '{print substr($0, 1, length($0)-6)}' | head -1) katana.local\" >> /etc/hosts" &&\
cp config.sample.toml config.toml && \
./bin/katana run

set-env-prod: build
kubectl apply -f $(CONTROLLER_MANIFEST) && \
kubectl apply -f $(MANIFEST) && \
cp config.sample.toml config.toml && \
sudo ./bin/katana run

Expand Down
5 changes: 2 additions & 3 deletions config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ broadcastcount = 2
broadcastlabel = "broadcast"
teamcount = 1
teamlabel = "ctfteam"
manifest_dir = "manifests/templates"
manifest_runtime_dir = "manifests/templates/runtime"
manifests = [
templated_manifest_dir = "kubernetes/templates"
templated_manifests = [
"harbor.yml",
]

Expand Down
15 changes: 7 additions & 8 deletions configs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ type API struct {
}

type ClusterCfg struct {
DeploymentLabel string `toml:"deploymentlabel"`
BroadcastCount uint `toml:"broadcastcount"`
BroadcastLabel string `toml:"broadcastlabel"`
TeamCount uint `toml:"teamcount"`
TeamLabel string `toml:"teamlabel"`
ManifestDir string `toml:"manifest_dir"`
ManifestRuntimeDir string `toml:"manifest_runtime_dir"`
Manifests []string `toml:"manifests"`
DeploymentLabel string `toml:"deploymentlabel"`
BroadcastCount uint `toml:"broadcastcount"`
BroadcastLabel string `toml:"broadcastlabel"`
TeamCount uint `toml:"teamcount"`
TeamLabel string `toml:"teamlabel"`
TemplatedManifestDir string `toml:"templated_manifest_dir"`
TemplatedManifests []string `toml:"templated_manifests"`
}

type ChallengeDeployerCfg struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: v1
kind: ServiceAccount
metadata:
name: ingress-nginx-admission
namespace: katana
namespace: kube-system
annotations:
"helm.sh/hook": pre-install,pre-upgrade,post-install,post-upgrade
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
Expand Down Expand Up @@ -64,14 +64,14 @@ roleRef:
subjects:
- kind: ServiceAccount
name: ingress-nginx-admission
namespace: "katana"
namespace: "kube-system"
---
# Source: ingress-nginx/templates/admission-webhooks/job-patch/role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: ingress-nginx-admission
namespace: katana
namespace: kube-system
annotations:
"helm.sh/hook": pre-install,pre-upgrade,post-install,post-upgrade
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
Expand All @@ -97,7 +97,7 @@ apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: ingress-nginx-admission
namespace: katana
namespace: kube-system
annotations:
"helm.sh/hook": pre-install,pre-upgrade,post-install,post-upgrade
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
Expand All @@ -116,14 +116,14 @@ roleRef:
subjects:
- kind: ServiceAccount
name: ingress-nginx-admission
namespace: "katana"
namespace: "kube-system"
---
# Source: ingress-nginx/templates/admission-webhooks/job-patch/job-createSecret.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: ingress-nginx-admission-create
namespace: katana
namespace: kube-system
annotations:
"helm.sh/hook": pre-install,pre-upgrade
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
Expand Down Expand Up @@ -178,7 +178,7 @@ apiVersion: batch/v1
kind: Job
metadata:
name: ingress-nginx-admission-patch
namespace: katana
namespace: kube-system
annotations:
"helm.sh/hook": post-install,post-upgrade
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
Expand Down Expand Up @@ -243,7 +243,7 @@ metadata:
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/component: controller
name: ingress-nginx
namespace: katana
namespace: kube-system
automountServiceAccountToken: true
---
# Source: ingress-nginx/templates/controller-configmap.yaml
Expand All @@ -259,7 +259,7 @@ metadata:
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/component: controller
name: ingress-nginx-controller
namespace: katana
namespace: kube-system
data:
allow-snippet-annotations: "true"
---
Expand Down Expand Up @@ -366,7 +366,7 @@ roleRef:
subjects:
- kind: ServiceAccount
name: ingress-nginx
namespace: "katana"
namespace: "kube-system"
---
# Source: ingress-nginx/templates/controller-role.yaml
apiVersion: rbac.authorization.k8s.io/v1
Expand All @@ -381,7 +381,7 @@ metadata:
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/component: controller
name: ingress-nginx
namespace: katana
namespace: kube-system
rules:
- apiGroups:
- ""
Expand Down Expand Up @@ -474,15 +474,15 @@ metadata:
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/component: controller
name: ingress-nginx
namespace: katana
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: ingress-nginx
subjects:
- kind: ServiceAccount
name: ingress-nginx
namespace: "katana"
namespace: "kube-system"
---
# Source: ingress-nginx/templates/controller-service-webhook.yaml
apiVersion: v1
Expand All @@ -497,7 +497,7 @@ metadata:
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/component: controller
name: ingress-nginx-controller-admission
namespace: katana
namespace: kube-system
spec:
type: ClusterIP
ports:
Expand Down Expand Up @@ -525,7 +525,7 @@ metadata:
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/component: controller
name: ingress-nginx-controller
namespace: katana
namespace: kube-system
spec:
type: LoadBalancer
ipFamilyPolicy: SingleStack
Expand Down Expand Up @@ -560,7 +560,7 @@ metadata:
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/component: controller
name: ingress-nginx-controller
namespace: katana
namespace: kube-system
spec:
selector:
matchLabels:
Expand Down Expand Up @@ -720,6 +720,6 @@ webhooks:
- v1
clientConfig:
service:
namespace: "katana"
namespace: "kube-system"
name: ingress-nginx-controller-admission
path: /networking/v1/ingresses
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions kubernetes/templates/namespace.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: katana
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions lib/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ func DeployCluster(kubeconfig *rest.Config, kubeclientset *kubernetes.Clientset)

deploymentConfig := utils.DeploymentConfig()

for _, m := range clusterConfig.Manifests {
for _, m := range clusterConfig.TemplatedManifests {
manifest := &bytes.Buffer{}
log.Printf("Applying: %s\n", m)
tmpl, err := template.ParseFiles(filepath.Join(clusterConfig.ManifestDir, m))
tmpl, err := template.ParseFiles(filepath.Join(clusterConfig.TemplatedManifestDir, m))
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions lib/harbor/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func addHarborHostsEntry() error {
deploymentName := "katana-release-harbor-core"
namespace := "katana"

err = waitForLoadBalancerExternalIP(client, serviceName)
err = utils.WaitForLoadBalancerExternalIP(client, serviceName, namespace)
if err != nil {
return err
}

err = waitForDeploymentReady(client, deploymentName)
err = utils.WaitForDeploymentReady(client, deploymentName, namespace)
if err != nil {
return err
}
Expand Down Expand Up @@ -133,7 +133,7 @@ func deployHarborClusterDaemonSet() error {

manifest := &bytes.Buffer{}

tmpl, err := template.ParseFiles(filepath.Join(configs.ClusterConfig.ManifestRuntimeDir, "harbor-daemonset.yml"))
tmpl, err := template.ParseFiles(filepath.Join(configs.ClusterConfig.TemplatedManifestDir, "runtime", "harbor-daemonset.yml"))
if err != nil {
return err
}
Expand Down
56 changes: 0 additions & 56 deletions lib/harbor/kube.go

This file was deleted.

64 changes: 64 additions & 0 deletions lib/utils/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,67 @@ func DeleteConfigMapAndWait(kubeClientset *kubernetes.Clientset, kubeConfig *res

watcher.Stop()
}

func WaitForLoadBalancerExternalIP(clientset *kubernetes.Clientset, serviceName string, namespace string) error {
service, err := clientset.CoreV1().Services(namespace).Get(context.TODO(), serviceName, metav1.GetOptions{})
if err != nil {
return err
}

if service.Status.LoadBalancer.Ingress != nil && len(service.Status.LoadBalancer.Ingress) > 0 && service.Status.LoadBalancer.Ingress[0].IP != "" {
return nil
}

watcher, err := clientset.CoreV1().Services(namespace).Watch(context.TODO(), metav1.ListOptions{
FieldSelector: "metadata.name=" + serviceName,
})
if err != nil {
return err
}
defer watcher.Stop()

for event := range watcher.ResultChan() {
service, ok := event.Object.(*v1.Service)
if !ok {
continue
}

if service.Status.LoadBalancer.Ingress != nil && len(service.Status.LoadBalancer.Ingress) > 0 && service.Status.LoadBalancer.Ingress[0].IP != "" {
return nil
}
}

return nil
}

func WaitForDeploymentReady(clientset *kubernetes.Clientset, deploymentName string, namespace string) error {
deployment, err := clientset.AppsV1().Deployments(namespace).Get(context.TODO(), deploymentName, metav1.GetOptions{})
if err != nil {
return err
}

if deployment.Status.ReadyReplicas > 0 {
return nil
}

watcher, err := clientset.AppsV1().Deployments(namespace).Watch(context.TODO(), metav1.ListOptions{
FieldSelector: "metadata.name=" + deploymentName,
})
if err != nil {
return err
}
defer watcher.Stop()

for event := range watcher.ResultChan() {
deployment, ok := event.Object.(*appsv1.Deployment)
if !ok {
continue
}

if deployment.Status.ReadyReplicas > 0 {
return nil
}
}

return nil
}
Loading

0 comments on commit 71e6b45

Please sign in to comment.