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