github.com/kyma-project/kyma/components/asset-store-controller-manager@v0.0.0-20191203152857-3792b5df17c5/main.go (about) 1 /* 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package main 17 18 import ( 19 "flag" 20 "github.com/kyma-project/kyma/components/asset-store-controller-manager/internal/assethook" 21 "github.com/kyma-project/kyma/components/asset-store-controller-manager/internal/loader" 22 "github.com/kyma-project/kyma/components/asset-store-controller-manager/internal/store" 23 "github.com/minio/minio-go" 24 "github.com/vrischmann/envconfig" 25 "net/http" 26 "os" 27 28 "github.com/kyma-project/kyma/components/asset-store-controller-manager/internal/controllers" 29 assetstorev1alpha2 "github.com/kyma-project/kyma/components/asset-store-controller-manager/pkg/apis/assetstore/v1alpha2" 30 "k8s.io/apimachinery/pkg/runtime" 31 clientgoscheme "k8s.io/client-go/kubernetes/scheme" 32 _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" 33 ctrl "sigs.k8s.io/controller-runtime" 34 "sigs.k8s.io/controller-runtime/pkg/log/zap" 35 // +kubebuilder:scaffold:imports 36 ) 37 38 var ( 39 scheme = runtime.NewScheme() 40 setupLog = ctrl.Log.WithName("setup") 41 ) 42 43 func init() { 44 _ = clientgoscheme.AddToScheme(scheme) 45 46 _ = assetstorev1alpha2.AddToScheme(scheme) 47 // +kubebuilder:scaffold:scheme 48 } 49 50 type Config struct { 51 Store store.Config 52 Loader loader.Config 53 Webhook assethook.Config 54 Asset controllers.AssetConfig 55 ClusterAsset controllers.ClusterAssetConfig 56 Bucket controllers.BucketConfig 57 ClusterBucket controllers.ClusterBucketConfig 58 } 59 60 func main() { 61 var metricsAddr string 62 var enableLeaderElection bool 63 flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.") 64 flag.BoolVar(&enableLeaderElection, "enable-leader-election", false, 65 "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.") 66 flag.Parse() 67 68 ctrl.SetLogger(zap.Logger(true)) 69 70 cfg, err := loadConfig("APP") 71 if err != nil { 72 setupLog.Error(err, "unable to load config") 73 os.Exit(1) 74 } 75 76 httpClient := &http.Client{} 77 minioClient, err := minio.New(cfg.Store.Endpoint, cfg.Store.AccessKey, cfg.Store.SecretKey, cfg.Store.UseSSL) 78 if err != nil { 79 setupLog.Error(err, "unable initialize Minio client") 80 os.Exit(1) 81 } 82 83 mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ 84 Scheme: scheme, 85 MetricsBindAddress: metricsAddr, 86 LeaderElection: enableLeaderElection, 87 LeaderElectionID: "assetstore-controller-leader-election-helper", 88 }) 89 if err != nil { 90 setupLog.Error(err, "unable to start manager") 91 os.Exit(1) 92 } 93 94 container := &controllers.Container{ 95 Manager: mgr, 96 Store: store.New(minioClient, cfg.Store.UploadWorkersCount), 97 Loader: loader.New(cfg.Loader.TemporaryDirectory, cfg.Loader.VerifySSL), 98 Validator: assethook.NewValidator(httpClient, cfg.Webhook.ValidationTimeout, cfg.Webhook.ValidationWorkersCount), 99 Mutator: assethook.NewMutator(httpClient, cfg.Webhook.MutationTimeout, cfg.Webhook.MutationWorkersCount), 100 Extractor: assethook.NewMetadataExtractor(httpClient, cfg.Webhook.MetadataExtractionTimeout), 101 } 102 103 if err = controllers.NewClusterAsset(cfg.ClusterAsset, ctrl.Log.WithName("controllers").WithName("ClusterAsset"), container).SetupWithManager(mgr); err != nil { 104 setupLog.Error(err, "unable to create controller", "controller", "ClusterAsset") 105 os.Exit(1) 106 } 107 if err = controllers.NewClusterBucket(cfg.ClusterBucket, ctrl.Log.WithName("controllers").WithName("ClusterBucket"), container).SetupWithManager(mgr); err != nil { 108 setupLog.Error(err, "unable to create controller", "controller", "ClusterBucket") 109 os.Exit(1) 110 } 111 if err = controllers.NewAsset(cfg.Asset, ctrl.Log.WithName("controllers").WithName("Asset"), container).SetupWithManager(mgr); err != nil { 112 setupLog.Error(err, "unable to create controller", "controller", "Asset") 113 os.Exit(1) 114 } 115 if err = controllers.NewBucket(cfg.Bucket, ctrl.Log.WithName("controllers").WithName("Bucket"), container).SetupWithManager(mgr); err != nil { 116 setupLog.Error(err, "unable to create controller", "controller", "Bucket") 117 os.Exit(1) 118 } 119 // +kubebuilder:scaffold:builder 120 121 setupLog.Info("starting manager") 122 if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { 123 setupLog.Error(err, "problem running manager") 124 os.Exit(1) 125 } 126 } 127 128 func loadConfig(prefix string) (Config, error) { 129 cfg := Config{} 130 err := envconfig.InitWithPrefix(&cfg, prefix) 131 if err != nil { 132 return cfg, err 133 } 134 cfg.Bucket.ExternalEndpoint = cfg.Store.ExternalEndpoint 135 cfg.ClusterBucket.ExternalEndpoint = cfg.Store.ExternalEndpoint 136 return cfg, nil 137 }