Skip to content

Commit

Permalink
Merge pull request #15 from goci-io/initial
Browse files Browse the repository at this point in the history
complete test with git pull enhancer
  • Loading branch information
etwillbefine committed Jul 10, 2020
2 parents cc36663 + 88b8871 commit 01e9d28
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 12 deletions.
8 changes: 4 additions & 4 deletions cmd/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ func (client *Client) CreateJob(job *DeploymentJob) error {
Image: job.Image,
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: *resource.NewQuantity(300, resource.DecimalSI),
corev1.ResourceMemory: *resource.NewQuantity(156 * 1024*1024, resource.BinarySI),
corev1.ResourceCPU: resource.MustParse("300m"),
corev1.ResourceMemory: resource.MustParse("156Mi"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: *resource.NewQuantity(300, resource.DecimalSI),
corev1.ResourceMemory: *resource.NewQuantity(156 * 1024*1024, resource.BinarySI),
corev1.ResourceCPU: resource.MustParse("300m"),
corev1.ResourceMemory: resource.MustParse("156Mi"),
},
},
EnvFrom: []corev1.EnvFromSource{
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubernetes/enhancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func loadAndParseEnhancers(path string) ([]Enhancer, error) {
for i := range configs {
provider := &configs[i]
attributes, _ := yaml.Marshal(provider.Config)
enhancer, err := unmarshalEnhancerAttributes(provider, attributes);
enhancer, err := unmarshalEnhancerAttributes(provider, attributes)
if err != nil {
return enhancers, err
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/kubernetes/enhancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ func TestEnhancerLoadAndParseCreatesEnhancers(t *testing.T) {
enhancers, err := loadAndParseEnhancers("../../config/enhancers.yaml")

if err != nil {
t.Error("error loading providers config: " + err.Error())
t.Error("error loading enhancer config: " + err.Error())
}

if len(enhancers) != 1 {
t.Error("expected exactly one example kiam provider to be configured")
if len(enhancers) != 2 {
t.Error("expected exactly two example enhancers to be configured")
}

kiam := enhancers[0].(*KiamConigEnhancer)
if kiam.KeySuffix != "goci-app" || kiam.Key() != "aws-kiam-goci-app" {
gp := enhancers[0].(*PullGitSourcesEnhancer)
kiam := enhancers[1].(*KiamConigEnhancer)
if gp.Key() != "git-pull" || kiam.KeySuffix != "goci-app" || kiam.Key() != "aws-kiam-goci-app" {
t.Error("key suffix not correctly mapped. got: " + kiam.KeySuffix)
}

Expand Down
9 changes: 9 additions & 0 deletions cmd/kubernetes/git_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ func (enhancer *PullGitSourcesEnhancer) EnhanceJob(job *batchv1.Job, d JobData)
if job.Spec.Template.Spec.Volumes == nil {
job.Spec.Template.Spec.Volumes = []corev1.Volume{}
}
if job.Spec.Template.Spec.Containers[0].VolumeMounts == nil {
job.Spec.Template.Spec.Containers[0].VolumeMounts = []corev1.VolumeMount{}
}

job.Spec.Template.Spec.Containers[0].VolumeMounts = append(job.Spec.Template.Spec.Containers[0].VolumeMounts, corev1.VolumeMount{
Name: "sources",
ReadOnly: false,
MountPath: "/run/workspace",
})

pullCmd := fmt.Sprintf("git clone %s@%s:%s/%s.git",
enhancer.GitUser, enhancer.GitHost, data.Organization(), data.Repository())
Expand Down
22 changes: 20 additions & 2 deletions cmd/server/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,40 @@ type DeploymentsHandler struct {
configs map[string]RepositoryConfig
}

type DeploymentWebhookData struct {
repository string
organization string
}

func (d *DeploymentsHandler) deploy(context *WebhookContext) error {
jobName := fmt.Sprintf("%s-%s-%s", context.Repository.Organization, context.Repository.Name, randStringBytes(6))
configName := fmt.Sprintf("%s-%s", context.Repository.Organization, context.Repository.Name)
configName := fmt.Sprintf("%s/%s", context.Repository.Organization, context.Repository.Name)
secretName := fmt.Sprintf("%s-%s", context.Repository.Organization, context.Repository.Name)
config := d.configs[configName]

job := &k8s.DeploymentJob{
Name: jobName,
SecretEnvName: configName,
SecretEnvName: secretName,
Labels: map[string]string{},
Data: &DeploymentWebhookData{
repository: context.Repository.Name,
organization: context.Repository.Organization,
},
}

copyConfigInto(config, job)

return d.kubernetes.CreateJob(job)
}

func (d *DeploymentWebhookData) Repository() string {
return d.repository
}

func (d *DeploymentWebhookData) Organization() string {
return d.organization
}

func copyConfigInto(config RepositoryConfig, into *k8s.DeploymentJob) {
into.Image = config.Image
into.Enhancers = config.Enhancers
Expand Down
1 change: 1 addition & 0 deletions config/enhancers.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- provider: git-pull
- externalId: AvoidConfusedDeputyProblem
roleName: goci-build-app-role
accountId: 123456789012
Expand Down

0 comments on commit 01e9d28

Please sign in to comment.