Skip to content

Commit

Permalink
Merge pull request #3 from stakater/handle-create-project
Browse files Browse the repository at this point in the history
Handle create project
  • Loading branch information
ahmedwaleedmalik authored Jul 30, 2020
2 parents 2618d9f + 0f62594 commit 4253a61
Show file tree
Hide file tree
Showing 14 changed files with 1,196 additions and 103 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ bin
*.swo
*~
/_output/
/jira-service-desk-operator
2 changes: 2 additions & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: CONFIG_SECRET_NAME
value: jira-service-desk-config
terminationGracePeriodSeconds: 10
51 changes: 32 additions & 19 deletions controllers/project_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import (
"context"
"time"

log "github.com/sirupsen/logrus"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

jiraservicedeskv1alpha1 "github.com/stakater/jira-service-desk-operator/api/v1alpha1"
"github.com/stakater/jira-service-desk-operator/jiraservicedeskclient"
jiraservicedeskclient "github.com/stakater/jira-service-desk-operator/jiraservicedesk/client"
)

const (
Expand All @@ -38,6 +38,7 @@ const (
type ProjectReconciler struct {
client.Client
Scheme *runtime.Scheme
Log logr.Logger
JiraServiceDeskClient jiraservicedeskclient.Client
}

Expand All @@ -46,6 +47,7 @@ type ProjectReconciler struct {

func (r *ProjectReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
_ = context.Background()
log := r.Log.WithValues("project", req.NamespacedName)

log.Info("Reconciling Project")

Expand All @@ -65,23 +67,23 @@ func (r *ProjectReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
}

// Check if the Project already exists
project, err := r.JiraServiceDeskClient.GetProjectByName(instance.Spec.Name)
if err != nil {
return ctrl.Result{}, err
}
// Project already exists
// TODO: This should be project != nil
if err != nil {
updatedProject := r.JiraServiceDeskClient.GetProjectFromCR(instance.Spec)
if !r.JiraServiceDeskClient.ProjectEqual(project, updatedProject) {
return r.handleUpdate(req, instance)
} else {
log.Info("Skipping update. No changes found")
return ctrl.Result{}, nil
}
}
// project, err := r.JiraServiceDeskClient.GetProjectByKey(instance.Spec.Key)
// if err != nil {
// return ctrl.Result{}, err
// }
// // Project already exists
// // TODO: This should be project != nil
// if err != nil {
// updatedProject := r.JiraServiceDeskClient.GetProjectFromProjectSpec(instance.Spec)
// if !r.JiraServiceDeskClient.ProjectEqual(project, updatedProject) {
// return r.handleUpdate(req, instance)
// } else {
// log.Info("Skipping update. No changes found")
// return ctrl.Result{}, nil
// }
// }
// TODO: Think of use cases and add a default return ctrl.Result{}, nil
return r.handleCreate(req, instance)
return r.handleCreate(req, instance, log)
}

func (r *ProjectReconciler) SetupWithManager(mgr ctrl.Manager) error {
Expand All @@ -90,7 +92,18 @@ func (r *ProjectReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

func (r *ProjectReconciler) handleCreate(req ctrl.Request, instance *jiraservicedeskv1alpha1.Project) (ctrl.Result, error) {
func (r *ProjectReconciler) handleCreate(req ctrl.Request, instance *jiraservicedeskv1alpha1.Project, log logr.Logger) (ctrl.Result, error) {

log.Info("Creating Jira Service Desk Project: " + instance.Spec.Name)

project := r.JiraServiceDeskClient.GetProjectFromProjectSpec(instance.Spec)
err := r.JiraServiceDeskClient.CreateProject(project)
if err != nil {
return ctrl.Result{}, err
}

log.Info("Successfully created Jira Service Desk Project: " + instance.Spec.Name)

return ctrl.Result{RequeueAfter: defaultRequeueTime}, nil
}

Expand Down
14 changes: 14 additions & 0 deletions examples/project/project.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: jiraservicedesk.stakater.com/v1alpha1
kind: Project
metadata:
name: stakater
spec:
name: stakater
key: STK
projectTypeKey: service_desk
projectTemplateKey: com.atlassian.servicedesk:itil-v2-service-desk-project
description: "Sample project for jira-service-desk-operator"
assigneeType: PROJECT_LEAD
leadAccountId: 5ebfbc3ead226b0ba46c3590
url: https://stakater.com

14 changes: 9 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
module github.com/stakater/jira-service-desk-operator

go 1.13
go 1.14

require (
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.8.1
github.com/sirupsen/logrus v1.4.2
github.com/go-logr/logr v0.1.0
github.com/onsi/ginkgo v1.12.0
github.com/onsi/gomega v1.9.0
github.com/operator-framework/operator-sdk v0.19.2
k8s.io/api v0.18.2
k8s.io/apimachinery v0.18.2
k8s.io/client-go v0.18.2
k8s.io/client-go v12.0.0+incompatible
sigs.k8s.io/controller-runtime v0.6.0
)

replace k8s.io/client-go => k8s.io/client-go v0.18.2
Loading

0 comments on commit 4253a61

Please sign in to comment.