Skip to content

Commit

Permalink
feat(deploy) lets add a helm chart so we can run cronjobs
Browse files Browse the repository at this point in the history
  • Loading branch information
rawlingsj committed Jun 8, 2018
1 parent 0f16367 commit 5fe662d
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 40 deletions.
12 changes: 12 additions & 0 deletions Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
name: jx
description: Jenkins X next gen cloud CI / CD platform for Kubernetes
home: https://jenkins-x.io/
version: 0.0.1-SNAPSHOT
appVersion: 0.1
icon: https://jenkins-x.github.io/jenkins-x-website/img/profile.png
sources:
- https://github.com/jenkins-x/jx
maintainers:
- name: Jenkins X Team
email: [email protected]
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
FROM scratch
FROM centos:7

ENTRYPOINT ["/jx", "version"]

COPY build/jx-linux-amd64 /jx
ENTRYPOINT ["jx", "version"]

COPY build/jx-linux-amd64 /usr/bin/jx
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ clean:
linux:
CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=amd64 $(GO) build $(BUILDFLAGS) -o build/$(NAME)-linux-amd64 cmd/jx/jx.go

docker: linux Dockerfile.buildgo
docker build --no-cache -t rawlingsj/jx:dev -f Dockerfile .
docker: linux
docker build --no-cache -t jenkinsxio/jx:dev .
docker push jenkinsxio/jx:dev

docker-go: linux Dockerfile.buildgo
docker build --no-cache -t builder-go -f Dockerfile.buildgo .
Expand Down
8 changes: 4 additions & 4 deletions charts/jx/values.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
image:
repository: rawlingsj/jx
tag: dev10
repository: jenkinsxio/jx
tag: dev
pullPolicy: IfNotPresent

replicaCount: 1
Expand All @@ -21,13 +21,13 @@ cronjob:
successfulJobsHistoryLimit: 3
concurrencyPolicy: Forbid

activeDeadlineSeconds: 100
activeDeadlineSeconds: 300
backoffLimit: 5

# default is Always but for Jobs use Never or OnFailure
restartPolicy: Never

command: ["/jx"]
command: ["jx"]
args:
- "gc"
- "activities"
Expand Down
7 changes: 3 additions & 4 deletions pkg/jx/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,21 @@ func (o *CommonOptions) JenkinsClient() (*gojenkins.Jenkins, error) {
return o.jenkinsClient, nil
}

func (o *CommonOptions) inclusterSetup() error {
// TODO find a better way to figure out of we are incluster
func (o *CommonOptions) inclusterJenkinsAuthSetup() error {
c, ns, err := o.KubeClient()
if err != nil {
return err
}
s, err := c.CoreV1().Secrets(ns).Get(kube.SecretJenkins, v1.GetOptions{})
if err != nil {
// not running incluster so fallback to getting auth file from local machine
return nil
return err
}
apiToken := s.Data[kube.JenkinsAdminApiToken]
j := CreateJenkinsUserOptions{
CreateOptions: CreateOptions{
CommonOptions: *o,
},
Username: "admin",
ApiToken: string(apiToken),
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/jx/cmd/delete_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,10 @@ func (o *DeleteEnvOptions) Run() error {
return err
}
kube.RegisterEnvironmentCRD(apisClient)

ns, currentEnv, err := kube.GetDevNamespace(kubeClient, currentNs)
if err != nil {
return err
}

envMap, envNames, err := kube.GetEnvironments(jxClient, ns)
if err != nil {
return err
Expand Down
10 changes: 6 additions & 4 deletions pkg/jx/cmd/gc_previews.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,16 @@ func (o *GCPreviewsOptions) Run() error {
if err != nil {
return err
}

// we need pull request info to include
authConfigSvc, err := o.Factory.CreateGitAuthConfigService()
if err != nil {
return err
}

gitKind, err := o.GitServerKind(gitInfo)
if err != nil {
return err
}

gitProvider, err := gitInfo.CreateProvider(authConfigSvc, gitKind)
if err != nil {
return err
Expand All @@ -112,8 +111,10 @@ func (o *GCPreviewsOptions) Run() error {
if err != nil {
log.Warn("Unable to convert PR " + e.Spec.PreviewGitSpec.Name + " to a number" + "\n")
}

pullRequest, _ := gitProvider.GetPullRequest(gitInfo.Organisation, gitInfo.Name, prNum)
pullRequest, err := gitProvider.GetPullRequest(gitInfo.Organisation, gitInfo.Name, prNum)
if err != nil {
return err
}
lowerState := strings.ToLower(*pullRequest.State)

if strings.HasPrefix(lowerState, "clos") {
Expand All @@ -122,6 +123,7 @@ func (o *GCPreviewsOptions) Run() error {
DeleteNamespace: true,
CommonOptions: o.CommonOptions,
}
log.Info("6\n")
deleteOpts.CommonOptions.Args = []string{e.Name}
err = deleteOpts.Run()
if err != nil {
Expand Down
108 changes: 88 additions & 20 deletions pkg/jx/cmd/util/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"github.com/jenkins-x/jx/pkg/gits"
_ "k8s.io/client-go/plugin/pkg/client/auth"

"net/url"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -76,6 +78,8 @@ type Factory interface {
LoadPipelineSecrets(kind string, serviceKind string) (*corev1.SecretList, error)

ImpersonateUser(user string) Factory

IsInCluster() bool
}

type factory struct {
Expand Down Expand Up @@ -104,7 +108,6 @@ func (f *factory) ImpersonateUser(user string) Factory {

// CreateJenkinsClient creates a new jenkins client
func (f *factory) CreateJenkinsClient() (*gojenkins.Jenkins, error) {

svc, err := f.CreateJenkinsAuthConfigService()
if err != nil {
return nil, err
Expand Down Expand Up @@ -148,10 +151,49 @@ func (f *factory) CreateJenkinsAuthConfigService() (auth.AuthConfigService, erro
if err != nil {
return authConfigSvc, err
}
_, err = authConfigSvc.LoadConfig()
config, err := authConfigSvc.LoadConfig()
if err != nil {
return authConfigSvc, err
}

if len(config.Servers) == 0 {
c, ns, err := f.CreateClient()
if err != nil {
return authConfigSvc, err
}

s, err := c.CoreV1().Secrets(ns).Get(kube.SecretJenkins, metav1.GetOptions{})
if err != nil {
return authConfigSvc, err
}

userAuth := auth.UserAuth{
Username: "admin",
ApiToken: string(s.Data[kube.JenkinsAdminApiToken]),
}
svc, err := c.CoreV1().Services(ns).Get(kube.ServiceJenkins, metav1.GetOptions{})
if err != nil {
return authConfigSvc, err
}
svcURL := kube.GetServiceURL(svc)
if svcURL == "" {
return authConfigSvc, fmt.Errorf("unable to find external URL annotation on service %s", svc.Name)
}

u, err := url.Parse(svcURL)
if err != nil {
return authConfigSvc, err
}
if !userAuth.IsInvalid() {
config.Servers = []*auth.AuthServer{
{
Name: u.Host,
URL: svcURL,
Users: []*auth.UserAuth{&userAuth},
},
}
}
}
return authConfigSvc, err
}

Expand Down Expand Up @@ -255,7 +297,7 @@ func (f *factory) authMergePipelineSecrets(config *auth.AuthConfig, secrets *cor
func (f *factory) CreateGitAuthConfigServiceDryRun(dryRun bool) (auth.AuthConfigService, error) {
if dryRun {
fileName := GitAuthConfigFile
return f.createGitAuthConfigServiceFromSecrets(fileName, nil, false)
return f.createGitAuthConfigServiceFromSecrets(fileName, nil, f.IsInCluster())
}
return f.CreateGitAuthConfigService()
}
Expand All @@ -278,10 +320,10 @@ func (f *factory) CreateGitAuthConfigService() (auth.AuthConfigService, error) {
}

fileName := GitAuthConfigFile
return f.createGitAuthConfigServiceFromSecrets(fileName, secrets, f.isInCDPIpeline())
return f.createGitAuthConfigServiceFromSecrets(fileName, secrets, f.IsInCluster())
}

func (f *factory) createGitAuthConfigServiceFromSecrets(fileName string, secrets *corev1.SecretList, isCDPipeline bool) (auth.AuthConfigService, error) {
func (f *factory) createGitAuthConfigServiceFromSecrets(fileName string, secrets *corev1.SecretList, isIncluster bool) (auth.AuthConfigService, error) {
authConfigSvc, err := f.CreateAuthConfigService(fileName)
if err != nil {
return authConfigSvc, err
Expand All @@ -293,26 +335,43 @@ func (f *factory) createGitAuthConfigServiceFromSecrets(fileName string, secrets
}

if secrets != nil {
f.authMergePipelineSecrets(config, secrets, kube.ValueKindGit, isCDPipeline)
f.authMergePipelineSecrets(config, secrets, kube.ValueKindGit, isIncluster)
}

// lets add a default if there's none defined yet
if len(config.Servers) == 0 {
// if in cluster then there's no user configfile, so check for env vars first
userAuth := auth.CreateAuthUserFromEnvironment("GIT")
if !userAuth.IsInvalid() {
// if no config file is being used lets grab the git server from the current directory
server, err := gits.GetGitServer("")
if err != nil {
fmt.Printf("WARNING: unable to get remote git repo server, %v\n", err)
server = "https://github.com"
}
config.Servers = []*auth.AuthServer{
{
Name: "Git",
URL: server,
Users: []*auth.UserAuth{&userAuth},
},
secretList, err := f.LoadPipelineSecrets(kube.ValueKindGit, "")
if err != nil {
return authConfigSvc, err
}

var data map[string][]byte

if secretList != nil {
for _, secret := range secretList.Items {
labels := secret.Labels
annotations := secret.Annotations
data = secret.Data
if labels != nil && labels[kube.LabelKind] == kube.ValueKindGit && annotations != nil {
u := annotations[kube.AnnotationURL]
if u != "" && data != nil {
userAuth := auth.UserAuth{
Username: string(data[kube.SecretDataUsername]),
ApiToken: string(data[kube.SecretDataPassword]),
}
if !userAuth.IsInvalid() {
config.Servers = []*auth.AuthServer{
{
Name: string(data[kube.AnnotationName]),
URL: u,
Users: []*auth.UserAuth{&userAuth},
},
}
break
}
}
}
}
}
}
Expand Down Expand Up @@ -494,3 +553,12 @@ func (f *factory) isInCDPIpeline() bool {
// or we should test if we are in the cluster and get the current ServiceAccount name?
return os.Getenv("BUILD_NUMBER") != ""
}

// function to tell if we are running incluster
func (f *factory) IsInCluster() bool {
_, err := rest.InClusterConfig()
if err != nil {
return false
}
return true
}
Binary file added templates/.syncthing.cronjob.yaml.tmp
Binary file not shown.
Binary file added templates/.syncthing.daemonset.yaml.tmp
Binary file not shown.
Binary file added templates/.syncthing.deployment.yaml.tmp
Binary file not shown.
Binary file added templates/.syncthing.job.yaml.tmp
Binary file not shown.
1 change: 1 addition & 0 deletions templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

32 changes: 32 additions & 0 deletions templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "jx.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "jx.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "jx.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
23 changes: 23 additions & 0 deletions templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: v1
kind: Service
metadata:
name: {{ template "jx.name" . }}
labels:
app: {{ template "jx.name" . }}
chart: {{ template "jx.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
{{- if .Values.service.serviceAnnotations }}
annotations:
{{ toYaml .Values.service.serviceAnnotations | indent 4 }}
{{- end }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
app: {{ template "jx.name" . }}
release: {{ .Release.Name }}

0 comments on commit 5fe662d

Please sign in to comment.