Skip to content

Commit

Permalink
Rework replica logic (#427)
Browse files Browse the repository at this point in the history
* rework replica logic

Signed-off-by: Kim Tsao <[email protected]>

* Address review comments

Signed-off-by: Kim Tsao <[email protected]>

---------

Signed-off-by: Kim Tsao <[email protected]>
  • Loading branch information
kim-tsao authored Dec 7, 2023
1 parent 5c90c13 commit 599e4d3
Show file tree
Hide file tree
Showing 5 changed files with 391 additions and 25 deletions.
4 changes: 2 additions & 2 deletions controllers/component_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,7 @@ var _ = Describe("Component controller", func() {
Expect(createdHasComp.Spec.Replicas).Should(BeNil())

//Update replica
updatedHasComp.Spec.Replicas = &numReplica
updatedHasComp.Spec.Replicas = &oneReplica
Expect(k8sClient.Update(ctx, updatedHasComp)).Should(Succeed())
newUpdatedHasComp := &appstudiov1alpha1.Component{}
Eventually(func() bool {
Expand All @@ -1505,7 +1505,7 @@ var _ = Describe("Component controller", func() {
Expect(newUpdatedHasComp.Status.Conditions[len(newUpdatedHasComp.Status.Conditions)-1].Status).Should(Equal(metav1.ConditionTrue))
//replica should not be nil and should have a value
Expect(newUpdatedHasComp.Spec.Replicas).Should(Not(BeNil()))
Expect(*newUpdatedHasComp.Spec.Replicas).Should(Equal(numReplica))
Expect(*newUpdatedHasComp.Spec.Replicas).Should(Equal(oneReplica))

// Delete the specified HASComp resource
deleteHASCompCR(hasCompLookupKey)
Expand Down
72 changes: 66 additions & 6 deletions controllers/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,84 @@ func (r *ComponentReconciler) updateComponentDevfileModel(req ctrl.Request, hasC
for _, kubernetesComponent := range kubernetesComponents {
compUpdateRequired := false
// Update for Replica
currentReplica := 0
currentReplica := 1 // default value is 1
keyFound := true

if len(kubernetesComponent.Attributes) == 0 {
kubernetesComponent.Attributes = attributes.Attributes{}
keyFound = false
} else {
var err error
currentReplica = int(kubernetesComponent.Attributes.GetNumber(devfile.ReplicaKey, &err))
if err != nil {
if _, ok := err.(*attributes.KeyNotFoundError); !ok {
return err
} else {
keyFound = false
currentReplica = 1 //if an error is raised, it'll set currentReplica to 0 so we need to reset back to the default
}
}
}

numReplicas := util.GetIntValue(component.Spec.Replicas)
if currentReplica != numReplicas {
log.Info(fmt.Sprintf("setting devfile component %s attribute component.Spec.Replicas to %v", kubernetesComponent.Name, numReplicas))
kubernetesComponent.Attributes = kubernetesComponent.Attributes.PutInteger(devfile.ReplicaKey, numReplicas)
compUpdateRequired = true
numReplicas := 1 //default value
if component.Spec.Replicas != nil {
numReplicas = util.GetIntValue(component.Spec.Replicas)
// Component.Spec.Replicas will override any other settings.
// We will write the attribute if it doesn't exist for the initial creation case when comp.spec.replica is 1
if currentReplica != numReplicas || !keyFound {
log.Info(fmt.Sprintf("setting devfile component %s attribute %s to %v", kubernetesComponent.Name, devfile.ReplicaKey, numReplicas))
kubernetesComponent.Attributes = kubernetesComponent.Attributes.PutInteger(devfile.ReplicaKey, numReplicas)
compUpdateRequired = true
}
} else {
//check to see if we have an inlined deployment
isDeployReplicaSet := false
inlined := kubernetesComponent.Kubernetes.Inlined
if inlined != "" {
log.Info(fmt.Sprintf("reading the kubernetes inline from component %s", component.Name))
src := parser.YamlSrc{
Data: []byte(inlined),
}

values, err := parser.ReadKubernetesYaml(src, nil, nil)
if err != nil {
return err
}

resources, err := parser.ParseKubernetesYaml(values)
if err != nil {
return err
}

if len(resources.Deployments) > 0 {
replica := resources.Deployments[0].Spec.Replicas
if replica != nil {
isDeployReplicaSet = true
//remove the deployment/replicas attribute which can be left behind if we go from a set value to an unset value
if kubernetesComponent.Attributes.Exists(devfile.ReplicaKey) {
var err error
num := int(kubernetesComponent.Attributes.GetNumber(devfile.ReplicaKey, &err))

if err != nil {
if _, ok := err.(*attributes.KeyNotFoundError); !ok {
return err
} else {
log.Info(fmt.Sprintf("deleting %s attribute with value %v", devfile.ReplicaKey, num))
delete(kubernetesComponent.Attributes, devfile.ReplicaKey)
}
}

}
}
}
}

//set the default if replicas is unset in the component and deployment spec.
if !isDeployReplicaSet {
log.Info(fmt.Sprintf("setting devfile component %s attribute component.Spec.Replicas to %v", kubernetesComponent.Name, 1))
kubernetesComponent.Attributes = kubernetesComponent.Attributes.PutInteger(devfile.ReplicaKey, 1)
compUpdateRequired = true
}
}

// Update for Port
Expand Down
Loading

0 comments on commit 599e4d3

Please sign in to comment.