-
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.
- Loading branch information
Husni Alhamdani
committed
Apr 18, 2022
0 parents
commit 34afb12
Showing
8 changed files
with
389 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,60 @@ | ||
/* | ||
Copyright © 2022 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
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) | ||
} | ||
} |
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,38 @@ | ||
/* | ||
Copyright © 2022 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
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") | ||
} |
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,42 @@ | ||
/* | ||
Copyright © 2022 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
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") | ||
} |
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,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 | ||
) |
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,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= |
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,11 @@ | ||
/* | ||
Copyright © 2022 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package main | ||
|
||
import "github.com/husnialhamdani/kyvernop/cmd" | ||
|
||
func main() { | ||
cmd.Execute() | ||
} |
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,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 } |