Skip to content

Commit

Permalink
POWERMON-251: ensure redfish config change restart kepler
Browse files Browse the repository at this point in the history
This commit modifies kepler reconciler to add the hash of redfish spec
to pod annotations so that any change to the redfish spec will redeploy
the pod.
This commit also fixes POWERMON-250 by removing the default redfish
config added to kepler configmap.

Signed-off-by: Sunil Thaha <sthaha@redhat.com>
  • Loading branch information
sthaha committed Mar 13, 2024
1 parent 33ac756 commit 71b2b7b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 26 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
toolchain go1.21.7

require (
github.com/cespare/xxhash v1.1.0
github.com/go-logr/logr v1.4.1
github.com/openshift/api v0.0.0-20240212125214-04ea3891d9cb
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.71.2
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -98,6 +102,8 @@ github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
4 changes: 3 additions & 1 deletion pkg/components/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const (
RedfishArgs = "-redfish-cred-file-path=/etc/redfish/redfish.csv"
RedfishCSV = "redfish.csv"
RedfishSecretAnnotation = "kepler.system.sustainable.computing.io/redfish-secret-ref"
RedfishConfigHash = "kepler.system.sustainable.computing.io/redfish-config-hash"
)

const (
Expand Down Expand Up @@ -140,7 +141,7 @@ func NewDaemonSet(detail components.Detail, k *v1alpha1.KeplerInternal) *appsv1.
}
}

func MountRedfishSecretToDaemonSet(ds *appsv1.DaemonSet, secret *corev1.Secret) {
func MountRedfishSecretToDaemonSet(ds *appsv1.DaemonSet, secret *corev1.Secret, hash uint64) {
spec := &ds.Spec.Template.Spec
keplerContainer := &spec.Containers[KeplerContainerIndex]
keplerContainer.Command = append(keplerContainer.Command, RedfishArgs)
Expand All @@ -154,6 +155,7 @@ func MountRedfishSecretToDaemonSet(ds *appsv1.DaemonSet, secret *corev1.Secret)
// forces pods to be reployed if the secret chanage
ds.Spec.Template.Annotations = map[string]string{
RedfishSecretAnnotation: secret.ResourceVersion,
RedfishConfigHash: fmt.Sprint(hash),
}
}

Expand Down
26 changes: 14 additions & 12 deletions pkg/components/exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ func TestDaemonSet(t *testing.T) {
},
},
annotation: map[string]string{
"kepler.system.sustainable.computing.io/redfish-secret-ref": "123",

RedfishSecretAnnotation: "123",
RedfishConfigHash: "1337",
},
scenario: "redfish case",
},
Expand All @@ -202,23 +204,23 @@ func TestDaemonSet(t *testing.T) {
}
ds := NewDaemonSet(components.Full, &k)
if tc.addRedfish {
MountRedfishSecretToDaemonSet(ds, tc.redfishSecret)
MountRedfishSecretToDaemonSet(ds, tc.redfishSecret, 1337)
}

actual_hostPID := k8s.HostPIDFromDS(ds)
assert.Equal(t, actual_hostPID, tc.hostPID)
actualHostPID := k8s.HostPIDFromDS(ds)
assert.Equal(t, tc.hostPID, actualHostPID)

actual_exporterCommand := k8s.CommandFromDS(ds, KeplerContainerIndex)
assert.Equal(t, actual_exporterCommand, tc.exporterCommand)
actualExporterCommand := k8s.CommandFromDS(ds, KeplerContainerIndex)
assert.Equal(t, tc.exporterCommand, actualExporterCommand)

actual_volumeMounts := k8s.VolumeMountsFromDS(ds, KeplerContainerIndex)
assert.Equal(t, actual_volumeMounts, tc.volumeMounts)
actualVolumeMounts := k8s.VolumeMountsFromDS(ds, KeplerContainerIndex)
assert.Equal(t, tc.volumeMounts, actualVolumeMounts)

actual_Volumes := k8s.VolumesFromDS(ds)
assert.Equal(t, actual_Volumes, tc.volumes)
actualVolumes := k8s.VolumesFromDS(ds)
assert.Equal(t, tc.volumes, actualVolumes)

actual_Annotation := k8s.AnnotationFromDS(ds)
assert.Equal(t, actual_Annotation, tc.annotation)
actualAnnotation := k8s.AnnotationFromDS(ds)
assert.Equal(t, tc.annotation, actualAnnotation)
})
}
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/controllers/kepler_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,19 +541,20 @@ func exporterReconcilers(ki *v1alpha1.KeplerInternal, cluster k8s.Cluster) []rec
exporter.NewServiceMonitor(ki),
exporter.NewPrometheusRule(ki),
)...)

if ki.Spec.Exporter.Redfish == nil {
rs = append(rs, resourceReconcilers(updateResource,
exporter.NewDaemonSet(components.Full, ki),
exporter.NewConfigMap(components.Full, ki),
)...)
} else {
rs = append(rs,
reconciler.KeplerDaemonSetReconciler{
Ki: *ki,
reconciler.KeplerReconciler{
Ki: ki,
Ds: exporter.NewDaemonSet(components.Full, ki),
},
reconciler.KeplerConfigMapReconciler{
Ki: *ki,
Ki: ki,
Cfm: exporter.NewConfigMap(components.Full, ki),
},
)
Expand Down
27 changes: 17 additions & 10 deletions pkg/reconciler/kepler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package reconciler

import (
"context"
"encoding/json"
"fmt"
"strconv"

"github.com/cespare/xxhash"
"github.com/sustainable.computing.io/kepler-operator/pkg/api/v1alpha1"
"github.com/sustainable.computing.io/kepler-operator/pkg/components/exporter"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -31,14 +33,19 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

type KeplerDaemonSetReconciler struct {
Ki v1alpha1.KeplerInternal
type KeplerReconciler struct {
Ki *v1alpha1.KeplerInternal
Ds *appsv1.DaemonSet
}

func (r KeplerDaemonSetReconciler) Reconcile(ctx context.Context, cli client.Client, s *runtime.Scheme) Result {
func (r KeplerReconciler) Reconcile(ctx context.Context, cli client.Client, s *runtime.Scheme) Result {
redfish := r.Ki.Spec.Exporter.Redfish
redfishBytes, err := json.Marshal(redfish)
if err != nil {
return Result{Action: Stop, Error: fmt.Errorf("Error occurred while marshaling Redfish spec %w", err)}
}

secretRef := r.Ki.Spec.Exporter.Redfish.SecretRef
secretRef := redfish.SecretRef
secret, err := r.getRedfishSecret(ctx, cli, secretRef)

if err != nil {
Expand All @@ -55,12 +62,12 @@ func (r KeplerDaemonSetReconciler) Reconcile(ctx context.Context, cli client.Cli
return Result{Action: Stop, Error: fmt.Errorf("Redfish secret is missing %q key", exporter.RedfishCSV)}
}

exporter.MountRedfishSecretToDaemonSet(r.Ds, secret)

return Updater{Owner: &r.Ki, Resource: r.Ds}.Reconcile(ctx, cli, s)
redfishHash := xxhash.Sum64(redfishBytes)
exporter.MountRedfishSecretToDaemonSet(r.Ds, secret, redfishHash)
return Updater{Owner: r.Ki, Resource: r.Ds}.Reconcile(ctx, cli, s)
}

func (r KeplerDaemonSetReconciler) getRedfishSecret(ctx context.Context, cli client.Client, secretName string) (*corev1.Secret, error) {
func (r KeplerReconciler) getRedfishSecret(ctx context.Context, cli client.Client, secretName string) (*corev1.Secret, error) {
ns := r.Ki.Spec.Exporter.Deployment.Namespace
redfishSecret := corev1.Secret{}
if err := cli.Get(ctx, types.NamespacedName{Namespace: ns, Name: secretName}, &redfishSecret); err != nil {
Expand All @@ -70,7 +77,7 @@ func (r KeplerDaemonSetReconciler) getRedfishSecret(ctx context.Context, cli cli
}

type KeplerConfigMapReconciler struct {
Ki v1alpha1.KeplerInternal
Ki *v1alpha1.KeplerInternal
Cfm *corev1.ConfigMap
}

Expand All @@ -81,5 +88,5 @@ func (r KeplerConfigMapReconciler) Reconcile(ctx context.Context, cli client.Cli
r.Cfm.Data["REDFISH_PROBE_INTERVAL_IN_SECONDS"] = fmt.Sprintf("%f", rf.ProbeInterval.Duration.Seconds())
}
r.Cfm.Data["REDFISH_SKIP_SSL_VERIFY"] = strconv.FormatBool(rf.SkipSSLVerify)
return Updater{Owner: &r.Ki, Resource: r.Cfm}.Reconcile(ctx, cli, s)
return Updater{Owner: r.Ki, Resource: r.Cfm}.Reconcile(ctx, cli, s)
}

0 comments on commit 71b2b7b

Please sign in to comment.