1010 lines
28 KiB
Go
1010 lines
28 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"main/argohandler"
|
|
"main/db"
|
|
"main/helpers"
|
|
"main/models"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
corev1 "k8s.io/api/core/v1"
|
|
"k8s.io/kubectl/pkg/scheme"
|
|
|
|
// "github.com/gorilla/mux"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/client-go/kubernetes"
|
|
"k8s.io/client-go/rest"
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
"k8s.io/client-go/tools/remotecommand"
|
|
)
|
|
|
|
func CreateClusterHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var cluster models.Cluster
|
|
_ = json.NewDecoder(r.Body).Decode(&cluster)
|
|
|
|
// vclusterCollection := db.Vclusters_details.FindOne(context.TODO(), bson.M{"name": Cluster.Name}).Decode(&existsCluster)
|
|
|
|
if cluster.Name == "" || cluster.ControlPlane == "" || cluster.PlatformVersion == "" || cluster.Cpu == "" || cluster.Memory == "" {
|
|
http.Error(w, "Invalid input", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
var existsCluster models.Cluster
|
|
_ = db.Vclusters_details.FindOne(context.TODO(), bson.M{"name": cluster.Name}).Decode(&existsCluster)
|
|
|
|
if existsCluster.Name == cluster.Name {
|
|
http.Error(w, "Cluster name is duplicated", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
res, err := db.Vclusters_details.InsertOne(context.TODO(), cluster)
|
|
if err != nil {
|
|
http.Error(w, `{"message": "Could not create cluster"}`, http.StatusInternalServerError)
|
|
}
|
|
|
|
objectID := res.InsertedID.(primitive.ObjectID)
|
|
idStr := objectID.Hex()
|
|
|
|
argohandler.CreateApp(idStr, cluster.Name, cluster.ControlPlane, cluster.PlatformVersion, cluster.Cpu, cluster.Memory, "userid")
|
|
|
|
response := map[string]string{"message": "Cluster created"}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
|
|
}
|
|
|
|
func Deletecluster(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clusterName := r.URL.Query().Get("Name")
|
|
if clusterName == "" {
|
|
http.Error(w, "Invalid input", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
err := argohandler.DeleteApp(clusterName)
|
|
if err != nil {
|
|
http.Error(w, "File to delete "+clusterName, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
res, err := db.Vclusters_details.DeleteOne(context.TODO(), bson.M{"name": clusterName})
|
|
if err != nil {
|
|
http.Error(w, `{"message": "Could not delete cluster"}`, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if res.DeletedCount == 0 {
|
|
http.Error(w, `{"message": "No cluster found to delete"}`, http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
}
|
|
|
|
func getClientset(w http.ResponseWriter, clustername string) (*kubernetes.Clientset, *rest.Config, error) {
|
|
|
|
kubeconfig, err := getClusterConfig(clustername)
|
|
if err != nil {
|
|
http.Error(w, "File to get kubeconfig", http.StatusInternalServerError)
|
|
return nil, nil, err
|
|
}
|
|
|
|
kubeconfigbyte := []byte(kubeconfig)
|
|
config, err := clientcmd.RESTConfigFromKubeConfig(kubeconfigbyte)
|
|
if err != nil {
|
|
log.Println("Error creating rest config:", err)
|
|
return nil, nil, err
|
|
}
|
|
|
|
clientset, err := kubernetes.NewForConfig(config)
|
|
if err != nil {
|
|
log.Println("Error creating clientSet:", err)
|
|
return nil, nil, err
|
|
}
|
|
|
|
return clientset, config, nil
|
|
}
|
|
|
|
func ListUserClusters(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var header models.Header
|
|
var user models.User
|
|
header.Authorization = r.Header.Get("Authorization")
|
|
helpers.DecodeJwt(&header.Authorization, &user)
|
|
|
|
err := helpers.ValidateUser(user.Username)
|
|
if err != nil {
|
|
http.Error(w, "Anauthorized User", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
_, clusterList := argohandler.ListUserClusters("userid")
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(clusterList)
|
|
|
|
}
|
|
|
|
func Cluster_namespaces(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clustername := r.URL.Query().Get("Name")
|
|
|
|
if clustername == "" {
|
|
http.Error(w, "Missing 'Name' parameter", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting clientset: ", err)
|
|
}
|
|
|
|
listOfnamespaces, err := clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting list of pods: ", err)
|
|
}
|
|
|
|
var nslist []string
|
|
for _, ns := range listOfnamespaces.Items {
|
|
nslist = append(nslist, ns.Name)
|
|
}
|
|
|
|
if err != nil {
|
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(nslist)
|
|
|
|
}
|
|
|
|
func Cluster_services(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
|
|
if clustername == "" {
|
|
http.Error(w, "Missing 'Name' parameter", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting clientset: ", err)
|
|
}
|
|
|
|
services, err := clientset.CoreV1().Services(namespace).List(context.TODO(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting list of services: ", err)
|
|
}
|
|
|
|
Allservice := []models.Service{}
|
|
|
|
var service models.Service
|
|
now := time.Now()
|
|
for _, s := range services.Items {
|
|
service.Name = s.Name
|
|
service.Namespace = s.Namespace
|
|
service.Type = string(s.Spec.Type)
|
|
service.Ports = servicePortsToString(s.Spec.Ports)
|
|
service.ClusterIP = s.Spec.ClusterIP
|
|
age := now.Sub(s.CreationTimestamp.Time)
|
|
service.Age = helpers.Human(age)
|
|
if len(s.Spec.ExternalIPs) > 0 {
|
|
service.ExternalIP = s.Spec.ExternalIPs[0]
|
|
}
|
|
|
|
Allservice = append(Allservice, service)
|
|
}
|
|
|
|
//pod_list, err := json.Marshal(Allservice)
|
|
if err != nil {
|
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(Allservice)
|
|
|
|
}
|
|
|
|
func Cluster_statefulset(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
|
|
if clustername == "" {
|
|
http.Error(w, "Missing 'Name' parameter", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting clientset: ", err)
|
|
}
|
|
|
|
statefulSets, err := clientset.AppsV1().StatefulSets(namespace).List(context.TODO(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting list of StatefulSets: ", err)
|
|
}
|
|
|
|
AllstatefulSets := []models.StatefulSet{}
|
|
|
|
var StatefulSet models.StatefulSet
|
|
now := time.Now()
|
|
for _, s := range statefulSets.Items {
|
|
desired := int32(1)
|
|
if s.Spec.Replicas != nil {
|
|
desired = *s.Spec.Replicas
|
|
}
|
|
StatefulSet.Ready = fmt.Sprintf("%d/%d", s.Status.ReadyReplicas, desired)
|
|
StatefulSet.Name = s.Name
|
|
StatefulSet.Namespace = s.Namespace
|
|
age := now.Sub(s.CreationTimestamp.Time) // same as kubectl AGE
|
|
StatefulSet.Age = helpers.Human(age)
|
|
AllstatefulSets = append(AllstatefulSets, StatefulSet)
|
|
}
|
|
|
|
//pod_list, err := json.Marshal(Allservice)
|
|
if err != nil {
|
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(AllstatefulSets)
|
|
|
|
}
|
|
|
|
func Cluster_daemonsets(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
|
|
if clustername == "" {
|
|
http.Error(w, "Missing 'Name' parameter", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting clientset: ", err)
|
|
}
|
|
|
|
DaemonSetss, err := clientset.AppsV1().DaemonSets(namespace).List(context.TODO(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting list of DaemonSets: ", err)
|
|
}
|
|
|
|
AllDaemonSets := []models.Daemonset{}
|
|
|
|
var DaemonSets models.Daemonset
|
|
now := time.Now()
|
|
for _, s := range DaemonSetss.Items {
|
|
DaemonSets.Name = s.Name
|
|
DaemonSets.Namespace = s.Namespace
|
|
DaemonSets.DESIRED = s.Status.DesiredNumberScheduled
|
|
DaemonSets.CURRENT = s.Status.CurrentNumberScheduled
|
|
DaemonSets.Available = s.Status.NumberAvailable
|
|
DaemonSets.Ready = s.Status.NumberReady
|
|
DaemonSets.UpdateToDate = s.Status.UpdatedNumberScheduled
|
|
DaemonSets.Node = s.Spec.Template.Spec.NodeName
|
|
DaemonSets.Selector = helpers.JoinNodeSelector(s.Spec.Template.Spec.NodeSelector)
|
|
age := now.Sub(s.CreationTimestamp.Time) // same as kubectl AGE
|
|
DaemonSets.Age = helpers.Human(age)
|
|
|
|
AllDaemonSets = append(AllDaemonSets, DaemonSets)
|
|
}
|
|
|
|
//pod_list, err := json.Marshal(Allservice)
|
|
if err != nil {
|
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(AllDaemonSets)
|
|
|
|
}
|
|
|
|
func Cluster_deployments(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
|
|
if clustername == "" {
|
|
http.Error(w, "Missing 'Name' parameter", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting clientset: ", err)
|
|
}
|
|
|
|
deployments, err := clientset.AppsV1().Deployments(namespace).List(context.TODO(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting list of deployments: ", err)
|
|
}
|
|
|
|
Alldeployment := []models.Deployment{}
|
|
|
|
var deployment models.Deployment
|
|
var avail bool
|
|
var reason, msg string
|
|
for _, d := range deployments.Items {
|
|
deployment.Name = d.Name
|
|
deployment.Namespace = d.Namespace
|
|
deployment.Replicas = *d.Spec.Replicas
|
|
for _, c := range d.Status.Conditions {
|
|
if c.Type == appsv1.DeploymentAvailable {
|
|
avail = (c.Status == corev1.ConditionTrue)
|
|
reason, msg = c.Reason, c.Message
|
|
deployment.Available = fmt.Sprintf("%t", avail)
|
|
deployment.Message = msg
|
|
deployment.Reason = reason
|
|
}
|
|
}
|
|
|
|
Alldeployment = append(Alldeployment, deployment)
|
|
}
|
|
|
|
if err != nil {
|
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(Alldeployment)
|
|
|
|
}
|
|
|
|
func servicePortsToString(ports []corev1.ServicePort) string {
|
|
var parts []string
|
|
for _, p := range ports {
|
|
// Example format: "80/TCP"
|
|
parts = append(parts, fmt.Sprintf("%d/%s", p.Port, p.Protocol))
|
|
}
|
|
return strings.Join(parts, ", ")
|
|
}
|
|
func Cluster_pods(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
|
|
if clustername == "" {
|
|
http.Error(w, "Missing 'Name' parameter", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting clientset: ", err)
|
|
}
|
|
|
|
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting list of pods: ", err)
|
|
}
|
|
|
|
Allpod := []models.Pod{}
|
|
|
|
var pod models.Pod
|
|
now := time.Now()
|
|
for _, p := range pods.Items {
|
|
fmt.Printf(p.Name, p.Namespace)
|
|
pod.Name = p.Name
|
|
pod.Namespace = p.Namespace
|
|
pod.Status = string(p.Status.Phase)
|
|
age := now.Sub(p.CreationTimestamp.Time) // same as kubectl AGE
|
|
pod.Age = helpers.Human(age)
|
|
|
|
var restartedCount int32
|
|
var restarts int32
|
|
for _, st := range p.Status.ContainerStatuses {
|
|
restarts += st.RestartCount
|
|
}
|
|
pod.Restart = restartedCount
|
|
|
|
Allpod = append(Allpod, pod)
|
|
}
|
|
|
|
//pod_list, err := json.Marshal(Allpod)
|
|
if err != nil {
|
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(Allpod)
|
|
|
|
}
|
|
|
|
func getClusterConfig(clustername string) (string, error) {
|
|
var existsCluster models.Cluster
|
|
err := db.Vclusters_details.FindOne(context.TODO(), bson.M{"name": clustername}).Decode(&existsCluster)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
decoded, err := base64.StdEncoding.DecodeString(existsCluster.Cluster_config)
|
|
|
|
return string(decoded), nil
|
|
}
|
|
|
|
func Connect(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clusterName := r.URL.Query().Get("Name")
|
|
if clusterName == "" {
|
|
http.Error(w, "Missing 'Name' parameter", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
var existsCluster models.Cluster
|
|
_ = db.Vclusters_details.FindOne(context.TODO(), bson.M{"name": clusterName}).Decode(&existsCluster)
|
|
|
|
decoded, err := base64.StdEncoding.DecodeString(existsCluster.Cluster_config)
|
|
if err != nil {
|
|
http.Error(w, "Failed to decode cluster config", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/x-yaml")
|
|
w.Header().Set("Content-Disposition", `attachment; filename="`+clusterName+`.yaml"`)
|
|
w.Write(decoded)
|
|
|
|
}
|
|
|
|
// func Cluster_details(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// }
|
|
|
|
func Cluster_jobs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
|
|
if clustername == "" {
|
|
http.Error(w, "Missing 'Name' parameter", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting Jobs: ", err)
|
|
}
|
|
|
|
jobs, err := clientset.BatchV1().Jobs(namespace).List(context.TODO(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting list of Jobs: ", err)
|
|
}
|
|
|
|
AllJob := []models.Jobs{}
|
|
|
|
var job models.Jobs
|
|
now := time.Now()
|
|
for _, d := range jobs.Items {
|
|
job.Name = d.Name
|
|
job.Namespace = d.Namespace
|
|
|
|
status := "Active"
|
|
if d.Status.Succeeded > 0 {
|
|
status = "Complete"
|
|
} else if d.Status.Failed > 0 {
|
|
status = "Failed"
|
|
}
|
|
job.Status = status
|
|
|
|
completions := fmt.Sprintf("%d/%d", d.Status.Succeeded, *d.Spec.Completions)
|
|
job.Completion = completions
|
|
|
|
duration := "-"
|
|
if d.Status.StartTime != nil && d.Status.CompletionTime != nil {
|
|
d := d.Status.CompletionTime.Time.Sub(d.Status.StartTime.Time)
|
|
duration = d.String()
|
|
}
|
|
job.Duration = duration
|
|
|
|
age := now.Sub(d.CreationTimestamp.Time) // same as kubectl AGE
|
|
job.Age = helpers.Human(age)
|
|
|
|
AllJob = append(AllJob, job)
|
|
}
|
|
|
|
if err != nil {
|
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(AllJob)
|
|
|
|
}
|
|
|
|
func Cluster_replicasets(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
|
|
if clustername == "" {
|
|
http.Error(w, "Missing 'Name' parameter", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting replicasets: ", err)
|
|
}
|
|
|
|
replicasets, err := clientset.AppsV1().ReplicaSets(namespace).List(context.TODO(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting list of Replicaset: ", err)
|
|
}
|
|
|
|
Allreplicaset := []models.Replicaset{}
|
|
|
|
var replicaset models.Replicaset
|
|
now := time.Now()
|
|
for _, d := range replicasets.Items {
|
|
replicaset.Name = d.Name
|
|
replicaset.Namespace = d.Namespace
|
|
replicaset.Desired = *d.Spec.Replicas
|
|
replicaset.Current = d.Status.Replicas
|
|
replicaset.Ready = d.Status.ReadyReplicas
|
|
age := now.Sub(d.CreationTimestamp.Time) // same as kubectl AGE
|
|
replicaset.Age = helpers.Human(age)
|
|
Allreplicaset = append(Allreplicaset, replicaset)
|
|
}
|
|
|
|
if err != nil {
|
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(Allreplicaset)
|
|
|
|
}
|
|
|
|
func Cluster_replicationcontrollers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
|
|
if clustername == "" {
|
|
http.Error(w, "Missing 'Name' parameter", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting Replicationcontrollers: ", err)
|
|
}
|
|
|
|
replicationcontrollers, err := clientset.CoreV1().ReplicationControllers(namespace).List(context.TODO(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting list of ReplicationController: ", err)
|
|
}
|
|
|
|
AllreplicationController := []models.ReplicationController{}
|
|
|
|
var ReplicationController models.ReplicationController
|
|
now := time.Now()
|
|
for _, d := range replicationcontrollers.Items {
|
|
ReplicationController.Name = d.Name
|
|
ReplicationController.Namespace = d.Namespace
|
|
age := now.Sub(d.CreationTimestamp.Time) // same as kubectl AGE
|
|
ReplicationController.Age = helpers.Human(age)
|
|
AllreplicationController = append(AllreplicationController, ReplicationController)
|
|
}
|
|
|
|
if err != nil {
|
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(AllreplicationController)
|
|
|
|
}
|
|
|
|
func Cluster_cronjobs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
|
|
if clustername == "" {
|
|
http.Error(w, "Missing 'Name' parameter", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting Replicationcontrollers: ", err)
|
|
}
|
|
|
|
replicationcontrollers, err := clientset.CoreV1().ReplicationControllers(namespace).List(context.TODO(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting list of ReplicationController: ", err)
|
|
}
|
|
|
|
AllreplicationController := []models.ReplicationController{}
|
|
|
|
var ReplicationController models.ReplicationController
|
|
now := time.Now()
|
|
for _, d := range replicationcontrollers.Items {
|
|
ReplicationController.Name = d.Name
|
|
ReplicationController.Namespace = d.Namespace
|
|
age := now.Sub(d.CreationTimestamp.Time) // same as kubectl AGE
|
|
ReplicationController.Age = helpers.Human(age)
|
|
|
|
ReplicationController.Desired = *d.Spec.Replicas
|
|
ReplicationController.Current = d.Status.Replicas
|
|
ReplicationController.Ready = d.Status.ReadyReplicas
|
|
|
|
AllreplicationController = append(AllreplicationController, ReplicationController)
|
|
}
|
|
|
|
if err != nil {
|
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(AllreplicationController)
|
|
|
|
}
|
|
|
|
func Pod_logs(w http.ResponseWriter, r *http.Request) {
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
podName := r.URL.Query().Get("Pod")
|
|
// containerName := podName
|
|
if clustername == "" {
|
|
http.Error(w, "Missing 'Name' parameter", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
|
|
if err != nil {
|
|
log.Fatal("Error getting Replicationcontrollers: ", err)
|
|
}
|
|
|
|
podLogOpts := corev1.PodLogOptions{}
|
|
req := clientset.CoreV1().Pods(namespace).GetLogs(podName, &podLogOpts)
|
|
podLogs, err := req.Stream(context.TODO())
|
|
if err != nil {
|
|
http.Error(w, "an error happend in getting logs", http.StatusBadRequest)
|
|
return
|
|
}
|
|
defer podLogs.Close()
|
|
|
|
buf := new([]byte)
|
|
*buf, err = io.ReadAll(podLogs)
|
|
if err != nil {
|
|
http.Error(w, "an error happend in getting logs", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(string(*buf))
|
|
}
|
|
|
|
func Pod_exec(w http.ResponseWriter, r *http.Request) {
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
podName := r.URL.Query().Get("Pod")
|
|
command := r.URL.Query().Get("Command")
|
|
|
|
//var cmd []string
|
|
parsed, err := strconv.Unquote(command)
|
|
if err != nil {
|
|
http.Error(w, "Invalid command string: "+err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
// cmd = strings.Fields(parsed)
|
|
cmd := []string{"sh", "-c", parsed}
|
|
|
|
if clustername == "" || namespace == "" || podName == "" {
|
|
http.Error(w, "Missing required parameters (Name, Namespace, Pod)", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, config, err := getClientset(w, clustername)
|
|
if err != nil {
|
|
http.Error(w, "Error getting Kubernetes clientset", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
req := clientset.CoreV1().RESTClient().
|
|
Post().
|
|
Resource("pods").
|
|
Name(podName).
|
|
Namespace(namespace).
|
|
SubResource("exec").
|
|
VersionedParams(&corev1.PodExecOptions{
|
|
Command: cmd,
|
|
Stdout: true,
|
|
Stderr: true,
|
|
Stdin: false,
|
|
TTY: false,
|
|
}, scheme.ParameterCodec)
|
|
|
|
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
|
|
if err != nil {
|
|
http.Error(w, "Error creating executor: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
err = exec.StreamWithContext(r.Context(), remotecommand.StreamOptions{
|
|
Stdout: &stdout,
|
|
Stderr: &stderr,
|
|
Tty: false,
|
|
})
|
|
|
|
if err != nil {
|
|
http.Error(w, "Error streaming command output: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
output := map[string]interface{}{
|
|
"stdout": strings.Split(strings.TrimSpace(stdout.String()), "\n"),
|
|
"stderr": strings.TrimSpace(stderr.String()),
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(output)
|
|
}
|
|
|
|
func Pod_delete(w http.ResponseWriter, r *http.Request) {
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
podName := r.URL.Query().Get("Pod")
|
|
|
|
if clustername == "" || namespace == "" || podName == "" {
|
|
http.Error(w, "Missing required parameters (Name, Namespace, Pod)", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
if err != nil {
|
|
http.Error(w, "Error getting Kubernetes clientset", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
err = clientset.CoreV1().Pods(namespace).Delete(context.TODO(), podName, metav1.DeleteOptions{})
|
|
if err != nil {
|
|
http.Error(w, "Error deleting pod", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(podName + " Has been deleted")
|
|
}
|
|
|
|
func Service_delete(w http.ResponseWriter, r *http.Request) {
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
serviceName := r.URL.Query().Get("serviceName")
|
|
|
|
if clustername == "" || namespace == "" || serviceName == "" {
|
|
http.Error(w, "Missing required parameters (Name, Namespace, Pod)", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
if err != nil {
|
|
http.Error(w, "Error getting Kubernetes clientset", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
err = clientset.CoreV1().Services(namespace).Delete(context.TODO(), serviceName, metav1.DeleteOptions{})
|
|
if err != nil {
|
|
http.Error(w, "Error deleting service", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(serviceName + " Has been deleted")
|
|
}
|
|
|
|
func Deployment_delete(w http.ResponseWriter, r *http.Request) {
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
deploymenteName := r.URL.Query().Get("deploymenteName")
|
|
|
|
if clustername == "" || namespace == "" || deploymenteName == "" {
|
|
http.Error(w, "Missing required parameters (Name, Namespace, Pod)", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
if err != nil {
|
|
http.Error(w, "Error getting Kubernetes clientset", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
err = clientset.AppsV1().Deployments(namespace).Delete(context.TODO(), deploymenteName, metav1.DeleteOptions{})
|
|
if err != nil {
|
|
http.Error(w, "Error deleting deploymente", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(deploymenteName + " Has been deleted")
|
|
}
|
|
|
|
func StatefulSet_delete(w http.ResponseWriter, r *http.Request) {
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
statefulSetName := r.URL.Query().Get("statefulSetName")
|
|
|
|
if clustername == "" || namespace == "" || statefulSetName == "" {
|
|
http.Error(w, "Missing required parameters (Name, Namespace, Pod)", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
if err != nil {
|
|
http.Error(w, "Error getting Kubernetes clientset", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
err = clientset.AppsV1().StatefulSets(namespace).Delete(context.TODO(), statefulSetName, metav1.DeleteOptions{})
|
|
if err != nil {
|
|
http.Error(w, "Error deleting statefulSet", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(statefulSetName + " Has been deleted")
|
|
}
|
|
|
|
func Daemonsets_delete(w http.ResponseWriter, r *http.Request) {
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
daemonsetsName := r.URL.Query().Get("daemonsetsName")
|
|
|
|
if clustername == "" || namespace == "" || daemonsetsName == "" {
|
|
http.Error(w, "Missing required parameters (Name, Namespace, Pod)", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
if err != nil {
|
|
http.Error(w, "Error getting Kubernetes clientset", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
err = clientset.AppsV1().DaemonSets(namespace).Delete(context.TODO(), daemonsetsName, metav1.DeleteOptions{})
|
|
if err != nil {
|
|
http.Error(w, "Error deleting daemonsets"+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(daemonsetsName + " Has been deleted")
|
|
}
|
|
|
|
func JobsName_delete(w http.ResponseWriter, r *http.Request) {
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
jobsName := r.URL.Query().Get("jobsName")
|
|
|
|
if clustername == "" || namespace == "" || jobsName == "" {
|
|
http.Error(w, "Missing required parameters (Name, Namespace, Pod)", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
if err != nil {
|
|
http.Error(w, "Error getting Kubernetes clientset", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
err = clientset.BatchV1().Jobs(namespace).Delete(context.TODO(), jobsName, metav1.DeleteOptions{})
|
|
if err != nil {
|
|
http.Error(w, "Error deleting Jobs", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(jobsName + " Has been deleted")
|
|
}
|
|
|
|
func Replicaset_delete(w http.ResponseWriter, r *http.Request) {
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
replicasetName := r.URL.Query().Get("replicasetName")
|
|
|
|
if clustername == "" || namespace == "" || replicasetName == "" {
|
|
http.Error(w, "Missing required parameters (Name, Namespace, Pod)", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
if err != nil {
|
|
http.Error(w, "Error getting Kubernetes clientset", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
err = clientset.AppsV1().ReplicaSets(namespace).Delete(context.TODO(), replicasetName, metav1.DeleteOptions{})
|
|
if err != nil {
|
|
http.Error(w, "Error deleting ReplicaSets", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(replicasetName + " Has been deleted")
|
|
}
|
|
|
|
func Replicationcontroller_delete(w http.ResponseWriter, r *http.Request) {
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
replicationcontrollerName := r.URL.Query().Get("replicationcontrollerName")
|
|
|
|
if clustername == "" || namespace == "" || replicationcontrollerName == "" {
|
|
http.Error(w, "Missing required parameters (Name, Namespace, Pod)", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
if err != nil {
|
|
http.Error(w, "Error getting Kubernetes clientset", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
err = clientset.CoreV1().ReplicationControllers(namespace).Delete(context.TODO(), replicationcontrollerName, metav1.DeleteOptions{})
|
|
if err != nil {
|
|
http.Error(w, "Error deleting ReplicationControllers", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(replicationcontrollerName + " Has been deleted")
|
|
}
|
|
|
|
func Cronjob_delete(w http.ResponseWriter, r *http.Request) {
|
|
clustername := r.URL.Query().Get("Name")
|
|
namespace := r.URL.Query().Get("Namespace")
|
|
cronjobName := r.URL.Query().Get("cronjobName")
|
|
|
|
if clustername == "" || namespace == "" || cronjobName == "" {
|
|
http.Error(w, "Missing required parameters (Name, Namespace, Pod)", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
clientset, _, err := getClientset(w, clustername)
|
|
if err != nil {
|
|
http.Error(w, "Error getting Kubernetes clientset", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
err = clientset.BatchV1().CronJobs(namespace).Delete(context.TODO(), cronjobName, metav1.DeleteOptions{})
|
|
if err != nil {
|
|
http.Error(w, "Error deleting CronJobs", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(cronjobName + " Has been deleted")
|
|
}
|