Skip to content

Commit

Permalink
Merge pull request #21 from reactiveops/rs/fix-delete-bug
Browse files Browse the repository at this point in the history
Ensure that resources are deleted before replacing them
  • Loading branch information
robscott authored Jun 28, 2018
2 parents 36635b7 + e2ddb21 commit 54cdb5b
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions pkg/controller/rbacdefinition/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (bc *RBACDefinitionController) reconcileClusterRoleBindings(
ownerReferences *[]metav1.OwnerReference) {

matchingClusterRoleBindings := []rbacv1.ClusterRoleBinding{}
clusterRoleBindingsToCreate := []rbacv1.ClusterRoleBinding{}

for _, requestedCRB := range *requestedClusterRoleBindings {
alreadyExists := false
Expand All @@ -41,11 +42,7 @@ func (bc *RBACDefinitionController) reconcileClusterRoleBindings(
}

if !alreadyExists {
logrus.Infof("Creating Cluster Role Binding: %v", requestedCRB.Name)
_, err := bc.kubernetesClientSet.RbacV1().ClusterRoleBindings().Create(&requestedCRB)
if err != nil {
logrus.Errorf("Error creating Cluster Role Binding: %v", err)
}
clusterRoleBindingsToCreate = append(clusterRoleBindingsToCreate, requestedCRB)
} else {
logrus.Debugf("Cluster Role Binding already exists %v", requestedCRB.Name)
}
Expand All @@ -72,6 +69,14 @@ func (bc *RBACDefinitionController) reconcileClusterRoleBindings(
}
}
}

for _, clusterRoleBindingToCreate := range clusterRoleBindingsToCreate {
logrus.Infof("Creating Cluster Role Binding: %v", clusterRoleBindingToCreate.Name)
_, err := bc.kubernetesClientSet.RbacV1().ClusterRoleBindings().Create(&clusterRoleBindingToCreate)
if err != nil {
logrus.Errorf("Error creating Cluster Role Binding: %v", err)
}
}
}

func (bc *RBACDefinitionController) reconcileRoleBindings(
Expand All @@ -80,6 +85,7 @@ func (bc *RBACDefinitionController) reconcileRoleBindings(
ownerReferences *[]metav1.OwnerReference) {

matchingRoleBindings := []rbacv1.RoleBinding{}
roleBindingsToCreate := []rbacv1.RoleBinding{}

for _, requestedRB := range *requestedRoleBindings {
alreadyExists := false
Expand All @@ -92,11 +98,7 @@ func (bc *RBACDefinitionController) reconcileRoleBindings(
}

if !alreadyExists {
logrus.Infof("Creating Role Binding: %v", requestedRB.Name)
_, err := bc.kubernetesClientSet.RbacV1().RoleBindings(requestedRB.ObjectMeta.Namespace).Create(&requestedRB)
if err != nil {
logrus.Errorf("Error creating Role Binding: %v", err)
}
roleBindingsToCreate = append(roleBindingsToCreate, requestedRB)
} else {
logrus.Debugf("Role Binding already exists %v", requestedRB.Name)
}
Expand All @@ -123,6 +125,14 @@ func (bc *RBACDefinitionController) reconcileRoleBindings(
}
}
}

for _, roleBindingToCreate := range roleBindingsToCreate {
logrus.Infof("Creating Role Binding: %v", roleBindingToCreate.Name)
_, err := bc.kubernetesClientSet.RbacV1().RoleBindings(roleBindingToCreate.ObjectMeta.Namespace).Create(&roleBindingToCreate)
if err != nil {
logrus.Errorf("Error creating Role Binding: %v", err)
}
}
}

func (bc *RBACDefinitionController) reconcileServiceAccounts(
Expand All @@ -131,6 +141,7 @@ func (bc *RBACDefinitionController) reconcileServiceAccounts(
ownerReferences *[]metav1.OwnerReference) {

matchingServiceAccounts := []v1.ServiceAccount{}
serviceAccountsToCreate := []v1.ServiceAccount{}

for _, requestedSA := range *requestedServiceAccounts {
alreadyExists := false
Expand All @@ -143,11 +154,7 @@ func (bc *RBACDefinitionController) reconcileServiceAccounts(
}

if !alreadyExists {
logrus.Infof("Creating Service Account: %v", requestedSA.Name)
_, err := bc.kubernetesClientSet.CoreV1().ServiceAccounts(requestedSA.ObjectMeta.Namespace).Create(&requestedSA)
if err != nil {
logrus.Errorf("Error creating Service Account: %v", err)
}
serviceAccountsToCreate = append(serviceAccountsToCreate, requestedSA)
} else {
logrus.Debugf("Service Account already exists %v", requestedSA.Name)
}
Expand All @@ -174,4 +181,12 @@ func (bc *RBACDefinitionController) reconcileServiceAccounts(
}
}
}

for _, serviceAccountToCreate := range serviceAccountsToCreate {
logrus.Infof("Creating Service Account: %v", serviceAccountToCreate.Name)
_, err := bc.kubernetesClientSet.CoreV1().ServiceAccounts(serviceAccountToCreate.ObjectMeta.Namespace).Create(&serviceAccountToCreate)
if err != nil {
logrus.Errorf("Error creating Service Account: %v", err)
}
}
}

0 comments on commit 54cdb5b

Please sign in to comment.