diff --git a/cmd/k8s.go b/cmd/k8s.go new file mode 100644 index 0000000..2672d30 --- /dev/null +++ b/cmd/k8s.go @@ -0,0 +1,42 @@ +package cmd + +import ( + "fmt" + + "github.com/aryansharma9917/codewise-cli/pkg/k8s" + "github.com/spf13/cobra" +) + +var ( + k8sAppName string + k8sImage string +) + +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) { + err := k8s.InitK8sManifests(k8s.Options{ + AppName: k8sAppName, + Image: k8sImage, + }) + if err != nil { + fmt.Println("ℹ️", err.Error()) + return + } + 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) +} 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 diff --git a/pkg/k8s/k8s.go b/pkg/k8s/k8s.go new file mode 100644 index 0000000..72e913a --- /dev/null +++ b/pkg/k8s/k8s.go @@ -0,0 +1,87 @@ +package k8s + +import ( + "fmt" + "os" + "path/filepath" +) + +const k8sDir = "k8s/app" + +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: %s +spec: + replicas: 1 + selector: + matchLabels: + app: %s + template: + metadata: + labels: + app: %s + spec: + containers: + - 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" + } + + return []byte(fmt.Sprintf(`apiVersion: v1 +kind: Service +metadata: + name: %s-service +spec: + type: ClusterIP + selector: + app: %s + ports: + - port: 80 + targetPort: 8080 +`, appName, appName)) +} + +// InitK8sManifests creates Kubernetes manifests with optional values +func InitK8sManifests(opts Options) error { + if _, err := os.Stat(k8sDir); err == nil { + return fmt.Errorf("k8s/app 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(opts), 0644); err != nil { + return err + } + + if err := os.WriteFile(svcPath, serviceYAML(opts.AppName), 0644); err != nil { + return err + } + + return nil +}