Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
syntassodev committed Feb 9, 2024
2 parents 4fb28dc + 524ea3e commit e1e26ef
Show file tree
Hide file tree
Showing 27 changed files with 183 additions and 184 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ commands:
default: ':red_circle: A $CIRCLE_JOB job has failed!'
channel:
type: string
default: eng-chatter
default: eng-alerts-ci
steps:
- slack/status:
webhook: ${SLACK_ENG_CHATTER}
Expand Down
15 changes: 11 additions & 4 deletions api/v1alpha1/const.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package v1alpha1

const (
KratixSystemNamespace = "kratix-platform-system"
KratixActionConfigure = "configure"
KratixWorkflowTypeResource = "resource"
KratixWorkflowTypePromise = "promise"
SystemNamespace = "kratix-platform-system"

WorkflowActionConfigure Action = "configure"
WorkflowActionDelete Action = "delete"

WorkflowTypeResource Type = "resource"
WorkflowTypePromise Type = "promise"
)

// So we can set a functions arguments to be of type Action instead of string
type Action string
type Type string
2 changes: 1 addition & 1 deletion api/v1alpha1/work_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func NewPromiseDependenciesWork(promise *Promise) (*Work, error) {
work := &Work{
ObjectMeta: metav1.ObjectMeta{
Name: promise.GetName(),
Namespace: KratixSystemNamespace,
Namespace: SystemNamespace,
Labels: promise.GenerateSharedLabels(),
},
Spec: WorkSpec{
Expand Down
10 changes: 5 additions & 5 deletions controllers/destination_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"sigs.k8s.io/yaml"

"github.com/go-logr/logr"
platformv1alpha1 "github.com/syntasso/kratix/api/v1alpha1"
"github.com/syntasso/kratix/api/v1alpha1"
"github.com/syntasso/kratix/lib/writers"
)

Expand All @@ -55,7 +55,7 @@ func (r *DestinationReconciler) Reconcile(ctx context.Context, req ctrl.Request)
"destination", req.NamespacedName,
)

destination := &platformv1alpha1.Destination{}
destination := &v1alpha1.Destination{}
logger.Info("Registering Destination", "requestName", req.Name)
if err := r.Client.Get(ctx, client.ObjectKey{Name: req.Name}, destination); err != nil {
if errors.IsNotFound(err) {
Expand Down Expand Up @@ -117,7 +117,7 @@ func (r *DestinationReconciler) createResourcePathWithExample(writer writers.Sta
}
nsBytes, _ := yaml.Marshal(kratixConfigMap)

return writer.WriteDirWithObjects(writers.PreserveExistingContentsInDir, resourcesDir, platformv1alpha1.Workload{
return writer.WriteDirWithObjects(writers.PreserveExistingContentsInDir, resourcesDir, v1alpha1.Workload{
Filepath: "kratix-canary-configmap.yaml",
Content: string(nsBytes),
})
Expand All @@ -133,7 +133,7 @@ func (r *DestinationReconciler) createDependenciesPathWithExample(writer writers
}
nsBytes, _ := yaml.Marshal(kratixNamespace)

return writer.WriteDirWithObjects(writers.PreserveExistingContentsInDir, dependenciesDir, platformv1alpha1.Workload{
return writer.WriteDirWithObjects(writers.PreserveExistingContentsInDir, dependenciesDir, v1alpha1.Workload{
Filepath: "kratix-canary-namespace.yaml",
Content: string(nsBytes),
})
Expand All @@ -142,6 +142,6 @@ func (r *DestinationReconciler) createDependenciesPathWithExample(writer writers
// SetupWithManager sets up the controller with the Manager.
func (r *DestinationReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&platformv1alpha1.Destination{}).
For(&v1alpha1.Destination{}).
Complete(r)
}
6 changes: 3 additions & 3 deletions controllers/promise_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (r *PromiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

if promise.ContainsAPI() {
var work v1alpha1.Work
err = r.Client.Get(ctx, types.NamespacedName{Name: promise.GetName(), Namespace: v1alpha1.KratixSystemNamespace}, &work)
err = r.Client.Get(ctx, types.NamespacedName{Name: promise.GetName(), Namespace: v1alpha1.SystemNamespace}, &work)
if err != nil {
logger.Error(err, "Error getting Work")
return ctrl.Result{}, err
Expand Down Expand Up @@ -525,7 +525,7 @@ func (r *PromiseReconciler) createResourcesForDynamicControllerIfTheyDontExist(c
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Namespace: v1alpha1.KratixSystemNamespace,
Namespace: v1alpha1.SystemNamespace,
Name: "kratix-platform-controller-manager",
},
},
Expand Down Expand Up @@ -769,7 +769,7 @@ func (r *PromiseReconciler) deleteResourceRequests(o opts, promise *v1alpha1.Pro
}

var work v1alpha1.Work
err = r.Client.Get(o.ctx, types.NamespacedName{Name: promise.GetName(), Namespace: v1alpha1.KratixSystemNamespace}, &work)
err = r.Client.Get(o.ctx, types.NamespacedName{Name: promise.GetName(), Namespace: v1alpha1.SystemNamespace}, &work)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/promise_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ var _ = Describe("PromiseController", func() {
Expect(binding.Subjects).To(HaveLen(1))
Expect(binding.Subjects[0]).To(Equal(rbacv1.Subject{
Kind: "ServiceAccount",
Namespace: v1alpha1.KratixSystemNamespace,
Namespace: v1alpha1.SystemNamespace,
Name: "kratix-platform-controller-manager",
}))
Expect(binding.GetLabels()).To(Equal(promiseCommonLabels))
Expand All @@ -187,7 +187,7 @@ var _ = Describe("PromiseController", func() {
By("creating a Work resource for the dependencies", func() {
workNamespacedName := types.NamespacedName{
Name: promise.GetName(),
Namespace: v1alpha1.KratixSystemNamespace,
Namespace: v1alpha1.SystemNamespace,
}
Expect(fakeK8sClient.Get(ctx, workNamespacedName, &v1alpha1.Work{})).To(Succeed())
})
Expand Down
41 changes: 20 additions & 21 deletions controllers/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/go-logr/logr"
"github.com/syntasso/kratix/api/v1alpha1"
platformv1alpha1 "github.com/syntasso/kratix/api/v1alpha1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -42,7 +41,7 @@ type Scheduler struct {
// Destinations.
// Only reconciles Works that are from a Promise Dependency.
func (s *Scheduler) ReconcileAllDependencyWorks() error {
works := platformv1alpha1.WorkList{}
works := v1alpha1.WorkList{}
lo := &client.ListOptions{}
if err := s.Client.List(context.Background(), &works, lo); err != nil {
return err
Expand All @@ -61,7 +60,7 @@ func (s *Scheduler) ReconcileAllDependencyWorks() error {

// Reconciles all WorkloadGroups in a Work by scheduling them to Destinations via
// Workplacements.
func (s *Scheduler) ReconcileWork(work *platformv1alpha1.Work) ([]string, error) {
func (s *Scheduler) ReconcileWork(work *v1alpha1.Work) ([]string, error) {
unschedulable := []string{}
misscheduled := []string{}
for _, wg := range work.Spec.WorkloadGroups {
Expand All @@ -86,7 +85,7 @@ func (s *Scheduler) ReconcileWork(work *platformv1alpha1.Work) ([]string, error)
return unschedulable, s.cleanupDanglingWorkplacements(work)
}

func (s *Scheduler) updateWorkStatus(work *platformv1alpha1.Work, unscheduledWorkloadGroupIDs, missscheduledWorkloadGroupIDs []string) error {
func (s *Scheduler) updateWorkStatus(work *v1alpha1.Work, unscheduledWorkloadGroupIDs, missscheduledWorkloadGroupIDs []string) error {
work = work.DeepCopy()
conditions := []metav1.Condition{
{
Expand Down Expand Up @@ -137,7 +136,7 @@ func (s *Scheduler) updateWorkStatus(work *platformv1alpha1.Work, unscheduledWor
return s.Client.Status().Update(context.Background(), work)
}

func (s *Scheduler) cleanupDanglingWorkplacements(work *platformv1alpha1.Work) error {
func (s *Scheduler) cleanupDanglingWorkplacements(work *v1alpha1.Work) error {
workplacementsThatShouldExist := map[string]interface{}{}
for _, wg := range work.Spec.WorkloadGroups {
workPlacements, err := s.getExistingWorkPlacementsForWorkloadGroup(work.Namespace, work.Name, wg)
Expand Down Expand Up @@ -168,7 +167,7 @@ func (s *Scheduler) cleanupDanglingWorkplacements(work *platformv1alpha1.Work) e
}

// Reconciles a WorkloadGroup by scheduling it to a Destination via a Workplacement.
func (s *Scheduler) reconcileWorkloadGroup(workloadGroup platformv1alpha1.WorkloadGroup, work *platformv1alpha1.Work) (schedulingStatus, error) {
func (s *Scheduler) reconcileWorkloadGroup(workloadGroup v1alpha1.WorkloadGroup, work *v1alpha1.Work) (schedulingStatus, error) {
// TODO why pointer for work?

existingWorkplacements, err := s.getExistingWorkPlacementsForWorkloadGroup(work.Namespace, work.Name, workloadGroup)
Expand Down Expand Up @@ -237,7 +236,7 @@ func (s *Scheduler) reconcileWorkloadGroup(workloadGroup platformv1alpha1.Worklo
return status, nil
}

func (s *Scheduler) updateWorkPlacement(workloadGroup platformv1alpha1.WorkloadGroup, work *platformv1alpha1.Work, workPlacement *platformv1alpha1.WorkPlacement) (bool, error) {
func (s *Scheduler) updateWorkPlacement(workloadGroup v1alpha1.WorkloadGroup, work *v1alpha1.Work, workPlacement *v1alpha1.WorkPlacement) (bool, error) {
misscheduled := true
destinationSelectors := resolveDestinationSelectorsForWorkloadGroup(workloadGroup, work)
for _, dest := range s.getTargetDestinationNames(destinationSelectors, work) {
Expand Down Expand Up @@ -291,21 +290,21 @@ func misscheduledWorkPlacements(listA, listB []v1alpha1.WorkPlacement) []v1alpha
return diff
}

func (s *Scheduler) getExistingWorkPlacementsForWorkloadGroup(namespace, workName string, workloadGroup platformv1alpha1.WorkloadGroup) ([]platformv1alpha1.WorkPlacement, error) {
func (s *Scheduler) getExistingWorkPlacementsForWorkloadGroup(namespace, workName string, workloadGroup v1alpha1.WorkloadGroup) ([]v1alpha1.WorkPlacement, error) {
return s.listWorkplacementWithLabels(namespace, map[string]string{
workLabelKey: workName,
workloadGroupIDKey: workloadGroup.ID,
})
}

func (s *Scheduler) getExistingWorkPlacementsForWork(namespace, workName string) ([]platformv1alpha1.WorkPlacement, error) {
func (s *Scheduler) getExistingWorkPlacementsForWork(namespace, workName string) ([]v1alpha1.WorkPlacement, error) {
return s.listWorkplacementWithLabels(namespace, map[string]string{
workLabelKey: workName,
})
}

func (s *Scheduler) listWorkplacementWithLabels(namespace string, matchLabels map[string]string) ([]platformv1alpha1.WorkPlacement, error) {
workPlacementList := &platformv1alpha1.WorkPlacementList{}
func (s *Scheduler) listWorkplacementWithLabels(namespace string, matchLabels map[string]string) ([]v1alpha1.WorkPlacement, error) {
workPlacementList := &v1alpha1.WorkPlacementList{}
workPlacementListOptions := &client.ListOptions{
Namespace: namespace,
}
Expand All @@ -328,10 +327,10 @@ func (s *Scheduler) listWorkplacementWithLabels(namespace string, matchLabels ma
return workPlacementList.Items, nil
}

func (s *Scheduler) applyWorkplacementsForTargetDestinations(workloadGroup platformv1alpha1.WorkloadGroup, work *platformv1alpha1.Work, targetDestinationNames map[string]bool) (bool, error) {
func (s *Scheduler) applyWorkplacementsForTargetDestinations(workloadGroup v1alpha1.WorkloadGroup, work *v1alpha1.Work, targetDestinationNames map[string]bool) (bool, error) {
containsMischeduledWorkplacement := false
for targetDestinationName, misscheduled := range targetDestinationNames {
workPlacement := &platformv1alpha1.WorkPlacement{}
workPlacement := &v1alpha1.WorkPlacement{}
workPlacement.Namespace = work.GetNamespace()
workPlacement.Name = work.Name + "." + targetDestinationName + "-" + shortID(workloadGroup.ID)

Expand Down Expand Up @@ -371,8 +370,8 @@ func (s *Scheduler) applyWorkplacementsForTargetDestinations(workloadGroup platf
return containsMischeduledWorkplacement, nil
}

func (s *Scheduler) updateStatus(workPlacement *platformv1alpha1.WorkPlacement, misscheduled bool) error {
updatedWorkPlacement := &platformv1alpha1.WorkPlacement{}
func (s *Scheduler) updateStatus(workPlacement *v1alpha1.WorkPlacement, misscheduled bool) error {
updatedWorkPlacement := &v1alpha1.WorkPlacement{}
if err := s.Client.Get(context.Background(), client.ObjectKeyFromObject(workPlacement), updatedWorkPlacement); err != nil {
return err
}
Expand All @@ -395,7 +394,7 @@ func (s *Scheduler) updateStatus(workPlacement *platformv1alpha1.WorkPlacement,

// Where Work is a Resource Request return one random Destination name, where Work is a
// DestinationWorkerResource return all Destination names
func (s *Scheduler) getTargetDestinationNames(destinationSelectors map[string]string, work *platformv1alpha1.Work) []string {
func (s *Scheduler) getTargetDestinationNames(destinationSelectors map[string]string, work *v1alpha1.Work) []string {
destinations := s.getDestinationsForWorkloadGroup(destinationSelectors)

if len(destinations) == 0 {
Expand Down Expand Up @@ -426,8 +425,8 @@ func (s *Scheduler) getTargetDestinationNames(destinationSelectors map[string]st
}

// By default, all destinations are returned. However, if scheduling is provided, only matching destinations will be returned.
func (s *Scheduler) getDestinationsForWorkloadGroup(destinationSelectors map[string]string) []platformv1alpha1.Destination {
destinationList := &platformv1alpha1.DestinationList{}
func (s *Scheduler) getDestinationsForWorkloadGroup(destinationSelectors map[string]string) []v1alpha1.Destination {
destinationList := &v1alpha1.DestinationList{}
lo := &client.ListOptions{}

if len(destinationSelectors) > 0 {
Expand All @@ -450,7 +449,7 @@ func (s *Scheduler) getDestinationsForWorkloadGroup(destinationSelectors map[str
return destinationList.Items
}

destinations := []platformv1alpha1.Destination{}
destinations := []v1alpha1.Destination{}
for _, destination := range destinationList.Items {
if destination.Spec.StrictMatchLabels && len(destination.GetLabels()) > 0 {
continue
Expand All @@ -460,7 +459,7 @@ func (s *Scheduler) getDestinationsForWorkloadGroup(destinationSelectors map[str
return destinations
}

func resolveDestinationSelectorsForWorkloadGroup(workloadGroup platformv1alpha1.WorkloadGroup, work *platformv1alpha1.Work) map[string]string {
func resolveDestinationSelectorsForWorkloadGroup(workloadGroup v1alpha1.WorkloadGroup, work *v1alpha1.Work) map[string]string {
sortedWorkloadGroupDestinations := sortWorkloadGroupDestinationsByLowestPriority(workloadGroup.DestinationSelectors)
destinationSelectors := map[string]string{}

Expand All @@ -477,7 +476,7 @@ func resolveDestinationSelectorsForWorkloadGroup(workloadGroup platformv1alpha1.
// Resource-workflow, then
// Promise-workflow, then
// Promise
func sortWorkloadGroupDestinationsByLowestPriority(selector []platformv1alpha1.WorkloadGroupScheduling) []platformv1alpha1.WorkloadGroupScheduling {
func sortWorkloadGroupDestinationsByLowestPriority(selector []v1alpha1.WorkloadGroupScheduling) []v1alpha1.WorkloadGroupScheduling {
sort.SliceStable(selector, func(i, j int) bool {
iSource := selector[i].Source
jSource := selector[j].Source
Expand Down
10 changes: 5 additions & 5 deletions controllers/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ var _ = Describe("Controllers/Scheduler", func() {
Expect(workPlacements.Items).To(HaveLen(3))

Expect(workPlacements.Items[0].Name).To(HavePrefix("dev-work-name.dev-1"))
Expect(workPlacements.Items[0].Namespace).To(Equal(KratixSystemNamespace))
Expect(workPlacements.Items[0].Namespace).To(Equal(SystemNamespace))
Expect(workPlacements.Items[0].Spec.TargetDestinationName).To(Equal(devDestination.Name))
Expect(workPlacements.Items[0].Spec.Workloads).To(Equal(dependencyWorkForDev.Spec.WorkloadGroups[0].Workloads))
Expect(workPlacements.Items[0].Spec.ID).To(Equal(dependencyWorkForDev.Spec.WorkloadGroups[0].ID))

Expect(workPlacements.Items[1].Name).To(HavePrefix("dev-work-name.dev-2"))
Expect(workPlacements.Items[1].Namespace).To(Equal(KratixSystemNamespace))
Expect(workPlacements.Items[1].Namespace).To(Equal(SystemNamespace))
Expect(workPlacements.Items[1].Spec.TargetDestinationName).To(Equal(devDestination2.Name))
Expect(workPlacements.Items[1].Spec.Workloads).To(Equal(dependencyWorkForDev.Spec.WorkloadGroups[0].Workloads))
Expect(workPlacements.Items[1].Spec.ID).To(Equal(dependencyWorkForDev.Spec.WorkloadGroups[0].ID))

Expect(workPlacements.Items[2].Name).To(HavePrefix("dev-work-name.dev-3"))
Expect(workPlacements.Items[2].Namespace).To(Equal(KratixSystemNamespace))
Expect(workPlacements.Items[2].Namespace).To(Equal(SystemNamespace))
Expect(workPlacements.Items[2].Spec.TargetDestinationName).To(Equal(devDestination3.Name))
Expect(workPlacements.Items[2].Spec.Workloads).To(Equal(dependencyWorkForDev.Spec.WorkloadGroups[0].Workloads))
Expect(workPlacements.Items[2].Spec.ID).To(Equal(dependencyWorkForDev.Spec.WorkloadGroups[0].ID))
Expand Down Expand Up @@ -888,7 +888,7 @@ func newDestination(name string, labels map[string]string) Destination {
func newWork(name string, replicas int, scheduling ...WorkloadGroupScheduling) Work {
namespace := "default"
if replicas == DependencyReplicas {
namespace = KratixSystemNamespace
namespace = SystemNamespace
}
work := &Work{
ObjectMeta: v1.ObjectMeta{
Expand Down Expand Up @@ -927,7 +927,7 @@ func newWork(name string, replicas int, scheduling ...WorkloadGroupScheduling) W
func newWorkWithTwoWorkloadGroups(name string, replicas int, promiseScheduling, directoryOverrideScheduling WorkloadGroupScheduling) Work {
namespace := "default"
if replicas == DependencyReplicas {
namespace = KratixSystemNamespace
namespace = SystemNamespace
}

work := &Work{
Expand Down
4 changes: 2 additions & 2 deletions controllers/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type jobOpts struct {
func ensureConfigurePipelineIsReconciled(j jobOpts) (*ctrl.Result, error) {
namespace := j.obj.GetNamespace()
if namespace == "" {
namespace = v1alpha1.KratixSystemNamespace
namespace = v1alpha1.SystemNamespace
}

pipelineJobs, err := getJobsWithLabels(j.opts, j.pipelineLabels, namespace)
Expand Down Expand Up @@ -278,7 +278,7 @@ func fetchObjectAndSecret(o opts, stateStoreRef client.ObjectKey, stateStore Sta

namespace := stateStore.GetSecretRef().Namespace
if namespace == "" {
namespace = v1alpha1.KratixSystemNamespace
namespace = v1alpha1.SystemNamespace
}

secret := &v1.Secret{}
Expand Down
2 changes: 1 addition & 1 deletion controllers/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func autoCompleteJobAndCreateWork(labels map[string]string, workName string) fun

namespace := obj.GetNamespace()
if obj.GetNamespace() == "" {
namespace = v1alpha1.KratixSystemNamespace
namespace = v1alpha1.SystemNamespace
}

Expect(fakeK8sClient.Get(ctx, client.ObjectKeyFromObject(job), job)).To(Succeed())
Expand Down
18 changes: 9 additions & 9 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
platformv1alpha1 "github.com/syntasso/kratix/api/v1alpha1"
"github.com/syntasso/kratix/api/v1alpha1"

fakeclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1"
Expand Down Expand Up @@ -56,7 +56,7 @@ var (
)

var _ = BeforeSuite(func(_ SpecContext) {
err := platformv1alpha1.AddToScheme(scheme.Scheme)
err := v1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
//+kubebuilder:scaffold:scheme

Expand All @@ -75,13 +75,13 @@ var _ = BeforeEach(func() {
Expect(yaml.Unmarshal(yamlFile, resReq)).To(Succeed())

fakeK8sClient = fake.NewClientBuilder().WithScheme(scheme.Scheme).WithStatusSubresource(
&platformv1alpha1.PromiseRelease{},
&platformv1alpha1.Promise{},
&platformv1alpha1.Work{},
&platformv1alpha1.WorkPlacement{},
&platformv1alpha1.Destination{},
&platformv1alpha1.GitStateStore{},
&platformv1alpha1.BucketStateStore{},
&v1alpha1.PromiseRelease{},
&v1alpha1.Promise{},
&v1alpha1.Work{},
&v1alpha1.WorkPlacement{},
&v1alpha1.Destination{},
&v1alpha1.GitStateStore{},
&v1alpha1.BucketStateStore{},
//Add redis.marketplace.kratix.io/v1alpha1 so we can update its status
resReq,
).Build()
Expand Down
Loading

0 comments on commit e1e26ef

Please sign in to comment.