Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow webhook to create a deployment #1227

Merged
merged 2 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions deploy/webhook/cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ steps:
- '-f'
- 'deploy/webhook/skaffold.yaml'
- 'run'
- '--tag'
- '${COMMIT_SHA}'
3 changes: 3 additions & 0 deletions pkg/webhook/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ const (

// HugoPort is the port that hugo defaults to
HugoPort = 1313

// DeploymentImage is the image the controller deploys, must contain hugo and git
DeploymentImage = "gcr.io/k8s-skaffold/docs-controller@sha256:e4b58adfdc59fd916092d6c30026e5e1a75095013fec5381d8e58a85161b9977"
)
131 changes: 131 additions & 0 deletions pkg/webhook/kubernetes/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
Copyright 2018 The Skaffold Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kubernetes

import (
"context"
"fmt"
"path"
"time"

pkgkubernetes "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
"github.com/GoogleContainerTools/skaffold/pkg/webhook/constants"
"github.com/GoogleContainerTools/skaffold/pkg/webhook/labels"
"github.com/google/go-github/github"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
emptyVol = "empty-vol"
emptyVolPath = "/empty"
)

// CreateDeployment creates a deployment for this pull request
// The deployment has two containers:
// 1. An init container to git clone the PR branch
// 2. A container to run hugo server
// and one emptyDir volume to hold the git repository
func CreateDeployment(pr *github.PullRequestEvent, svc *v1.Service, externalIP string) (*appsv1.Deployment, error) {
clientset, err := pkgkubernetes.GetClientset()
if err != nil {
return nil, errors.Wrap(err, "getting clientset")
}

deploymentLabels := svc.Spec.Selector
_, name := labels.RetrieveLabel(pr.GetNumber())

userRepo := fmt.Sprintf("https://github.com/%s.git", *pr.PullRequest.Head.Repo.FullName)
// path to the docs directory, which we will run "hugo server -D" in
docsPath := path.Join(emptyVolPath, *pr.PullRequest.Head.Repo.Name, "docs")

d := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: deploymentLabels,
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: deploymentLabels,
},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: deploymentLabels,
},
Spec: v1.PodSpec{
InitContainers: []v1.Container{
{
Name: "git-clone",
Image: constants.DeploymentImage,
Args: []string{"git", "clone", userRepo, "--branch", pr.PullRequest.Head.GetRef()},
WorkingDir: emptyVolPath,
VolumeMounts: []v1.VolumeMount{
{
Name: emptyVol,
MountPath: emptyVolPath,
},
},
},
},
Containers: []v1.Container{
{
Name: "server",
Image: constants.DeploymentImage,
Args: []string{"hugo", "server", "--bind=0.0.0.0", "-D", "--baseURL", baseURL(externalIP)},
WorkingDir: docsPath,
VolumeMounts: []v1.VolumeMount{
{
Name: emptyVol,
MountPath: emptyVolPath,
},
},
Ports: []v1.ContainerPort{
{
ContainerPort: constants.HugoPort,
},
},
},
},
Volumes: []v1.Volume{
{
Name: emptyVol,
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
},
},
},
},
}
return clientset.AppsV1().Deployments(constants.Namespace).Create(d)
}

// WaitForDeploymentToStabilize waits till the Deployment has stabilized
func WaitForDeploymentToStabilize(d *appsv1.Deployment) error {
client, err := pkgkubernetes.GetClientset()
if err != nil {
return errors.Wrap(err, "getting clientset")
}
return pkgkubernetes.WaitForDeploymentToStabilize(context.Background(), client, d.Namespace, d.Name, 5*time.Minute)
}

func baseURL(ip string) string {
return fmt.Sprintf("http://%s:%d", ip, constants.HugoPort)
}
13 changes: 9 additions & 4 deletions webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func handlePullRequestEvent(event *github.PullRequestEvent) error {
}

log.Printf("Label %s found on PR %d, creating service", constants.DocsLabel, prNumber)

svc, err := kubernetes.CreateService(event)
if err != nil {
return errors.Wrap(err, "creating service")
Expand All @@ -91,8 +90,14 @@ func handlePullRequestEvent(event *github.PullRequestEvent) error {
return errors.Wrap(err, "getting external IP")
}

log.Printf("External IP %s ready for PR %d", ip, prNumber)

// TODO: priyawadhwa@ to add logic for creating a deployment mapping to a service here
log.Printf("Creating deployment for pull request %d", prNumber)
deployment, err := kubernetes.CreateDeployment(event, svc, ip)
if err != nil {
return errors.Wrap(err, "creating deployment")
}
if err := kubernetes.WaitForDeploymentToStabilize(deployment); err != nil {
return errors.Wrapf(err, "waiting for deployment %s to stabilize", deployment.Name)
}
// TODO: priyawadhwa@ to add logic for commenting on Github once the deployment is ready
return nil
}