Skip to content

Commit

Permalink
Merge secret stringData into data fields
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Oct 12, 2024
1 parent 3fe6a96 commit 96456df
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions pkg/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions pkg/resources/secret.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions pkg/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 96456df

Please sign in to comment.