From b0368b2aca924bfe82dbadf29af098194b55d142 Mon Sep 17 00:00:00 2001 From: gaby tal Date: Mon, 5 Sep 2022 22:05:59 +0300 Subject: [PATCH 1/6] timeframe --- api/v1alpha1/resourcemanager_types.go | 15 ++++-- api/v1alpha1/zz_generated.deepcopy.go | 36 ++++++++++++- ...anagement.tikalk.com_resourcemanagers.yaml | 7 +-- .../samples/resource-managmr-timeframe.yaml | 16 ++++++ controllers/handlers/namespace.go | 53 ++++++++++++++----- controllers/resourcemanager_controller.go | 14 ++--- controllers/utils/utils.go | 25 +++++++++ 7 files changed, 138 insertions(+), 28 deletions(-) create mode 100644 config/samples/resource-managmr-timeframe.yaml diff --git a/api/v1alpha1/resourcemanager_types.go b/api/v1alpha1/resourcemanager_types.go index e2736c3..d36ff51 100644 --- a/api/v1alpha1/resourcemanager_types.go +++ b/api/v1alpha1/resourcemanager_types.go @@ -38,7 +38,13 @@ type ResourceManagerSpec struct { // TODO: add validation + enum Action string `json:"action"` - Condition []ExpiryCondition `json:"condition"` + Condition []Conditions `json:"condition"` +} + +type Conditions struct { + ExpiryCondition + IntervalCondition + Condition } type Condition struct { @@ -50,9 +56,10 @@ type ExpiryCondition struct { After string `json:"after"` } -// type IntervalCondition struct { -// Condition -// } +type IntervalCondition struct { + Condition `json:",inline"` + Timeframe string `json:"hour"` +} // ResourceManagerStatus defines the observed state of ResourceManager type ResourceManagerStatus struct { diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 77a1abc..084921d 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -31,6 +31,24 @@ func (in *Condition) DeepCopy() *Condition { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Conditions) DeepCopyInto(out *Conditions) { + *out = *in + out.ExpiryCondition = in.ExpiryCondition + out.IntervalCondition = in.IntervalCondition + out.Condition = in.Condition +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Conditions. +func (in *Conditions) DeepCopy() *Conditions { + if in == nil { + return nil + } + out := new(Conditions) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ExpiryCondition) DeepCopyInto(out *ExpiryCondition) { *out = *in @@ -47,6 +65,22 @@ func (in *ExpiryCondition) DeepCopy() *ExpiryCondition { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *IntervalCondition) DeepCopyInto(out *IntervalCondition) { + *out = *in + out.Condition = in.Condition +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new IntervalCondition. +func (in *IntervalCondition) DeepCopy() *IntervalCondition { + if in == nil { + return nil + } + out := new(IntervalCondition) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ResourceManager) DeepCopyInto(out *ResourceManager) { *out = *in @@ -116,7 +150,7 @@ func (in *ResourceManagerSpec) DeepCopyInto(out *ResourceManagerSpec) { } if in.Condition != nil { in, out := &in.Condition, &out.Condition - *out = make([]ExpiryCondition, len(*in)) + *out = make([]Conditions, len(*in)) copy(*out, *in) } } diff --git a/config/crd/bases/resource-management.tikalk.com_resourcemanagers.yaml b/config/crd/bases/resource-management.tikalk.com_resourcemanagers.yaml index 0f07cb7..4881df9 100644 --- a/config/crd/bases/resource-management.tikalk.com_resourcemanagers.yaml +++ b/config/crd/bases/resource-management.tikalk.com_resourcemanagers.yaml @@ -43,12 +43,13 @@ spec: condition: items: properties: - after: - type: string type: type: string + hour: + type: string + after: + type: string required: - - after - type type: object type: array diff --git a/config/samples/resource-managmr-timeframe.yaml b/config/samples/resource-managmr-timeframe.yaml new file mode 100644 index 0000000..a882be4 --- /dev/null +++ b/config/samples/resource-managmr-timeframe.yaml @@ -0,0 +1,16 @@ +apiVersion: resource-management.tikalk.com/v1alpha1 +kind: ResourceManager +metadata: + name: resource-manager-sample +spec: + active: true + dry-run: false + resources: "namespace" + selector: + matchLabels: + name: managed-namespace1 + action: "delete" + condition: + - type: timeframe + hour: "17:28" + diff --git a/controllers/handlers/namespace.go b/controllers/handlers/namespace.go index da9d55f..b4b789d 100644 --- a/controllers/handlers/namespace.go +++ b/controllers/handlers/namespace.go @@ -8,14 +8,13 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) -// keep everything in a struct - // HandleNamespaceObj handle namespace objects that related to the resource-manager controller func (o Obj) HandleNamespaceObj() { // get all the namespaces with the desired selector labels namespaces, err := GetNamespacesByLabel(o) if err != nil { o.L.Error(err, fmt.Sprintf("%s: cannot list namespaces\n", o.Name)) + return } if len(namespaces) <= 0 { @@ -26,21 +25,42 @@ func (o Obj) HandleNamespaceObj() { fmt.Printf("found %d namespaces with the requested label\n", len(namespaces)) for _, namespace := range namespaces { - expired, secondsUntilExpire := utils.IsObjExpired(namespace.CreationTimestamp, o.Spec.Condition[0].After) - if expired { - switch o.Spec.Action { - case "delete": - fmt.Printf("namespace '%s' has been expired and will be deleted \n", namespace.Name) - err := o.C.Delete(o.Ctx, namespace.DeepCopy(), &client.DeleteOptions{}) - if err != nil { - o.L.Error(err, fmt.Sprintf("cannot delete namespaces\n")) + switch o.Spec.Condition[0].Type { + case "expiry": + expired, secondsUntilExpire := utils.IsObjExpired(namespace.CreationTimestamp, o.Spec.Condition[0].After) + if expired { + switch o.Spec.Action { + case "delete": + fmt.Printf("namespace '%s' has been expired and will be deleted \n", namespace.Name) + o.deleteNamespace(namespace) + fmt.Printf("%s: namespace '%s' has been deleted \n", o.Name, namespace.Name) } - fmt.Printf("%s: namespace '%s' has been deleted \n", o.Name, namespace.Name) + } else { + fmt.Printf("%s: %d seconds has left to namespace '%s' \n", o.Name, secondsUntilExpire, namespace.Name) + } + + case "timeframe": + fmt.Printf("'%s' will be deleted at timeframe: %s \n", o.Name, o.Spec.Condition[0].Timeframe) + err, doesIntervalOccurred := utils.IsIntervalOccurred(o.Spec.Condition[0].Timeframe) + if err != nil { + o.L.Error(err, fmt.Sprintf("cannot calculate timeframe\n")) + } + if doesIntervalOccurred { + switch o.Spec.Action { + case "delete": + fmt.Printf("namespace '%s' is in timeframe and will be deleted \n", namespace.Name) + err := o.C.Delete(o.Ctx, namespace.DeepCopy(), &client.DeleteOptions{}) + if err != nil { + o.L.Error(err, fmt.Sprintf("cannot delete namespaces\n")) + } + fmt.Printf("%s: namespace '%s' has been deleted \n", o.Name, namespace.Name) + + } + } - } else { - fmt.Printf("%s: %d seconds has left to namespace '%s' \n", o.Name, secondsUntilExpire, namespace.Name) } + } } @@ -62,3 +82,10 @@ func GetNamespacesByLabel(o Obj) ([]v1.Namespace, error) { } return listOfNamespaces, nil } + +func (o Obj) deleteNamespace(namespace v1.Namespace) { + err := o.C.Delete(o.Ctx, namespace.DeepCopy(), &client.DeleteOptions{}) + if err != nil { + o.L.Error(err, fmt.Sprintf("cannot delete namespaces\n")) + } +} diff --git a/controllers/resourcemanager_controller.go b/controllers/resourcemanager_controller.go index 8b832f7..b04c2a6 100644 --- a/controllers/resourcemanager_controller.go +++ b/controllers/resourcemanager_controller.go @@ -106,6 +106,8 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ h.Spec.Condition[0].After, h.Spec.Condition[0].Type)) + l.Info("I'm on collection deletion block") + // check if resource exists in our collection, if so, delete if _, ok := collection[h.Name]; ok { l.Info(fmt.Sprintf("Stopping loop for %s\n", h.Name)) @@ -116,6 +118,8 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ switch h.Spec.Resources { case "namespace": + l.Info("I'm on NAMESPACE block") + // add the function and its stop-channel to collection collection[h.Name] = FHandler{ F: func(stop chan bool) { @@ -125,6 +129,8 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ l.Info(fmt.Sprintf("%s Got stop signal!\n", h.Name)) return default: + l.Info("I'm on Default block") + h.HandleNamespaceObj() time.Sleep(5 * time.Second) } @@ -137,15 +143,9 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ c := collection[h.Name] // execute in a new thread - go c.F(c.Stop) + } - // - //deploy := &appsv1.DeploymentList{} - //err = r.Client.List(ctx, deploy, &client.ListOptions{}) - //fmt.Printf("There are %d deployments in the cluster\n", len(deploy.Items)) - // - //l.Info(fmt.Sprintf("Done reconcile 12-- obj %s", name)) return ctrl.Result{}, nil } diff --git a/controllers/utils/utils.go b/controllers/utils/utils.go index e5887cc..290e9e2 100644 --- a/controllers/utils/utils.go +++ b/controllers/utils/utils.go @@ -1,6 +1,7 @@ package utils import ( + "fmt" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "time" ) @@ -16,3 +17,27 @@ func IsObjExpired(creation v1.Time, expiration string) (bool, int) { secondsUntilExp := objExpiredAt.Sub(now).Seconds() return secondsUntilExp <= 0, int(secondsUntilExp) } + +func IsIntervalOccurred(timeframe string) (error, bool) { + + // current hour in 15:04 format + currentHour := time.Now().Format("15:04") + + // parse timeframe to real time object + timeframeTime, err := time.Parse("15:04", timeframe) + // current hour in the same time format + nowTime, err := time.Parse("15:04", currentHour) + if err != nil { + fmt.Println("Could not parse time:", err) + return err, false + } + fmt.Println(fmt.Sprintf("The time now is: %s", nowTime)) + fmt.Println(fmt.Sprintf("The timeframe is: %s", timeframeTime)) + secondsUntilInterval := timeframeTime.Sub(nowTime).Seconds() + fmt.Println(fmt.Sprintf("secondsUntilInterval: %d", int(secondsUntilInterval))) + if secondsUntilInterval <= 0 { + fmt.Println(fmt.Sprintf("timeframe triggerd: '%s'", timeframeTime)) + return nil, true + } + return nil, false +} From 2af3a47af24008ddc17d6c5ee55f179533383191 Mon Sep 17 00:00:00 2001 From: gaby tal Date: Thu, 8 Sep 2022 09:16:17 +0300 Subject: [PATCH 2/6] add timeframe + only one function handler for all resource cases --- api/v1alpha1/resourcemanager_types.go | 23 ++--- api/v1alpha1/zz_generated.deepcopy.go | 56 ++++++------ ...anagement.tikalk.com_resourcemanagers.yaml | 7 +- ...ce-managment_v1alpha1_resourcemanager.yaml | 5 -- .../samples/resource-managmr-timeframe.yaml | 3 +- controllers/handlers/namespace.go | 85 ++++++++++--------- controllers/resourcemanager_controller.go | 48 +++++------ controllers/utils/utils.go | 22 ++--- 8 files changed, 122 insertions(+), 127 deletions(-) diff --git a/api/v1alpha1/resourcemanager_types.go b/api/v1alpha1/resourcemanager_types.go index d36ff51..4732aa5 100644 --- a/api/v1alpha1/resourcemanager_types.go +++ b/api/v1alpha1/resourcemanager_types.go @@ -38,27 +38,28 @@ type ResourceManagerSpec struct { // TODO: add validation + enum Action string `json:"action"` - Condition []Conditions `json:"condition"` + Condition []Condition `json:"condition"` } -type Conditions struct { - ExpiryCondition - IntervalCondition - Condition +type Condition struct { + ExpiryCondition `json:",inline,omitempty"` + TimeframeCondition `json:",inline,omitempty"` + BaseCondition `json:",inline"` } -type Condition struct { +type BaseCondition struct { Type string `json:"type"` } type ExpiryCondition struct { - Condition `json:",inline"` - After string `json:"after"` + BaseCondition `json:",inline"` + After string `json:"after,omitempty"` } -type IntervalCondition struct { - Condition `json:",inline"` - Timeframe string `json:"hour"` +type TimeframeCondition struct { + BaseCondition `json:",inline"` + Timeframe string `json:"time, omitempty"` + //Timezone string `json:"timeZone"` } // ResourceManagerStatus defines the observed state of ResourceManager diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 084921d..bc9d3ae 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -17,34 +17,34 @@ import ( ) // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Condition) DeepCopyInto(out *Condition) { +func (in *BaseCondition) DeepCopyInto(out *BaseCondition) { *out = *in } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Condition. -func (in *Condition) DeepCopy() *Condition { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new BaseCondition. +func (in *BaseCondition) DeepCopy() *BaseCondition { if in == nil { return nil } - out := new(Condition) + out := new(BaseCondition) in.DeepCopyInto(out) return out } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *Conditions) DeepCopyInto(out *Conditions) { +func (in *Condition) DeepCopyInto(out *Condition) { *out = *in out.ExpiryCondition = in.ExpiryCondition - out.IntervalCondition = in.IntervalCondition - out.Condition = in.Condition + out.TimeframeCondition = in.TimeframeCondition + out.BaseCondition = in.BaseCondition } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Conditions. -func (in *Conditions) DeepCopy() *Conditions { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Condition. +func (in *Condition) DeepCopy() *Condition { if in == nil { return nil } - out := new(Conditions) + out := new(Condition) in.DeepCopyInto(out) return out } @@ -52,7 +52,7 @@ func (in *Conditions) DeepCopy() *Conditions { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ExpiryCondition) DeepCopyInto(out *ExpiryCondition) { *out = *in - out.Condition = in.Condition + out.BaseCondition = in.BaseCondition } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ExpiryCondition. @@ -65,22 +65,6 @@ func (in *ExpiryCondition) DeepCopy() *ExpiryCondition { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *IntervalCondition) DeepCopyInto(out *IntervalCondition) { - *out = *in - out.Condition = in.Condition -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new IntervalCondition. -func (in *IntervalCondition) DeepCopy() *IntervalCondition { - if in == nil { - return nil - } - out := new(IntervalCondition) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ResourceManager) DeepCopyInto(out *ResourceManager) { *out = *in @@ -150,7 +134,7 @@ func (in *ResourceManagerSpec) DeepCopyInto(out *ResourceManagerSpec) { } if in.Condition != nil { in, out := &in.Condition, &out.Condition - *out = make([]Conditions, len(*in)) + *out = make([]Condition, len(*in)) copy(*out, *in) } } @@ -179,3 +163,19 @@ func (in *ResourceManagerStatus) DeepCopy() *ResourceManagerStatus { in.DeepCopyInto(out) return out } + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TimeframeCondition) DeepCopyInto(out *TimeframeCondition) { + *out = *in + out.BaseCondition = in.BaseCondition +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TimeframeCondition. +func (in *TimeframeCondition) DeepCopy() *TimeframeCondition { + if in == nil { + return nil + } + out := new(TimeframeCondition) + in.DeepCopyInto(out) + return out +} diff --git a/config/crd/bases/resource-management.tikalk.com_resourcemanagers.yaml b/config/crd/bases/resource-management.tikalk.com_resourcemanagers.yaml index 4881df9..e67becf 100644 --- a/config/crd/bases/resource-management.tikalk.com_resourcemanagers.yaml +++ b/config/crd/bases/resource-management.tikalk.com_resourcemanagers.yaml @@ -43,13 +43,14 @@ spec: condition: items: properties: - type: + after: type: string - hour: + time: type: string - after: + type: type: string required: + - time - type type: object type: array diff --git a/config/samples/resource-managment_v1alpha1_resourcemanager.yaml b/config/samples/resource-managment_v1alpha1_resourcemanager.yaml index b0abcf4..781eba1 100644 --- a/config/samples/resource-managment_v1alpha1_resourcemanager.yaml +++ b/config/samples/resource-managment_v1alpha1_resourcemanager.yaml @@ -13,8 +13,3 @@ spec: condition: - type: expiry after: "1m" - # OR - # - type: time - # interval: daily - # time: "20:59:59" -#status: diff --git a/config/samples/resource-managmr-timeframe.yaml b/config/samples/resource-managmr-timeframe.yaml index a882be4..da384fd 100644 --- a/config/samples/resource-managmr-timeframe.yaml +++ b/config/samples/resource-managmr-timeframe.yaml @@ -12,5 +12,6 @@ spec: action: "delete" condition: - type: timeframe - hour: "17:28" + time: "11:45" +# timeZone: "Asia/Jerusalem" diff --git a/controllers/handlers/namespace.go b/controllers/handlers/namespace.go index b4b789d..704e03f 100644 --- a/controllers/handlers/namespace.go +++ b/controllers/handlers/namespace.go @@ -2,70 +2,41 @@ package handlers import ( "fmt" - utils "github.com/tikalk/resource-manager/controllers/utils" + "github.com/tikalk/resource-manager/controllers/utils" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/labels" "sigs.k8s.io/controller-runtime/pkg/client" + "time" ) // HandleNamespaceObj handle namespace objects that related to the resource-manager controller func (o Obj) HandleNamespaceObj() { // get all the namespaces with the desired selector labels - namespaces, err := GetNamespacesByLabel(o) + namespacesToHandle, err := o.GetNamespacesByLabel() if err != nil { o.L.Error(err, fmt.Sprintf("%s: cannot list namespaces\n", o.Name)) return } - if len(namespaces) <= 0 { - fmt.Printf("%s: did not found any namespaces with the requested label\n", o.Name) + if len(namespacesToHandle) <= 0 { + fmt.Printf("%s: did not found any namespace with the requested label\n", o.Name) return } - fmt.Printf("found %d namespaces with the requested label\n", len(namespaces)) - - for _, namespace := range namespaces { + for _, ns := range namespacesToHandle { switch o.Spec.Condition[0].Type { case "expiry": - expired, secondsUntilExpire := utils.IsObjExpired(namespace.CreationTimestamp, o.Spec.Condition[0].After) - if expired { - switch o.Spec.Action { - case "delete": - fmt.Printf("namespace '%s' has been expired and will be deleted \n", namespace.Name) - o.deleteNamespace(namespace) - fmt.Printf("%s: namespace '%s' has been deleted \n", o.Name, namespace.Name) - } - } else { - fmt.Printf("%s: %d seconds has left to namespace '%s' \n", o.Name, secondsUntilExpire, namespace.Name) - } - + o.handleExpiry(ns) case "timeframe": - fmt.Printf("'%s' will be deleted at timeframe: %s \n", o.Name, o.Spec.Condition[0].Timeframe) - err, doesIntervalOccurred := utils.IsIntervalOccurred(o.Spec.Condition[0].Timeframe) - if err != nil { - o.L.Error(err, fmt.Sprintf("cannot calculate timeframe\n")) - } - if doesIntervalOccurred { - switch o.Spec.Action { - case "delete": - fmt.Printf("namespace '%s' is in timeframe and will be deleted \n", namespace.Name) - err := o.C.Delete(o.Ctx, namespace.DeepCopy(), &client.DeleteOptions{}) - if err != nil { - o.L.Error(err, fmt.Sprintf("cannot delete namespaces\n")) - } - fmt.Printf("%s: namespace '%s' has been deleted \n", o.Name, namespace.Name) - - } - - } - + o.handleTimeframe(ns) } } + time.Sleep(5 * time.Second) } // GetNamespacesByLabel get only namespaces that contains a specific label -func GetNamespacesByLabel(o Obj) ([]v1.Namespace, error) { +func (o Obj) GetNamespacesByLabel() ([]v1.Namespace, error) { var listOfNamespaces []v1.Namespace nsListObj := &v1.NamespaceList{} @@ -83,9 +54,45 @@ func GetNamespacesByLabel(o Obj) ([]v1.Namespace, error) { return listOfNamespaces, nil } +// deleteNamespace delete namespace obj func (o Obj) deleteNamespace(namespace v1.Namespace) { err := o.C.Delete(o.Ctx, namespace.DeepCopy(), &client.DeleteOptions{}) if err != nil { o.L.Error(err, fmt.Sprintf("cannot delete namespaces\n")) } + time.Sleep(5 * time.Second) + fmt.Printf("%s: namespace '%s' has been deleted \n", o.Name, namespace.Name) + +} + +// handleTimeframe handle timeframe type +func (o Obj) handleTimeframe(namespace v1.Namespace) { + fmt.Printf("namespace '%s' will be deleted at timeframe: %s \n", namespace.Name, o.Spec.Condition[0].Timeframe) + err, doesIntervalOccurred := utils.IsIntervalOccurred(o.Spec.Condition[0].Timeframe) + if err != nil { + o.L.Error(err, fmt.Sprintf("cannot calculate timeframe\n")) + return + } + if doesIntervalOccurred { + switch o.Spec.Action { + case "delete": + fmt.Printf("namespace '%s' is in timeframe and will be deleted \n", namespace.Name) + o.deleteNamespace(namespace) + } + } +} + +// handleExpiry handle expiry type +func (o Obj) handleExpiry(namespace v1.Namespace) { + expired, secondsUntilExpire := utils.IsObjExpired(namespace.CreationTimestamp, o.Spec.Condition[0].After) + if expired { + switch o.Spec.Action { + case "delete": + fmt.Printf("namespace '%s' has been expired and will be deleted \n", namespace.Name) + o.deleteNamespace(namespace) + fmt.Printf("%s: namespace '%s' has been deleted \n", o.Name, namespace.Name) + } + } else { + fmt.Printf("%s: %d seconds has left to namespace '%s' \n", o.Name, secondsUntilExpire, namespace.Name) + } } diff --git a/controllers/resourcemanager_controller.go b/controllers/resourcemanager_controller.go index b04c2a6..85ee349 100644 --- a/controllers/resourcemanager_controller.go +++ b/controllers/resourcemanager_controller.go @@ -19,8 +19,6 @@ package controllers import ( "context" "fmt" - "time" - "github.com/tikalk/resource-manager/api/v1alpha1" "github.com/tikalk/resource-manager/controllers/handlers" "k8s.io/apimachinery/pkg/api/errors" @@ -106,8 +104,6 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ h.Spec.Condition[0].After, h.Spec.Condition[0].Type)) - l.Info("I'm on collection deletion block") - // check if resource exists in our collection, if so, delete if _, ok := collection[h.Name]; ok { l.Info(fmt.Sprintf("Stopping loop for %s\n", h.Name)) @@ -116,36 +112,30 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ delete(collection, h.Name) } - switch h.Spec.Resources { - case "namespace": - l.Info("I'm on NAMESPACE block") - - // add the function and its stop-channel to collection - collection[h.Name] = FHandler{ - F: func(stop chan bool) { - for { - select { - case <-stop: - l.Info(fmt.Sprintf("%s Got stop signal!\n", h.Name)) - return - default: - l.Info("I'm on Default block") - + // create new collection + collection[h.Name] = FHandler{ + F: func(stop chan bool) { + for { + select { + case <-stop: + l.Info(fmt.Sprintf("%s Got stop signal!\n", h.Name)) + return + default: + switch h.Spec.Resources { // here we decide which handler to use + case "namespace": h.HandleNamespaceObj() - time.Sleep(5 * time.Second) } } - }, - Stop: make(chan bool), - } - - // export to new var - c := collection[h.Name] + } + }, + Stop: make(chan bool), + } - // execute in a new thread - go c.F(c.Stop) + // export to new var + c := collection[h.Name] - } + // execute in a new thread + go c.F(c.Stop) return ctrl.Result{}, nil } diff --git a/controllers/utils/utils.go b/controllers/utils/utils.go index 290e9e2..badea5e 100644 --- a/controllers/utils/utils.go +++ b/controllers/utils/utils.go @@ -20,22 +20,22 @@ func IsObjExpired(creation v1.Time, expiration string) (bool, int) { func IsIntervalOccurred(timeframe string) (error, bool) { - // current hour in 15:04 format - currentHour := time.Now().Format("15:04") - - // parse timeframe to real time object + // parse timeframe to time object timeframeTime, err := time.Parse("15:04", timeframe) - // current hour in the same time format - nowTime, err := time.Parse("15:04", currentHour) + if err != nil { + fmt.Println("Could not parse timeframe:", err) + return err, false + } + + // current hour in the same time format "15:04" + nowTime, err := time.Parse("15:04", time.Now().Format("15:04")) if err != nil { fmt.Println("Could not parse time:", err) return err, false } - fmt.Println(fmt.Sprintf("The time now is: %s", nowTime)) - fmt.Println(fmt.Sprintf("The timeframe is: %s", timeframeTime)) - secondsUntilInterval := timeframeTime.Sub(nowTime).Seconds() - fmt.Println(fmt.Sprintf("secondsUntilInterval: %d", int(secondsUntilInterval))) - if secondsUntilInterval <= 0 { + + secondsUntilTimeframe := timeframeTime.Sub(nowTime).Seconds() + if secondsUntilTimeframe <= 0 && secondsUntilTimeframe > -60 { fmt.Println(fmt.Sprintf("timeframe triggerd: '%s'", timeframeTime)) return nil, true } From 46fe1c107b46b7fbdd8b24dcb37bdf5aaa10853c Mon Sep 17 00:00:00 2001 From: Daniel Rozner Date: Wed, 12 Oct 2022 13:04:38 +0300 Subject: [PATCH 3/6] refactor to the go way --- controllers/handlers/namespace.go | 33 +++++++-------- controllers/handlers/types.go | 49 +++++++++++++++++++++-- controllers/resourcemanager_controller.go | 32 +++++++-------- 3 files changed, 77 insertions(+), 37 deletions(-) diff --git a/controllers/handlers/namespace.go b/controllers/handlers/namespace.go index 704e03f..66bf254 100644 --- a/controllers/handlers/namespace.go +++ b/controllers/handlers/namespace.go @@ -2,19 +2,20 @@ package handlers import ( "fmt" + "time" + "github.com/tikalk/resource-manager/controllers/utils" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/labels" "sigs.k8s.io/controller-runtime/pkg/client" - "time" ) // HandleNamespaceObj handle namespace objects that related to the resource-manager controller -func (o Obj) HandleNamespaceObj() { +func (o *Obj) HandleNamespaceObj() { // get all the namespaces with the desired selector labels namespacesToHandle, err := o.GetNamespacesByLabel() if err != nil { - o.L.Error(err, fmt.Sprintf("%s: cannot list namespaces\n", o.Name)) + o.l.Error(err, fmt.Sprintf("%s: cannot list namespaces\n", o.Name)) return } @@ -24,7 +25,7 @@ func (o Obj) HandleNamespaceObj() { } for _, ns := range namespacesToHandle { - switch o.Spec.Condition[0].Type { + switch o.rm.Spec.Condition[0].Type { case "expiry": o.handleExpiry(ns) case "timeframe": @@ -36,15 +37,15 @@ func (o Obj) HandleNamespaceObj() { } // GetNamespacesByLabel get only namespaces that contains a specific label -func (o Obj) GetNamespacesByLabel() ([]v1.Namespace, error) { +func (o *Obj) GetNamespacesByLabel() ([]v1.Namespace, error) { var listOfNamespaces []v1.Namespace nsListObj := &v1.NamespaceList{} - if err := o.C.List(o.Ctx, nsListObj, &client.ListOptions{ - LabelSelector: labels.SelectorFromSet(o.Spec.Selector.MatchLabels), + if err := o.c.List(o.ctx, nsListObj, &client.ListOptions{ + LabelSelector: labels.SelectorFromSet(o.rm.Spec.Selector.MatchLabels), }); err != nil { - o.L.Error(err, fmt.Sprintf("%s: unable to fetch namespaces", o.Name)) + o.l.Error(err, fmt.Sprintf("%s: unable to fetch namespaces", o.Name)) return nil, err } @@ -55,10 +56,10 @@ func (o Obj) GetNamespacesByLabel() ([]v1.Namespace, error) { } // deleteNamespace delete namespace obj -func (o Obj) deleteNamespace(namespace v1.Namespace) { - err := o.C.Delete(o.Ctx, namespace.DeepCopy(), &client.DeleteOptions{}) +func (o *Obj) deleteNamespace(namespace v1.Namespace) { + err := o.c.Delete(o.ctx, namespace.DeepCopy(), &client.DeleteOptions{}) if err != nil { - o.L.Error(err, fmt.Sprintf("cannot delete namespaces\n")) + o.l.Error(err, fmt.Sprintf("cannot delete namespaces\n")) } time.Sleep(5 * time.Second) fmt.Printf("%s: namespace '%s' has been deleted \n", o.Name, namespace.Name) @@ -67,14 +68,14 @@ func (o Obj) deleteNamespace(namespace v1.Namespace) { // handleTimeframe handle timeframe type func (o Obj) handleTimeframe(namespace v1.Namespace) { - fmt.Printf("namespace '%s' will be deleted at timeframe: %s \n", namespace.Name, o.Spec.Condition[0].Timeframe) - err, doesIntervalOccurred := utils.IsIntervalOccurred(o.Spec.Condition[0].Timeframe) + fmt.Printf("namespace '%s' will be deleted at timeframe: %s \n", namespace.Name, o.rm.Spec.Condition[0].Timeframe) + err, doesIntervalOccurred := utils.IsIntervalOccurred(o.rm.Spec.Condition[0].Timeframe) if err != nil { - o.L.Error(err, fmt.Sprintf("cannot calculate timeframe\n")) + o.l.Error(err, fmt.Sprintf("cannot calculate timeframe\n")) return } if doesIntervalOccurred { - switch o.Spec.Action { + switch o.rm.Spec.Action { case "delete": fmt.Printf("namespace '%s' is in timeframe and will be deleted \n", namespace.Name) o.deleteNamespace(namespace) @@ -86,7 +87,7 @@ func (o Obj) handleTimeframe(namespace v1.Namespace) { func (o Obj) handleExpiry(namespace v1.Namespace) { expired, secondsUntilExpire := utils.IsObjExpired(namespace.CreationTimestamp, o.Spec.Condition[0].After) if expired { - switch o.Spec.Action { + switch o.rm.Spec.Action { case "delete": fmt.Printf("namespace '%s' has been expired and will be deleted \n", namespace.Name) o.deleteNamespace(namespace) diff --git a/controllers/handlers/types.go b/controllers/handlers/types.go index ed68303..6d719f9 100644 --- a/controllers/handlers/types.go +++ b/controllers/handlers/types.go @@ -2,6 +2,8 @@ package handlers import ( "context" + "fmt" + "github.com/go-logr/logr" resourcemanagmentv1alpha1 "github.com/tikalk/resource-manager/api/v1alpha1" "k8s.io/apimachinery/pkg/types" @@ -12,8 +14,47 @@ import ( type Obj struct { Name types.NamespacedName - C client.Client - Ctx context.Context - L logr.Logger - Spec resourcemanagmentv1alpha1.ResourceManagerSpec + c client.Client + ctx context.Context + l logr.Logger + rm resourcemanagmentv1alpha1.ResourceManager + + stop chan bool +} + +func InitObj(rm resourcemanagmentv1alpha1.ResourceManager, c client.Client, ctx context.Context, l logr.Logger) *Obj { + stop := make(chan bool) + + name := types.NamespacedName{ + Name: rm.Name, + Namespace: rm.Namespace, + } + + o := &Obj{ + Name: name, + c: c, + ctx: ctx, + l: l, + stop: stop, + } + return o +} + +func (o *Obj) Stop() { + o.stop <- true +} + +func (o *Obj) Run() { + for { + select { + case <-o.stop: + o.l.Info(fmt.Sprintf("%s Got stop signal!\n", h.Name)) + return + default: + switch o.rm.Spec.Resources { // here we decide which handler to use + case "namespace": + o.HandleNamespaceObj() + } + } + } } diff --git a/controllers/resourcemanager_controller.go b/controllers/resourcemanager_controller.go index 85ee349..d568f31 100644 --- a/controllers/resourcemanager_controller.go +++ b/controllers/resourcemanager_controller.go @@ -19,8 +19,10 @@ package controllers import ( "context" "fmt" + "github.com/tikalk/resource-manager/api/v1alpha1" "github.com/tikalk/resource-manager/controllers/handlers" + "github.com/tikalk/resource-manager/controllers/new_handlers" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" @@ -32,7 +34,8 @@ import ( // ResourceManagerReconciler reconciles a ResourceManager object type ResourceManagerReconciler struct { client.Client - Scheme *runtime.Scheme + Scheme *runtime.Scheme + collection map[types.NamespacedName]FHandler } //+kubebuilder:rbac:groups=resource-management.tikalk.com,resources=resourcemanagers,verbs=get;list;watch;create;update;patch;delete @@ -52,13 +55,6 @@ type ResourceManagerReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.10.0/pkg/reconcile -var collection map[types.NamespacedName]FHandler - -type FHandler struct { - F func(stop chan bool) - Stop chan bool -} - func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { l := log.FromContext(ctx) @@ -70,10 +66,10 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ if err != nil { if errors.IsNotFound(err) { l.Info(fmt.Sprintf("ResourceManager object %s has Not Found!!! \n", req.NamespacedName)) - collection[req.NamespacedName].Stop <- true + r.collection[req.NamespacedName].Stop <- true // delete the key from collection map - delete(collection, req.NamespacedName) + delete(r.collection, req.NamespacedName) return ctrl.Result{}, nil } @@ -105,15 +101,17 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ h.Spec.Condition[0].Type)) // check if resource exists in our collection, if so, delete - if _, ok := collection[h.Name]; ok { + if _, ok := r.collection[h.Name]; ok { l.Info(fmt.Sprintf("Stopping loop for %s\n", h.Name)) - collection[h.Name].Stop <- true + r.collection[h.Name].Stop <- true // delete the key from collection map - delete(collection, h.Name) + delete(r.collection, h.Name) } // create new collection - collection[h.Name] = FHandler{ + r.collection[h.Name] = new_handlers.InitFHandler() + + r.collection[h.Name] = &new_handlers.FHandler{ F: func(stop chan bool) { for { select { @@ -128,11 +126,11 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ } } }, - Stop: make(chan bool), + stop: make(chan bool), } // export to new var - c := collection[h.Name] + c := r.collection[h.Name] // execute in a new thread go c.F(c.Stop) @@ -142,7 +140,7 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ // SetupWithManager sets up the controller with the Manager. func (r *ResourceManagerReconciler) SetupWithManager(mgr ctrl.Manager) error { - collection = make(map[types.NamespacedName]FHandler) + r.collection = make(map[types.NamespacedName]FHandler) return ctrl.NewControllerManagedBy(mgr). For(&v1alpha1.ResourceManager{}). Complete(r) From 5db3ca847133753c7c7428eeba7da7c217e2f8d6 Mon Sep 17 00:00:00 2001 From: gaby tal Date: Wed, 12 Oct 2022 14:38:00 +0300 Subject: [PATCH 4/6] fixing stuff --- controllers/handlers/namespace.go | 8 +-- controllers/handlers/types.go | 10 ++-- controllers/resourcemanager_controller.go | 70 +++++------------------ 3 files changed, 23 insertions(+), 65 deletions(-) diff --git a/controllers/handlers/namespace.go b/controllers/handlers/namespace.go index 66bf254..f6ccbe3 100644 --- a/controllers/handlers/namespace.go +++ b/controllers/handlers/namespace.go @@ -11,7 +11,7 @@ import ( ) // HandleNamespaceObj handle namespace objects that related to the resource-manager controller -func (o *Obj) HandleNamespaceObj() { +func (o Obj) HandleNamespaceObj() { // get all the namespaces with the desired selector labels namespacesToHandle, err := o.GetNamespacesByLabel() if err != nil { @@ -37,7 +37,7 @@ func (o *Obj) HandleNamespaceObj() { } // GetNamespacesByLabel get only namespaces that contains a specific label -func (o *Obj) GetNamespacesByLabel() ([]v1.Namespace, error) { +func (o Obj) GetNamespacesByLabel() ([]v1.Namespace, error) { var listOfNamespaces []v1.Namespace nsListObj := &v1.NamespaceList{} @@ -56,7 +56,7 @@ func (o *Obj) GetNamespacesByLabel() ([]v1.Namespace, error) { } // deleteNamespace delete namespace obj -func (o *Obj) deleteNamespace(namespace v1.Namespace) { +func (o Obj) deleteNamespace(namespace v1.Namespace) { err := o.c.Delete(o.ctx, namespace.DeepCopy(), &client.DeleteOptions{}) if err != nil { o.l.Error(err, fmt.Sprintf("cannot delete namespaces\n")) @@ -85,7 +85,7 @@ func (o Obj) handleTimeframe(namespace v1.Namespace) { // handleExpiry handle expiry type func (o Obj) handleExpiry(namespace v1.Namespace) { - expired, secondsUntilExpire := utils.IsObjExpired(namespace.CreationTimestamp, o.Spec.Condition[0].After) + expired, secondsUntilExpire := utils.IsObjExpired(namespace.CreationTimestamp, o.rm.Spec.Condition[0].After) if expired { switch o.rm.Spec.Action { case "delete": diff --git a/controllers/handlers/types.go b/controllers/handlers/types.go index 6d719f9..4a6f5a9 100644 --- a/controllers/handlers/types.go +++ b/controllers/handlers/types.go @@ -22,7 +22,7 @@ type Obj struct { stop chan bool } -func InitObj(rm resourcemanagmentv1alpha1.ResourceManager, c client.Client, ctx context.Context, l logr.Logger) *Obj { +func InitObj(rm resourcemanagmentv1alpha1.ResourceManager, c client.Client, ctx context.Context, l logr.Logger) Obj { stop := make(chan bool) name := types.NamespacedName{ @@ -30,7 +30,7 @@ func InitObj(rm resourcemanagmentv1alpha1.ResourceManager, c client.Client, ctx Namespace: rm.Namespace, } - o := &Obj{ + o := Obj{ Name: name, c: c, ctx: ctx, @@ -40,15 +40,15 @@ func InitObj(rm resourcemanagmentv1alpha1.ResourceManager, c client.Client, ctx return o } -func (o *Obj) Stop() { +func (o Obj) Stop() { o.stop <- true } -func (o *Obj) Run() { +func (o Obj) Run() { for { select { case <-o.stop: - o.l.Info(fmt.Sprintf("%s Got stop signal!\n", h.Name)) + o.l.Info(fmt.Sprintf("%s Got stop signal!\n", o.Name)) return default: switch o.rm.Spec.Resources { // here we decide which handler to use diff --git a/controllers/resourcemanager_controller.go b/controllers/resourcemanager_controller.go index d568f31..67f1eb8 100644 --- a/controllers/resourcemanager_controller.go +++ b/controllers/resourcemanager_controller.go @@ -22,7 +22,6 @@ import ( "github.com/tikalk/resource-manager/api/v1alpha1" "github.com/tikalk/resource-manager/controllers/handlers" - "github.com/tikalk/resource-manager/controllers/new_handlers" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" @@ -35,7 +34,7 @@ import ( type ResourceManagerReconciler struct { client.Client Scheme *runtime.Scheme - collection map[types.NamespacedName]FHandler + collection map[types.NamespacedName]handlers.Obj } //+kubebuilder:rbac:groups=resource-management.tikalk.com,resources=resourcemanagers,verbs=get;list;watch;create;update;patch;delete @@ -66,7 +65,7 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ if err != nil { if errors.IsNotFound(err) { l.Info(fmt.Sprintf("ResourceManager object %s has Not Found!!! \n", req.NamespacedName)) - r.collection[req.NamespacedName].Stop <- true + //r.collection[req.NamespacedName].Stop <- true // delete the key from collection map delete(r.collection, req.NamespacedName) @@ -79,68 +78,27 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ fmt.Printf("found ResourceManager object: %s \n", resourceManagerObj.Name) // config handler object - h := handlers.Obj{ - Name: req.NamespacedName, - C: r.Client, - Ctx: ctx, - L: l, - Spec: resourceManagerObj.Spec, - } - - l.Info(fmt.Sprintf( - "\n"+ - " ResourceType: %s \n"+ - " selectorLables %s \n"+ - " action: %s \n"+ - " condition: %s \n"+ - " type: %s \n", - h.Spec.Resources, - h.Spec.Selector.MatchLabels, - h.Spec.Action, - h.Spec.Condition[0].After, - h.Spec.Condition[0].Type)) - - // check if resource exists in our collection, if so, delete - if _, ok := r.collection[h.Name]; ok { - l.Info(fmt.Sprintf("Stopping loop for %s\n", h.Name)) - r.collection[h.Name].Stop <- true - // delete the key from collection map - delete(r.collection, h.Name) - } + h := handlers.InitObj(*resourceManagerObj, r.Client, ctx, l) - // create new collection - r.collection[h.Name] = new_handlers.InitFHandler() - - r.collection[h.Name] = &new_handlers.FHandler{ - F: func(stop chan bool) { - for { - select { - case <-stop: - l.Info(fmt.Sprintf("%s Got stop signal!\n", h.Name)) - return - default: - switch h.Spec.Resources { // here we decide which handler to use - case "namespace": - h.HandleNamespaceObj() - } - } - } - }, - stop: make(chan bool), - } + //// check if resource exists in our collection, if so, delete + //if _, ok := r.collection[h.Name]; ok { + // l.Info(fmt.Sprintf("Stopping loop for %s\n", h.Name)) + // r.collection[h.Name] + // // delete the key from collection map + // delete(r.collection, h.Name) + //} - // export to new var - c := r.collection[h.Name] + // add handler to collection + r.collection[req.NamespacedName] = h - // execute in a new thread - go c.F(c.Stop) + r.collection[req.NamespacedName].Run() return ctrl.Result{}, nil } // SetupWithManager sets up the controller with the Manager. func (r *ResourceManagerReconciler) SetupWithManager(mgr ctrl.Manager) error { - r.collection = make(map[types.NamespacedName]FHandler) + r.collection = make(map[types.NamespacedName]handlers.Obj) return ctrl.NewControllerManagedBy(mgr). For(&v1alpha1.ResourceManager{}). Complete(r) From 43496c37a43f88a68787896f3d322c83a8e3ad18 Mon Sep 17 00:00:00 2001 From: gaby tal Date: Wed, 12 Oct 2022 15:05:41 +0300 Subject: [PATCH 5/6] fixing stuff --- ...-management.tikalk.com_resourcemanagers.yaml | 1 - controllers/handlers/namespace.go | 2 +- controllers/handlers/types.go | 2 ++ controllers/resourcemanager_controller.go | 17 ++++++++--------- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/config/crd/bases/resource-management.tikalk.com_resourcemanagers.yaml b/config/crd/bases/resource-management.tikalk.com_resourcemanagers.yaml index e67becf..06444cc 100644 --- a/config/crd/bases/resource-management.tikalk.com_resourcemanagers.yaml +++ b/config/crd/bases/resource-management.tikalk.com_resourcemanagers.yaml @@ -50,7 +50,6 @@ spec: type: type: string required: - - time - type type: object type: array diff --git a/controllers/handlers/namespace.go b/controllers/handlers/namespace.go index f6ccbe3..2535a17 100644 --- a/controllers/handlers/namespace.go +++ b/controllers/handlers/namespace.go @@ -33,7 +33,7 @@ func (o Obj) HandleNamespaceObj() { } } - time.Sleep(5 * time.Second) + time.Sleep(10 * time.Second) } // GetNamespacesByLabel get only namespaces that contains a specific label diff --git a/controllers/handlers/types.go b/controllers/handlers/types.go index 4a6f5a9..8fabc9d 100644 --- a/controllers/handlers/types.go +++ b/controllers/handlers/types.go @@ -31,6 +31,7 @@ func InitObj(rm resourcemanagmentv1alpha1.ResourceManager, c client.Client, ctx } o := Obj{ + rm: rm, Name: name, c: c, ctx: ctx, @@ -45,6 +46,7 @@ func (o Obj) Stop() { } func (o Obj) Run() { + fmt.Printf("Processing object: %s \n", o.Name) for { select { case <-o.stop: diff --git a/controllers/resourcemanager_controller.go b/controllers/resourcemanager_controller.go index 67f1eb8..2db12df 100644 --- a/controllers/resourcemanager_controller.go +++ b/controllers/resourcemanager_controller.go @@ -65,7 +65,7 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ if err != nil { if errors.IsNotFound(err) { l.Info(fmt.Sprintf("ResourceManager object %s has Not Found!!! \n", req.NamespacedName)) - //r.collection[req.NamespacedName].Stop <- true + r.collection[req.NamespacedName].Stop() // delete the key from collection map delete(r.collection, req.NamespacedName) @@ -80,18 +80,17 @@ func (r *ResourceManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ // config handler object h := handlers.InitObj(*resourceManagerObj, r.Client, ctx, l) - //// check if resource exists in our collection, if so, delete - //if _, ok := r.collection[h.Name]; ok { - // l.Info(fmt.Sprintf("Stopping loop for %s\n", h.Name)) - // r.collection[h.Name] - // // delete the key from collection map - // delete(r.collection, h.Name) - //} + // check if resource exists in our collection, if so, delete + if _, ok := r.collection[h.Name]; ok { + l.Info(fmt.Sprintf("Stopping loop for %s\n", h.Name)) + // delete the key from collection map + delete(r.collection, h.Name) + } // add handler to collection r.collection[req.NamespacedName] = h - r.collection[req.NamespacedName].Run() + go r.collection[req.NamespacedName].Run() return ctrl.Result{}, nil } From 847ef2563beb99e62d3e300ee04f7d663a674691 Mon Sep 17 00:00:00 2001 From: gaby tal Date: Wed, 12 Oct 2022 15:21:20 +0300 Subject: [PATCH 6/6] fixing stuff --- config/samples/resource-managmr-timeframe.yaml | 2 +- controllers/handlers/namespace.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/config/samples/resource-managmr-timeframe.yaml b/config/samples/resource-managmr-timeframe.yaml index da384fd..492d3ff 100644 --- a/config/samples/resource-managmr-timeframe.yaml +++ b/config/samples/resource-managmr-timeframe.yaml @@ -12,6 +12,6 @@ spec: action: "delete" condition: - type: timeframe - time: "11:45" + time: "12:20" # timeZone: "Asia/Jerusalem" diff --git a/controllers/handlers/namespace.go b/controllers/handlers/namespace.go index 2535a17..a2fe29b 100644 --- a/controllers/handlers/namespace.go +++ b/controllers/handlers/namespace.go @@ -21,6 +21,7 @@ func (o Obj) HandleNamespaceObj() { if len(namespacesToHandle) <= 0 { fmt.Printf("%s: did not found any namespace with the requested label\n", o.Name) + time.Sleep(10 * time.Second) return }