44 "context"
55 "encoding/json"
66 "fmt"
7+ "io/ioutil"
78 "os"
89
910 mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/pkg/apis/mongodb/v1"
@@ -31,6 +32,7 @@ const (
3132 agentName = "mongodb-agent"
3233 mongodbName = "mongod"
3334 agentImageEnvVariable = "AGENT_IMAGE"
35+ versionManifestFilePath = "/usr/local/version_manifest.json"
3436 readinessProbePath = "/var/lib/mongodb-mms-automation/probes/readinessprobe"
3537 agentHealthStatusFilePath = "/var/log/mongodb-mms-automation/agent-health-status.json"
3638 clusterFilePath = "/var/lib/automation/config/automation-config"
@@ -39,13 +41,21 @@ const (
3941// Add creates a new MongoDB Controller and adds it to the Manager. The Manager will set fields on the Controller
4042// and Start it when the Manager is Started.
4143func Add (mgr manager.Manager ) error {
42- return add (mgr , newReconciler (mgr ))
44+ return add (mgr , newReconciler (mgr , readVersionManifestFromDisk ))
4345}
4446
47+ // ManifestProvider is a function which returns the VersionManifest which
48+ // contains the list of all available MongoDB versions
49+ type ManifestProvider func () (automationconfig.VersionManifest , error )
50+
4551// newReconciler returns a new reconcile.Reconciler
46- func newReconciler (mgr manager.Manager ) reconcile.Reconciler {
52+ func newReconciler (mgr manager.Manager , manifestProvider ManifestProvider ) reconcile.Reconciler {
4753 mgrClient := mgr .GetClient ()
48- return & ReplicaSetReconciler {client : mdbClient .NewClient (mgrClient ), scheme : mgr .GetScheme ()}
54+ return & ReplicaSetReconciler {
55+ client : mdbClient .NewClient (mgrClient ),
56+ scheme : mgr .GetScheme (),
57+ manifestProvider : manifestProvider ,
58+ }
4959}
5060
5161// add adds a new Controller to mgr with r as the reconcile.Reconciler
@@ -71,8 +81,9 @@ var _ reconcile.Reconciler = &ReplicaSetReconciler{}
7181type ReplicaSetReconciler struct {
7282 // This client, initialized using mgr.Client() above, is a split client
7383 // that reads objects from the cache and writes to the apiserver
74- client mdbClient.Client
75- scheme * runtime.Scheme
84+ client mdbClient.Client
85+ scheme * runtime.Scheme
86+ manifestProvider func () (automationconfig.VersionManifest , error )
7687}
7788
7889// Reconcile reads that state of the cluster for a MongoDB object and makes changes based on the state read
@@ -102,7 +113,7 @@ func (r *ReplicaSetReconciler) Reconcile(request reconcile.Request) (reconcile.R
102113
103114 // TODO: Read current automation config version from config map
104115 if err := r .ensureAutomationConfig (mdb ); err != nil {
105- log .Warnf ("failed creating config map: %s" , err )
116+ log .Warnf ("error creating automation config config map: %s" , err )
106117 return reconcile.Result {}, err
107118 }
108119
@@ -127,7 +138,7 @@ func (r *ReplicaSetReconciler) Reconcile(request reconcile.Request) (reconcile.R
127138}
128139
129140func (r ReplicaSetReconciler ) ensureAutomationConfig (mdb mdbv1.MongoDB ) error {
130- cm , err := buildAutomationConfigConfigMap (mdb )
141+ cm , err := r . buildAutomationConfigConfigMap (mdb )
131142 if err != nil {
132143 return err
133144 }
@@ -137,7 +148,7 @@ func (r ReplicaSetReconciler) ensureAutomationConfig(mdb mdbv1.MongoDB) error {
137148 return nil
138149}
139150
140- func buildAutomationConfig (mdb mdbv1.MongoDB ) automationconfig.AutomationConfig {
151+ func buildAutomationConfig (mdb mdbv1.MongoDB , mdbVersionConfig automationconfig. MongoDbVersionConfig ) automationconfig.AutomationConfig {
141152 domain := getDomain (mdb .ServiceName (), mdb .Namespace , "" )
142153 return automationconfig .NewBuilder ().
143154 SetTopology (automationconfig .ReplicaSetTopology ).
@@ -146,10 +157,26 @@ func buildAutomationConfig(mdb mdbv1.MongoDB) automationconfig.AutomationConfig
146157 SetMembers (mdb .Spec .Members ).
147158 SetMongoDBVersion (mdb .Spec .Version ).
148159 SetAutomationConfigVersion (1 ). // TODO: Correctly set the version
149- AddVersion (buildVersion406 () ).
160+ AddVersion (mdbVersionConfig ).
150161 Build ()
151162}
152163
164+ func readVersionManifestFromDisk () (automationconfig.VersionManifest , error ) {
165+ bytes , err := ioutil .ReadFile (versionManifestFilePath )
166+ if err != nil {
167+ return automationconfig.VersionManifest {}, err
168+ }
169+ return versionManifestFromBytes (bytes )
170+ }
171+
172+ func versionManifestFromBytes (bytes []byte ) (automationconfig.VersionManifest , error ) {
173+ versionManifest := automationconfig.VersionManifest {}
174+ if err := json .Unmarshal (bytes , & versionManifest ); err != nil {
175+ return automationconfig.VersionManifest {}, err
176+ }
177+ return versionManifest , nil
178+ }
179+
153180// buildService creates a Service that will be used for the Replica Set StatefulSet
154181// that allows all the members of the STS to see each other.
155182// TODO: Make sure this Service is as minimal as posible, to not interfere with
@@ -167,36 +194,12 @@ func buildService(mdb mdbv1.MongoDB) corev1.Service {
167194 Build ()
168195}
169196
170- // buildVersion406 returns a compatible version.
171- func buildVersion406 () automationconfig.MongoDbVersionConfig {
172- // TODO: For now we only have 2 versions, that match the versions used for testing.
173- return automationconfig.MongoDbVersionConfig {
174- Builds : []automationconfig.BuildConfig {
175- {
176- Architecture : "amd64" ,
177- GitVersion : "caa42a1f75a56c7643d0b68d3880444375ec42e3" ,
178- Platform : "linux" ,
179- Url : "/linux/mongodb-linux-x86_64-rhel62-4.0.6.tgz" ,
180- Flavor : "rhel" ,
181- MaxOsVersion : "8.0" ,
182- MinOsVersion : "7.0" ,
183- },
184- {
185- Architecture : "amd64" ,
186- GitVersion : "caa42a1f75a56c7643d0b68d3880444375ec42e3" ,
187- Platform : "linux" ,
188- Url : "/linux/mongodb-linux-x86_64-ubuntu1604-4.0.6.tgz" ,
189- Flavor : "ubuntu" ,
190- MaxOsVersion : "17.04" ,
191- MinOsVersion : "16.04" ,
192- },
193- },
194- Name : "4.0.6" ,
197+ func (r ReplicaSetReconciler ) buildAutomationConfigConfigMap (mdb mdbv1.MongoDB ) (corev1.ConfigMap , error ) {
198+ manifest , err := r .manifestProvider ()
199+ if err != nil {
200+ return corev1.ConfigMap {}, fmt .Errorf ("error reading version manifest from disk: %+v" , err )
195201 }
196- }
197-
198- func buildAutomationConfigConfigMap (mdb mdbv1.MongoDB ) (corev1.ConfigMap , error ) {
199- ac := buildAutomationConfig (mdb )
202+ ac := buildAutomationConfig (mdb , manifest .BuildsForVersion (mdb .Spec .Version ))
200203 acBytes , err := json .Marshal (ac )
201204 if err != nil {
202205 return corev1.ConfigMap {}, err
0 commit comments