Skip to content

Commit

Permalink
configmap: cleanup
Browse files Browse the repository at this point in the history
remove unused code.

Signed-off-by: Talor Itzhak <[email protected]>
  • Loading branch information
Tal-or committed Mar 10, 2025
1 parent d0afcdc commit 89b9277
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 58 deletions.
23 changes: 1 addition & 22 deletions pkg/hash/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,19 @@
package hash

import (
"context"
"crypto/sha256"
"fmt"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// hash package purpose is to compute a ConfigMap hash
// that will be attached as an annotation to workload resources (DaemonSet, Deployment, etc.)
// in order to allow them to track ConfigMap changes
// to allow them to track ConfigMap changes
// more about this technique here: https://blog.questionable.services/article/kubernetes-deployments-configmap-change/

const ConfigMapAnnotation = "configmap.hash"

func ComputeCurrentConfigMap(ctx context.Context, cli client.Reader, cm *corev1.ConfigMap) (string, error) {
updatedConfigMap := &corev1.ConfigMap{}
key := client.ObjectKeyFromObject(cm)

if err := cli.Get(ctx, key, updatedConfigMap); err != nil {
// ConfigMap not created yet, use the data from the manifests
if apierrors.IsNotFound(err) {
updatedConfigMap = cm
} else {
return "", fmt.Errorf("could not calculate ConfigMap %q hash: %w", key.String(), err)
}
}
cmHash := ConfigMapData(updatedConfigMap)
klog.InfoS("configmap hash calculated", "hash", cmHash)
return cmHash, nil
}

func ConfigMapData(cm *corev1.ConfigMap) string {
var dataAsString string

Expand Down
36 changes: 0 additions & 36 deletions pkg/hash/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,14 @@
package hash

import (
"context"
"regexp"
"strings"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/openshift-kni/numaresources-operator/internal/objects"
)

func TestComputeCurrentConfigMap(t *testing.T) {
testCases := []struct {
name string
cli client.Client
cm *corev1.ConfigMap
expectedOut string
expectedErr bool
}{
{
name: "map not created",
cli: fake.NewFakeClient(),
cm: objects.NewKubeletConfigConfigMap("test", map[string]string{}, objects.NewKubeletConfigAutoresizeControlPlane()),
// verified manually
expectedOut: "SHA256:93909e569a15b6e4a5eefdcac4153f2c8179bf155143d10dac589f62ddcdf742",
expectedErr: false,
},
}

for _, tc := range testCases {
out, err := ComputeCurrentConfigMap(context.TODO(), tc.cli, tc.cm.DeepCopy())
gotErr := (err != nil)
if gotErr != tc.expectedErr {
t.Fatalf("got error %v expected error=%v", err, tc.expectedErr)
}
if out != tc.expectedOut {
t.Fatalf("got output %q expected %q", out, tc.expectedOut)
}
}
}

func TestConfigMapData(t *testing.T) {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Expand Down

0 comments on commit 89b9277

Please sign in to comment.