From 9f7f265fd1986e032ba81bdd18d7492a932f1fd8 Mon Sep 17 00:00:00 2001 From: Aatman Date: Wed, 8 Sep 2021 14:13:26 +0530 Subject: [PATCH] feat: multiple configs (#1) * feat: multiple configs * chore: remove temp file at the end --- README.md | 122 ++++++++++- cmd/typescript.go | 17 +- main.go | 11 +- ...ube2cdk8s-TestKube2CDK8SMultipleDeployment | 72 ++++++ ...dk8s-TestKube2CDK8SMultipleDeploymentThree | 69 ++++++ ...2cdk8s-TestKube2CDK8SMultipleDeploymentTwo | 72 ++++++ pkg/kube2cdk8s/kube2cdk8s.go | 39 ++++ pkg/kube2cdk8s/kube2cdk8s_test.go | 206 ++++++++++++++++-- util/util.go | 23 ++ 9 files changed, 610 insertions(+), 21 deletions(-) create mode 100644 pkg/kube2cdk8s/.snapshots/kube2cdk8s-TestKube2CDK8SMultipleDeployment create mode 100644 pkg/kube2cdk8s/.snapshots/kube2cdk8s-TestKube2CDK8SMultipleDeploymentThree create mode 100644 pkg/kube2cdk8s/.snapshots/kube2cdk8s-TestKube2CDK8SMultipleDeploymentTwo create mode 100644 util/util.go diff --git a/README.md b/README.md index e36e9f1..41ab2f1 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ $ pulumi plugin install resource kubernetes v2.4.2 ## Usage +``` +$ go test +$ go build +``` ``` $ printf 'apiVersion: v1 kind: ServiceAccount @@ -25,9 +29,7 @@ metadata: name: my-service-account namespace: my-namespace' > temp.yaml ``` - ``` -$ go build $ ./kube2cdk8s typescript -f temp.yaml new cdk8s.ApiObject("", this, { apiVersion: "v1", @@ -38,3 +40,119 @@ new cdk8s.ApiObject("", this, { }, }); ``` +``` +printf '--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: my-deployment + namespace: my-namespace +spec: + selector: + matchLabels: + app: my-deployment + replicas: 3 + template: + metadata: + labels: + app: my-deployment + spec: + containers: + - name: my-deployment + image: my-image + imagePullPolicy: Always + ports: + - containerPort: 8080 +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: my-deployment-2 + namespace: my-namespace-2 +spec: + selector: + matchLabels: + app: my-deployment-2 + replicas: 4 + template: + metadata: + labels: + app: my-deployment-2 + spec: + containers: + - name: my-deployment-2 + image: my-image-2 + imagePullPolicy: Always + ports: + - containerPort: 8080' > temp.yaml +``` +``` +$ ./kube2cdk8s typescript -m true -f temp.yaml +new cdk8s.ApiObject("", this, { + apiVersion: "apps/v1", + kind: "Deployment", + metadata: { + name: "my-deployment", + namespace: "my-namespace", + }, + spec: { + selector: { + matchLabels: { + app: "my-deployment", + }, + }, + replicas: 3, + template: { + metadata: { + labels: { + app: "my-deployment", + }, + }, + spec: { + containers: [{ + name: "my-deployment", + image: "my-image", + imagePullPolicy: "Always", + ports: [{ + containerPort: 8080, + }], + }], + }, + }, + }, +}); + +new cdk8s.ApiObject("", this, { + apiVersion: "apps/v1", + kind: "Deployment", + metadata: { + name: "my-deployment-2", + namespace: "my-namespace-2", + }, + spec: { + selector: { + matchLabels: { + app: "my-deployment-2", + }, + }, + replicas: 4, + template: { + metadata: { + labels: { + app: "my-deployment-2", + }, + }, + spec: { + containers: [{ + name: "my-deployment-2", + image: "my-image-2", + imagePullPolicy: "Always", + ports: [{ + containerPort: 8080, + }], + }], + }, + }, + }, +}); +``` diff --git a/cmd/typescript.go b/cmd/typescript.go index f304684..a0fbfcb 100644 --- a/cmd/typescript.go +++ b/cmd/typescript.go @@ -15,17 +15,30 @@ func TSCommand() *cobra.Command { Long: "convert k8s yaml to typescript", RunE: func(cmd *cobra.Command, args []string) error { filePath := viper.GetString("file") + multiple := viper.GetBool("multiple") + + var result string if filePath == "" { log.Fatal("-f, --file is required") } - data, err := kube2cdk8s.Kube2CDK8S(filePath) + if multiple { + result, err := kube2cdk8s.Kube2CDK8SMultiple(filePath) + if err != nil { + return err + } + + fmt.Print(result) + return nil + } + + result, err := kube2cdk8s.Kube2CDK8S(filePath) if err != nil { return err } - fmt.Print(data) + fmt.Print(result) return nil }} diff --git a/main.go b/main.go index 4ad8e23..f6932dc 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,10 @@ import ( "github.com/spf13/viper" ) -var manifestFile string +var ( + manifestFile string + multiple bool +) func configureCLI() *cobra.Command { rootCmd := &cobra.Command{Use: "kube2cdk8s", Long: "converts k8s yaml to cdk8s"} @@ -38,6 +41,12 @@ func configureCLI() *cobra.Command { log.Println(err) } + rootCmd.PersistentFlags().BoolVarP(&multiple, "multiple", "m", false, "convert multiple yamls seperated by ---") + err = viper.BindPFlag("multiple", rootCmd.PersistentFlags().Lookup("multiple")) + if err != nil { + log.Println(err) + } + return rootCmd } diff --git a/pkg/kube2cdk8s/.snapshots/kube2cdk8s-TestKube2CDK8SMultipleDeployment b/pkg/kube2cdk8s/.snapshots/kube2cdk8s-TestKube2CDK8SMultipleDeployment new file mode 100644 index 0000000..232058d --- /dev/null +++ b/pkg/kube2cdk8s/.snapshots/kube2cdk8s-TestKube2CDK8SMultipleDeployment @@ -0,0 +1,72 @@ +new cdk8s.ApiObject("", this, { + apiVersion: "apps/v1", + kind: "Deployment", + metadata: { + name: "my-deployment", + namespace: "my-namespace", + }, + spec: { + selector: { + matchLabels: { + app: "my-deployment", + }, + }, + replicas: 3, + template: { + metadata: { + labels: { + app: "my-deployment", + }, + }, + spec: { + containers: [{ + name: "my-deployment", + image: "my-image", + imagePullPolicy: "Always", + ports: [{ + containerPort: 8080, + }], + }], + }, + }, + }, +}); + +new cdk8s.ApiObject("", this, { + apiVersion: "apps/v1", + kind: "Deployment", + metadata: { + name: "my-deployment-2", + namespace: "my-namespace-2", + }, + spec: { + selector: { + matchLabels: { + app: "my-deployment-2", + }, + }, + replicas: 4, + template: { + metadata: { + labels: { + app: "my-deployment-2", + }, + }, + spec: { + containers: [{ + name: "my-deployment-2", + image: "my-image-2", + imagePullPolicy: "Always", + ports: [{ + containerPort: 8080, + }], + }], + }, + }, + }, +}); + + + + + diff --git a/pkg/kube2cdk8s/.snapshots/kube2cdk8s-TestKube2CDK8SMultipleDeploymentThree b/pkg/kube2cdk8s/.snapshots/kube2cdk8s-TestKube2CDK8SMultipleDeploymentThree new file mode 100644 index 0000000..3cb91ed --- /dev/null +++ b/pkg/kube2cdk8s/.snapshots/kube2cdk8s-TestKube2CDK8SMultipleDeploymentThree @@ -0,0 +1,69 @@ +new cdk8s.ApiObject("", this, { + apiVersion: "apps/v1", + kind: "Deployment", + metadata: { + name: "my-deployment", + namespace: "my-namespace", + }, + spec: { + selector: { + matchLabels: { + app: "my-deployment", + }, + }, + replicas: 3, + template: { + metadata: { + labels: { + app: "my-deployment", + }, + }, + spec: { + containers: [{ + name: "my-deployment", + image: "my-image", + imagePullPolicy: "Always", + ports: [{ + containerPort: 8080, + }], + }], + }, + }, + }, +}); + +new cdk8s.ApiObject("", this, { + apiVersion: "apps/v1", + kind: "Deployment", + metadata: { + name: "my-deployment-2", + namespace: "my-namespace-2", + }, + spec: { + selector: { + matchLabels: { + app: "my-deployment-2", + }, + }, + replicas: 4, + template: { + metadata: { + labels: { + app: "my-deployment-2", + }, + }, + spec: { + containers: [{ + name: "my-deployment-2", + image: "my-image-2", + imagePullPolicy: "Always", + ports: [{ + containerPort: 8080, + }], + }], + }, + }, + }, +}); + + diff --git a/pkg/kube2cdk8s/.snapshots/kube2cdk8s-TestKube2CDK8SMultipleDeploymentTwo b/pkg/kube2cdk8s/.snapshots/kube2cdk8s-TestKube2CDK8SMultipleDeploymentTwo new file mode 100644 index 0000000..232058d --- /dev/null +++ b/pkg/kube2cdk8s/.snapshots/kube2cdk8s-TestKube2CDK8SMultipleDeploymentTwo @@ -0,0 +1,72 @@ +new cdk8s.ApiObject("", this, { + apiVersion: "apps/v1", + kind: "Deployment", + metadata: { + name: "my-deployment", + namespace: "my-namespace", + }, + spec: { + selector: { + matchLabels: { + app: "my-deployment", + }, + }, + replicas: 3, + template: { + metadata: { + labels: { + app: "my-deployment", + }, + }, + spec: { + containers: [{ + name: "my-deployment", + image: "my-image", + imagePullPolicy: "Always", + ports: [{ + containerPort: 8080, + }], + }], + }, + }, + }, +}); + +new cdk8s.ApiObject("", this, { + apiVersion: "apps/v1", + kind: "Deployment", + metadata: { + name: "my-deployment-2", + namespace: "my-namespace-2", + }, + spec: { + selector: { + matchLabels: { + app: "my-deployment-2", + }, + }, + replicas: 4, + template: { + metadata: { + labels: { + app: "my-deployment-2", + }, + }, + spec: { + containers: [{ + name: "my-deployment-2", + image: "my-image-2", + imagePullPolicy: "Always", + ports: [{ + containerPort: 8080, + }], + }], + }, + }, + }, +}); + + + + + diff --git a/pkg/kube2cdk8s/kube2cdk8s.go b/pkg/kube2cdk8s/kube2cdk8s.go index 3dab88c..9250ac0 100644 --- a/pkg/kube2cdk8s/kube2cdk8s.go +++ b/pkg/kube2cdk8s/kube2cdk8s.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/pulumi/kube2pulumi/pkg/kube2pulumi" + "github.com/smallcase/kube2cdk8s/util" ) func Kube2CDK8S(filePath string) (string, error) { @@ -37,3 +38,41 @@ func Kube2CDK8S(filePath string) (string, error) { return res, nil } + +func Kube2CDK8SMultiple(filePath string) (string, error) { + + var result string + + input, err := ioutil.ReadFile(filePath) + if err != nil { + return "", err + } + + m := strings.Split(string(input), "---") + + for _, v := range m { + + if v == "" { + continue + } + f, err := util.CreateTempFile([]byte(v)) + if err != nil { + return "", err + } + + res, err := Kube2CDK8S(f.Name()) + if err != nil { + return "", err + } + + result += res + result += "\n" + + err = os.Remove(f.Name()) + if err != nil { + return "", err + } + } + + return result, nil +} diff --git a/pkg/kube2cdk8s/kube2cdk8s_test.go b/pkg/kube2cdk8s/kube2cdk8s_test.go index c84d64e..713db8d 100644 --- a/pkg/kube2cdk8s/kube2cdk8s_test.go +++ b/pkg/kube2cdk8s/kube2cdk8s_test.go @@ -1,12 +1,12 @@ package kube2cdk8s import ( - "fmt" - "io/ioutil" "log" "os" "testing" + "github.com/smallcase/kube2cdk8s/util" + "github.com/bradleyjkemp/cupaloy" ) @@ -20,7 +20,7 @@ metadata: name: my-service-account namespace: my-namespace ` - serviceAccountFile, err := createTempFile([]byte(serviceAccount)) + serviceAccountFile, err := util.CreateTempFile([]byte(serviceAccount)) if err != nil { log.Println(err.Error()) } @@ -63,7 +63,7 @@ spec: ports: - containerPort: 8080 ` - deploymentFile, err := createTempFile([]byte(deployment)) + deploymentFile, err := util.CreateTempFile([]byte(deployment)) if err != nil { log.Println(err.Error()) } @@ -81,25 +81,199 @@ spec: defer os.Remove(deploymentFile.Name()) } -func createTempFile(text []byte) (*os.File, error) { - tmpFile, err := ioutil.TempFile(os.TempDir(), "prefix-") +func TestKube2CDK8SMultipleDeployment(t *testing.T) { + + deployment := ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: my-deployment + namespace: my-namespace +spec: + selector: + matchLabels: + app: my-deployment + replicas: 3 + template: + metadata: + labels: + app: my-deployment + spec: + containers: + - name: my-deployment + image: my-image + imagePullPolicy: Always + ports: + - containerPort: 8080 +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: my-deployment-2 + namespace: my-namespace-2 +spec: + selector: + matchLabels: + app: my-deployment-2 + replicas: 4 + template: + metadata: + labels: + app: my-deployment-2 + spec: + containers: + - name: my-deployment-2 + image: my-image-2 + imagePullPolicy: Always + ports: + - containerPort: 8080 +--- +` + deploymentFile, err := util.CreateTempFile([]byte(deployment)) + if err != nil { + log.Println(err.Error()) + } + + d, err := Kube2CDK8SMultiple(deploymentFile.Name()) + if err != nil { + log.Println(err.Error()) + } + + err = cupaloy.Snapshot(d) + if err != nil { + t.Error(err.Error()) + } + + defer os.Remove(deploymentFile.Name()) +} + +func TestKube2CDK8SMultipleDeploymentTwo(t *testing.T) { + + deployment := `--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: my-deployment + namespace: my-namespace +spec: + selector: + matchLabels: + app: my-deployment + replicas: 3 + template: + metadata: + labels: + app: my-deployment + spec: + containers: + - name: my-deployment + image: my-image + imagePullPolicy: Always + ports: + - containerPort: 8080 +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: my-deployment-2 + namespace: my-namespace-2 +spec: + selector: + matchLabels: + app: my-deployment-2 + replicas: 4 + template: + metadata: + labels: + app: my-deployment-2 + spec: + containers: + - name: my-deployment-2 + image: my-image-2 + imagePullPolicy: Always + ports: + - containerPort: 8080 +--- +` + deploymentFile, err := util.CreateTempFile([]byte(deployment)) + if err != nil { + log.Println(err.Error()) + } + + d, err := Kube2CDK8SMultiple(deploymentFile.Name()) + if err != nil { + log.Println(err.Error()) + } + + err = cupaloy.Snapshot(d) if err != nil { - return nil, err + t.Error(err.Error()) } - // Remember to clean up the file afterwards + defer os.Remove(deploymentFile.Name()) +} - fmt.Println("Created File: " + tmpFile.Name()) +func TestKube2CDK8SMultipleDeploymentThree(t *testing.T) { - // Example writing to the file - if _, err = tmpFile.Write(text); err != nil { - return nil, err + deployment := `--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: my-deployment + namespace: my-namespace +spec: + selector: + matchLabels: + app: my-deployment + replicas: 3 + template: + metadata: + labels: + app: my-deployment + spec: + containers: + - name: my-deployment + image: my-image + imagePullPolicy: Always + ports: + - containerPort: 8080 +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: my-deployment-2 + namespace: my-namespace-2 +spec: + selector: + matchLabels: + app: my-deployment-2 + replicas: 4 + template: + metadata: + labels: + app: my-deployment-2 + spec: + containers: + - name: my-deployment-2 + image: my-image-2 + imagePullPolicy: Always + ports: + - containerPort: 8080 +` + deploymentFile, err := util.CreateTempFile([]byte(deployment)) + if err != nil { + log.Println(err.Error()) } - // Close the file - if err := tmpFile.Close(); err != nil { - return nil, err + d, err := Kube2CDK8SMultiple(deploymentFile.Name()) + if err != nil { + log.Println(err.Error()) } - return tmpFile, nil + err = cupaloy.Snapshot(d) + if err != nil { + t.Error(err.Error()) + } + + defer os.Remove(deploymentFile.Name()) } diff --git a/util/util.go b/util/util.go new file mode 100644 index 0000000..3de508e --- /dev/null +++ b/util/util.go @@ -0,0 +1,23 @@ +package util + +import ( + "io/ioutil" + "os" +) + +func CreateTempFile(text []byte) (*os.File, error) { + tmpFile, err := ioutil.TempFile(os.TempDir(), "prefix-") + if err != nil { + return nil, err + } + + if _, err = tmpFile.Write(text); err != nil { + return nil, err + } + + if err := tmpFile.Close(); err != nil { + return nil, err + } + + return tmpFile, nil +}