@@ -20,6 +20,7 @@ import (
2020 "crypto/tls"
2121 "flag"
2222 "fmt"
23+ _ "net/http/pprof"
2324 "net/url"
2425 "os"
2526 "strings"
@@ -32,6 +33,7 @@ import (
3233 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3334 _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
3435 ctrl "sigs.k8s.io/controller-runtime"
36+ "sigs.k8s.io/controller-runtime/pkg/client"
3537 "sigs.k8s.io/controller-runtime/pkg/log/zap"
3638
3739 "github.com/uselagoon/remote-controller/internal/dockerhost"
@@ -174,7 +176,7 @@ func main() {
174176 var pruneLongRunningPodsCron string
175177
176178 var lffQoSEnabled bool
177- var qosMaxBuilds int
179+ var qosTotalBuilds int
178180 var qosMaxContainerBuilds int
179181 var qosDefaultPriority int
180182
@@ -394,11 +396,11 @@ func main() {
394396 flag .IntVar (& timeoutForLongRunningTaskPods , "timeout-longrunning-task-pod-cleanup" , 6 , "How many hours a task pod should run before forcefully closed." )
395397
396398 // Build QoS configuration
397- flag .BoolVar (& lffQoSEnabled , "enable-qos" , false , "Flag to enable this controller with QoS for builds." )
399+ flag .BoolVar (& lffQoSEnabled , "enable-qos" , true , "Deprecated: Flag to enable this controller with QoS for builds. No longer configurable " )
398400 // this flag remains the same, the number of max builds flag remains unchanged to be backwards compatible
399401 flag .IntVar (& qosMaxContainerBuilds , "qos-max-builds" , 20 , "The total number of builds during the container build phase that can run at any one time." )
400402 // this new flag is added but defaults to 0, if it is greater than `qos-max-builds` then it will be used, otherwise it will default to the value of `qos-max-builds`
401- flag .IntVar (& qosMaxBuilds , "qos-total-builds" , 0 , "The total number of builds that can run at any one time. Defaults to qos-max-builds if not provided or less than qos-max-builds." )
403+ flag .IntVar (& qosTotalBuilds , "qos-total-builds" , 0 , "The total number of builds that can run at any one time. Defaults to qos-max-builds if not provided or less than qos-max-builds." )
402404 flag .IntVar (& qosDefaultPriority , "qos-default" , 5 , "The default qos priority value to apply if one is not provided." )
403405
404406 // Task QoS configuration
@@ -565,7 +567,7 @@ func main() {
565567 os .Exit (1 )
566568 }
567569
568- cacheSize := helpers .GetEnvInt ("CACHE_SIZE" , 1000 )
570+ cacheSize := helpers .GetEnvInt ("CACHE_SIZE" , 2000 )
569571 // create the cancellation cache
570572 cache := expirable .NewLRU [string , string ](cacheSize , nil , time .Minute * 60 )
571573 // create queue cache
@@ -744,8 +746,12 @@ func main() {
744746 TLSSkipVerify : tlsSkipVerify ,
745747 }
746748
749+ // setup harbor config
750+ var lagoonHarbor * harbor.Harbor
751+ lagoonHarbor , _ = harbor .New (harborConfig )
752+
747753 deletion := deletions .New (mgr .GetClient (),
748- harborConfig ,
754+ lagoonHarbor ,
749755 deletions.DeleteConfig {
750756 PVCRetryAttempts : pvcRetryAttempts ,
751757 PVCRetryInterval : pvcRetryInterval ,
@@ -756,6 +762,7 @@ func main() {
756762
757763 messaging := messenger .New (config ,
758764 mgr .GetClient (),
765+ mgr .GetAPIReader (),
759766 startupConnectionAttempts ,
760767 startupConnectionInterval ,
761768 controllerNamespace ,
@@ -767,7 +774,11 @@ func main() {
767774 enableDebug ,
768775 lffSupportK8UPv2 ,
769776 cache ,
770- harborConfig ,
777+ lagoonHarbor ,
778+ lagoonTargetName ,
779+ buildsCache ,
780+ buildsQueueCache ,
781+ qosDefaultPriority ,
771782 )
772783
773784 reuseCache , _ := lru.New [string , string ](cacheSize )
@@ -790,19 +801,13 @@ func main() {
790801 buildCache ,
791802 )
792803 c := cron .New ()
793- // if we are running with MQ support, then start the consumer handler
794-
795- if enableMQ {
796- setupLog .Info ("starting messaging handler" )
797- go messaging .Consumer (lagoonTargetName )
798- }
799804
800805 // this ensures that the max number of builds is not less than the container builds support
801- if qosMaxBuilds < qosMaxContainerBuilds {
802- qosMaxBuilds = qosMaxContainerBuilds
806+ if qosTotalBuilds < qosMaxContainerBuilds {
807+ qosTotalBuilds = qosMaxContainerBuilds
803808 }
804809 buildQoSConfigv1beta2 := lagoonv1beta2ctrl.BuildQoS {
805- MaxBuilds : qosMaxBuilds ,
810+ TotalBuilds : qosTotalBuilds ,
806811 MaxContainerBuilds : qosMaxContainerBuilds ,
807812 DefaultPriority : qosDefaultPriority ,
808813 }
@@ -828,6 +833,7 @@ func main() {
828833 }
829834
830835 resourceCleanup := pruner .New (mgr .GetClient (),
836+ mgr .GetAPIReader (),
831837 buildsToKeep ,
832838 buildPodsToKeep ,
833839 tasksToKeep ,
@@ -896,7 +902,6 @@ func main() {
896902 // use cron to run a task pod cleanup task
897903 // this will check any Lagoon task pods and attempt to delete them
898904 _ , err := c .AddFunc (harborCredentialCron , func () {
899- lagoonHarbor , _ := harbor .New (harborConfig )
900905 lagoonHarbor .RotateRobotCredentials (context .Background (), mgr .GetClient ())
901906 })
902907 if err != nil {
@@ -930,12 +935,24 @@ func main() {
930935
931936 c .Start ()
932937
933- // @TODO: maybe insert a pre-controller start state collector to try and seed the queue/build caches before the controllers start
938+ // create a temporary client to use in seed functions
939+ tmpClient , _ := client .New (ctrl .GetConfigOrDie (), client.Options {
940+ Scheme : scheme ,
941+ })
942+ // pre-seed the queues with the current state of builds
943+ if err := lagoonv1beta2 .SeedBuildStartup (tmpClient , scheme , controllerNamespace , qosDefaultPriority , buildsCache , buildsQueueCache ); err != nil {
944+ setupLog .Error (err , "unable to seed controller startup state" )
945+ }
946+ // pre-seed the queues with the current state of tasks
947+ if err := lagoonv1beta2 .SeedTaskStartup (tmpClient , scheme , controllerNamespace , tasksCache , tasksQueueCache ); err != nil {
948+ setupLog .Error (err , "unable to seed controller startup state" )
949+ }
934950
935951 setupLog .Info ("starting build controller" )
936952 // v1beta2 is the latest version
937953 if err = (& lagoonv1beta2ctrl.LagoonBuildReconciler {
938954 Client : mgr .GetClient (),
955+ APIReader : mgr .GetAPIReader (),
939956 Log : ctrl .Log .WithName ("v1beta2" ).WithName ("LagoonBuild" ),
940957 Scheme : mgr .GetScheme (),
941958 EnableMQ : enableMQ ,
@@ -973,8 +990,7 @@ func main() {
973990 LFFBackupWeeklyRandom : lffBackupWeeklyRandom ,
974991 LFFRouterURL : lffRouterURL ,
975992 LFFHarborEnabled : lffHarborEnabled ,
976- Harbor : harborConfig ,
977- LFFQoSEnabled : lffQoSEnabled ,
993+ Harbor : lagoonHarbor ,
978994 BuildQoS : buildQoSConfigv1beta2 ,
979995 NativeCronPodMinFrequency : nativeCronPodMinFrequency ,
980996 LagoonTargetName : lagoonTargetName ,
@@ -1038,6 +1054,7 @@ func main() {
10381054 setupLog .Info ("starting build pod monitor controller" )
10391055 if err = (& lagoonv1beta2ctrl.BuildMonitorReconciler {
10401056 Client : mgr .GetClient (),
1057+ APIReader : mgr .GetAPIReader (),
10411058 Log : ctrl .Log .WithName ("v1beta2" ).WithName ("LagoonBuildPodMonitor" ),
10421059 Scheme : mgr .GetScheme (),
10431060 EnableMQ : enableMQ ,
@@ -1047,7 +1064,6 @@ func main() {
10471064 RandomNamespacePrefix : randomPrefix ,
10481065 EnableDebug : enableDebug ,
10491066 LagoonTargetName : lagoonTargetName ,
1050- LFFQoSEnabled : lffQoSEnabled ,
10511067 BuildQoS : buildQoSConfigv1beta2 ,
10521068 Cache : cache ,
10531069 DockerHost : dockerhosts ,
@@ -1081,18 +1097,25 @@ func main() {
10811097 if lffHarborEnabled {
10821098 if err = (& harborctrl.HarborCredentialReconciler {
10831099 Client : mgr .GetClient (),
1100+ APIReader : mgr .GetAPIReader (),
10841101 Log : ctrl .Log .WithName ("harbor" ).WithName ("HarborCredentialReconciler" ),
10851102 Scheme : mgr .GetScheme (),
10861103 LFFHarborEnabled : lffHarborEnabled ,
10871104 ControllerNamespace : controllerNamespace ,
1088- Harbor : harborConfig ,
1105+ Harbor : lagoonHarbor ,
10891106 }).SetupWithManager (mgr ); err != nil {
10901107 setupLog .Error (err , "unable to create controller" , "controller" , "HarborCredentialReconciler" )
10911108 os .Exit (1 )
10921109 }
10931110 }
10941111 // +kubebuilder:scaffold:builder
10951112
1113+ // if we are running with MQ support, then start the consumer handler
1114+ if enableMQ {
1115+ setupLog .Info ("starting messaging handler" )
1116+ go messaging .Consumer (lagoonTargetName )
1117+ }
1118+
10961119 setupLog .Info ("starting manager" )
10971120 if err := mgr .Start (ctrl .SetupSignalHandler ()); err != nil {
10981121 setupLog .Error (err , "problem running manager" )
0 commit comments