From 5cb06ebd22cce469174e80efe1762cb8232bbe2c Mon Sep 17 00:00:00 2001 From: aryansharma9917 Date: Wed, 7 Jan 2026 23:52:08 +0530 Subject: [PATCH 1/5] fix k8s init to use k8s/app directory --- pkg/k8s/k8s.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 pkg/k8s/k8s.go diff --git a/pkg/k8s/k8s.go b/pkg/k8s/k8s.go new file mode 100644 index 0000000..5eb8d27 --- /dev/null +++ b/pkg/k8s/k8s.go @@ -0,0 +1,67 @@ +package k8s + +import ( + "fmt" + "os" + "path/filepath" +) + +const k8sDir = "k8s/app" + +var deploymentYAML = []byte(`apiVersion: apps/v1 +kind: Deployment +metadata: + name: codewise-app +spec: + replicas: 1 + selector: + matchLabels: + app: codewise + template: + metadata: + labels: + app: codewise + spec: + containers: + - name: codewise + image: codewise:latest + ports: + - containerPort: 8080 +`) + +var serviceYAML = []byte(`apiVersion: v1 +kind: Service +metadata: + name: codewise-service +spec: + type: ClusterIP + selector: + app: codewise + ports: + - port: 80 + targetPort: 8080 +`) + +// InitK8sManifests creates basic Kubernetes manifests +func InitK8sManifests() error { + if _, err := os.Stat(k8sDir); err == nil { + return fmt.Errorf("k8s directory already exists") + } + + if err := os.MkdirAll(k8sDir, 0755); err != nil { + return err + } + + deployPath := filepath.Join(k8sDir, "deployment.yaml") + svcPath := filepath.Join(k8sDir, "service.yaml") + + if err := os.WriteFile(deployPath, deploymentYAML, 0644); err != nil { + return err + } + + if err := os.WriteFile(svcPath, serviceYAML, 0644); err != nil { + return err + } + + return nil +} From 6926628ef56c4202d1496d33b5bbfea4f59cbf93 Mon Sep 17 00:00:00 2001 From: aryansharma9917 Date: Wed, 7 Jan 2026 23:52:58 +0530 Subject: [PATCH 2/5] Added init command in cmd --- cmd/k8s.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 cmd/k8s.go diff --git a/cmd/k8s.go b/cmd/k8s.go new file mode 100644 index 0000000..b62148f --- /dev/null +++ b/cmd/k8s.go @@ -0,0 +1,30 @@ +package cmd + +import ( + "fmt" + + "github.com/aryansharma9917/codewise-cli/pkg/k8s" + "github.com/spf13/cobra" +) + +var k8sCmd = &cobra.Command{ + Use: "k8s", + Short: "Kubernetes helpers", +} + +var k8sInitCmd = &cobra.Command{ + Use: "init", + Short: "Generate Kubernetes manifests", + Run: func(cmd *cobra.Command, args []string) { + if err := k8s.InitK8sManifests(); err != nil { + fmt.Println("ℹ️", err.Error()) + return + } + fmt.Println("✅ Kubernetes manifests created in ./k8s") + }, +} + +func init() { + k8sCmd.AddCommand(k8sInitCmd) + rootCmd.AddCommand(k8sCmd) +} From ab98f58adfc7dfc0260b16384cb0a0ab2d62bd1a Mon Sep 17 00:00:00 2001 From: aryansharma9917 Date: Wed, 7 Jan 2026 23:53:35 +0530 Subject: [PATCH 3/5] Added k8s application func --- k8s/app/deployment.yaml | 19 +++++++++++++++++++ k8s/app/service.yaml | 11 +++++++++++ 2 files changed, 30 insertions(+) create mode 100644 k8s/app/deployment.yaml create mode 100644 k8s/app/service.yaml diff --git a/k8s/app/deployment.yaml b/k8s/app/deployment.yaml new file mode 100644 index 0000000..3bf31d6 --- /dev/null +++ b/k8s/app/deployment.yaml @@ -0,0 +1,19 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: codewise-app +spec: + replicas: 1 + selector: + matchLabels: + app: codewise + template: + metadata: + labels: + app: codewise + spec: + containers: + - name: codewise + image: codewise:latest + ports: + - containerPort: 8080 diff --git a/k8s/app/service.yaml b/k8s/app/service.yaml new file mode 100644 index 0000000..c9b1bf3 --- /dev/null +++ b/k8s/app/service.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: codewise-service +spec: + type: ClusterIP + selector: + app: codewise + ports: + - port: 80 + targetPort: 8080 From 828cea3e5c3a80587aa184a1d0b3f9859fa9e1b0 Mon Sep 17 00:00:00 2001 From: aryansharma9917 Date: Thu, 8 Jan 2026 00:03:19 +0530 Subject: [PATCH 4/5] add values support for k8s manifests --- pkg/k8s/k8s.go | 52 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/pkg/k8s/k8s.go b/pkg/k8s/k8s.go index 5eb8d27..72e913a 100644 --- a/pkg/k8s/k8s.go +++ b/pkg/k8s/k8s.go @@ -8,44 +8,64 @@ import ( const k8sDir = "k8s/app" -var deploymentYAML = []byte(`apiVersion: apps/v1 +type Options struct { + AppName string + Image string +} + +func deploymentYAML(opts Options) []byte { + if opts.AppName == "" { + opts.AppName = "codewise-app" + } + if opts.Image == "" { + opts.Image = "codewise:latest" + } + + return []byte(fmt.Sprintf(`apiVersion: apps/v1 kind: Deployment metadata: - name: codewise-app + name: %s spec: replicas: 1 selector: matchLabels: - app: codewise + app: %s template: metadata: labels: - app: codewise + app: %s spec: containers: - - name: codewise - image: codewise:latest + - name: %s + image: %s ports: - containerPort: 8080 -`) +`, opts.AppName, opts.AppName, opts.AppName, opts.AppName, opts.Image)) +} + +func serviceYAML(appName string) []byte { + if appName == "" { + appName = "codewise-app" + } -var serviceYAML = []byte(`apiVersion: v1 + return []byte(fmt.Sprintf(`apiVersion: v1 kind: Service metadata: - name: codewise-service + name: %s-service spec: type: ClusterIP selector: - app: codewise + app: %s ports: - port: 80 targetPort: 8080 -`) +`, appName, appName)) +} -// InitK8sManifests creates basic Kubernetes manifests -func InitK8sManifests() error { +// InitK8sManifests creates Kubernetes manifests with optional values +func InitK8sManifests(opts Options) error { if _, err := os.Stat(k8sDir); err == nil { - return fmt.Errorf("k8s directory already exists") + return fmt.Errorf("k8s/app directory already exists") } if err := os.MkdirAll(k8sDir, 0755); err != nil { @@ -55,11 +75,11 @@ func InitK8sManifests() error { deployPath := filepath.Join(k8sDir, "deployment.yaml") svcPath := filepath.Join(k8sDir, "service.yaml") - if err := os.WriteFile(deployPath, deploymentYAML, 0644); err != nil { + if err := os.WriteFile(deployPath, deploymentYAML(opts), 0644); err != nil { return err } - if err := os.WriteFile(svcPath, serviceYAML, 0644); err != nil { + if err := os.WriteFile(svcPath, serviceYAML(opts.AppName), 0644); err != nil { return err } From a0b93ad58a89f06b037374536a3841faa10fcc1b Mon Sep 17 00:00:00 2001 From: aryansharma9917 Date: Thu, 8 Jan 2026 00:03:19 +0530 Subject: [PATCH 5/5] add app and image flags to k8s init --- cmd/k8s.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/cmd/k8s.go b/cmd/k8s.go index b62148f..2672d30 100644 --- a/cmd/k8s.go +++ b/cmd/k8s.go @@ -7,6 +7,11 @@ import ( "github.com/spf13/cobra" ) +var ( + k8sAppName string + k8sImage string +) + var k8sCmd = &cobra.Command{ Use: "k8s", Short: "Kubernetes helpers", @@ -16,15 +21,22 @@ var k8sInitCmd = &cobra.Command{ Use: "init", Short: "Generate Kubernetes manifests", Run: func(cmd *cobra.Command, args []string) { - if err := k8s.InitK8sManifests(); err != nil { + err := k8s.InitK8sManifests(k8s.Options{ + AppName: k8sAppName, + Image: k8sImage, + }) + if err != nil { fmt.Println("ℹ️", err.Error()) return } - fmt.Println("✅ Kubernetes manifests created in ./k8s") + fmt.Println("✅ Kubernetes manifests created in ./k8s/app") }, } func init() { + k8sInitCmd.Flags().StringVar(&k8sAppName, "app", "", "Application name") + k8sInitCmd.Flags().StringVar(&k8sImage, "image", "", "Docker image name") + k8sCmd.AddCommand(k8sInitCmd) rootCmd.AddCommand(k8sCmd) }