From 34afb12844e4a67c9bce8beb99c549b979e604de Mon Sep 17 00:00:00 2001 From: Husni Alhamdani Date: Mon, 18 Apr 2022 11:23:14 +0700 Subject: [PATCH] initial commit --- LICENSE | 0 cmd/cleanup.go | 60 +++++++++++++ cmd/execute.go | 38 ++++++++ cmd/root.go | 42 +++++++++ go.mod | 9 ++ go.sum | 10 +++ main.go | 11 +++ objects/objects.go | 219 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 389 insertions(+) create mode 100644 LICENSE create mode 100644 cmd/cleanup.go create mode 100644 cmd/execute.go create mode 100644 cmd/root.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 objects/objects.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e69de29 diff --git a/cmd/cleanup.go b/cmd/cleanup.go new file mode 100644 index 0000000..9234ffe --- /dev/null +++ b/cmd/cleanup.go @@ -0,0 +1,60 @@ +/* +Copyright © 2022 NAME HERE + +*/ +package cmd + +import ( + "fmt" + "log" + "strconv" + + "github.com/spf13/cobra" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/clientcmd" +) + +// cleanupCmd represents the cleanup command +var cleanupCmd = &cobra.Command{ + Use: "cleanup", + Short: "Cleanup all resources created", + Long: `Cleanup all resources created`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("cleanup called") + cleanup(10, "default") + }, +} + +func init() { + rootCmd.AddCommand(cleanupCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // cleanupCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // cleanupCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} + +func cleanup(size int, namespace string) { + rules := clientcmd.NewDefaultClientConfigLoadingRules() + kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, &clientcmd.ConfigOverrides{}) + config, err := kubeconfig.ClientConfig() + if err != nil { + panic(err) + } + clientset := kubernetes.NewForConfigOrDie(config) + log.Print("Cleaning up resources...") + for i := size - 1; i >= 0; i-- { + counter := strconv.Itoa(i) + objects.deleteNamespace(clientset, counter) + objects.deleteDeployment(clientset, counter, namespace) + objects.deleteConfigmap(clientset, counter, namespace) + objects.deletePod(clientset, counter, namespace) + objects.deleteSecret(clientset, counter, namespace) + objects.deleteCronjob(clientset, counter, namespace) + } +} diff --git a/cmd/execute.go b/cmd/execute.go new file mode 100644 index 0000000..9fd01ed --- /dev/null +++ b/cmd/execute.go @@ -0,0 +1,38 @@ +/* +Copyright © 2022 NAME HERE + +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// executeCmd represents the execute command +var executeCmd = &cobra.Command{ + Use: "execute", + Short: "Run loads of resources creation", + Long: `Run loadsd of resources creation`, + Run: func(cmd *cobra.Command, args []string) { + //fmt.Println("execute called") + scales, _ := cmd.Flags().GetString("scales") + fmt.Println(scales) + }, +} + +func init() { + executeCmd.Flags().StringP("scales", "s", "xs", "choose the scale size (small/medium/large/xl) default: xs") + rootCmd.AddCommand(executeCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // executeCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // executeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..5ba361f --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,42 @@ +/* +Copyright © 2022 NAME HERE + +*/ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "kyvernop", + Short: "Get kyverno performance on different scale of workloads", + Long: `Kyvernop CLI is a tool that give you an insight of how your Kyverno perform on different size of scale`, + // Uncomment the following line if your bare application + // has an action associated with it: + // Run: func(cmd *cobra.Command, args []string) { }, +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + + // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kyvernop.yaml)") + + // Cobra also supports local flags, which will only run + // when this action is called directly. + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3266bd1 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/husnialhamdani/kyvernop + +go 1.17 + +require ( + github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/spf13/cobra v1.4.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0dd8697 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q= +github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..aae136e --- /dev/null +++ b/main.go @@ -0,0 +1,11 @@ +/* +Copyright © 2022 NAME HERE + +*/ +package main + +import "github.com/husnialhamdani/kyvernop/cmd" + +func main() { + cmd.Execute() +} diff --git a/objects/objects.go b/objects/objects.go new file mode 100644 index 0000000..7b92fed --- /dev/null +++ b/objects/objects.go @@ -0,0 +1,219 @@ +package objects + +import ( + "context" + "fmt" + + appsv1 "k8s.io/api/apps/v1" + batchv1 "k8s.io/api/batch/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" +) + +func createNamespace(clientset kubernetes.Clientset, name string) { + nsSpec := &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: "namespace-" + name, + }, + } + namespace, err := clientset.CoreV1().Namespaces().Create(context.Background(), nsSpec, metav1.CreateOptions{}) + if err != nil { + fmt.Println(err) + return + } + fmt.Println("Namespace successfully created:", namespace.GetName()) +} + +func deleteNamespace(clientset kubernetes.Clientset, name string) { + deletePolicy := metav1.DeletePropagationForeground + if err := clientset.CoreV1().Namespaces().Delete(context.TODO(), "namespace-"+name, metav1.DeleteOptions{ + PropagationPolicy: &deletePolicy, + }); err != nil { + fmt.Println(err) + return + } + fmt.Println("Namespace deleted:", "namespace-"+name) +} + +func createConfigmap(clientset kubernetes.Clientset, name string, namespace string) { + configmapSpec := &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: "cm-" + name, + Namespace: namespace, + }, + Data: map[string]string{"color.good": "purple", "color.bad": "yellow"}, + } + configMap, err := clientset.CoreV1().ConfigMaps("default").Create(context.Background(), configmapSpec, metav1.CreateOptions{}) + if err != nil { + fmt.Println(err) + return + } + fmt.Println("ConfigMap created:", configMap.GetName()) +} + +func deleteConfigmap(clientset kubernetes.Clientset, name string, namespace string) { + deletePolicy := metav1.DeletePropagationForeground + if err := clientset.CoreV1().ConfigMaps(namespace).Delete(context.TODO(), "cm-"+name, metav1.DeleteOptions{ + PropagationPolicy: &deletePolicy, + }); err != nil { + fmt.Println(err) + return + } + fmt.Println("ConfigMap deleted:", "cm-"+name) +} + +func createSecret(clientset kubernetes.Clientset, name string, namespace string) { + secretSpec := &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: "secret-" + name, + Namespace: namespace, + }, + Data: map[string][]byte{"test": []byte("test")}, + } + + secret, err := clientset.CoreV1().Secrets("default").Create(context.Background(), secretSpec, metav1.CreateOptions{}) + if err != nil { + fmt.Println(err) + return + } + fmt.Println("Secret created", secret.GetName()) +} + +func deleteSecret(clientset kubernetes.Clientset, name string, namespace string) { + deletePolicy := metav1.DeletePropagationForeground + if err := clientset.CoreV1().Secrets(namespace).Delete(context.TODO(), "secret-"+name, metav1.DeleteOptions{ + PropagationPolicy: &deletePolicy, + }); err != nil { + fmt.Println(err) + return + } + fmt.Println("Secret deleted:", "secret-"+name) +} + +func createPod(clientset kubernetes.Clientset, name string, namespace string, image string) { + podSpec := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pod-" + name, + Namespace: namespace, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + {Name: "busybox", Image: "busybox:latest", Command: []string{"sleep", "100000"}}, + }, + }, + } + pod, err := clientset.CoreV1().Pods("default").Create(context.Background(), podSpec, metav1.CreateOptions{}) + if err != nil { + fmt.Println(err) + return + } + fmt.Println("Pod created:", pod.GetName()) +} + +func deletePod(clientset kubernetes.Clientset, name string, namespace string) { + deletePolicy := metav1.DeletePropagationForeground + if err := clientset.CoreV1().Pods(namespace).Delete(context.TODO(), "pod-"+name, metav1.DeleteOptions{ + PropagationPolicy: &deletePolicy, + }); err != nil { + fmt.Println(err) + return + } + fmt.Println("Pod deleted:", "pod-"+name) +} + +func createCronjob(clientset kubernetes.Clientset, name string, namespace string, schedule string) { + cronjobSpec := &batchv1.CronJob{ + ObjectMeta: metav1.ObjectMeta{ + Name: "cronjob-" + name, + Namespace: namespace, + }, + Spec: batchv1.CronJobSpec{ + Schedule: schedule, + JobTemplate: batchv1.JobTemplateSpec{ + Spec: batchv1.JobSpec{ + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + {Name: "busybox", Image: "busybox", Command: []string{"sleep", "60"}}, + }, + RestartPolicy: "OnFailure", + }, + }, + }, + }, + }, + } + cronjob, err := clientset.BatchV1().CronJobs("default").Create(context.Background(), cronjobSpec, metav1.CreateOptions{}) + if err != nil { + fmt.Println(err) + return + } + fmt.Println("Cronjob created :", cronjob.GetName()) +} + +func deleteCronjob(clientset kubernetes.Clientset, name string, namespace string) { + deletePolicy := metav1.DeletePropagationForeground + if err := clientset.BatchV1().CronJobs(namespace).Delete(context.TODO(), "cronjob-"+name, metav1.DeleteOptions{ + PropagationPolicy: &deletePolicy, + }); err != nil { + fmt.Println(err) + return + } + fmt.Println("Cronjob deleted:", "cronjob-"+name) +} + +func createDeployment(clientset kubernetes.Clientset, name string, namespace string, image string, label map[string]string) { + deploymentSpec := &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "deployment-" + name, + Namespace: namespace, + }, + Spec: appsv1.DeploymentSpec{ + Replicas: int32Ptr(1), + Selector: &metav1.LabelSelector{ + MatchLabels: label, + }, + Template: corev1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Labels: label, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "web", + Image: image, + Ports: []corev1.ContainerPort{ + { + Name: "http", + Protocol: corev1.ProtocolTCP, + ContainerPort: 80, + }, + }, + }, + }, + }, + }, + }, + } + + deployment, err := clientset.AppsV1().Deployments("default").Create(context.Background(), deploymentSpec, metav1.CreateOptions{}) + if err != nil { + fmt.Println(err) + return + } + fmt.Println("deployment sucessfully created:", deployment.GetName()) +} + +func deleteDeployment(clientset kubernetes.Clientset, name string, namespace string) { + deletePolicy := metav1.DeletePropagationForeground + if err := clientset.AppsV1().Deployments(namespace).Delete(context.TODO(), "deployment-"+name, metav1.DeleteOptions{ + PropagationPolicy: &deletePolicy, + }); err != nil { + fmt.Println(err) + return + } + fmt.Println("Deployment deleted:", "deployment-"+name) +} + +func int32Ptr(i int32) *int32 { return &i }