diff --git a/cmd/helmChart.go b/cmd/helmChart.go new file mode 100644 index 000000000..3ebe525a0 --- /dev/null +++ b/cmd/helmChart.go @@ -0,0 +1,88 @@ +package cmd + +import ( + "os" + "path/filepath" + "sort" + + "github.com/kubeshark/kubeshark/utils" + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" +) + +var helmChartCmd = &cobra.Command{ + Use: "helm-chart", + Short: "Generate Helm chart of Kubeshark", + RunE: func(cmd *cobra.Command, args []string) error { + runHelmChart() + return nil + }, +} + +func init() { + rootCmd.AddCommand(helmChartCmd) +} + +func runHelmChart() { + namespace, + serviceAccount, + clusterRole, + clusterRoleBinding, + hubPod, + hubService, + frontPod, + frontService, + workerDaemonSet, + err := generateManifests() + if err != nil { + log.Error().Err(err).Send() + return + } + + err = dumpHelmChart(map[string]interface{}{ + "00-namespace.yaml": namespace, + "01-service-account.yaml": serviceAccount, + "02-cluster-role.yaml": clusterRole, + "03-cluster-role-binding.yaml": clusterRoleBinding, + "04-hub-pod.yaml": hubPod, + "05-hub-service.yaml": hubService, + "06-front-pod.yaml": frontPod, + "07-front-service.yaml": frontService, + "08-worker-daemon-set.yaml": workerDaemonSet, + }) + if err != nil { + log.Error().Err(err).Send() + return + } +} + +func dumpHelmChart(objects map[string]interface{}) error { + folder := filepath.Join(".", "helm-chart/templates") + err := os.MkdirAll(folder, os.ModePerm) + if err != nil { + return err + } + + // Sort by filenames + filenames := make([]string, 0) + for filename := range objects { + filenames = append(filenames, filename) + } + sort.Strings(filenames) + + for _, filename := range filenames { + manifest, err := utils.PrettyYamlOmitEmpty(objects[filename]) + if err != nil { + return err + } + + path := filepath.Join(folder, filename) + err = os.WriteFile(path, []byte(manifest), 0644) + if err != nil { + return err + } + log.Info().Msgf("Helm chart template generated: %s", path) + } + + return nil +} diff --git a/cmd/manifests.go b/cmd/manifests.go index 254d7f93a..7856f3ded 100644 --- a/cmd/manifests.go +++ b/cmd/manifests.go @@ -13,6 +13,8 @@ import ( "github.com/kubeshark/kubeshark/utils" "github.com/rs/zerolog/log" "github.com/spf13/cobra" + v1 "k8s.io/api/core/v1" + rbac "k8s.io/api/rbac/v1" ) const manifestSeperator = "---" @@ -38,65 +40,16 @@ func init() { } func runManifests() { - kubernetesProvider, err := getKubernetesProviderForCli(true) - if err != nil { - log.Error().Err(err).Send() - return - } - - namespace := kubernetesProvider.BuildNamespace(config.Config.Tap.SelfNamespace) - - serviceAccount := kubernetesProvider.BuildServiceAccount() - - clusterRole := kubernetesProvider.BuildClusterRole() - - clusterRoleBinding := kubernetesProvider.BuildClusterRoleBinding() - - hubPod, err := kubernetesProvider.BuildHubPod(&kubernetes.PodOptions{ - Namespace: config.Config.Tap.SelfNamespace, - PodName: kubernetes.HubPodName, - PodImage: docker.GetHubImage(), - ServiceAccountName: kubernetes.ServiceAccountName, - Resources: config.Config.Tap.Resources.Hub, - ImagePullPolicy: config.Config.ImagePullPolicy(), - ImagePullSecrets: config.Config.ImagePullSecrets(), - Debug: config.Config.Tap.Debug, - }) - if err != nil { - log.Error().Err(err).Send() - return - } - - hubService := kubernetesProvider.BuildHubService(config.Config.Tap.SelfNamespace) - - frontPod, err := kubernetesProvider.BuildFrontPod(&kubernetes.PodOptions{ - Namespace: config.Config.Tap.SelfNamespace, - PodName: kubernetes.FrontPodName, - PodImage: docker.GetHubImage(), - ServiceAccountName: kubernetes.ServiceAccountName, - Resources: config.Config.Tap.Resources.Hub, - ImagePullPolicy: config.Config.ImagePullPolicy(), - ImagePullSecrets: config.Config.ImagePullSecrets(), - Debug: config.Config.Tap.Debug, - }, config.Config.Tap.Proxy.Host, fmt.Sprintf("%d", config.Config.Tap.Proxy.Hub.SrcPort)) - if err != nil { - log.Error().Err(err).Send() - return - } - - frontService := kubernetesProvider.BuildFrontService(config.Config.Tap.SelfNamespace) - - workerDaemonSet, err := kubernetesProvider.BuildWorkerDaemonSet( - docker.GetWorkerImage(), - kubernetes.WorkerDaemonSetName, - kubernetes.ServiceAccountName, - config.Config.Tap.Resources.Worker, - config.Config.ImagePullPolicy(), - config.Config.ImagePullSecrets(), - config.Config.Tap.ServiceMesh, - config.Config.Tap.Tls, - config.Config.Tap.Debug, - ) + namespace, + serviceAccount, + clusterRole, + clusterRoleBinding, + hubPod, + hubService, + frontPod, + frontService, + workerDaemonSet, + err := generateManifests() if err != nil { log.Error().Err(err).Send() return @@ -133,6 +86,82 @@ func runManifests() { } } +func generateManifests() ( + namespace *v1.Namespace, + serviceAccount *v1.ServiceAccount, + clusterRole *rbac.ClusterRole, + clusterRoleBinding *rbac.ClusterRoleBinding, + hubPod *v1.Pod, + hubService *v1.Service, + frontPod *v1.Pod, + frontService *v1.Service, + workerDaemonSet *kubernetes.DaemonSet, + err error, +) { + var kubernetesProvider *kubernetes.Provider + kubernetesProvider, err = getKubernetesProviderForCli(true) + if err != nil { + return + } + + namespace = kubernetesProvider.BuildNamespace(config.Config.Tap.SelfNamespace) + + serviceAccount = kubernetesProvider.BuildServiceAccount() + + clusterRole = kubernetesProvider.BuildClusterRole() + + clusterRoleBinding = kubernetesProvider.BuildClusterRoleBinding() + + hubPod, err = kubernetesProvider.BuildHubPod(&kubernetes.PodOptions{ + Namespace: config.Config.Tap.SelfNamespace, + PodName: kubernetes.HubPodName, + PodImage: docker.GetHubImage(), + ServiceAccountName: kubernetes.ServiceAccountName, + Resources: config.Config.Tap.Resources.Hub, + ImagePullPolicy: config.Config.ImagePullPolicy(), + ImagePullSecrets: config.Config.ImagePullSecrets(), + Debug: config.Config.Tap.Debug, + }) + if err != nil { + return + } + + hubService = kubernetesProvider.BuildHubService(config.Config.Tap.SelfNamespace) + + frontPod, err = kubernetesProvider.BuildFrontPod(&kubernetes.PodOptions{ + Namespace: config.Config.Tap.SelfNamespace, + PodName: kubernetes.FrontPodName, + PodImage: docker.GetHubImage(), + ServiceAccountName: kubernetes.ServiceAccountName, + Resources: config.Config.Tap.Resources.Hub, + ImagePullPolicy: config.Config.ImagePullPolicy(), + ImagePullSecrets: config.Config.ImagePullSecrets(), + Debug: config.Config.Tap.Debug, + }, config.Config.Tap.Proxy.Host, fmt.Sprintf("%d", config.Config.Tap.Proxy.Hub.SrcPort)) + if err != nil { + return + } + + frontService = kubernetesProvider.BuildFrontService(config.Config.Tap.SelfNamespace) + + workerDaemonSet, err = kubernetesProvider.BuildWorkerDaemonSet( + docker.GetWorkerImage(), + kubernetes.WorkerDaemonSetName, + kubernetes.ServiceAccountName, + config.Config.Tap.Resources.Worker, + config.Config.ImagePullPolicy(), + config.Config.ImagePullSecrets(), + config.Config.Tap.ServiceMesh, + config.Config.Tap.Tls, + config.Config.Tap.Debug, + ) + if err != nil { + return + } + + return +} + func dumpManifests(objects map[string]interface{}) error { folder := filepath.Join(".", "manifests") err := os.MkdirAll(folder, os.ModePerm)