Skip to content

Commit cb7b381

Browse files
Merge pull request #16 from PDOK/jd/simulate-k8s-validation
Get configmap names
2 parents ceaecc2 + f82cffc commit cb7b381

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ toolchain go1.24.2
77
godebug default=go1.23
88

99
require (
10+
github.com/google/go-cmp v0.7.0
1011
github.com/onsi/ginkgo/v2 v2.22.1
1112
github.com/onsi/gomega v1.36.2
1213
github.com/pkg/errors v0.9.1
14+
k8s.io/api v0.32.3
1315
k8s.io/apiextensions-apiserver v0.32.3
1416
k8s.io/apimachinery v0.32.3
1517
k8s.io/apiserver v0.32.3
1618
k8s.io/client-go v0.32.3
17-
k8s.io/kube-openapi v0.0.0-20241212222426-2c72e554b1e7
1819
sigs.k8s.io/controller-runtime v0.20.4
1920
sigs.k8s.io/kustomize/api v0.19.0
2021
sigs.k8s.io/kustomize/kyaml v0.19.0
@@ -51,7 +52,6 @@ require (
5152
github.com/google/btree v1.1.3 // indirect
5253
github.com/google/cel-go v0.22.1 // indirect
5354
github.com/google/gnostic-models v0.6.9 // indirect
54-
github.com/google/go-cmp v0.7.0 // indirect
5555
github.com/google/gofuzz v1.2.0 // indirect
5656
github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad // indirect
5757
github.com/google/uuid v1.6.0 // indirect
@@ -100,9 +100,9 @@ require (
100100
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
101101
gopkg.in/inf.v0 v0.9.1 // indirect
102102
gopkg.in/yaml.v3 v3.0.1 // indirect
103-
k8s.io/api v0.32.3 // indirect
104103
k8s.io/component-base v0.32.3 // indirect
105104
k8s.io/klog/v2 v2.130.1 // indirect
105+
k8s.io/kube-openapi v0.0.0-20241212222426-2c72e554b1e7 // indirect
106106
k8s.io/utils v0.0.0-20241210054802-24370beab758 // indirect
107107
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.1 // indirect
108108
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect

pkg/k8s/clientset.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package k8s
2+
3+
import (
4+
"context"
5+
"errors"
6+
v1 "k8s.io/api/core/v1"
7+
"sigs.k8s.io/controller-runtime/pkg/client"
8+
"strings"
9+
)
10+
11+
func GetConfigMap(k8sClient client.Client, namespace, prefix string, labels map[string]string) (*v1.ConfigMap, error) {
12+
if !strings.HasSuffix(prefix, "-") {
13+
prefix += "-"
14+
}
15+
16+
configMapList := &v1.ConfigMapList{}
17+
err := k8sClient.List(context.TODO(), configMapList, client.InNamespace(namespace), client.MatchingLabels(labels))
18+
if err != nil {
19+
return nil, err
20+
}
21+
22+
for _, configMap := range configMapList.Items {
23+
// If the name contains "-" after the prefix, it means that the prefix is longer
24+
// Example 'blobs' and 'blobs-premium'
25+
if strings.HasPrefix(configMap.Name, prefix) && !strings.Contains(configMap.Name[len(prefix):], "-") {
26+
return &configMap, nil
27+
}
28+
}
29+
30+
return nil, errors.New("no configmap found with prefix " + prefix)
31+
}
32+
33+
func GetSecret(k8sClient client.Client, namespace, prefix string, labels map[string]string) (*v1.Secret, error) {
34+
if !strings.HasSuffix(prefix, "-") {
35+
prefix += "-"
36+
}
37+
38+
secretList := &v1.SecretList{}
39+
err := k8sClient.List(context.TODO(), secretList, client.InNamespace(namespace), client.MatchingLabels(labels))
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
for _, secret := range secretList.Items {
45+
// If the name contains "-" after the prefix, it means that the prefix is longer
46+
// Example 'blobs' and 'blobs-premium'
47+
if strings.HasPrefix(secret.Name, prefix) && !strings.Contains(secret.Name[len(prefix):], "-") {
48+
return &secret, nil
49+
}
50+
}
51+
52+
return nil, errors.New("no secret found with prefix " + prefix)
53+
}

pkg/k8s/diff.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package k8s
2+
3+
import (
4+
"context"
5+
"github.com/google/go-cmp/cmp"
6+
"sigs.k8s.io/controller-runtime/pkg/client"
7+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
8+
"sigs.k8s.io/controller-runtime/pkg/log"
9+
)
10+
11+
func ShowDiff(ctx context.Context, k8sClient client.Client, obj client.Object, f controllerutil.MutateFn) {
12+
lgr := log.FromContext(ctx)
13+
14+
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(obj), obj)
15+
if err != nil {
16+
// Object not found, not able to show diff
17+
return
18+
}
19+
20+
current := obj.DeepCopyObject().(client.Object)
21+
_ = f()
22+
23+
if diff := cmp.Diff(current, obj); diff != "" {
24+
lgr.Info(diff)
25+
}
26+
}

0 commit comments

Comments
 (0)