added configmap resource implementation

This also changes the resource to be a concrete type instead of
interface.
This commit is contained in:
Sunil Arora 2018-02-21 19:57:48 -08:00
parent 8ae33d805c
commit 225f166de3
2 changed files with 103 additions and 5 deletions

View File

@ -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
}

View File

@ -16,10 +16,41 @@ limitations under the License.
package resource package resource
import "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" import (
"encoding/json"
// Examples: deployment, service, configmap, secret, etc. "k8s.io/apimachinery/pkg/api/meta"
type Resource interface { "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Name() string "k8s.io/apimachinery/pkg/runtime"
Data() unstructured.Unstructured "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
} }