Skip to content
This repository was archived by the owner on Jun 14, 2023. It is now read-only.

Commit fc86e4e

Browse files
committed
feat: refactor implementation to be able to create and modify GH deployment through a k8s object
1 parent 2c33a6a commit fc86e4e

4 files changed

Lines changed: 93 additions & 38 deletions

File tree

config/samples/git_v1_gitdeployment.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ apiVersion: git.flanksource.com/v1
22
kind: GitDeployment
33
metadata:
44
name: gitdeployment-sample
5+
labels:
6+
git.flanksource.com/repository: gitrepository-sample
57
spec:
68
# Add fields here
7-
ref: add-git-ssh-connector
9+
ref: git-deployment-branch-1

connectors/github.go

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

connectors/github_util.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package connectors
33
import (
44
"context"
55
"fmt"
6+
"math/rand"
67
"strconv"
78
"strings"
89
"time"
@@ -157,30 +158,15 @@ func (g *GithubFetcher) BuildDeploymentCRDsFromGithub(ctx context.Context, lastU
157158
func (g *GithubFetcher) BuildDeploymentCRDFromGithub(ctx context.Context, deployment *scm.Deployment) *gitv1.GitDeployment {
158159
crd := &gitv1.GitDeployment{
159160
ObjectMeta: metav1.ObjectMeta{
160-
Name: g.deploymentName(g.repository.Name, deployment.Name, deployment.Ref),
161+
Name: g.deploymentName(g.repository.Name),
161162
Namespace: g.repository.Namespace,
162163
Labels: map[string]string{
163164
"git.flanksource.com/repository": g.repository.Name,
164165
"git.flanksource.com/deployment": deployment.Name,
165166
},
166167
},
167-
Spec: gitv1.GitDeploymentSpec{
168-
Ref: deployment.Ref,
169-
Sha: deployment.Sha,
170-
Name: deployment.Name,
171-
ID: deployment.ID,
172-
Environment: deployment.Environment,
173-
Description: deployment.Description,
174-
},
175-
Status: gitv1.GitDeploymentStatus{
176-
Ref: deployment.Ref,
177-
Sha: deployment.Sha,
178-
DeploymentLink: deployment.Link,
179-
StatusLink: deployment.StatusLink,
180-
ID: deployment.ID,
181-
Name: deployment.Name,
182-
Environment: deployment.Environment,
183-
},
168+
Spec: getGitDeploymentSpec(deployment),
169+
Status: getGitDeploymentStatus(deployment),
184170
}
185171
return crd
186172
}
@@ -193,10 +179,33 @@ func (g *GithubFetcher) branchName(repository string, name string) string {
193179
return fmt.Sprintf("%s-%s", repository, name)
194180
}
195181

196-
func (g *GithubFetcher) deploymentName(repository, name, ref string) string {
197-
return fmt.Sprintf("%s-%s-%s", repository, name, ref)
182+
func (g *GithubFetcher) deploymentName(repository string) string {
183+
return fmt.Sprintf("%s-%d", repository, rand.Int())
198184
}
199185

200186
func (g *GithubFetcher) pullRequestName(repository string, number int) string {
201187
return fmt.Sprintf("%s-%d", repository, number)
202188
}
189+
190+
func getGitDeploymentStatus(deployment *scm.Deployment) gitv1.GitDeploymentStatus {
191+
return gitv1.GitDeploymentStatus{
192+
Ref: deployment.Ref,
193+
Sha: deployment.Sha,
194+
DeploymentLink: deployment.Link,
195+
StatusLink: deployment.StatusLink,
196+
ID: deployment.ID,
197+
Name: deployment.Name,
198+
Environment: deployment.Environment,
199+
}
200+
}
201+
202+
func getGitDeploymentSpec(deployment *scm.Deployment) gitv1.GitDeploymentSpec {
203+
return gitv1.GitDeploymentSpec{
204+
Ref: deployment.Ref,
205+
Sha: deployment.Sha,
206+
Name: deployment.Name,
207+
ID: deployment.ID,
208+
Environment: deployment.Environment,
209+
Description: deployment.Description,
210+
}
211+
}

controllers/suite_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ func TestAPIs(t *testing.T) {
5050
}
5151

5252
var _ = BeforeSuite(func(done Done) {
53-
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
53+
//TODO zap.LoggerTo is deprecated, need to move to Zap.New()
54+
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true)) //nolint: staticcheck
5455

5556
By("bootstrapping test environment")
5657
testEnv = &envtest.Environment{

0 commit comments

Comments
 (0)