Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Refactor Marked for Termination feature #213

Merged
merged 2 commits into from
Mar 12, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Update to Operator SDK 0.15.1 ([#200](https://github.com/Dynatrace/dynatrace-oneagent-operator/pull/200))
* Initial work to ease release automation ([#198](https://github.com/Dynatrace/dynatrace-oneagent-operator/pull/198))
* Added automatic creation of CSV file for OLM ([#210](https://github.com/Dynatrace/dynatrace-oneagent-operator/pull/210))
* Now Marked for Termination events will be sent only for deleted Nodes ([#213](https://github.com/Dynatrace/dynatrace-oneagent-operator/pull/213))

## v0.6

Expand Down
10 changes: 9 additions & 1 deletion deploy/kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ rules:
- create
- update
- delete
- apiGroups:
- apps
resources:
- replicasets
- deployments
verbs:
- get
- list
- watch
- apiGroups:
- "" # "" indicates the core API group
resources:
Expand All @@ -115,7 +124,6 @@ rules:
- apiGroups:
- "" # "" indicates the core API group
resources:
- configmaps
- pods
verbs:
- get
Expand Down
10 changes: 9 additions & 1 deletion deploy/kubernetes/role-operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ rules:
- create
- update
- delete
- apiGroups:
- apps
resources:
- replicasets
- deployments
verbs:
- get
- list
- watch
- apiGroups:
- "" # "" indicates the core API group
resources:
Expand All @@ -41,7 +50,6 @@ rules:
- apiGroups:
- "" # "" indicates the core API group
resources:
- configmaps
- pods
verbs:
- get
Expand Down
9 changes: 9 additions & 0 deletions deploy/openshift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ rules:
- list
- watch
- delete
- apiGroups:
- apps
resources:
- replicasets
- deployments
verbs:
- get
- list
- watch
- apiGroups:
- "" # "" indicates the core API group
resources:
Expand Down
9 changes: 9 additions & 0 deletions deploy/openshift/role-operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ rules:
- list
- watch
- delete
- apiGroups:
- apps
resources:
- replicasets
- deployments
verbs:
- get
- list
- watch
- apiGroups:
- "" # "" indicates the core API group
resources:
Expand Down
72 changes: 72 additions & 0 deletions pkg/controller/nodes/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package nodes

import (
"encoding/json"
"errors"
"time"

corev1 "k8s.io/api/core/v1"
)

// ErrNotFound is returned when entry hasn't been found on the cache.
var ErrNotFound = errors.New("not found")

// CacheEntry constains information about a Node.
type CacheEntry struct {
Instance string `json:"instance"`
IPAddress string `json:"ip"`
LastSeen time.Time `json:"seen"`
}

// Cache manages information about Nodes.
type Cache struct {
Obj *corev1.ConfigMap
Create bool
upd bool
}

// Get returns the information about node, or error if not found or failed to unmarshall the data.
func (c *Cache) Get(node string) (CacheEntry, error) {
raw, ok := c.Obj.Data[node]
if !ok {
return CacheEntry{}, ErrNotFound
}

var out CacheEntry
if err := json.Unmarshal([]byte(raw), &out); err != nil {
return CacheEntry{}, err
}

return out, nil
}

// Set updates the information about node, or error if failed to marshall the data.
func (c *Cache) Set(node string, entry CacheEntry) error {
raw, err := json.Marshal(entry)
if err != nil {
return err
}
c.Obj.Data[node] = string(raw)
c.upd = true
return nil
}

// Delete removes the node from the cache.
func (c *Cache) Delete(node string) {
delete(c.Obj.Data, node)
c.upd = true
}

// Keys returns a list of node names on the cache.
func (c *Cache) Keys() []string {
out := make([]string, 0, len(c.Obj.Data))
for k := range c.Obj.Data {
out = append(out, k)
}
return out
}

// Changed returns true if changes have been made to the cache instance.
func (c *Cache) Changed() bool {
return c.Create || c.upd
}
Loading