95 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			95 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package main
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"github.com/spf13/cobra"
 | |
| 	"log"
 | |
| 	"os"
 | |
| )
 | |
| 
 | |
| type Options struct {
 | |
| 	cni        string
 | |
| 	policyType string
 | |
| }
 | |
| 
 | |
| func readConfig(files []string) (*Config, error) {
 | |
| 	config := &Config{}
 | |
| 	for _, file := range files {
 | |
| 		log.Printf("LOADING %s\n", file)
 | |
| 		configNew, err := LoadConfig(file)
 | |
| 		if err != nil {
 | |
| 			return nil, fmt.Errorf("%s: %w", file, err)
 | |
| 		}
 | |
| 		config.Update(configNew)
 | |
| 	}
 | |
| 	err := config.Validate()
 | |
| 	if err != nil {
 | |
| 		return nil, fmt.Errorf("Error loading configuration: %w", err)
 | |
| 	}
 | |
| 	return config, nil
 | |
| }
 | |
| 
 | |
| func generate(files []string, options *Options) error {
 | |
| 	if len(files) == 0 {
 | |
| 		return fmt.Errorf("File expected")
 | |
| 	}
 | |
| 	config, err := readConfig(files)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	policyTemplates, err := NewPolicyTemplates()
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	var generator Generator
 | |
| 	generator = NetworkPolicyGenerrator{
 | |
| 		config:          config,
 | |
| 		policyTemplates: policyTemplates,
 | |
| 	}
 | |
| 	err = Generate(os.Stdout, generator, config)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func main() {
 | |
| 
 | |
| 	options := Options{
 | |
| 		cni:        "cilium",
 | |
| 		policyType: "netpol",
 | |
| 	}
 | |
| 	cmd := &cobra.Command{
 | |
| 		Use:   "policygen",
 | |
| 		Short: "Defining policies to enforce topology using network policies and service meshes",
 | |
| 		Long:  "Defining policies to enforce topology using network policies and service meshes",
 | |
| 	}
 | |
| 
 | |
| 	generate := &cobra.Command{
 | |
| 		Use:   "generate",
 | |
| 		Short: "Generate policies",
 | |
| 		Long:  "Generate policies",
 | |
| 		RunE: func(cmd *cobra.Command, args []string) error {
 | |
| 			return generate(args, &options)
 | |
| 		},
 | |
| 	}
 | |
| 	cmd.AddCommand(generate)
 | |
| 
 | |
| 	validate := &cobra.Command{
 | |
| 		Use:   "validate",
 | |
| 		Short: "Validate configuration",
 | |
| 		Long:  "Validate configuration",
 | |
| 		RunE: func(cmd *cobra.Command, args []string) error {
 | |
| 			return validate(args, &options)
 | |
| 		},
 | |
| 	}
 | |
| 	cmd.AddCommand(validate)
 | |
| 
 | |
| 	err := cmd.Execute()
 | |
| 	if err != nil {
 | |
| 		os.Exit(1)
 | |
| 	}
 | |
| }
 |