Skip to content

Commit 96456df

Browse files
committed
Merge secret stringData into data fields
Signed-off-by: Tamal Saha <[email protected]>
1 parent 3fe6a96 commit 96456df

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

pkg/create.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ func (s *Server) CreateImpl(store *APIStorage, codec runtime.Codec, r *http.Requ
8484
cm := resources.CreateKubeRootCACert()
8585
cm.SetNamespace(obj.GetName())
8686
s.StoreForGVR(core.SchemeGroupVersion.WithResource("configmaps")).Insert(cm)
87+
} else if store.GVK == core.SchemeGroupVersion.WithKind("Secret") {
88+
err = resources.ProcessSecret(&obj)
89+
if err != nil {
90+
return nil, err
91+
}
8792
} else if store.GVK == apiextensionsv1.SchemeGroupVersion.WithKind("CustomResourceDefinition") {
8893
err = resources.ProcessCRD(&obj)
8994
if err != nil {

pkg/patch.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ import (
2121
"io"
2222
"net/http"
2323

24+
"kmodules.xyz/fake-apiserver/pkg/resources"
25+
2426
jsonpatch "github.com/evanphx/json-patch"
2527
"github.com/go-chi/chi/v5"
2628
httpw "go.wandrs.dev/http"
29+
core "k8s.io/api/core/v1"
2730
apierrors "k8s.io/apimachinery/pkg/api/errors"
2831
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2932
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -90,6 +93,14 @@ func (s *Server) PatchImpl(store *APIStorage, codec runtime.Codec, r *http.Reque
9093
} else {
9194
objToUpdate.SetNamespace("")
9295
}
96+
97+
if store.GVK == core.SchemeGroupVersion.WithKind("Secret") {
98+
err = resources.ProcessSecret(&objToUpdate)
99+
if err != nil {
100+
return nil, err
101+
}
102+
}
103+
93104
store.Insert(&objToUpdate)
94105

95106
return &objToUpdate, nil

pkg/resources/secret.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright AppsCode Inc. and Contributors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package resources
18+
19+
import (
20+
core "k8s.io/api/core/v1"
21+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
22+
"k8s.io/apimachinery/pkg/runtime"
23+
)
24+
25+
func ProcessSecret(u *unstructured.Unstructured) error {
26+
var obj core.Secret
27+
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), &obj)
28+
if err != nil {
29+
return err
30+
}
31+
32+
if len(obj.StringData) > 0 && len(obj.Data) == 0 {
33+
obj.Data = map[string][]byte{}
34+
}
35+
for k, v := range obj.StringData {
36+
obj.Data[k] = []byte(v)
37+
}
38+
39+
result, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&obj)
40+
if err != nil {
41+
return err
42+
}
43+
u.SetUnstructuredContent(result)
44+
return nil
45+
}

pkg/update.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ import (
2020
"io"
2121
"net/http"
2222

23+
"kmodules.xyz/fake-apiserver/pkg/resources"
24+
2325
"github.com/go-chi/chi/v5"
2426
httpw "go.wandrs.dev/http"
27+
core "k8s.io/api/core/v1"
2528
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2629
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2730
"k8s.io/apimachinery/pkg/runtime"
@@ -63,6 +66,14 @@ func (s *Server) UpdateImpl(store *APIStorage, codec runtime.Codec, r *http.Requ
6366
} else {
6467
obj.SetNamespace("")
6568
}
69+
70+
if store.GVK == core.SchemeGroupVersion.WithKind("Secret") {
71+
err = resources.ProcessSecret(&obj)
72+
if err != nil {
73+
return nil, err
74+
}
75+
}
76+
6677
store.Insert(&obj)
6778

6879
return &obj, nil

0 commit comments

Comments
 (0)