-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: bishal7679 <bishalhnj127@gmail.com>
- Loading branch information
1 parent
457a610
commit 2c7801c
Showing
50 changed files
with
3,593 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,290 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"strings" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
) | ||
|
||
// This func will create any pod | ||
func CreatePod(Clusterns string, Podname string, Podimage string, Ports string) { | ||
clientset := Kconfig | ||
var hostPort, contPort int64 | ||
ctx := context.Background() | ||
|
||
if Podname == "" || Podimage == "" || Ports == "" { | ||
logging.Err("🚨 podname and podimage and Ports should be provided") | ||
return | ||
} | ||
ports := strings.Split(Ports, ":") | ||
ports[0] = strings.Trim(ports[0], " ") | ||
ports[1] = strings.Trim(ports[1], " ") | ||
|
||
hostPort, _ = strconv.ParseInt(ports[0], 10, 32) | ||
contPort, _ = strconv.ParseInt(ports[1], 10, 32) | ||
|
||
if Clusterns == "" { | ||
// logging.Warn("-n flag is empty! Setting up Namespace = default") | ||
Clusterns = "default" | ||
} | ||
|
||
pod := &v1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: Podname, | ||
Namespace: Clusterns, | ||
Labels: map[string]string{ | ||
"app": Podname, | ||
}, | ||
}, | ||
Spec: v1.PodSpec{ | ||
Containers: []v1.Container{ | ||
{ | ||
Name: Podname + "-container", | ||
Image: Podimage, | ||
Ports: []v1.ContainerPort{ | ||
{ | ||
Name: "http", | ||
Protocol: v1.ProtocolTCP, | ||
HostPort: int32(hostPort), | ||
ContainerPort: int32(contPort), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
podcreate, err := clientset.CoreV1().Pods(Clusterns).Create(ctx, pod, metav1.CreateOptions{}) | ||
if err != nil { | ||
logging.Err("Error creating pod 😢") | ||
logging.Err(err.Error()) | ||
} else { | ||
logging.Print("pod/" + podcreate.GetObjectMeta().GetName() + " created 🎉") | ||
} | ||
} | ||
|
||
// This func will delete any pod | ||
func DeletePod(Clusterns string, PodName string) { | ||
clientset := Kconfig | ||
ctx := context.Background() | ||
|
||
if Clusterns == "" { | ||
Clusterns = "default" | ||
} | ||
|
||
err := clientset.CoreV1().Pods(Clusterns).Delete(ctx, PodName, metav1.DeleteOptions{}) | ||
if err != nil { | ||
logging.Err("Error deleting pod 😢") | ||
return | ||
} | ||
logging.Print("pod " + "\"" + PodName + "\"" + " deleted 🎉") | ||
} | ||
|
||
// This func will create any service | ||
func CreateService(Clusterns string, Podname string, Servicename string, Servicetype string, Ports string, Nodeport int32) { | ||
clientset := Kconfig | ||
ctx := context.Background() | ||
var Sv string | ||
ports := strings.Split(Ports, ":") | ||
ports[0] = strings.Trim(ports[0], " ") | ||
ports[1] = strings.Trim(ports[1], " ") | ||
|
||
servicePort, _ := strconv.ParseInt(ports[0], 10, 32) | ||
targetPort, _ := strconv.ParseInt(ports[1], 10, 32) | ||
|
||
if Podname == "" || Servicename == "" || Ports == "" { | ||
logging.Err("🚨 podname, servicename, ports should be provided") | ||
return | ||
} | ||
if Clusterns == "" { | ||
// logging.Warn("-n flag is empty! Setting up Namespace = default") | ||
Clusterns = "default" | ||
} | ||
|
||
switch strings.ToLower(Servicetype) { | ||
case "nodeport": | ||
Sv = "NodePort" | ||
|
||
case "loadbalancer": | ||
Sv = "LoadBalancer" | ||
default: | ||
Sv = "ClusterIP" | ||
|
||
} | ||
|
||
// defining service manifest | ||
service := &v1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: Servicename, | ||
Namespace: Clusterns, | ||
Labels: map[string]string{ | ||
"app": Servicename + "-service", | ||
}, | ||
}, | ||
Spec: v1.ServiceSpec{ | ||
Type: v1.ServiceType(Sv), | ||
Ports: []v1.ServicePort{ | ||
{ | ||
Name: Servicename + "-port", | ||
Protocol: v1.ProtocolTCP, | ||
// AppProtocol: v1.URISchemeHTTP, | ||
Port: int32(servicePort), | ||
TargetPort: intstr.IntOrString{ | ||
IntVal: int32(targetPort), | ||
}, | ||
}, | ||
}, | ||
Selector: map[string]string{ | ||
"app": Podname, | ||
}, | ||
}, | ||
} | ||
|
||
if service.Spec.Type == "NodePort" { | ||
|
||
service.Spec.Ports = append(service.Spec.Ports, v1.ServicePort{NodePort: Nodeport}) | ||
} | ||
servicecreate, err := clientset.CoreV1().Services(Clusterns).Create(ctx, service, metav1.CreateOptions{}) | ||
if err != nil { | ||
logging.Err("Error creating service 😢") | ||
logging.Err(err.Error()) | ||
} else { | ||
logging.Print("service/" + servicecreate.GetObjectMeta().GetName() + " created 🎉") | ||
} | ||
|
||
} | ||
|
||
// This func will delete any service | ||
func DeleteService(Clusterns string, ServiceName string) { | ||
clientset := Kconfig | ||
ctx := context.Background() | ||
|
||
if Clusterns == "" { | ||
Clusterns = "default" | ||
} | ||
|
||
err := clientset.CoreV1().Services(Clusterns).Delete(ctx, ServiceName, metav1.DeleteOptions{}) | ||
if err != nil { | ||
logging.Err("Error deleting service 😢") | ||
logging.Err(err.Error()) | ||
} | ||
logging.Print("service " + "\"" + ServiceName + "\"" + " deleted 🎉") | ||
} | ||
|
||
// This func will create any deployment | ||
|
||
func CreateDeployment(Clusterns string, Deploymentname string, Podimage string, Containerport int32) { | ||
clientset := Kconfig | ||
ctx := context.Background() | ||
|
||
if Deploymentname == "" || Podimage == "" { | ||
logging.Err("🚨 deploymentname and podimage should be provided") | ||
return | ||
} | ||
|
||
if Clusterns == "" { | ||
Clusterns = "default" | ||
} | ||
|
||
deployment := &appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: Deploymentname, | ||
Labels: map[string]string{ | ||
"app": Deploymentname, | ||
}, | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
// Replicas: 2, | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"app": Deploymentname, | ||
}, | ||
}, | ||
Template: v1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{ | ||
"app": Deploymentname, | ||
}, | ||
}, | ||
Spec: v1.PodSpec{ | ||
RestartPolicy: v1.RestartPolicyAlways, | ||
Containers: []v1.Container{ | ||
{ | ||
Name: Deploymentname + "-container", | ||
Image: Podimage, | ||
ImagePullPolicy: v1.PullIfNotPresent, | ||
Ports: []v1.ContainerPort{ | ||
{ | ||
Name: "http", | ||
Protocol: v1.ProtocolTCP, | ||
ContainerPort: Containerport, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
deploymentcreate, err := clientset.AppsV1().Deployments(Clusterns).Create(ctx, deployment, metav1.CreateOptions{}) | ||
if err != nil { | ||
logging.Err("Error creating deployment 😢") | ||
logging.Err(err.Error()) | ||
} else { | ||
logging.Print("deployment/" + deploymentcreate.GetObjectMeta().GetName() + " created 🎉") | ||
} | ||
|
||
} | ||
|
||
// This function Deletes the Deployments | ||
func DeleteDeployment(Clusterns string, DeploymentName string) { | ||
clientset := Kconfig | ||
ctx := context.Background() | ||
if Clusterns == "" { | ||
Clusterns = "default" | ||
} | ||
err := clientset.AppsV1().Deployments(Clusterns).Delete(ctx, DeploymentName, metav1.DeleteOptions{}) | ||
if err != nil { | ||
logging.Err("Error deleting deployment 😢") | ||
logging.Err(err.Error()) | ||
} | ||
logging.Print("deployment " + "\"" + DeploymentName + "\"" + " deleted 🎉") | ||
} | ||
|
||
// This func will create namespace | ||
func CreateNamespace(Namespacename string) { | ||
clientset := Kconfig | ||
ctx := context.Background() | ||
namespace := &v1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: Namespacename, | ||
Labels: map[string]string{ | ||
"name": Namespacename, | ||
}, | ||
}, | ||
} | ||
namespacecreate, err := clientset.CoreV1().Namespaces().Create(ctx, namespace, metav1.CreateOptions{}) | ||
if err != nil { | ||
logging.Err("Error creating namespace 😢") | ||
logging.Err(err.Error()) | ||
} else { | ||
logging.Print("namespace/" + namespacecreate.GetObjectMeta().GetName() + " created 🎉") | ||
} | ||
} | ||
|
||
// This func will delete namespace | ||
func DeleteNamespace(Namespacename string) { | ||
clientset := Kconfig | ||
ctx := context.Background() | ||
err := clientset.CoreV1().Namespaces().Delete(ctx, Namespacename, metav1.DeleteOptions{}) | ||
if err != nil { | ||
logging.Err("Error deleting namespace 😢") | ||
logging.Err(err.Error()) | ||
} | ||
logging.Print("namespace " + "\"" + Namespacename + "\"" + " deleted 🎉") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package api | ||
|
||
import ( | ||
// "fmt" | ||
"os" | ||
"time" | ||
|
||
log "github.com/bishal7679/ksapify/internal/logger" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
var ( | ||
logging log.Logger | ||
Kconfig *kubernetes.Clientset | ||
) | ||
|
||
func Main() { | ||
logging.Info("ksapify is running successfully!🥳 🎉", "") | ||
|
||
// Uncomment this if you want to run outside the cluster | ||
OutsideClusterConfigFancy() | ||
// This will be used in case you have to run the code inside the cluster | ||
// config, err := rest.InClusterConfig() | ||
// if err != nil { | ||
// logging.Err("error getting config inClusterconfig\n") | ||
// } else { | ||
// logging.Info("config built successfully!✅", "") | ||
// } | ||
|
||
// clientset, err := kubernetes.NewForConfig(config) | ||
// if err != nil { | ||
// logging.Err("error creating clientset 😢") | ||
// } else { | ||
// logging.Info("clientset created successfully!✅", "") | ||
// } | ||
|
||
// Kconfig = clientset | ||
|
||
} | ||
|
||
func OutsideClusterConfigFancy() { | ||
|
||
// You will have to export the KUBECONFIG variable to point to the kubeconfig file based on your OS in the terminal | ||
logging.Note("Export KUBECONFIG env variable is important!") | ||
kubeconfig := os.Getenv("KUBECONFIG") | ||
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) | ||
|
||
if err != nil { | ||
// fmt.Errorf("error %s building config from env variable\n", err.Error()) | ||
config, err = rest.InClusterConfig() | ||
if err != nil { | ||
logging.Err("error getting config inClusterconfig 😢") | ||
} | ||
} else { | ||
time.Sleep(3 * time.Second) | ||
logging.Info("config built successfully!✅", "") | ||
} | ||
|
||
// creating the clientset to access all the resources | ||
clientset, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
logging.Err("error creating clientset 😢") | ||
} else { | ||
time.Sleep(4 * time.Second) | ||
logging.Info("clientset created successfully!✅", "") | ||
|
||
} | ||
|
||
Kconfig = clientset | ||
|
||
} | ||
|
||
func OutsideClusterConfig() { | ||
|
||
// You will have to export the KUBECONFIG variable to point to the kubeconfig file based on your OS in the terminal | ||
kubeconfig := os.Getenv("KUBECONFIG") | ||
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) | ||
|
||
if err != nil { | ||
// fmt.Errorf("error %s building config from env variable\n", err.Error()) | ||
config, _ = rest.InClusterConfig() | ||
} | ||
|
||
// creating the clientset to access all the resources | ||
clientset, _ := kubernetes.NewForConfig(config) | ||
|
||
Kconfig = clientset | ||
|
||
} |
Oops, something went wrong.