Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove orphaned network policy resource #49

Merged
merged 7 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Also check this project's [releases](https://github.com/powerhome/redis-operator

## Unreleased

### Fixed

- [In version 2.0.1, the approach to generating network policy by the operator was modified. From v2.0.1 onwards, the operator no longer creates network policy for redis but continues to do it for sentinels. This fix automatically removes any leftover network policy from the namespace, eliminating the need for manual intervention](https://github.com/powerhome/redis-operator/pull/49)

### Changed

- Add default haproxy image #47
Expand Down
14 changes: 14 additions & 0 deletions mocks/operator/redisfailover/service/RedisFailoverClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions operator/redisfailover/ensurer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func (w *RedisFailoverHandler) Ensure(rf *redisfailoverv1.RedisFailover, labels
}
}

if err := w.rfService.DestroyRemainedRedisNetworkPolicy(rf); err != nil {
return err
}

if rf.Spec.Haproxy != nil {
if err := w.rfService.EnsureHAProxyRedisMasterService(rf, labels, or); err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions operator/redisfailover/ensurer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ func TestEnsure(t *testing.T) {
mrfs.On("EnsureRedisReadinessConfigMap", rf, mock.Anything, mock.Anything).Once().Return(nil)
mrfs.On("EnsureRedisStatefulset", rf, mock.Anything, mock.Anything).Once().Return(nil)

mrfs.On("DestroyRemainedRedisNetworkPolicy", rf, mock.Anything, mock.Anything).Once().Return(nil)

// Create the Kops client and call the valid logic.
handler := rfOperator.NewRedisFailoverHandler(config, mrfs, mrfc, mrfh, mk, metrics.Dummy, log.Dummy)
err := handler.Ensure(rf, map[string]string{}, []metav1.OwnerReference{}, metrics.Dummy)
Expand Down
18 changes: 18 additions & 0 deletions operator/redisfailover/service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type RedisFailoverClient interface {

DestroySentinelResources(rFailover *redisfailoverv1.RedisFailover) error
UpdateStatus(rFailover *redisfailoverv1.RedisFailover) (*redisfailoverv1.RedisFailover, error)

DestroyRemainedRedisNetworkPolicy(rFailover *redisfailoverv1.RedisFailover) error
}

// RedisFailoverKubeClient implements the required methods to talk with kubernetes
Expand Down Expand Up @@ -212,6 +214,22 @@ func (r *RedisFailoverKubeClient) DestroySentinelResources(rf *redisfailoverv1.R
return err
}

// DestroyRemainedRedisNetworkPolicy remove remained network policy
rurkss marked this conversation as resolved.
Show resolved Hide resolved
func (r *RedisFailoverKubeClient) DestroyRemainedRedisNetworkPolicy(rf *redisfailoverv1.RedisFailover) error {
rurkss marked this conversation as resolved.
Show resolved Hide resolved

name := GetRedisNetworkPolicyName(rf)

if _, err := r.K8SService.GetNetworkPolicy(rf.Namespace, name); err != nil {
// If no resource, do nothing
rurkss marked this conversation as resolved.
Show resolved Hide resolved
if errors.IsNotFound(err) {
return nil
}
rurkss marked this conversation as resolved.
Show resolved Hide resolved
}

err := r.K8SService.DeleteNetworkPolicy(rf.Namespace, name)
return err
}

// EnsureRedisStatefulset makes sure the redis statefulset exists in the desired state
func (r *RedisFailoverKubeClient) EnsureRedisStatefulset(rf *redisfailoverv1.RedisFailover, labels map[string]string, ownerRefs []metav1.OwnerReference) error {
if !rf.Spec.Redis.DisablePodDisruptionBudget {
Expand Down
Loading