From 225f166de34a3d34f0a09f76aba9d314d272ee6d Mon Sep 17 00:00:00 2001 From: Sunil Arora Date: Wed, 21 Feb 2018 19:57:48 -0800 Subject: [PATCH] added configmap resource implementation This also changes the resource to be a concrete type instead of interface. --- pkg/kinflate/resource/configmap.go | 67 ++++++++++++++++++++++++++++++ pkg/kinflate/resource/resource.go | 41 +++++++++++++++--- 2 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 pkg/kinflate/resource/configmap.go diff --git a/pkg/kinflate/resource/configmap.go b/pkg/kinflate/resource/configmap.go new file mode 100644 index 000000000..cf77864ff --- /dev/null +++ b/pkg/kinflate/resource/configmap.go @@ -0,0 +1,67 @@ +/* +Copyright 2018 The Kubernetes Authors. + +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 resource + +import ( + corev1 "k8s.io/api/core/v1" + manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1" + cutil "k8s.io/kubectl/pkg/kinflate/configmapandsecret/util" +) + +// (Note): pass in loader which has rootPath context and knows how to load +// files given a relative path. +// NewFromConfigMap returns a Resource given a configmap metadata from manifest +// file. +func NewFromConfigMap(cm manifest.ConfigMap) (*Resource, error) { + corev1CM, err := makeConfigMap(cm) + if err != nil { + return nil, err + } + + data, err := objectToUnstructured(corev1CM) + if err != nil { + return nil, err + } + return &Resource{Data: data}, nil +} + +func makeConfigMap(cm manifest.ConfigMap) (*corev1.ConfigMap, error) { + corev1cm := &corev1.ConfigMap{} + corev1cm.APIVersion = "v1" + corev1cm.Kind = "ConfigMap" + corev1cm.Name = cm.Name + corev1cm.Data = map[string]string{} + + // TODO: move the configmap helpers functions in this file/package + if cm.EnvSource != "" { + if err := cutil.HandleConfigMapFromEnvFileSource(corev1cm, cm.EnvSource); err != nil { + return nil, err + } + } + if cm.FileSources != nil { + if err := cutil.HandleConfigMapFromFileSources(corev1cm, cm.FileSources); err != nil { + return nil, err + } + } + if cm.LiteralSources != nil { + if err := cutil.HandleConfigMapFromLiteralSources(corev1cm, cm.LiteralSources); err != nil { + return nil, err + } + } + + return corev1cm, nil +} diff --git a/pkg/kinflate/resource/resource.go b/pkg/kinflate/resource/resource.go index 0b892bee7..13fc195f3 100644 --- a/pkg/kinflate/resource/resource.go +++ b/pkg/kinflate/resource/resource.go @@ -16,10 +16,41 @@ limitations under the License. package resource -import "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" +import ( + "encoding/json" -// Examples: deployment, service, configmap, secret, etc. -type Resource interface { - Name() string - Data() unstructured.Unstructured + "k8s.io/apimachinery/pkg/api/meta" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/kubectl/pkg/kinflate/types" +) + +// Resource represents a Kubernetes Resource Object for ex. Deployment, Server +// ConfigMap etc. +type Resource struct { + Data *unstructured.Unstructured +} + +// GVKN returns Group/Version/Kind/Name for the resource. +func (r *Resource) GVKN() types.GroupVersionKindName { + var emptyZVKN types.GroupVersionKindName + if r.Data == nil { + return emptyZVKN + } + accessor, err := meta.Accessor(r.Data) + if err != nil { + return emptyZVKN + } + gvk := r.Data.GetObjectKind().GroupVersionKind() + return types.GroupVersionKindName{GVK: gvk, Name: accessor.GetName()} +} + +func objectToUnstructured(in runtime.Object) (*unstructured.Unstructured, error) { + marshaled, err := json.Marshal(in) + if err != nil { + return nil, err + } + var out unstructured.Unstructured + err = out.UnmarshalJSON(marshaled) + return &out, err }