Browse Source

idempotent calls

pull/3/head
Vladimir Smagin 1 year ago
parent
commit
227a01e0ea
2 changed files with 19 additions and 63 deletions
  1. +11
    -23
      pkg/controller/rabbitmq/rabbitmq_policies.go
  2. +8
    -40
      pkg/controller/rabbitmq/rabbitmq_users.go

+ 11
- 23
pkg/controller/rabbitmq/rabbitmq_policies.go View File

@@ -37,7 +37,7 @@ func (r *ReconcileRabbitmq) setPolicies(ctx context.Context, reqLogger logr.Logg
reqLogger.Info("Rabbitmq API service failed", "Service name", r.apiServiceHostname(cr), "Error", err.Error())
return err
}
reqLogger.Info("Using API service: "+r.apiServiceAddress(cr), "username", serviceAccount.username, "password", serviceAccount.password)
reqLogger.Info("Policies: Using API service: "+r.apiServiceAddress(cr), "username", serviceAccount.username, "password", serviceAccount.password)

var policiesCR []rabbitmqv1.RabbitmqPolicy

@@ -77,28 +77,6 @@ func (r *ReconcileRabbitmq) setPolicies(ctx context.Context, reqLogger logr.Logg

// ok, now syncing

// add to rabbit from CR
for _, policyCR := range policiesCR {

//search
policyFound := false
for _, policyRabbit := range policiesRabbit {
if policyCR.Name == policyRabbit.Name {
policyFound =true
}
}

if !policyFound {
// send policy to api service
reqLogger.Info("Adding policy " + policyCR.Name + " to vhost " + policyCR.Vhost)
err = r.apiPolicyAdd(reqLogger, cr, serviceAccount, policyCR.Vhost, policyCR)
if err != nil {
reqLogger.Info("Error adding policy "+policyCR.Name+" to vhost "+policyCR.Vhost, "Error", err)
return err
}
}
}

// remove policies from rabbit
for _, policyRabbit := range policiesRabbit {

@@ -120,5 +98,15 @@ func (r *ReconcileRabbitmq) setPolicies(ctx context.Context, reqLogger logr.Logg

}

// add to rabbit from CR
for _, policyCR := range policiesCR {
reqLogger.Info("Adding policy " + policyCR.Name + " to vhost " + policyCR.Vhost)
err = r.apiPolicyAdd(reqLogger, cr, serviceAccount, policyCR.Vhost, policyCR)
if err != nil {
reqLogger.Info("Error adding policy "+policyCR.Name+" to vhost "+policyCR.Vhost, "Error", err)
return err
}
}

return nil
}

+ 8
- 40
pkg/controller/rabbitmq/rabbitmq_users.go View File

@@ -38,7 +38,7 @@ func (r *ReconcileRabbitmq) syncUsersCredentials(ctx context.Context, reqLogger
reqLogger.Info("Rabbitmq API service failed", "Service name", r.apiServiceHostname(cr), "Error", err.Error())
return err
}
reqLogger.Info("Using API service: "+r.apiServiceAddress(cr), "username", serviceAccount.username, "password", serviceAccount.password)
reqLogger.Info("Users: Using API service: "+r.apiServiceAddress(cr), "username", serviceAccount.username, "password", serviceAccount.password)

// get user from secret
usersSecret, err := r.getSecret(secretNames.Credentials, cr.Namespace)
@@ -52,25 +52,7 @@ func (r *ReconcileRabbitmq) syncUsersCredentials(ctx context.Context, reqLogger
return err
}

var usersToAdd []string
var usersToRemove []string

// search users to add
for userSecretName, _ := range usersSecret.Data {

userFound := false

for _, userRabbitName := range usersRabbit {
if userSecretName == userRabbitName.Name {
userFound = true
}
}

// user from secret resource not found in RabbitMQ, so add to new list
if !userFound {
usersToAdd = append(usersToAdd, userSecretName)
}
}
reqLogger.Info("Sync users started")

// search users to remove
for _, userRabbitName := range usersRabbit {
@@ -85,32 +67,18 @@ func (r *ReconcileRabbitmq) syncUsersCredentials(ctx context.Context, reqLogger

// user from RabbitMQ not found in secret resource, so add to remove list
if (!userFound) && (userRabbitName.Name != serviceAccount.username) {
usersToRemove = append(usersToRemove, userRabbitName.Name)
}
}

reqLogger.Info("Sync users started", "Add", usersToAdd, "Remove", usersToRemove)

reqLogger.Info("Removing users", "Users", usersToRemove)
for _, user := range usersToRemove {
if user == serviceAccount.username {
// do not remove service account, seriosly
continue
}
reqLogger.Info("Removing " + user)
err = r.apiUserRemove(reqLogger, cr, serviceAccount, rabbitmqUserStruct{Name:user})
if err != nil {
return err
reqLogger.Info("Removing " + userRabbitName.Name)
err = r.apiUserRemove(reqLogger, cr, serviceAccount, rabbitmqUserStruct{Name:userRabbitName.Name})
if err != nil {
return err
}
}
}

reqLogger.Info("Uploading users from secret")

// add new users to Rabbit
for _, user := range usersToAdd {
userPassword := usersSecret.Data[user]
userName := user
for userName, userPassword := range usersSecret.Data {
reqLogger.Info("Adding user " + userName + " Password " + string(userPassword))

err = r.apiUserAdd(reqLogger, cr, serviceAccount, rabbitmqUserStruct{Name: userName, Password: string(userPassword), Tags: "management"})


Loading…
Cancel
Save