I am writing Kubernetes operator in golang and I am new to golang. I have few questions based on best practices:
1) The operator I am building can serve multiple instances of my application 2) Each application instance is independent of each other 3) Application is mostly stateful
type myApp struct {
name string
}
// MyAppReconciler reconciles a MyApp object
type MyAppReconciler struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
kubeClient kubernetes.Interface
kubeConfig clientcmd.ClientConfig
instance *appV1.MyApp
addNewApp []myApp
addSHState bool
addSemAcuireMsg string
}
// ConfigurationProvider wraps information about the account owner
var myAppSem = semaphore.NewWeighted(1)
var App1Sem = semaphore.NewWeighted(1)
If I declare a global variables like myAppSem, they become operator wise resource i.e. all my App instances ( like app1 inst, appnst2 CR ) access the same semaphores. Also, assign values to MyAppReconciler such as addNewApp then these values are available to all the same CR so technically if I run multiple app instances then operator mess up the env.
Is there a way to declare and define the struct which is at per CR resource. If I declare struct under following then I need to pass the struct to all the functions but I want in that app instance it must maintains its copy and each CR must have its own copy.
func (r *MyAppReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
// _ = context.Background()
_ = r.Log.WithValues("Instance.Namespace", req.NamespacedName)
// your logic here
}