From 98901f2909d29ad47b85c16c42ad747a86d26825 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Tue, 7 Apr 2020 10:49:57 +0300 Subject: [PATCH] Expose git artifacts inside the cluster --- Dockerfile | 12 +++++----- config/crd/kustomization.yaml | 5 ++--- config/default/kustomization.yaml | 5 ++--- .../manager/{manager.yaml => deployment.yaml} | 22 +++++++++---------- config/manager/kustomization.yaml | 10 ++++++++- config/manager/namespace.yaml | 6 +++++ config/manager/service.yaml | 16 ++++++++++++++ config/rbac/kustomization.yaml | 3 ++- controllers/gitrepository_controller.go | 16 +++++++++++--- 9 files changed, 66 insertions(+), 29 deletions(-) rename config/manager/{manager.yaml => deployment.yaml} (74%) create mode 100644 config/manager/namespace.yaml create mode 100644 config/manager/service.yaml diff --git a/Dockerfile b/Dockerfile index 7ffb17ba..28eafea7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,20 @@ -# Build the manager binary FROM golang:1.13 as builder WORKDIR /workspace -# Copy the Go Modules manifests + +# copy modules manifests COPY go.mod go.mod COPY go.sum go.sum -# cache deps before building and copying source so that we don't need to re-download as much -# and so that source changes don't invalidate our downloaded layer + +# cache modules RUN go mod download -# Copy the go source +# copy source code COPY main.go main.go COPY api/ api/ COPY controllers/ controllers/ -# Build +# build RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o sourcer main.go FROM alpine:3.11 diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index 3778d8ac..904adb68 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -1,6 +1,5 @@ -# This kustomization.yaml is not intended to be run by itself, -# since it depends on service name and namespace that are out of this kustomize package. -# It should be run by config/default +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization resources: - bases/sourcer.fluxcd.io_gitrepositories.yaml - bases/sourcer.fluxcd.io_helmrepositories.yaml diff --git a/config/default/kustomization.yaml b/config/default/kustomization.yaml index 5e8a1a1f..ddc00689 100644 --- a/config/default/kustomization.yaml +++ b/config/default/kustomization.yaml @@ -1,8 +1,7 @@ -# Adds namespace to all resources. +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization namespace: sourcer-system - namePrefix: sourcer- - bases: - ../crd - ../rbac diff --git a/config/manager/manager.yaml b/config/manager/deployment.yaml similarity index 74% rename from config/manager/manager.yaml rename to config/manager/deployment.yaml index 71b38f75..a021949f 100644 --- a/config/manager/manager.yaml +++ b/config/manager/deployment.yaml @@ -1,26 +1,19 @@ -apiVersion: v1 -kind: Namespace -metadata: - labels: - control-plane: controller-manager - name: system ---- apiVersion: apps/v1 kind: Deployment metadata: - name: controller-manager + name: controller namespace: system labels: - control-plane: controller-manager + control-plane: controller spec: selector: matchLabels: - control-plane: controller-manager + app: sourcer replicas: 1 template: metadata: labels: - control-plane: controller-manager + app: sourcer annotations: prometheus.io/scrape: "true" prometheus.io/port: "9090" @@ -28,7 +21,7 @@ spec: terminationGracePeriodSeconds: 10 containers: - name: manager - image: fluxcd/sourcer:latest + image: controller ports: - containerPort: 8080 name: http @@ -37,6 +30,11 @@ spec: args: - --enable-leader-election - --storage-path=/data + env: + - name: RUNTIME_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace resources: limits: cpu: 1000m diff --git a/config/manager/kustomization.yaml b/config/manager/kustomization.yaml index 5c5f0b84..20f92b25 100644 --- a/config/manager/kustomization.yaml +++ b/config/manager/kustomization.yaml @@ -1,2 +1,10 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization resources: -- manager.yaml +- namespace.yaml +- service.yaml +- deployment.yaml +images: +- name: controller + newName: fluxcd/sourcer + newTag: 0.0.1-alpha.1 diff --git a/config/manager/namespace.yaml b/config/manager/namespace.yaml new file mode 100644 index 00000000..51b2818c --- /dev/null +++ b/config/manager/namespace.yaml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: Namespace +metadata: + labels: + control-plane: controller + name: system diff --git a/config/manager/service.yaml b/config/manager/service.yaml new file mode 100644 index 00000000..4ebd3dd7 --- /dev/null +++ b/config/manager/service.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Service +metadata: + name: controller + namespace: system + labels: + control-plane: controller +spec: + type: ClusterIP + selector: + app: sourcer + ports: + - name: http + port: 80 + protocol: TCP + targetPort: http diff --git a/config/rbac/kustomization.yaml b/config/rbac/kustomization.yaml index 08dd6cb5..eaa92570 100644 --- a/config/rbac/kustomization.yaml +++ b/config/rbac/kustomization.yaml @@ -1,6 +1,7 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization resources: - role.yaml - role_binding.yaml - leader_election_role.yaml - leader_election_role_binding.yaml - diff --git a/controllers/gitrepository_controller.go b/controllers/gitrepository_controller.go index 280b1193..12e733bc 100644 --- a/controllers/gitrepository_controller.go +++ b/controllers/gitrepository_controller.go @@ -24,6 +24,7 @@ import ( "os/exec" "path/filepath" "sigs.k8s.io/controller-runtime/pkg/event" + "strings" "time" "github.com/go-git/go-git/v5" @@ -197,7 +198,7 @@ func (r *GitRepositoryReconciler) sync(gr sourcerv1.GitRepository) (sourcerv1.Re // store artifacts artifacts := filepath.Join(storage, fmt.Sprintf("%s.tar.gz", ref.Hash().String())) - excludes := "--exclude=\\*.{jpg,gif,png,wmv,flv,tar.gz,zip} --exclude .git" + excludes := "--exclude=\\*.{jpg,jpeg,gif,png,wmv,flv,tar.gz,zip} --exclude .git" command := exec.Command("/bin/sh", "-c", fmt.Sprintf("cd %s && tar -c %s -f - . | gzip > %s", dir, excludes, artifacts)) err = command.Run() @@ -211,13 +212,22 @@ func (r *GitRepositoryReconciler) sync(gr sourcerv1.GitRepository) (sourcerv1.Re }, "", ex } - output := fmt.Sprintf("/repositories/%s-%s/%s.tar.gz", gr.Name, gr.Namespace, ref.Hash().String()) + // compose artifacts URL + hostname := "localhost" + if os.Getenv("RUNTIME_NAMESPACE") != "" { + svcParts := strings.Split(os.Getenv("HOSTNAME"), "-") + hostname = fmt.Sprintf("%s.%s", + strings.Join(svcParts[:len(svcParts)-2], "-"), os.Getenv("RUNTIME_NAMESPACE")) + } + artifactsURL := fmt.Sprintf("http://%s/repositories/%s-%s/%s.tar.gz", + hostname, gr.Name, gr.Namespace, ref.Hash().String()) + return sourcerv1.RepositoryCondition{ Type: sourcerv1.RepositoryConditionReady, Status: corev1.ConditionTrue, Reason: "GitCloneSucceed", Message: fmt.Sprintf("Fetched artifacts are available at %s", artifacts), - }, output, nil + }, artifactsURL, nil } func (r *GitRepositoryReconciler) shouldResetStatus(gr sourcerv1.GitRepository) bool {