-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
Copyright © 2024 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/conduktor/ctl/resource" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
var filePath *string | ||
|
||
// applyCmd represents the apply command | ||
var applyCmd = &cobra.Command{ | ||
Use: "apply", | ||
Short: "upsert a resource on kubernetes", | ||
Long: ``, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
resources, error := resource.FromFile(*filePath) | ||
if error != nil { | ||
fmt.Fprintf(os.Stderr, "%s\n", error) | ||
os.Exit(1) | ||
} | ||
fmt.Println(resources) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(applyCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
filePath = applyCmd. | ||
PersistentFlags().StringP("file", "f", "", "Specify the file to apply") | ||
|
||
applyCmd.MarkPersistentFlagRequired("file") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
Copyright © 2024 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "conduktor", | ||
Short: "command line tools for conduktor", | ||
Long: ``, | ||
// Uncomment the following line if your bare application | ||
// has an action associated with it: | ||
// Run: func(cmd *cobra.Command, args []string) { }, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module github.com/conduktor/ctl | ||
|
||
go 1.22.0 | ||
|
||
require github.com/spf13/cobra v1.8.0 | ||
|
||
require ( | ||
github.com/ghodss/yaml v1.0.0 // indirect | ||
github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | ||
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= | ||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= | ||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= | ||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= | ||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= | ||
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= | ||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "github.com/conduktor/ctl/cmd" | ||
|
||
func main() { | ||
cmd.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package resource | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
yamlJson "github.com/ghodss/yaml" | ||
yaml "gopkg.in/yaml.v3" | ||
"io" | ||
"os" | ||
) | ||
|
||
type Resource struct { | ||
Json []byte | ||
Kind string | ||
Name string | ||
ApiVersion string | ||
} | ||
|
||
func (r Resource) String() string { | ||
return fmt.Sprintf(`version: %s, kind: %s, name: %s, json: '%s'`, r.ApiVersion, r.Kind, r.Name, string(r.Json)) | ||
} | ||
|
||
type yamlRoot struct { | ||
ApiVersion string | ||
Kind string | ||
Metadata metadata | ||
} | ||
|
||
type metadata struct { | ||
Name string | ||
} | ||
|
||
func FromFile(path string) ([]Resource, error) { | ||
data, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return FromByte(data) | ||
} | ||
|
||
func FromByte(data []byte) ([]Resource, error) { | ||
reader := bytes.NewReader(data) | ||
var yamlData interface{} | ||
results := make([]Resource, 0, 2) | ||
d := yaml.NewDecoder(reader) | ||
for { | ||
err := d.Decode(&yamlData) | ||
if err == io.EOF { | ||
break | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
yamlByte, err := yaml.Marshal(yamlData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result, err := yamlByteToResource([]byte(yamlByte)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
results = append(results, result) | ||
} | ||
return results, nil | ||
} | ||
|
||
func yamlByteToResource(data []byte) (Resource, error) { | ||
jsonByte, err := yamlJson.YAMLToJSON(data) | ||
if err != nil { | ||
return Resource{}, nil | ||
} | ||
|
||
var yamlRoot yamlRoot | ||
err = json.Unmarshal(jsonByte, &yamlRoot) | ||
if err != nil { | ||
return Resource{}, nil | ||
} | ||
|
||
return Resource{Json: jsonByte, Kind: yamlRoot.Kind, Name: yamlRoot.Metadata.Name, ApiVersion: yamlRoot.ApiVersion}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package resource | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestFromByteForOneResourceWithValidResource(t *testing.T) { | ||
yamlByte := []byte(` | ||
# comment | ||
--- | ||
apiVersion: v1 | ||
kind: Topic | ||
metadata: | ||
name: abc.myTopic | ||
spec: | ||
replicationFactor: 1 | ||
--- | ||
apiVersion: v2 | ||
kind: ConsumerGroup | ||
metadata: | ||
name: cg1 | ||
`) | ||
|
||
results, err := FromByte(yamlByte) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if len(results) != 2 { | ||
t.Errorf("results expected1 of length 2, got length %d", len(results)) | ||
} | ||
|
||
result1 := results[0] | ||
expected1 := Resource{ | ||
ApiVersion: "v1", | ||
Kind: "Topic", | ||
Name: "abc.myTopic", | ||
Json: []byte(`{"apiVersion":"v1","kind":"Topic","metadata":{"name":"abc.myTopic"},"spec":{"replicationFactor":1}}`), | ||
} | ||
|
||
if result1.Name != expected1.Name { | ||
t.Errorf("Expected name %s got %s", expected1.Name, result1.Name) | ||
} | ||
|
||
if result1.Kind != expected1.Kind { | ||
t.Errorf("Expected name %s got %s", expected1.Kind, result1.Kind) | ||
} | ||
|
||
if result1.ApiVersion != expected1.ApiVersion { | ||
t.Errorf("Expected name %s got %s", expected1.ApiVersion, result1.ApiVersion) | ||
} | ||
|
||
expectedJsonString1 := string(expected1.Json) | ||
resultJsonString1 := string(result1.Json) | ||
if expectedJsonString1 != resultJsonString1 { | ||
t.Errorf("Expected json %s got %s", expectedJsonString1, resultJsonString1) | ||
} | ||
|
||
result2 := results[1] | ||
expected2 := Resource{ | ||
ApiVersion: "v2", | ||
Kind: "ConsumerGroup", | ||
Name: "cg1", | ||
Json: []byte(`{"apiVersion":"v2","kind":"ConsumerGroup","metadata":{"name":"cg1"}}`), | ||
} | ||
|
||
if result2.Name != expected2.Name { | ||
t.Errorf("Expected name %s got %s", expected2.Name, result2.Name) | ||
} | ||
|
||
if result2.Kind != expected2.Kind { | ||
t.Errorf("Expected name %s got %s", expected2.Kind, result2.Kind) | ||
} | ||
|
||
if result2.ApiVersion != expected2.ApiVersion { | ||
t.Errorf("Expected name %s got %s", expected2.ApiVersion, result2.ApiVersion) | ||
} | ||
|
||
expectedJsonString2 := string(expected2.Json) | ||
resultJsonString2 := string(result2.Json) | ||
if expectedJsonString2 != resultJsonString2 { | ||
t.Errorf("Expected json %s got %s", expectedJsonString2, resultJsonString2) | ||
} | ||
} | ||
|
||
func TestFromByte(t *testing.T) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# topics.yml | ||
--- | ||
apiVersion: v1 | ||
kind: Topic | ||
metadata: | ||
name: abc.myTopic | ||
spec: | ||
replicationFactor: 1 | ||
partitions: 3 | ||
configs: | ||
min.insync.replicas: 1 | ||
cleanup.policy: delete | ||
retention.ms: 604800000 | ||
--- | ||
apiVersion: v1 | ||
kind: Topic | ||
metadata: | ||
name: abcd.myTopicWrong | ||
spec: | ||
replicationFactor: 1 | ||
partitions: 3 | ||
configs: | ||
min.insync.replicas: 1 | ||
cleanup.policy: delete | ||
retention.ms: 604800000 |