diff --git a/apis/authorization/v1alpha1/zz_generated_terraformed.go b/apis/authorization/v1alpha1/zz_generated_terraformed.go index 58f8e78..1c70704 100755 --- a/apis/authorization/v1alpha1/zz_generated_terraformed.go +++ b/apis/authorization/v1alpha1/zz_generated_terraformed.go @@ -123,6 +123,7 @@ func (tr *RoleAssignment) LateInitialize(attrs []byte) (bool, error) { } opts := []resource.GenericLateInitializerOption{resource.WithZeroValueJSONOmitEmptyFilter(resource.CNameWildcard)} opts = append(opts, resource.WithNameFilter("RoleDefinitionID")) + opts = append(opts, resource.WithNameFilter("RoleDefinitionID")) li := resource.NewGenericLateInitializer(opts...) return li.LateInitialize(&tr.Spec.ForProvider, params) diff --git a/apis/azure/v1alpha1/zz_generated_terraformed.go b/apis/azure/v1alpha1/zz_generated_terraformed.go index 73cbe1a..414c68a 100755 --- a/apis/azure/v1alpha1/zz_generated_terraformed.go +++ b/apis/azure/v1alpha1/zz_generated_terraformed.go @@ -352,6 +352,8 @@ func (tr *Subscription) LateInitialize(attrs []byte) (bool, error) { opts := []resource.GenericLateInitializerOption{resource.WithZeroValueJSONOmitEmptyFilter(resource.CNameWildcard)} opts = append(opts, resource.WithNameFilter("BillingScopeID")) opts = append(opts, resource.WithNameFilter("SubscriptionID")) + opts = append(opts, resource.WithNameFilter("BillingScopeID")) + opts = append(opts, resource.WithNameFilter("SubscriptionID")) li := resource.NewGenericLateInitializer(opts...) return li.LateInitialize(&tr.Spec.ForProvider, params) diff --git a/apis/cosmosdb/v1alpha1/zz_generated_terraformed.go b/apis/cosmosdb/v1alpha1/zz_generated_terraformed.go index 3213d3f..bfef4f3 100755 --- a/apis/cosmosdb/v1alpha1/zz_generated_terraformed.go +++ b/apis/cosmosdb/v1alpha1/zz_generated_terraformed.go @@ -579,6 +579,7 @@ func (tr *CassandraTable) LateInitialize(attrs []byte) (bool, error) { } opts := []resource.GenericLateInitializerOption{resource.WithZeroValueJSONOmitEmptyFilter(resource.CNameWildcard)} opts = append(opts, resource.WithNameFilter("AnalyticalStorageTTL")) + opts = append(opts, resource.WithNameFilter("AnalyticalStorageTTL")) li := resource.NewGenericLateInitializer(opts...) return li.LateInitialize(&tr.Spec.ForProvider, params) diff --git a/apis/network/v1alpha1/zz_generated_terraformed.go b/apis/network/v1alpha1/zz_generated_terraformed.go index 4350248..684a508 100755 --- a/apis/network/v1alpha1/zz_generated_terraformed.go +++ b/apis/network/v1alpha1/zz_generated_terraformed.go @@ -921,6 +921,7 @@ func (tr *VirtualNetwork) LateInitialize(attrs []byte) (bool, error) { } opts := []resource.GenericLateInitializerOption{resource.WithZeroValueJSONOmitEmptyFilter(resource.CNameWildcard)} opts = append(opts, resource.WithNameFilter("Subnet")) + opts = append(opts, resource.WithNameFilter("Subnet")) li := resource.NewGenericLateInitializer(opts...) return li.LateInitialize(&tr.Spec.ForProvider, params) diff --git a/apis/sql/v1alpha1/zz_generated_terraformed.go b/apis/sql/v1alpha1/zz_generated_terraformed.go index 1036752..7fca40b 100755 --- a/apis/sql/v1alpha1/zz_generated_terraformed.go +++ b/apis/sql/v1alpha1/zz_generated_terraformed.go @@ -124,6 +124,8 @@ func (tr *MSSQLDatabase) LateInitialize(attrs []byte) (bool, error) { opts := []resource.GenericLateInitializerOption{resource.WithZeroValueJSONOmitEmptyFilter(resource.CNameWildcard)} opts = append(opts, resource.WithNameFilter("ElasticPoolID")) opts = append(opts, resource.WithNameFilter("MaintenanceConfigurationName")) + opts = append(opts, resource.WithNameFilter("ElasticPoolID")) + opts = append(opts, resource.WithNameFilter("MaintenanceConfigurationName")) li := resource.NewGenericLateInitializer(opts...) return li.LateInitialize(&tr.Spec.ForProvider, params) @@ -467,6 +469,7 @@ func (tr *MSSQLElasticPool) LateInitialize(attrs []byte) (bool, error) { } opts := []resource.GenericLateInitializerOption{resource.WithZeroValueJSONOmitEmptyFilter(resource.CNameWildcard)} opts = append(opts, resource.WithNameFilter("MaxSizeBytes")) + opts = append(opts, resource.WithNameFilter("MaxSizeBytes")) li := resource.NewGenericLateInitializer(opts...) return li.LateInitialize(&tr.Spec.ForProvider, params) diff --git a/cmd/dynamic-controller/generate_dynamic_controller.go b/cmd/dynamic-controller/generate_dynamic_controller.go index 7cc1700..97f6524 100644 --- a/cmd/dynamic-controller/generate_dynamic_controller.go +++ b/cmd/dynamic-controller/generate_dynamic_controller.go @@ -6,7 +6,6 @@ import ( "github.com/crossplane/upjet/pkg/config" "github.com/crossplane/upjet/pkg/examples" "github.com/crossplane/upjet/pkg/pipeline" - pconfig "kubedb.dev/provider-azure/config" "os" "path/filepath" "sort" @@ -17,7 +16,7 @@ var ( groupKind map[string]string ) -func GenerateController() { +func GenerateController(pc *config.Provider, absRootDir string) { groupKind = make(map[string]string) if len(os.Args) < 2 || os.Args[1] == "" { panic("root directory is required to be given as argument") @@ -27,7 +26,6 @@ func GenerateController() { if err != nil { panic(fmt.Sprintf("cannot calculate the absolute path with %s", rootDir)) } - pc := pconfig.GetProvider() resourcesGroups := map[string]map[string]map[string]*config.Resource{} for name, resource := range pc.Resources { group := pc.RootGroup @@ -41,7 +39,6 @@ func GenerateController() { resourcesGroups[group][resource.Version] = map[string]*config.Resource{} } resourcesGroups[group][resource.Version][name] = resource - //fmt.Println(group, " ", resource.Version, " ", name, " ", resource.Kind, " ", resource.ShortGroup) kind := strings.ToLower(resource.Kind) groupKind[kind] = resource.Kind } @@ -121,7 +118,7 @@ func GenerateController() { apiVersionPkgList = append(apiVersionPkgList, versionGen.Package().Path()) } } - if err := NewProviderGenerator(rootDir, pc.ModulePath).Generate(controllerPkgMap, pc.MainTemplate, pc.ShortName); err != nil { + if err := NewProviderGenerator(rootDir, pc.ModulePath).Generate(controllerPkgMap, pc.MainTemplate, pc.ShortName, pc.RootGroup, absRootDir); err != nil { panic(errors.Wrap(err, "cannot generate setup file")) } } diff --git a/cmd/dynamic-controller/setup.go b/cmd/dynamic-controller/setup.go index 3c6a856..865b840 100644 --- a/cmd/dynamic-controller/setup.go +++ b/cmd/dynamic-controller/setup.go @@ -41,7 +41,7 @@ type ProviderGenerator struct { // Generate writes the setup file and the corresponding provider main file // using the given list of version packages. -func (sg *ProviderGenerator) Generate(versionPkgMap map[string][]string, mainTemplate string, shortName string) error { +func (sg *ProviderGenerator) Generate(versionPkgMap map[string][]string, mainTemplate string, shortName string, rootGroup string, absRootDir string) error { var t *template.Template if len(mainTemplate) != 0 { tmpl, err := template.New("main").Parse(mainTemplate) @@ -51,11 +51,11 @@ func (sg *ProviderGenerator) Generate(versionPkgMap map[string][]string, mainTem t = tmpl } if t == nil { - return errors.Wrap(sg.generate("", versionPkgMap[config.PackageNameMonolith], shortName), "failed to generate the controller setup file") + return errors.Wrap(sg.generate(rootGroup, versionPkgMap[config.PackageNameMonolith], shortName, absRootDir), "failed to generate the controller setup file") } for g, versionPkgList := range versionPkgMap { - if err := sg.generate(g, versionPkgList, shortName); err != nil { + if err := sg.generate(rootGroup, versionPkgList, shortName, absRootDir); err != nil { return errors.Wrapf(err, "failed to generate the controller setup file for group: %s", g) } if err := generateProviderMain(sg.ProviderPath, g, t); err != nil { @@ -87,8 +87,7 @@ func generateProviderMain(providerPath, group string, t *template.Template) erro return nil } -func (sg *ProviderGenerator) generate(group string, versionPkgList []string, shortName string) error { - //pc := pconfig.GetProvider() +func (sg *ProviderGenerator) generate(rootGroup string, versionPkgList []string, shortName string, absRootDir string) error { setupFile := wrapper.NewFile(filepath.Join(sg.ModulePath, "apis"), "apis", templates.SetupTemplate, wrapper.WithGenStatement(pipeline.GenStatement), wrapper.WithHeaderPath(sg.LicenseHeaderPath), @@ -109,31 +108,30 @@ func (sg *ProviderGenerator) generate(group string, versionPkgList []string, sho group := words[siz-2] kind := words[siz-1] importData += rmDot[0] + " " + "\"" + pkgPath + "\"\n" - - if group == "azure" { - kindMapData += "schema.GroupKind{\"" + group + ".kubedb.com\", " + if group == shortName { + kindMapData += "schema.GroupKind{\"" + rootGroup + "\", " } else { - kindMapData += "schema.GroupKind{\"" + group + ".azure.kubedb.com\", " + kindMapData += "schema.GroupKind{\"" + group + "." + rootGroup + "\", " } kindMapData += "\"" + groupKind[kind] + "\"}: " + aliases[i] + "Setup,\n" } importData += ")\n\n" kindMapData += "}\n)\n\n" - if err := generateControllerFile(importData, kindMapData); err != nil { + if err := generateControllerFile(importData, kindMapData, absRootDir); err != nil { panic(errors.Wrap(err, "cannot create controller")) } return nil } -func generateControllerFile(importData string, kindMapData string) error { +func generateControllerFile(importData string, kindMapData string, absRootDir string) error { importData += kindMapData - filePath := "/home/arman/go/src/kubedb.dev/provider-azure/internal/controller/zz_crd_controller.go" // Replace with the path to your file + filePath := absRootDir + "/internal/controller/zz_dynamic_crd_controller.go" // Replace with the path to your file // Attempt to remove the file os.Remove(filePath) - filePath = "/home/arman/go/src/kubedb.dev/provider-azure/internal/controller/crd_controller.go.txt" // Replace with the path to your file + filePath = absRootDir + "/cmd/generator/crd_controller.go.txt" // Replace with the path to your file // Read the entire file content content, err := ioutil.ReadFile(filePath) if err != nil { @@ -143,7 +141,7 @@ func generateControllerFile(importData string, kindMapData string) error { fileContent := string(content) importData += fileContent - filePath = "/home/arman/go/src/kubedb.dev/provider-azure/internal/controller/zz_crd_controller.go" + filePath = absRootDir + "/internal/controller/zz_dynamic_crd_controller.go" // Open or create the file for writing file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0644) diff --git a/internal/controller/crd_controller.go.txt b/cmd/generator/crd_controller.go.txt similarity index 100% rename from internal/controller/crd_controller.go.txt rename to cmd/generator/crd_controller.go.txt diff --git a/cmd/generator/main.go b/cmd/generator/main.go index 699de29..2e120ca 100644 --- a/cmd/generator/main.go +++ b/cmd/generator/main.go @@ -23,7 +23,6 @@ func main() { panic(fmt.Sprintf("cannot calculate the absolute path with %s", rootDir)) } pc := pconfig.GetProvider() - dynamic_controller.GenerateController() + dynamic_controller.GenerateController(pc, absRootDir) pipeline.Run(pc, absRootDir) - } diff --git a/examples/providerconfig/providerconfig.yaml b/examples/providerconfig/providerconfig.yaml index 02e691e..df3b691 100644 --- a/examples/providerconfig/providerconfig.yaml +++ b/examples/providerconfig/providerconfig.yaml @@ -1,11 +1,13 @@ apiVersion: azure.kubedb.com/v1beta1 -kind: ProviderConfig metadata: name: default +kind: ProviderConfig spec: credentials: source: Secret secretRef: - name: example-creds + name: azure-secret + key: creds namespace: crossplane-system - key: credentials +#az ad sp create-for-rbac --sdk-auth --role Owner --scopes +#kubectl create secret generic azure-secret -n crossplane-system --from-file=creds=./azure-credentials.json \ No newline at end of file diff --git a/internal/controller/zz_crd_controller.go b/internal/controller/zz_dynamic_crd_controller.go similarity index 97% rename from internal/controller/zz_crd_controller.go rename to internal/controller/zz_dynamic_crd_controller.go index f1d18f6..7711b3e 100644 --- a/internal/controller/zz_crd_controller.go +++ b/internal/controller/zz_dynamic_crd_controller.go @@ -193,47 +193,27 @@ var ( //package controller -/* -var( - gk2 = schema.GroupKind{"azure.kubedb.com", "ResourceGroup"} - gk3 = schema.GroupKind{"azure.kubedb.com", "ProviderConfig"} - setupFns = map[schema.GroupKind]func(ctrl.Manager, controller.Options) error{ - gk2: resourcegroup.Setup, - gk3: providerregistration.Setup, - } -) -*/ - var ( setupDone = map[schema.GroupKind]bool{} mu sync.RWMutex ) -//func SetupControllerList(mgr ctrl.Manager, o controller.Options) error { -// -//} - type CustomResourceReconciler struct { mgr ctrl.Manager o controller.Options } func NewCustomResourceReconciler(mgr ctrl.Manager, o controller.Options) *CustomResourceReconciler { - //if err := SetupControllerList(mgr, o); err != nil { - // log.Error(err, "unable to fetch CustomResourceDefinition") - //} return &CustomResourceReconciler{mgr: mgr, o: o} } func (r *CustomResourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := log.FromContext(ctx) - var crd apiextensions.CustomResourceDefinition if err := r.mgr.GetClient().Get(ctx, req.NamespacedName, &crd); err != nil { log.Error(err, "unable to fetch CustomResourceDefinition") return ctrl.Result{}, client.IgnoreNotFound(err) } - gk := schema.GroupKind{ Group: crd.Spec.Group, Kind: crd.Spec.Names.Kind,