policy-generator/cmd/policygen/main.go

62 lines
1.2 KiB
Go

package main
import (
"bytes"
"fmt"
"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"os"
)
type Options struct {
cni string
policyType string
}
func execute(files []string, options *Options) error {
if len(files) == 0 {
return fmt.Errorf("File expected")
}
for _, file := range files {
fmt.Fprintf(os.Stderr, "Reading config %s\n", file)
yamlFile, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("Error reading YAML file: %v", err)
}
// Parse the YAML content
dec := yaml.NewDecoder(bytes.NewReader(yamlFile),
yaml.UseJSONUnmarshaler(),
yaml.DisallowUnknownField(),
)
var config Config
err = dec.Decode(&config)
if err != nil {
return fmt.Errorf("Error parsing YAML: %v", err)
}
err = config.Validate()
if err != nil {
return err
}
fmt.Printf("PARSED %+v\n", config)
}
return nil
}
func main() {
options := Options{
cni: "cilium",
policyType: "netpol",
}
cmd := &cobra.Command{
Use: "policygen",
Short: "Generate network policies",
Long: "Generated policies based on a more compact representation of topology",
RunE: func(cmd *cobra.Command, args []string) error {
return execute(args, &options)
},
}
cmd.Execute()
}