@@ -21,6 +21,7 @@ import (
2121 "os"
2222 "time"
2323
24+ "github.com/go-logr/logr"
2425 "go.uber.org/zap/zapcore"
2526 appsv1 "k8s.io/api/apps/v1"
2627 batchv1 "k8s.io/api/batch/v1"
@@ -42,6 +43,7 @@ import (
4243
4344 brv1alpha1 "github.com/pingcap/tidb-operator/api/v2/br/v1alpha1"
4445 "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1"
46+ "github.com/pingcap/tidb-operator/v2/manifests"
4547 "github.com/pingcap/tidb-operator/v2/pkg/adoption"
4648 "github.com/pingcap/tidb-operator/v2/pkg/client"
4749 "github.com/pingcap/tidb-operator/v2/pkg/controllers/br/backup"
@@ -71,6 +73,7 @@ import (
7173 "github.com/pingcap/tidb-operator/v2/pkg/controllers/tiproxygroup"
7274 "github.com/pingcap/tidb-operator/v2/pkg/controllers/tso"
7375 "github.com/pingcap/tidb-operator/v2/pkg/controllers/tsogroup"
76+ "github.com/pingcap/tidb-operator/v2/pkg/crd"
7477 "github.com/pingcap/tidb-operator/v2/pkg/image"
7578 "github.com/pingcap/tidb-operator/v2/pkg/metrics"
7679 "github.com/pingcap/tidb-operator/v2/pkg/scheme"
@@ -84,7 +87,7 @@ import (
8487 "github.com/pingcap/tidb-operator/v2/pkg/volumes"
8588)
8689
87- var setupLog = ctrl .Log .WithName ("setup" ). WithValues ( version . Get (). KeysAndValues () ... )
90+ var setupLog = ctrl .Log .WithName ("setup" )
8891
8992type brConfig struct {
9093 backupManagerImage string
@@ -98,6 +101,9 @@ func main() {
98101 var probeAddr string
99102 var maxConcurrentReconciles int
100103 var watchDelayDuration time.Duration
104+ var allowEmptyOldVersion bool
105+ var skipCRDsManagement bool
106+
101107 flag .StringVar (& metricsAddr , "metrics-bind-address" , ":8080" , "The address the metric endpoint binds to." )
102108 flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
103109 flag .StringVar (& brConf .backupManagerImage , "backup-manager-image" , "" , "The image of backup-manager." )
@@ -111,6 +117,9 @@ func main() {
111117 "kube clients's read-after-write inconsistency(always read from cache)." )
112118 flag .Var (& image .PrestopChecker , "default-prestop-checker-image" , "Default image of prestop checker." )
113119 flag .Var (& image .ResourceSyncer , "default-resource-syncer-image" , "Default image of resource syncer." )
120+ flag .BoolVar (& allowEmptyOldVersion , "allow-empty-old-version" , false ,
121+ "allow crd version is empty, if false, cannot update crd which has no version annotation" )
122+ flag .BoolVar (& skipCRDsManagement , "skip-crds-management" , false , "if true, skip checking and applying crds" )
114123
115124 opts := zap.Options {
116125 Development : false ,
@@ -136,6 +145,10 @@ func main() {
136145
137146 ctrl .SetLogger (zap .New (zap .UseFlagOptions (& opts )))
138147
148+ ctx := logr .NewContext (context .Background (), setupLog )
149+
150+ setupLog .Info ("start tidb operator" , version .Get ().KeysAndValues ()... )
151+
139152 utilruntime .PanicHandlers = append (utilruntime .PanicHandlers , func (_ context.Context , _ any ) {
140153 metrics .ControllerPanic .WithLabelValues ().Inc ()
141154 })
@@ -146,6 +159,13 @@ func main() {
146159 kubeconfig .UserAgent = client .DefaultFieldManager
147160 kubefeat .MustInitFeatureGates (kubeconfig )
148161
162+ if ! skipCRDsManagement {
163+ if err := applyCRDs (ctx , kubeconfig , allowEmptyOldVersion ); err != nil {
164+ setupLog .Error (err , "failed to apply crds" )
165+ os .Exit (1 )
166+ }
167+ }
168+
149169 cacheOpt := cache.Options {
150170 // Disable label selector for our own CRs
151171 // These CRs don't need to be filtered
@@ -178,12 +198,36 @@ func main() {
178198 os .Exit (1 )
179199 }
180200
181- if err := setup (context . Background () , mgr ); err != nil {
201+ if err := setup (ctx , mgr ); err != nil {
182202 setupLog .Error (err , "failed to setup" )
183203 os .Exit (1 )
184204 }
185205}
186206
207+ func applyCRDs (ctx context.Context , kubeconfig * rest.Config , allowEmptyOldVersion bool ) error {
208+ c , err := client .New (kubeconfig , ctrlcli.Options {
209+ Scheme : scheme .Scheme ,
210+ })
211+ if err != nil {
212+ return fmt .Errorf ("cannot new client for applying crd: %w" , err )
213+ }
214+
215+ info := version .Get ()
216+ cfg := crd.Config {
217+ Client : c ,
218+ Version : info .GitVersion ,
219+ AllowEmptyOldVersion : allowEmptyOldVersion ,
220+ IsDirty : info .IsDirty (),
221+ CRDDir : manifests .CRD ,
222+ }
223+
224+ if err := crd .ApplyCRDs (ctx , & cfg ); err != nil {
225+ return err
226+ }
227+
228+ return nil
229+ }
230+
187231func setup (ctx context.Context , mgr ctrl.Manager ) error {
188232 // client of manager should be newed by options.NewClient
189233 // which actually returns tidb-operator/v2/pkg/client.Client
0 commit comments