func (c *Client) CreateService(w http.ResponseWriter, r *http.Request) {
k8sClient, err := kubernetes.NewForConfig(c.Config)
if err != nil {
config.Error.Printf(err.Error())
config.ResponseWriter(w, config.STATUSINTERNALERROR, err.Error(), err.Error())
return
}
// Payload field must start with Upper Case, and Request Body must start with Lower Case
type Payload struct {
Project string
Name string
// Labels should always be name: xxxx, app: xxxx
Labels map[string]string
Ports []corev1.ServicePort
Selector map[string]string
Type corev1.ServiceType
}
decoder := json.NewDecoder(r.Body)
var payload Payload
err = decoder.Decode(&payload)
if err != nil {
config.Error.Printf(err.Error())
config.ResponseWriter(w, config.STATUSINTERNALERROR, err.Error(), err.Error())
return
}
params := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: payload.Name,
Labels: payload.Labels,
},
Spec: corev1.ServiceSpec{
Ports: payload.Ports,
Selector: payload.Selector,
Type: payload.Type,
},
}
result, err := k8sClient.CoreV1().Services(payload.Project).Create(params)
if err != nil {
config.Error.Printf(err.Error())
if err.Error() == "Unauthorized" {
config.ResponseWriter(w, config.STATUSUNAUTHORIZEDERROR, err.Error(), err.Error())
return
}
config.ResponseWriter(w, config.STATUSINTERNALERROR, err.Error(), err.Error())
return
}
config.ResponseWriter(w, config.STATUSOK, result, result)
return
}