Expose git artifacts inside the cluster

This commit is contained in:
stefanprodan 2020-04-07 10:49:57 +03:00
parent d2b0653c34
commit 98901f2909
9 changed files with 66 additions and 29 deletions

View File

@ -1,20 +1,20 @@
# Build the manager binary
FROM golang:1.13 as builder FROM golang:1.13 as builder
WORKDIR /workspace WORKDIR /workspace
# Copy the Go Modules manifests
# copy modules manifests
COPY go.mod go.mod COPY go.mod go.mod
COPY go.sum go.sum 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 RUN go mod download
# Copy the go source # copy source code
COPY main.go main.go COPY main.go main.go
COPY api/ api/ COPY api/ api/
COPY controllers/ controllers/ COPY controllers/ controllers/
# Build # build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o sourcer main.go RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o sourcer main.go
FROM alpine:3.11 FROM alpine:3.11

View File

@ -1,6 +1,5 @@
# This kustomization.yaml is not intended to be run by itself, apiVersion: kustomize.config.k8s.io/v1beta1
# since it depends on service name and namespace that are out of this kustomize package. kind: Kustomization
# It should be run by config/default
resources: resources:
- bases/sourcer.fluxcd.io_gitrepositories.yaml - bases/sourcer.fluxcd.io_gitrepositories.yaml
- bases/sourcer.fluxcd.io_helmrepositories.yaml - bases/sourcer.fluxcd.io_helmrepositories.yaml

View File

@ -1,8 +1,7 @@
# Adds namespace to all resources. apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: sourcer-system namespace: sourcer-system
namePrefix: sourcer- namePrefix: sourcer-
bases: bases:
- ../crd - ../crd
- ../rbac - ../rbac

View File

@ -1,26 +1,19 @@
apiVersion: v1
kind: Namespace
metadata:
labels:
control-plane: controller-manager
name: system
---
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
name: controller-manager name: controller
namespace: system namespace: system
labels: labels:
control-plane: controller-manager control-plane: controller
spec: spec:
selector: selector:
matchLabels: matchLabels:
control-plane: controller-manager app: sourcer
replicas: 1 replicas: 1
template: template:
metadata: metadata:
labels: labels:
control-plane: controller-manager app: sourcer
annotations: annotations:
prometheus.io/scrape: "true" prometheus.io/scrape: "true"
prometheus.io/port: "9090" prometheus.io/port: "9090"
@ -28,7 +21,7 @@ spec:
terminationGracePeriodSeconds: 10 terminationGracePeriodSeconds: 10
containers: containers:
- name: manager - name: manager
image: fluxcd/sourcer:latest image: controller
ports: ports:
- containerPort: 8080 - containerPort: 8080
name: http name: http
@ -37,6 +30,11 @@ spec:
args: args:
- --enable-leader-election - --enable-leader-election
- --storage-path=/data - --storage-path=/data
env:
- name: RUNTIME_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
resources: resources:
limits: limits:
cpu: 1000m cpu: 1000m

View File

@ -1,2 +1,10 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources: resources:
- manager.yaml - namespace.yaml
- service.yaml
- deployment.yaml
images:
- name: controller
newName: fluxcd/sourcer
newTag: 0.0.1-alpha.1

View File

@ -0,0 +1,6 @@
apiVersion: v1
kind: Namespace
metadata:
labels:
control-plane: controller
name: system

View File

@ -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

View File

@ -1,6 +1,7 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources: resources:
- role.yaml - role.yaml
- role_binding.yaml - role_binding.yaml
- leader_election_role.yaml - leader_election_role.yaml
- leader_election_role_binding.yaml - leader_election_role_binding.yaml

View File

@ -24,6 +24,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/event"
"strings"
"time" "time"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
@ -197,7 +198,7 @@ func (r *GitRepositoryReconciler) sync(gr sourcerv1.GitRepository) (sourcerv1.Re
// store artifacts // store artifacts
artifacts := filepath.Join(storage, fmt.Sprintf("%s.tar.gz", ref.Hash().String())) 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", command := exec.Command("/bin/sh", "-c",
fmt.Sprintf("cd %s && tar -c %s -f - . | gzip > %s", dir, excludes, artifacts)) fmt.Sprintf("cd %s && tar -c %s -f - . | gzip > %s", dir, excludes, artifacts))
err = command.Run() err = command.Run()
@ -211,13 +212,22 @@ func (r *GitRepositoryReconciler) sync(gr sourcerv1.GitRepository) (sourcerv1.Re
}, "", ex }, "", 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{ return sourcerv1.RepositoryCondition{
Type: sourcerv1.RepositoryConditionReady, Type: sourcerv1.RepositoryConditionReady,
Status: corev1.ConditionTrue, Status: corev1.ConditionTrue,
Reason: "GitCloneSucceed", Reason: "GitCloneSucceed",
Message: fmt.Sprintf("Fetched artifacts are available at %s", artifacts), Message: fmt.Sprintf("Fetched artifacts are available at %s", artifacts),
}, output, nil }, artifactsURL, nil
} }
func (r *GitRepositoryReconciler) shouldResetStatus(gr sourcerv1.GitRepository) bool { func (r *GitRepositoryReconciler) shouldResetStatus(gr sourcerv1.GitRepository) bool {