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

[Task] deployment email label #323

Merged
merged 2 commits into from
Sep 20, 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
14 changes: 11 additions & 3 deletions src/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ var allCapabilities = []corev1.Capability{"NET_ADMIN", "SYS_ADMIN", "NET_RAW"}
// VolumeNamePrefix will set prefix of volumename
const VolumeNamePrefix = "volume-"

// DefaultLabel is the label we used for our deploying application/deployment/pods
// DefaultLabel is the label we used for our deploying application/deployment/pods
const DefaultLabel = "vortex"

// NotificationEmailAccount is the label we do notify to user email account
const NotificationEmailAccount = "email_account"

// NotificationEmailDomain is the label we do notify to user email domain
const NotificationEmailDomain = "email_domain"

// CheckDeploymentParameter will Check Deployment's Parameter
func CheckDeploymentParameter(sp *serviceprovider.Container, deploy *entity.Deployment) error {
session := sp.Mongo.NewSession()
Expand Down Expand Up @@ -280,9 +286,9 @@ func CreateDeployment(sp *serviceprovider.Container, deploy *entity.Deployment)
nodeAffinity = utils.Intersection(nodeAffinity, tmp)
}
case entity.DeploymentClusterNetwork:
//For cluster network, we won't set the nodeAffinity and any network options.
// For cluster network, we won't set the nodeAffinity and any network options.
default:
err = fmt.Errorf("UnSupported Deployment NetworkType %s", deploy.NetworkType)
err = fmt.Errorf("Unsupported Deployment NetworkType %s", deploy.NetworkType)
}

if err != nil {
Expand Down Expand Up @@ -320,6 +326,7 @@ func CreateDeployment(sp *serviceprovider.Container, deploy *entity.Deployment)
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
// vortex default label
DefaultLabel: deploy.Name,
},
},
Expand All @@ -330,6 +337,7 @@ func CreateDeployment(sp *serviceprovider.Container, deploy *entity.Deployment)
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
// vortex default label
DefaultLabel: deploy.Name,
},
},
Expand Down
31 changes: 27 additions & 4 deletions src/server/handler_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math"
"net/http"
"strconv"
"strings"

"github.com/linkernetworks/utils/timeutils"
"github.com/linkernetworks/vortex/src/deployment"
Expand Down Expand Up @@ -58,6 +59,18 @@ func createDeploymentHandler(ctx *web.Context) {
return
}

p.OwnerID = bson.ObjectIdHex(userID)
// find owner in user entity
ownerUser, _ := backend.FindUserByID(session, p.OwnerID)

var account, domain string
components := strings.Split(ownerUser.LoginCredential.Username, "@")
account, domain = components[0], components[1]

// append label with owner email
p.Labels[deployment.NotificationEmailAccount] = account
p.Labels[deployment.NotificationEmailDomain] = domain

if err := deployment.CreateDeployment(sp, &p); err != nil {
if errors.IsAlreadyExists(err) {
response.Conflict(req.Request, resp.ResponseWriter, fmt.Errorf("Deployment Name: %s already existed", p.Name))
Expand All @@ -70,7 +83,6 @@ func createDeploymentHandler(ctx *web.Context) {
}
return
}
p.OwnerID = bson.ObjectIdHex(userID)
if err := session.Insert(entity.DeploymentCollectionName, &p); err != nil {
if mgo.IsDup(err) {
response.Conflict(req.Request, resp.ResponseWriter, fmt.Errorf("Deployment Name: %s already existed", p.Name))
Expand All @@ -79,8 +91,7 @@ func createDeploymentHandler(ctx *web.Context) {
}
return
}
// find owner in user entity
p.CreatedBy, _ = backend.FindUserByID(session, p.OwnerID)
p.CreatedBy = ownerUser
resp.WriteHeaderAndEntity(http.StatusCreated, p)
}

Expand Down Expand Up @@ -265,6 +276,18 @@ func uploadDeploymentYAMLHandler(ctx *web.Context) {
defer session.Close()

d.CreatedAt = timeutils.Now()

// find owner in user entity
ownerUser, _ := backend.FindUserByID(session, d.OwnerID)

var account, domain string
components := strings.Split(ownerUser.LoginCredential.Username, "@")
account, domain = components[0], components[1]

// append label with owner email
deploymentObj.ObjectMeta.Labels[deployment.NotificationEmailAccount] = account
deploymentObj.ObjectMeta.Labels[deployment.NotificationEmailDomain] = domain

_, err = sp.KubeCtl.CreateDeployment(deploymentObj, d.Namespace)
if err != nil {
if errors.IsAlreadyExists(err) {
Expand All @@ -288,6 +311,6 @@ func uploadDeploymentYAMLHandler(ctx *web.Context) {
return
}
// find owner in user entity
d.CreatedBy, _ = backend.FindUserByID(session, d.OwnerID)
d.CreatedBy = ownerUser
resp.WriteHeaderAndEntity(http.StatusCreated, d)
}