From dab81ac43aca68a08c172a8d2ae7724494e7656b Mon Sep 17 00:00:00 2001 From: Tamal Saha Date: Sat, 12 Oct 2024 01:47:24 -0800 Subject: [PATCH] Merge secret stringData into data fields (#4) Signed-off-by: Tamal Saha --- pkg/create.go | 5 +++++ pkg/patch.go | 11 ++++++++++ pkg/resources/secret.go | 45 +++++++++++++++++++++++++++++++++++++++++ pkg/update.go | 11 ++++++++++ 4 files changed, 72 insertions(+) create mode 100644 pkg/resources/secret.go diff --git a/pkg/create.go b/pkg/create.go index fccb1697..9a8757e2 100644 --- a/pkg/create.go +++ b/pkg/create.go @@ -84,6 +84,11 @@ func (s *Server) CreateImpl(store *APIStorage, codec runtime.Codec, r *http.Requ cm := resources.CreateKubeRootCACert() cm.SetNamespace(obj.GetName()) s.StoreForGVR(core.SchemeGroupVersion.WithResource("configmaps")).Insert(cm) + } else if store.GVK == core.SchemeGroupVersion.WithKind("Secret") { + err = resources.ProcessSecret(&obj) + if err != nil { + return nil, err + } } else if store.GVK == apiextensionsv1.SchemeGroupVersion.WithKind("CustomResourceDefinition") { err = resources.ProcessCRD(&obj) if err != nil { diff --git a/pkg/patch.go b/pkg/patch.go index 697329d8..73513c23 100644 --- a/pkg/patch.go +++ b/pkg/patch.go @@ -21,9 +21,12 @@ import ( "io" "net/http" + "kmodules.xyz/fake-apiserver/pkg/resources" + jsonpatch "github.com/evanphx/json-patch" "github.com/go-chi/chi/v5" httpw "go.wandrs.dev/http" + core "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -90,6 +93,14 @@ func (s *Server) PatchImpl(store *APIStorage, codec runtime.Codec, r *http.Reque } else { objToUpdate.SetNamespace("") } + + if store.GVK == core.SchemeGroupVersion.WithKind("Secret") { + err = resources.ProcessSecret(&objToUpdate) + if err != nil { + return nil, err + } + } + store.Insert(&objToUpdate) return &objToUpdate, nil diff --git a/pkg/resources/secret.go b/pkg/resources/secret.go new file mode 100644 index 00000000..450abd5d --- /dev/null +++ b/pkg/resources/secret.go @@ -0,0 +1,45 @@ +/* +Copyright AppsCode Inc. and Contributors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package resources + +import ( + core "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" +) + +func ProcessSecret(u *unstructured.Unstructured) error { + var obj core.Secret + err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), &obj) + if err != nil { + return err + } + + if len(obj.StringData) > 0 && len(obj.Data) == 0 { + obj.Data = map[string][]byte{} + } + for k, v := range obj.StringData { + obj.Data[k] = []byte(v) + } + + result, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&obj) + if err != nil { + return err + } + u.SetUnstructuredContent(result) + return nil +} diff --git a/pkg/update.go b/pkg/update.go index 92ce44c0..48454e41 100644 --- a/pkg/update.go +++ b/pkg/update.go @@ -20,8 +20,11 @@ import ( "io" "net/http" + "kmodules.xyz/fake-apiserver/pkg/resources" + "github.com/go-chi/chi/v5" httpw "go.wandrs.dev/http" + core "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" @@ -63,6 +66,14 @@ func (s *Server) UpdateImpl(store *APIStorage, codec runtime.Codec, r *http.Requ } else { obj.SetNamespace("") } + + if store.GVK == core.SchemeGroupVersion.WithKind("Secret") { + err = resources.ProcessSecret(&obj) + if err != nil { + return nil, err + } + } + store.Insert(&obj) return &obj, nil