@@ -202,36 +202,79 @@ func (g *Github) ReconcileDeployments(ctx context.Context, repository *gitv1.Git
202202 if err := g .k8sCrd .List (ctx , & k8sCrds , listOptions ); err != nil {
203203 return err
204204 }
205- inK8sByName := map [string ]* gitv1.GitDeployment {}
205+ inK8sByRef := map [string ]* gitv1.GitDeployment {}
206+ inGhByRef := map [string ]* gitv1.GitDeployment {}
207+ for _ , ghCrd := range ghCrds {
208+ inGhByRef [ghCrd .Spec .Ref ] = ghCrd .DeepCopy ()
209+ }
206210 for _ , k8sCrd := range k8sCrds .Items {
207- inK8sByName [k8sCrd .Spec .Name ] = k8sCrd .DeepCopy ()
211+ inK8sByRef [k8sCrd .Spec .Ref ] = k8sCrd .DeepCopy ()
212+ if k8sCrd .Status .Ref != "" {
213+ // Presence of Status.Ref will show that the GH Deployment is present.
214+ // Help us to make sure we don't create it again as in case of update request
215+ // we won't find this Ref in the Spec.Ref because of that we might create a new CRD will be created since there is GH deployment present
216+ // if Spec.Ref != Status.Ref => the Deployment needs to be updated
217+ inK8sByRef [k8sCrd .Status .Ref ] = k8sCrd .DeepCopy ()
218+ }
208219 }
209- for _ , ghCrd := range ghCrds {
210- k8sCrd , found := inK8sByName [ghCrd .Spec .Name ]
211- if ! found {
212- if err := g .k8sCrd .Create (ctx , & ghCrd ); err != nil {
213- log .Error (err , "failed to create GitDeployment CRD" , "Deployment" , ghCrd .Spec .Name )
220+ // Create Git Deployment from the CRD
221+ for _ , k8sCrd := range k8sCrds .Items {
222+ // Make sure that the GH deployment not exist while comparing with both Status.Ref and Spec.Ref
223+ ghCrd , found := inGhByRef [k8sCrd .Spec .Ref ]
224+ _ , foundInStatus := inGhByRef [k8sCrd .Status .Ref ]
225+ if ! found && ! foundInStatus {
226+ // Creates new GH deployment based on the Specs received in the k8s object
227+ log .Info ("Creating new deployment on GH" )
228+ deployment , _ , err := g .scm .Deployments .Create (ctx , githubFetcher .repositoryName (), getDeploymentInputObject (k8sCrd ))
229+ if err != nil {
214230 return err
215231 }
216- log .Info ("Deployment created" , "deployment" , ghCrd .Spec .Name )
217- } else if k8sCrd .Spec .Ref != ghCrd .Spec .Ref {
218- log .Info ("Creating a new deployment" )
232+ k8sCrd .Spec = getGitDeploymentSpec (deployment )
233+ k8sCrd .Status = getGitDeploymentStatus (deployment )
234+ err = g .k8sCrd .Update (ctx , & k8sCrd )
235+ if err != nil {
236+ return err
237+ }
238+ } else if k8sCrd .Status .Ref != "" && (k8sCrd .Status .Ref != k8sCrd .Spec .Ref ) {
239+ // Process the Update request
240+ log .Info ("Updating deployment" )
219241 //Delete the existing deployment on github and create a new one
220- if _ , _ , err := g .scm .Deployments .Create (ctx , githubFetcher .repositoryName (), getDeploymentInputObject (* k8sCrd )); err != nil {
242+ deployment , _ , err := g .scm .Deployments .Create (ctx , githubFetcher .repositoryName (), getDeploymentInputObject (k8sCrd ))
243+ if err != nil {
221244 return err
222245 }
223-
224- if _ , err := g .scm .Deployments .Delete (ctx , githubFetcher .repositoryName (), ghCrd . Spec .ID ); err != nil {
246+ log . Info ( "Deleting Deployment" , "ID" , k8sCrd . Status . ID )
247+ if _ , err := g .scm .Deployments .Delete (ctx , githubFetcher .repositoryName (), k8sCrd . Status .ID ); err != nil {
225248 return err
226249 }
227- if err := g .k8sCrd .Delete (ctx , & ghCrd ); err != nil {
250+ k8sCrd .Spec = getGitDeploymentSpec (deployment )
251+ k8sCrd .Status = getGitDeploymentStatus (deployment )
252+ err = g .k8sCrd .Update (ctx , & k8sCrd )
253+ if err != nil {
228254 return err
229255 }
230- log .Info ("Deployment updated" , "deployment" , ghCrd .Spec .Name )
231256 } else {
232- log .Info ("Deployment did not change" , "deployment" , ghCrd .Spec .Name )
257+ // Status.Ref == "" after the spec is found on the GH deployment.
258+ // Conclusion the k8sCRD is created with the same spec as present on the GH deployment prior to GH deployment start
259+ // In this case we'll update the k8sCRD from the details from GH deployment
260+ k8sCrd .Spec = ghCrd .Spec
261+ k8sCrd .Status = ghCrd .Status
262+ err = g .k8sCrd .Update (ctx , & k8sCrd )
263+ if err != nil {
264+ return err
265+ }
266+ }
267+ }
268+ // Create the k8s CRD for all other deployments present on the GH repository
269+ for _ , ghCrd := range ghCrds {
270+ _ , found := inK8sByRef [ghCrd .Spec .Ref ]
271+ if ! found {
272+ if err := g .k8sCrd .Create (ctx , & ghCrd ); err != nil {
273+ return err
274+ }
233275 }
234276 }
277+ log .Info ("GH deployments and k8s CRDs are in sync" )
235278
236279 return nil
237280}
0 commit comments