@@ -13,45 +13,45 @@ import (
1313)
1414
1515type GenerateConfig struct {
16- resourceGroupDefinitionFile string
16+ resourceGraphDefinitionFile string
1717 outputFormat string
1818}
1919
2020var config = & GenerateConfig {}
2121
2222func init () {
23- generateCmd .PersistentFlags ().StringVarP (& config .resourceGroupDefinitionFile , "file" , "f" , "" ,
24- "Path to the ResourceGroupDefinition file" )
23+ generateCmd .PersistentFlags ().StringVarP (& config .resourceGraphDefinitionFile , "file" , "f" , "" ,
24+ "Path to the ResourceGraphDefinition file" )
2525 generateCmd .PersistentFlags ().StringVarP (& config .outputFormat , "format" , "o" , "yaml" , "Output format (yaml|json)" )
2626}
2727
2828var generateCmd = & cobra.Command {
2929 Use : "generate" ,
30- Short : "Generate the CRD from a given ResourceGroupDefinition " ,
31- Long : "Generate the CRD from a given ResourceGroupDefinition ." +
32- "This command generates a CustomResourceDefinition (CRD) based on the provided ResourceGroupDefinition file." ,
30+ Short : "Generate the CRD from a given ResourceGraphDefinition " ,
31+ Long : "Generate the CRD from a given ResourceGraphDefinition ." +
32+ "This command generates a CustomResourceDefinition (CRD) based on the provided ResourceGraphDefinition file." ,
3333}
3434
3535var generateCRDCmd = & cobra.Command {
3636 Use : "crd" ,
3737 Short : "Generate a CustomResourceDefinition (CRD)" ,
3838 Long : "Generate a CustomResourceDefinition (CRD) from a " +
39- "ResourceGroupDefinition file. This command reads the " +
40- "ResourceGroupDefinition and outputs the corresponding CRD " +
39+ "ResourceGraphDefinition file. This command reads the " +
40+ "ResourceGraphDefinition and outputs the corresponding CRD " +
4141 "in the specified format." ,
4242 RunE : func (cmd * cobra.Command , args []string ) error {
43- if config .resourceGroupDefinitionFile == "" {
44- return fmt .Errorf ("ResourceGroupDefinition file is required" )
43+ if config .resourceGraphDefinitionFile == "" {
44+ return fmt .Errorf ("ResourceGraphDefinition file is required" )
4545 }
4646
47- data , err := os .ReadFile (config .resourceGroupDefinitionFile )
47+ data , err := os .ReadFile (config .resourceGraphDefinitionFile )
4848 if err != nil {
49- return fmt .Errorf ("failed to read ResourceGroupDefinition file: %w" , err )
49+ return fmt .Errorf ("failed to read ResourceGraphDefinition file: %w" , err )
5050 }
5151
5252 var rgd v1alpha1.ResourceGraphDefinition
5353 if err = yaml .Unmarshal (data , & rgd ); err != nil {
54- return fmt .Errorf ("failed to unmarshal ResourceGroupDefinition : %w" , err )
54+ return fmt .Errorf ("failed to unmarshal ResourceGraphDefinition : %w" , err )
5555 }
5656
5757 if err = generateCRD (& rgd ); err != nil {
@@ -62,6 +62,35 @@ var generateCRDCmd = &cobra.Command{
6262 },
6363}
6464
65+ var generateInstanceCmd = & cobra.Command {
66+ Use : "instance" ,
67+ Short : "Generate a ResourceGraphDefinition (RGD) instance" ,
68+ Long : "Generate a ResourceGraphDefinition (RGD) instance from a " +
69+ "ResourceGraphDefinition file. This command reads the " +
70+ "ResourceGraphDefinition and outputs the corresponding RGD instance" ,
71+ RunE : func (cmd * cobra.Command , args []string ) error {
72+ if config .resourceGraphDefinitionFile == "" {
73+ return fmt .Errorf ("ResourceGraphDefinition file is required" )
74+ }
75+
76+ data , err := os .ReadFile (config .resourceGraphDefinitionFile )
77+ if err != nil {
78+ return fmt .Errorf ("failed to read ResourceGraphDefinition file: %w" , err )
79+ }
80+
81+ var rgd v1alpha1.ResourceGraphDefinition
82+ if err = yaml .Unmarshal (data , & rgd ); err != nil {
83+ return fmt .Errorf ("failed to unmarshal ResourceGraphDefinition: %w" , err )
84+ }
85+
86+ if err = generateInstance (& rgd ); err != nil {
87+ return fmt .Errorf ("failed to generate RGD instance: %w" , err )
88+ }
89+
90+ return nil
91+ },
92+ }
93+
6594func generateCRD (rgd * v1alpha1.ResourceGraphDefinition ) error {
6695 rgdGraph , err := createGraphBuilder (rgd )
6796 if err != nil {
@@ -81,6 +110,27 @@ func generateCRD(rgd *v1alpha1.ResourceGraphDefinition) error {
81110 return nil
82111}
83112
113+ func generateInstance (rgd * v1alpha1.ResourceGraphDefinition ) error {
114+ rgdGraph , err := createGraphBuilder (rgd )
115+ if err != nil {
116+ return fmt .Errorf ("failed to create resource graph definition: %w" , err )
117+ }
118+
119+ emulatedObj := rgdGraph .Instance .GetEmulatedObject ()
120+ emulatedObj .SetAnnotations (map [string ]string {"kro.run/version" : "dev" })
121+
122+ delete (emulatedObj .Object , "status" )
123+
124+ b , err := marshalObject (emulatedObj , config .outputFormat )
125+ if err != nil {
126+ return fmt .Errorf ("failed to marshal CRD: %w" , err )
127+ }
128+
129+ fmt .Println (string (b ))
130+
131+ return nil
132+ }
133+
84134func createGraphBuilder (rgd * v1alpha1.ResourceGraphDefinition ) (* graph.Graph , error ) {
85135 set , err := kroclient .NewSet (kroclient.Config {})
86136 if err != nil {
@@ -130,5 +180,6 @@ func marshalObject(obj interface{}, outputFormat string) ([]byte, error) {
130180
131181func AddGenerateCommands (rootCmd * cobra.Command ) {
132182 generateCmd .AddCommand (generateCRDCmd )
183+ generateCmd .AddCommand (generateInstanceCmd )
133184 rootCmd .AddCommand (generateCmd )
134185}
0 commit comments