Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Generate deploy/ automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Jul 25, 2019
1 parent 4e3ba8a commit c312d79
Show file tree
Hide file tree
Showing 16 changed files with 362 additions and 91 deletions.
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.DEFAULT: all
.PHONY: all release-bins clean realclean test integration-test check-generated
.PHONY: all release-bins clean realclean test integration-test generate-deploy check-generated

SUDO := $(shell docker info > /dev/null 2> /dev/null || echo "sudo")

Expand Down Expand Up @@ -120,10 +120,15 @@ $(GOBIN)/helm-operator: $(HELM_OPERATOR_DEPS)
integration-test: all
test/bin/test-flux



generate-deploy: install/generated_templates.gogen.go
cd deploy && go run ../install/generate.go deploy

install/generated_templates.gogen.go: install/templates/*
cd install && go run generate.go
cd install && go run generate.go embedded-templates

check-generated: install/generated_templates.gogen.go
check-generated: generate-deploy install/generated_templates.gogen.go
./bin/helm/update_codegen.sh
git diff --exit-code -- integrations/apis intergrations/client install/generated_templates.gogen.go

13 changes: 8 additions & 5 deletions cmd/fluxctl/install_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io"
"os"

"github.com/spf13/cobra"
Expand All @@ -28,7 +27,7 @@ fluxctl install --git-url 'git@github.com:<your username>/flux-get-started' | ku
"URL of the Git repository to be used by Flux, e.g. git@github.com:<your username>/flux-get-started")
cmd.Flags().StringVarP(&opts.GitBranch, "git-branch", "", "master",
"Git branch to be used by Flux")
cmd.Flags().StringSliceVarP(&opts.GitPaths, "git-paths", "", []string{"/"},
cmd.Flags().StringSliceVarP(&opts.GitPaths, "git-paths", "", []string{},
"Relative paths within the Git repo for Flux to locate Kubernetes manifests")
cmd.Flags().StringVarP(&opts.GitLabel, "git-label", "", "flux",
"Git label to keep track of Flux's sync progress; overrides both --git-sync-tag and --git-notes-ref")
Expand All @@ -47,12 +46,16 @@ func (opts *installOpts) RunE(cmd *cobra.Command, args []string) error {
if opts.GitURL == "" {
fmt.Errorf("please supply a valid --git-url argument")
}
templates, err := install.FillInInstallTemplates(install.TemplateParameters(*opts))
manifests, err := install.FillInInstallTemplates(install.TemplateParameters(*opts))
if err != nil {
return err
}
_, err = io.Copy(os.Stdout, templates)
return err
for fileName, content := range manifests {
if _, err := os.Stdout.Write(content); err != nil {
fmt.Errorf("cannot output manifest file %s: %s", fileName, err)
}

}

return nil
}
37 changes: 37 additions & 0 deletions deploy/flux-account.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
# The service account, cluster roles, and cluster role binding are
# only needed for Kubernetes with role-based access control (RBAC).
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
name: flux
name: flux
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
labels:
name: flux
name: flux
rules:
- apiGroups: ['*']
resources: ['*']
verbs: ['*']
- nonResourceURLs: ['*']
verbs: ['*']
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
labels:
name: flux
name: flux
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: flux
subjects:
- kind: ServiceAccount
name: flux
namespace: default
150 changes: 150 additions & 0 deletions deploy/flux-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: flux
spec:
replicas: 1
selector:
matchLabels:
name: flux
strategy:
type: Recreate
template:
metadata:
annotations:
prometheus.io.port: "3031" # tell prometheus to scrape /metrics endpoint's port.
labels:
name: flux
spec:
serviceAccountName: flux
volumes:
- name: git-key
secret:
secretName: flux-git-deploy
defaultMode: 0400 # when mounted read-only, we won't be able to chmod

# This is a tmpfs used for generating SSH keys. In K8s >= 1.10,
# mounted secrets are read-only, so we need a separate volume we
# can write to.
- name: git-keygen
emptyDir:
medium: Memory

# The following volume is for using a customised known_hosts
# file, which you will need to do if you host your own git
# repo rather than using github or the like. You'll also need to
# mount it into the container, below. See
# https://github.com/weaveworks/flux/blob/master/site/standalone-setup.md#using-a-private-git-host
# - name: ssh-config
# configMap:
# name: flux-ssh-config

# The following volume is for using a customised .kube/config,
# which you will need to do if you wish to have a different
# default namespace. You will also need to provide the configmap
# with an entry for `config`, and uncomment the volumeMount and
# env entries below.
# - name: kubeconfig
# configMap:
# name: flux-kubeconfig

# The following volume is used to import GPG keys (for signing
# and verification purposes). You will also need to provide the
# secret with the keys, and uncomment the volumeMount and args
# below.
# - name: gpg-keys
# secret:
# secretName: flux-gpg-keys
# defaultMode: 0400

containers:
- name: flux
# There are no ":latest" images for flux. Find the most recent
# release or image version at https://hub.docker.com/r/weaveworks/flux/tags
# and replace the tag here.
image: docker.io/fluxcd/flux:1.13.2
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: 50m
memory: 64Mi
ports:
- containerPort: 3030 # informational
volumeMounts:
- name: git-key
mountPath: /etc/fluxd/ssh # to match location given in image's /etc/ssh/config
readOnly: true # this will be the case perforce in K8s >=1.10
- name: git-keygen
mountPath: /var/fluxd/keygen # to match location given in image's /etc/ssh/config

# Include this if you need to mount a customised known_hosts
# file; you'll also need the volume declared above.
# - name: ssh-config
# mountPath: /root/.ssh

# Include this and the volume "kubeconfig" above, and the
# environment entry "KUBECONFIG" below, to override the config
# used by kubectl.
# - name: kubeconfig
# mountPath: /etc/fluxd/kube

# Include this to point kubectl at a different config; you
# will need to do this if you have mounted an alternate config
# from a configmap, as in commented blocks above.
# env:
# - name: KUBECONFIG
# value: /etc/fluxd/kube/config

# Include this and the volume "gpg-keys" above, and the
# args below.
# - name: gpg-keys
# mountPath: /root/gpg-import
# readOnly: true

args:

# If you deployed memcached in a different namespace to flux,
# or with a different service name, you can supply these
# following two arguments to tell fluxd how to connect to it.
# - --memcached-hostname=memcached.default.svc.cluster.local

# Use the memcached ClusterIP service name by setting the
# memcached-service to string empty
- --memcached-service=

# This must be supplied, and be in the tmpfs (emptyDir)
# mounted above, for K8s >= 1.10
- --ssh-keygen-dir=/var/fluxd/keygen

# Replace the following URL to change the Git repository used by Flux.
- --git-url=git@github.com:weaveworks/flux-get-started
- --git-branch=master
# include this if you want to restrict the manifests considered by flux
# to those under the following relative paths in the git repository
# - --git-path=subdir1,subdir2

# Include these two to enable git commit signing
# - --git-gpg-key-import=/root/gpg-import
# - --git-signing-key=<key id>

# Include this to enable git signature verification
# - --git-verify-signatures

# Include these next two to connect to an "upstream" service
# (e.g., Weave Cloud). The token is particular to the service.
# - --connect=wss://cloud.weave.works/api/flux
# - --token=abc123abc123abc123abc123

# Serve /metrics endpoint at different port;
# make sure to set prometheus' annotation to scrape the port value.
- --listen-metrics=:3031


# Optional DNS settings, configuring the ndots option may resolve
# nslookup issues on some Kubernetes setups.
# dnsPolicy: "None"
# dnsConfig:
# options:
# - name: ndots
# value: "1"
6 changes: 6 additions & 0 deletions deploy/flux-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
apiVersion: v1
kind: Secret
metadata:
name: flux-git-deploy
type: Opaque
33 changes: 33 additions & 0 deletions deploy/memcache-dep.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
# memcached deployment used by Flux to cache
# container image metadata.
apiVersion: apps/v1
kind: Deployment
metadata:
name: memcached
spec:
replicas: 1
selector:
matchLabels:
name: memcached
template:
metadata:
labels:
name: memcached
spec:
containers:
- name: memcached
image: memcached:1.5.15
imagePullPolicy: IfNotPresent
args:
- -m 512 # Maximum memory to use, in megabytes
- -I 5m # Maximum size for one item
- -p 11211 # Default port
# - -vv # Uncomment to get logs of each request and response.
ports:
- name: clients
containerPort: 11211
securityContext:
runAsUser: 11211
runAsGroup: 11211
allowPrivilegeEscalation: false
11 changes: 11 additions & 0 deletions deploy/memcache-svc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
apiVersion: v1
kind: Service
metadata:
name: memcached
spec:
ports:
- name: memcached
port: 11211
selector:
name: memcached
49 changes: 41 additions & 8 deletions install/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,53 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"

"github.com/shurcooL/vfsgen"
"github.com/weaveworks/flux/install"
)

func main() {
var fs http.FileSystem = http.Dir("templates/")
err := vfsgen.Generate(fs, vfsgen.Options{
Filename: "generated_templates.gogen.go",
PackageName: "install",
VariableName: "templates",
})
if err != nil {
log.Fatalln(err)
usage := func() {
fmt.Fprintf(os.Stderr, "usage: %s {embedded-templates,deploy}\n", os.Args[0])
os.Exit(1)
}
if len(os.Args) != 2 {
usage()
}
switch os.Args[1] {
case "embedded-templates":
var fs http.FileSystem = http.Dir("templates/")
err := vfsgen.Generate(fs, vfsgen.Options{
Filename: "generated_templates.gogen.go",
PackageName: "install",
VariableName: "templates",
})
if err != nil {
log.Fatalln(err)
}
case "deploy":
params := install.TemplateParameters{
GitURL: "git@github.com:weaveworks/flux-get-started",
GitBranch: "master",
}
manifests, err := install.FillInInstallTemplates(params)
if err != nil {
fmt.Fprintf(os.Stderr, "error: failed to fill in templates: %s\n", err)
os.Exit(1)
}
for fileName, contents := range manifests {
if err := ioutil.WriteFile(fileName, contents, 0600); err != nil {
fmt.Fprintf(os.Stderr, "error: failed to write deploy file %s: %s\n", fileName, err)
os.Exit(1)
}
}

default:
usage()
}
}
Loading

0 comments on commit c312d79

Please sign in to comment.