Skip to content

Commit

Permalink
Add dynamically start crd reconciler
Browse files Browse the repository at this point in the history
Signed-off-by: SK Ali Arman <[email protected]>
  • Loading branch information
sheikh-arman committed Dec 18, 2023
1 parent 89b9760 commit 3dda7b6
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 44 deletions.
1 change: 1 addition & 0 deletions apis/authorization/v1alpha1/zz_generated_terraformed.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions apis/azure/v1alpha1/zz_generated_terraformed.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apis/cosmosdb/v1alpha1/zz_generated_terraformed.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apis/network/v1alpha1/zz_generated_terraformed.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions apis/sql/v1alpha1/zz_generated_terraformed.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions cmd/dynamic-controller/generate_dynamic_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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"))
}
}
Expand Down
26 changes: 12 additions & 14 deletions cmd/dynamic-controller/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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),
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions cmd/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
8 changes: 5 additions & 3 deletions examples/providerconfig/providerconfig.yaml
Original file line number Diff line number Diff line change
@@ -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 <your-subscription-id>
#kubectl create secret generic azure-secret -n crossplane-system --from-file=creds=./azure-credentials.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3dda7b6

Please sign in to comment.