Skip to content

Commit

Permalink
Update Clowder to reconcile on changes to non-app secrets/configmaps
Browse files Browse the repository at this point in the history
* Add hashCache to the Environment reconciler for usage later
* Remove all Secrets/ConfigMaps from Environment at start of reconciliation cycle
* Embue HashObject with the always flag
* Change updateHashCacheForConfigMapAndSecret to try to read in the sec/config and return true if always set
* Slight concern of erroring if not in the cache, but
* Make app_interface add the secret to the cache at the start of reconciliation so it should be there, and also link the objects.
  • Loading branch information
psav committed Jan 13, 2025
1 parent 1ba6e2c commit cac006b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func (r *ClowdEnvironmentReconciler) Reconcile(ctx context.Context, req ctrl.Req
env: &env,
log: &log,
oldStatus: env.Status.DeepCopy(),
hashCache: r.HashCache,
}

result, resErr := reconciliation.Reconcile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

crd "github.com/RedHatInsights/clowder/apis/cloud.redhat.com/v1alpha1"
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/clowderconfig"
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/hashcache"
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers"
rc "github.com/RedHatInsights/rhc-osdk-utils/resourceCache"
"github.com/go-logr/logr"
Expand Down Expand Up @@ -57,6 +58,7 @@ type ClowdEnvironmentReconciliation struct {
env *crd.ClowdEnvironment
log *logr.Logger
oldStatus *crd.ClowdEnvironmentStatus
hashCache *hashcache.HashCache
}

// Returns a list of step methods that should be run during reconciliation
Expand Down Expand Up @@ -285,6 +287,9 @@ func (r *ClowdEnvironmentReconciliation) isTargetNamespaceMarkedForDeletion() (c
}

func (r *ClowdEnvironmentReconciliation) runProviders() (ctrl.Result, error) {

r.hashCache.RemoveClowdObjectFromObjects(r.env)

provider := providers.Provider{
Ctx: r.ctx,
Client: r.client,
Expand Down
10 changes: 9 additions & 1 deletion controllers/cloud.redhat.com/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,15 @@ func (e *enqueueRequestForObjectCustom) updateHashCacheForConfigMapAndSecret(obj
switch obj.(type) {
case *core.ConfigMap, *core.Secret:
if obj.GetAnnotations()[clowderconfig.LoadedConfig.Settings.RestarterAnnotationName] == "true" {
return e.hashCache.CreateOrUpdateObject(obj)
return e.hashCache.CreateOrUpdateObject(obj, false)
} else {
hcOjb, err := e.hashCache.Read(obj)
if err != nil {
return false, err
}
if hcOjb.Always {
return e.hashCache.CreateOrUpdateObject(obj, false)
}
}
}
return false, nil
Expand Down
17 changes: 10 additions & 7 deletions controllers/cloud.redhat.com/hashcache/hashcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type HashObject struct {
Hash string
ClowdApps map[types.NamespacedName]bool
ClowdEnvs map[types.NamespacedName]bool
Always bool
}

type HashCache struct {
Expand All @@ -46,11 +47,12 @@ func NewHashCache() HashCache {
}
}

func NewHashObject(hash string) HashObject {
func NewHashObject(hash string, always bool) HashObject {
return HashObject{
Hash: hash,
ClowdApps: map[types.NamespacedName]bool{},
ClowdEnvs: map[types.NamespacedName]bool{},
Always: always,
}
}

Expand Down Expand Up @@ -101,7 +103,7 @@ func (hc *HashCache) RemoveClowdObjectFromObjects(obj client.Object) {
}
}

func (hc *HashCache) CreateOrUpdateObject(obj client.Object) (bool, error) {
func (hc *HashCache) CreateOrUpdateObject(obj client.Object, always bool) (bool, error) {
hc.lock.Lock()
defer hc.lock.Unlock()

Expand Down Expand Up @@ -129,7 +131,7 @@ func (hc *HashCache) CreateOrUpdateObject(obj client.Object) (bool, error) {
hashObject, ok := hc.data[id]

if !ok {
hashObj := NewHashObject(hash)
hashObj := NewHashObject(hash, always)
hc.data[id] = &hashObj
return true, nil
}
Expand Down Expand Up @@ -178,10 +180,6 @@ func (hc *HashCache) GetSuperHashForClowdObject(clowdObj object.ClowdObject) str

func (hc *HashCache) AddClowdObjectToObject(clowdObj object.ClowdObject, obj client.Object) error {

if obj.GetAnnotations()[clowderconfig.LoadedConfig.Settings.RestarterAnnotationName] != "true" {
return nil
}

var oType string

switch obj.(type) {
Expand All @@ -198,6 +196,11 @@ func (hc *HashCache) AddClowdObjectToObject(clowdObj object.ClowdObject, obj cli
if !ok {
return ItemNotFoundError{item: fmt.Sprintf("%s/%s", id.NN.Name, id.NN.Namespace)}
}

if obj.GetAnnotations()[clowderconfig.LoadedConfig.Settings.RestarterAnnotationName] != "true" && !hc.data[id].Always {
return nil
}

hc.lock.Lock()
defer hc.lock.Unlock()

Expand Down
16 changes: 8 additions & 8 deletions controllers/cloud.redhat.com/hashcache/hashcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestHashCacheAddItemAndRetrieve(t *testing.T) {
}

hc := NewHashCache()
update, err := hc.CreateOrUpdateObject(sec)
update, err := hc.CreateOrUpdateObject(sec, false)
assert.NoError(t, err)
assert.True(t, update)
obj, err := hc.Read(sec)
Expand All @@ -39,7 +39,7 @@ func TestHashCacheDeleteItem(t *testing.T) {
}

hc := NewHashCache()
shouldUpdate, err := hc.CreateOrUpdateObject(sec)
shouldUpdate, err := hc.CreateOrUpdateObject(sec, false)
assert.True(t, shouldUpdate)
assert.NoError(t, err)
obj, err := hc.Read(sec)
Expand All @@ -63,7 +63,7 @@ func TestHashCacheUpdateItem(t *testing.T) {
}

hc := NewHashCache()
_, err := hc.CreateOrUpdateObject(sec)
_, err := hc.CreateOrUpdateObject(sec, false)
assert.NoError(t, err)

obj, err := hc.Read(sec)
Expand All @@ -75,7 +75,7 @@ func TestHashCacheUpdateItem(t *testing.T) {
"test2": []byte("test2"),
}

update, err := hc.CreateOrUpdateObject(sec)
update, err := hc.CreateOrUpdateObject(sec, false)
assert.NoError(t, err)
assert.True(t, update)
obj, err = hc.Read(sec)
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestHashCacheAddClowdObj(t *testing.T) {
}

hc := NewHashCache()
_, err := hc.CreateOrUpdateObject(sec)
_, err := hc.CreateOrUpdateObject(sec, false)
assert.NoError(t, err)

err = hc.AddClowdObjectToObject(capp, sec)
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestHashCacheDeleteClowdObj(t *testing.T) {
}

hc := NewHashCache()
_, err := hc.CreateOrUpdateObject(sec)
_, err := hc.CreateOrUpdateObject(sec, false)
assert.NoError(t, err)

err = hc.AddClowdObjectToObject(capp, sec)
Expand Down Expand Up @@ -196,15 +196,15 @@ func TestHashCacheSuperCache(t *testing.T) {
}

hc := NewHashCache()
_, err := hc.CreateOrUpdateObject(sec)
_, err := hc.CreateOrUpdateObject(sec, false)
assert.NoError(t, err)
err = hc.AddClowdObjectToObject(capp, sec)
assert.NoError(t, err)
obj, err := hc.Read(sec)
assert.NoError(t, err)
assert.Contains(t, obj.ClowdApps, clowdObjNamespaceName)

_, err = hc.CreateOrUpdateObject(sec2)
_, err = hc.CreateOrUpdateObject(sec2, false)
assert.NoError(t, err)
err = hc.AddClowdObjectToObject(capp, sec2)
assert.NoError(t, err)
Expand Down
3 changes: 3 additions & 0 deletions controllers/cloud.redhat.com/providers/kafka/appinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func (a *appInterface) setKafkaCA(broker *config.BrokerConfig) error {
return err
}

a.HashCache.CreateOrUpdateObject(&kafkaCASecret, true)

Check failure on line 54 in controllers/cloud.redhat.com/providers/kafka/appinterface.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `a.HashCache.CreateOrUpdateObject` is not checked (errcheck)
a.HashCache.AddClowdObjectToObject(a.Env, &kafkaCASecret)

Check failure on line 55 in controllers/cloud.redhat.com/providers/kafka/appinterface.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `a.HashCache.AddClowdObjectToObject` is not checked (errcheck)

broker.Cacert = utils.StringPtr(string(kafkaCASecret.Data["ca.crt"]))
broker.Port = utils.IntPtr(9093)
broker.SecurityProtocol = utils.StringPtr("SSL")
Expand Down

0 comments on commit cac006b

Please sign in to comment.