Skip to content

Commit 356afd0

Browse files
committed
changes to the duration in which the operator is run
1 parent 40bd0c0 commit 356afd0

File tree

9 files changed

+35
-60
lines changed

9 files changed

+35
-60
lines changed

.vscode/launch.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
{
2-
// Use IntelliSense to learn about possible attributes.
3-
// Hover to view descriptions of existing attributes.
4-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
52
"version": "0.2.0",
63
"configurations": [
74
{

PROJECT

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ multigroup: true
99
projectName: controller
1010
repo: github.com/AnaisUrlichs/security-controller
1111
resources:
12-
- controller: true
13-
group: core
14-
kind: Pod
15-
path: k8s.io/api/core/v1
16-
version: v1
1712
- api:
1813
crdVersion: v1
1914
namespaced: true

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ More information can be found via the [Kubebuilder Documentation](https://book.k
133133
3. Build and push your image to the location specified by `IMG`:
134134

135135
```sh
136-
make docker-build docker-push IMG=<some-registry>/controller:tag
136+
make docker-buildx docker-push
137137
```
138138

139139
## License

apis/api/v1alpha1/configuration_types.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,8 @@ import (
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2222
)
2323

24-
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
25-
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
26-
27-
// ConfigurationSpec defines the desired state of Configuration
24+
// ConfigurationSpec defines the desired state of the Misconfiguration to be applied to deployments
2825
type ConfigurationSpec struct {
29-
// INSERT ADDITIONAL SPEC FIELDS - desired st ate of cluster
30-
// Important: Run "make" to regenerate code after modifying this file
3126

3227
// Set Container Imagetag
3328
ImageTag string `json:"imageTag,omitempty"`

config/samples/api_v1alpha1_configuration.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ spec:
1010
requests: "300m"
1111
runAsNonRoot: false
1212
memoryrequests: "65Mi"
13-
memorylimits: "130Mi"
13+
memorylimits: "130Mi"

config/samples/deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
name: nginx-deployment
55
namespace: demo
66
annotations:
7-
anaisurl.com/misconfiguration: "false"
7+
anaisurl.com/misconfiguration: "true"
88
labels:
99
app: nginx
1010
spec:

controllers/api/configuration_controller.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"k8s.io/apimachinery/pkg/types"
2828
ctrl "sigs.k8s.io/controller-runtime"
2929
"sigs.k8s.io/controller-runtime/pkg/client"
30-
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3130
"sigs.k8s.io/controller-runtime/pkg/log"
3231

3332
apiv1alpha1 "github.com/AnaisUrlichs/security-controller/apis/api/v1alpha1"
@@ -53,12 +52,9 @@ const (
5352
// +kubebuilder:rbac:groups=api.core.anaisurl.com,resources=configurations/finalizers,verbs=update
5453
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
5554
// +kubebuilder:rbac:groups=apps,resources=deployments/status,verbs=get;list;watch;create;update;patch;delete
55+
//
5656
// Reconcile is part of the main kubernetes reconciliation loop which aims to
5757
// move the current state of the cluster closer to the desired state.
58-
// TODO(user): Modify the Reconcile function to compare the state specified by
59-
// the Configuration object against the actual cluster state, and then
60-
// perform operations to make the cluster state reflect the state specified by
61-
// the user.
6258
//
6359
// For more details, check Reconcile and its Result here:
6460
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
@@ -68,7 +64,6 @@ func (r *ConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
6864
log.Info("Reconciling deployments")
6965

7066
mdConf := &apiv1alpha1.Configuration{}
71-
mdConfFinalizer := mdConf.GetFinalizers()
7267

7368
if err := r.Client.Get(ctx, req.NamespacedName, mdConf); err != nil {
7469
if errors.IsNotFound(err) {
@@ -80,25 +75,7 @@ func (r *ConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
8075
return r.finishReconcile(err, false)
8176
}
8277

83-
if mdConf.ObjectMeta.DeletionTimestamp.IsZero() {
84-
// The object is not being deleted, so if it does not have our finalizer,
85-
// then lets add the finalizer and update the object. This is equivalent
86-
// registering our finalizer.
87-
if mdConfFinalizer == nil {
88-
controllerutil.AddFinalizer(mdConf, annotationName)
89-
if err := r.Update(ctx, mdConf); err != nil {
90-
return r.finishReconcile(err, false)
91-
}
92-
}
93-
94-
} else {
95-
// The object is being deleted
96-
if mdConfFinalizer != nil {
97-
controllerutil.RemoveFinalizer(mdConf, annotationName)
98-
if err := r.Update(ctx, mdConf); err != nil {
99-
return ctrl.Result{}, err
100-
}
101-
}
78+
if !mdConf.ObjectMeta.DeletionTimestamp.IsZero() {
10279
// Stop reconciliation as the item is being deleted
10380
return r.finishReconcile(nil, false)
10481
}
@@ -141,6 +118,7 @@ func (r *ConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
141118
cm.Spec.Template.Spec.Containers[0].Resources.Limits[kcore.ResourceCPU] = mdConf.Spec.CPULimits
142119
cm.Spec.Template.Spec.Containers[0].Resources.Requests[kcore.ResourceMemory] = mdConf.Spec.MemoryRequests
143120
cm.Spec.Template.Spec.Containers[0].Resources.Limits[kcore.ResourceMemory] = mdConf.Spec.MemoryLimits
121+
cm.Annotations["anaisurl.com/last-updated"] = time.Now().Format(time.RFC3339)
144122

145123
val := "false"
146124
cm.Annotations["anaisurl.com/misconfiguration"] = val

controllers/apps/deplyment_controller.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package apps
1515

1616
import (
1717
"context"
18+
"time"
1819

1920
kapps "k8s.io/api/apps/v1"
2021
"k8s.io/apimachinery/pkg/runtime"
@@ -29,16 +30,12 @@ type DeploymentReconciler struct {
2930
Scheme *runtime.Scheme
3031
}
3132

32-
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
33-
//+kubebuilder:rbac:groups=apps,resources=deployments/status,verbs=get;update;patch
34-
//+kubebuilder:rbac:groups=apps,resources=deployments/finalizers,verbs=update
35-
33+
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
34+
// +kubebuilder:rbac:groups=apps,resources=deployments/status,verbs=get;update;patch
35+
// +kubebuilder:rbac:groups=apps,resources=deployments/finalizers,verbs=update
36+
//
3637
// Reconcile is part of the main kubernetes reconciliation loop which aims to
3738
// move the current state of the cluster closer to the desired state.
38-
// TODO(user): Modify the Reconcile function to compare the state specified by
39-
// the Deployment object against the actual cluster state, and then
40-
// perform operations to make the cluster state reflect the state specified by
41-
// the user.
4239
//
4340
// For more details, check Reconcile and its Result here:
4441
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
@@ -54,17 +51,30 @@ func (r *DeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Request)
5451

5552
l.Info("Deployment", "name", deployment.Name, "namespace", deployment.Namespace, "annotations", deployment.Annotations)
5653

54+
lastUpdated, has := deployment.GetAnnotations()["anaisurl.com/last-updated"]
5755
val, ok := deployment.GetAnnotations()["anaisurl.com/misconfiguration"]
58-
if ok && val == "false" {
59-
val = "true"
6056

61-
// Update deployment
62-
deployment.SetAnnotations(map[string]string{"anaisurl.com/misconfiguration": val})
57+
// check if lastUpdated is more than 1 minutes
58+
if ok && val == "false" && has {
59+
60+
lastUpdatedTime, err := time.Parse(time.RFC3339, lastUpdated)
61+
62+
if time.Now().Sub(lastUpdatedTime) > 5*time.Minute {
63+
val = "true"
64+
// Update deployment
65+
deployment.SetAnnotations(map[string]string{"anaisurl.com/misconfiguration": val})
66+
deployment.Annotations["anaisurl.com/last-updated"] = time.Now().Format(time.RFC3339)
67+
68+
err := r.Client.Update(ctx, deployment)
69+
if err != nil {
70+
return ctrl.Result{}, err
71+
}
72+
}
6373

64-
err := r.Client.Update(ctx, deployment)
6574
if err != nil {
66-
return ctrl.Result{}, err
75+
return ctrl.Result{}, client.IgnoreNotFound(err)
6776
}
77+
6878
}
6979

7080
return ctrl.Result{}, nil

main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,18 @@ func main() {
8989
setupLog.Error(err, "unable to start manager")
9090
os.Exit(1)
9191
}
92-
if err = (&apicontrollers.ConfigurationReconciler{
92+
if err = (&appscontrollers.DeploymentReconciler{
9393
Client: mgr.GetClient(),
9494
Scheme: mgr.GetScheme(),
9595
}).SetupWithManager(mgr); err != nil {
96-
setupLog.Error(err, "unable to create controller", "controller", "Configuration")
96+
setupLog.Error(err, "unable to create controller", "controller", "Deployment")
9797
os.Exit(1)
9898
}
99-
if err = (&appscontrollers.DeploymentReconciler{
99+
if err = (&apicontrollers.ConfigurationReconciler{
100100
Client: mgr.GetClient(),
101101
Scheme: mgr.GetScheme(),
102102
}).SetupWithManager(mgr); err != nil {
103-
setupLog.Error(err, "unable to create controller", "controller", "Deployment")
103+
setupLog.Error(err, "unable to create controller", "controller", "Configuration")
104104
os.Exit(1)
105105
}
106106
//+kubebuilder:scaffold:builder

0 commit comments

Comments
 (0)