Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions cmd/k8s.go
Original file line number Diff line number Diff line change
@@ -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)
}
19 changes: 19 additions & 0 deletions k8s/app/deployment.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions k8s/app/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: codewise-service
spec:
type: ClusterIP
selector:
app: codewise
ports:
- port: 80
targetPort: 8080
87 changes: 87 additions & 0 deletions pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
@@ -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
}