Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Delete extra pilots
Browse files Browse the repository at this point in the history
  • Loading branch information
kragniz committed Mar 20, 2018
1 parent c73ff00 commit 07bea31
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/cassandra/nodepool/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func StatefulSetForCluster(
ObjectMeta: metav1.ObjectMeta{
Name: statefulSetName,
Namespace: cluster.Namespace,
Labels: util.ClusterLabels(cluster),
Labels: nodePoolLabels,
Annotations: make(map[string]string),
OwnerReferences: []metav1.OwnerReference{util.NewControllerRef(cluster)},
},
Expand Down
55 changes: 48 additions & 7 deletions pkg/controllers/cassandra/pilot/pilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,43 +79,83 @@ func (c *pilotControl) clusterPods(cluster *v1alpha1.CassandraCluster) ([]*v1.Po
return clusterPods, nil
}

func (c *pilotControl) createOrUpdatePilot(cluster *v1alpha1.CassandraCluster, pod *v1.Pod) error {
func (c *pilotControl) clusterPilots(cluster *v1alpha1.CassandraCluster) ([]*v1alpha1.Pilot, error) {
var clusterPilots []*v1alpha1.Pilot
allPilots, err := c.pilots.Pilots(cluster.Namespace).List(labels.Everything())
if err != nil {
return clusterPilots, err
}
for _, pilot := range allPilots {
if metav1.IsControlledBy(pilot, cluster) {
clusterPilots = append(clusterPilots, pilot)
}
}
return clusterPilots, nil
}

func (c *pilotControl) createOrUpdatePilot(cluster *v1alpha1.CassandraCluster, pod *v1.Pod) (string, error) {
desiredPilot := PilotForCluster(cluster, pod)
client := c.naviClient.NavigatorV1alpha1().Pilots(desiredPilot.GetNamespace())
lister := c.pilots.Pilots(desiredPilot.GetNamespace())
existingPilot, err := lister.Get(desiredPilot.GetName())
if k8sErrors.IsNotFound(err) {
_, err = client.Create(desiredPilot)
return err
return desiredPilot.GetName(), err
}
if err != nil {
return err
return "", err
}
err = util.OwnerCheck(existingPilot, cluster)
if err != nil {
return err
return "", err
}
existingPilot = existingPilot.DeepCopy()
existingPilot.Status = v1alpha1.PilotStatus{}
desiredPilot = existingPilot.DeepCopy()
desiredPilot = updatePilotForCluster(cluster, pod, desiredPilot)
if !reflect.DeepEqual(desiredPilot, existingPilot) {
if !reflect.DeepEqual(desiredPilot.Spec, existingPilot.Spec) {
_, err = client.Update(desiredPilot)
}
return err
return desiredPilot.GetName(), err
}

func (c *pilotControl) syncPilots(cluster *v1alpha1.CassandraCluster) error {
pods, err := c.clusterPods(cluster)
if err != nil {
return err
}

desiredPilots := []string{}
for _, pod := range pods {
err = c.createOrUpdatePilot(cluster, pod)
pilotName, err := c.createOrUpdatePilot(cluster, pod)
if err != nil {
return err
}
if pilotName != "" {
desiredPilots = append(desiredPilots, pilotName)
}
}

clusterPilots, err := c.clusterPilots(cluster)
if err != nil {
return err
}

// delete unused pilots
outer:
for _, pilot := range clusterPilots {
for _, name := range desiredPilots {
if pilot.Name == name {
continue outer
}
}

err := c.naviClient.NavigatorV1alpha1().Pilots(pilot.Namespace).Delete(pilot.Name, nil)
if err != nil {
return err
}
}

return err
}

Expand All @@ -135,6 +175,7 @@ func PilotForCluster(cluster *v1alpha1.CassandraCluster, pod *v1.Pod) *v1alpha1.
util.NewControllerRef(cluster),
},
)
pilot.SetLabels(pod.Labels)
return updatePilotForCluster(cluster, pod, pilot)
}

Expand Down

0 comments on commit 07bea31

Please sign in to comment.