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

feat: add failurepolicy #108

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .changelog/93.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
`Image` - Add new annotation `kimup.cloudavenue.io/failure-policy` to control the failure policy of the image tag mutation. The default value is `Fail`. The supported values are `Fail` and `Ignore`.
```
4 changes: 2 additions & 2 deletions cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ func main() {
BindAddress: "0", // metrics are served by common metrics server
},
HealthProbeBindAddress: func() string {
if flag.Lookup(models.MetricsFlagName).Value.String() == "true" {
if flag.Lookup(models.HealthzFlagName).Value.String() == "true" {
return httpserver.HealthzPort
}

return "0" // disable healthz server
}(),
LivenessEndpointName: func() string {
if flag.Lookup(models.MetricsFlagName).Value.String() == "true" {
if flag.Lookup(models.HealthzFlagName).Value.String() == "true" {
return httpserver.HealthzPath
}

Expand Down
80 changes: 80 additions & 0 deletions docs/advanced/failurepolicy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
hide:
- toc
---

# Failure Policy

## Overview

Kimup operator allows you to manage the behavior of the operator when it fails to apply the tag on a pod. The failure policy is defined by the annotation `kimup.cloudavenue.io/failure-policy` on the **namespace** or the **pod**. The failure policy can be set to `fail` or `ignore`. The default value is `fail`.

!!! warning
The annotation `kimup.cloudavenue.io/enabled` must be set to `true` on the namespace or the pod to apply the failure policy. If the annotation is not set, the failure policy will be ignored. See [Scope](../getting-started/scope.md) for more information.

## Logical

![Logical pod creation schema](../getting-started/logical-pod-creation-light.png#only-light)
![Logical pod creation schema](../getting-started/logical-pod-creation-dark.png#only-dark)

## Apply the failure policy

When the annotation `kimup.cloudavenue.io/failure-policy: "fail"` is set on a namespace, the operator will fail if it can't apply the tag on a pod created in this namespace.

```yaml
apiVersion: v1
kind: Namespace
metadata:
name: your-env
annotations:
kimup.cloudavenue.io/enabled: "true"
kimup.cloudavenue.io/failure-policy: "fail"
```

When the annotation `kimup.cloudavenue.io/failure-policy: "ignore"` is set on a namespace, the operator will ignore the failure if it can't apply the tag on a pod created in this namespace.

```yaml
apiVersion: v1
kind: Namespace
metadata:
name: your-env
annotations:
kimup.cloudavenue.io/enabled: "true"
kimup.cloudavenue.io/failure-policy: "ignore"
```

For a pod, the same logic applies.

```yaml
apiVersion: v1
kind: Pod
metadata:
name: your-pod
namespace: your-env
annotations:
kimup.cloudavenue.io/enabled: "true"
kimup.cloudavenue.io/failure-policy: "fail"
```

## Override the failure policy for a pod

When the annotation `kimup.cloudavenue.io/failure-policy` is set on a namespace, the operator will apply the failure policy on all pods created in this namespace. If the annotation is set on a pod, the operator will apply the failure policy defined on the pod, but i will be necessary to set the annotation `kimup.cloudavenue.io/enabled` to `true` on the pod.

```yaml
apiVersion: v1
kind: Namespace
metadata:
name: your-env
annotations:
kimup.cloudavenue.io/enabled: "true"
kimup.cloudavenue.io/failure-policy: "fail"
---
apiVersion: v1
kind: Pod
metadata:
name: your-pod
namespace: your-env
annotations:
kimup.cloudavenue.io/enabled: "true"
kimup.cloudavenue.io/failure-policy: "ignore"
```
Binary file modified docs/getting-started/logical-pod-creation-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/getting-started/logical-pod-creation-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions internal/annotations/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package annotations

import "strings"

// * Action

type (
Action struct {
aChan aChan
value string
}

AActionKey string
)

const (
// Action Refresh
ActionRefresh AActionKey = "refresh"

// Action Reload
ActionReload AActionKey = "reload"

// Action Delete
ActionDelete AActionKey = "delete"
)

func (a *Annotation) Action() (ac *Action) {
ac = &Action{
aChan: make(aChan),
}

if v, ok := a.annotations[string(KeyAction)]; ok {
ac.value = v
}

go func() {
for {
select {
case x := <-ac.aChan:
a.annotations[string(x.key)] = x.value
case <-a.ctx.Done():
return
}
}
}()

return ac
}

func (a *Action) Is(action AActionKey) bool {
return strings.EqualFold(a.value, string(action))
}

func (a *Action) IsNull() bool {
return a.value == ""
}

func (a *Action) Get() AActionKey {
return AActionKey(a.value)
}

func (a *Action) Set(action AActionKey) {
a.aChan.Send(KeyAction, string(action))
}
Loading
Loading