Skip to content

Commit

Permalink
Merge pull request #125 from wlan0/master
Browse files Browse the repository at this point in the history
add storage class
  • Loading branch information
ublubu authored Dec 2, 2017
2 parents 1adf551 + 36c90b2 commit 284c80e
Show file tree
Hide file tree
Showing 20 changed files with 522 additions and 1 deletion.
42 changes: 42 additions & 0 deletions converter/converters/configmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package converters

import (
"k8s.io/api/core/v1"

"github.com/koki/short/types"
)

func Convert_Kube_v1_ConfigMap_to_Koki_ConfigMap(kubeConfigMap *v1.ConfigMap) (*types.ConfigMapWrapper, error) {
kokiConfigMapWrapper := &types.ConfigMapWrapper{}
kokiConfigMap := types.ConfigMap{}

kokiConfigMap.Name = kubeConfigMap.Name
kokiConfigMap.Namespace = kubeConfigMap.Namespace
kokiConfigMap.Version = kubeConfigMap.APIVersion
kokiConfigMap.Cluster = kubeConfigMap.ClusterName
kokiConfigMap.Labels = kubeConfigMap.Labels
kokiConfigMap.Annotations = kubeConfigMap.Annotations

kokiConfigMap.Data = kubeConfigMap.Data

kokiConfigMapWrapper.ConfigMap = kokiConfigMap

return kokiConfigMapWrapper, nil
}

func Convert_Koki_ConfigMap_to_Kube_v1_ConfigMap(kokiConfigMapWrapper *types.ConfigMapWrapper) (*v1.ConfigMap, error) {
kubeConfigMap := &v1.ConfigMap{}
kokiConfigMap := kokiConfigMapWrapper.ConfigMap

kubeConfigMap.Name = kokiConfigMap.Name
kubeConfigMap.Namespace = kokiConfigMap.Namespace
kubeConfigMap.APIVersion = kokiConfigMap.Version
kubeConfigMap.ClusterName = kokiConfigMap.Cluster
kubeConfigMap.Kind = "ConfigMap"
kubeConfigMap.Labels = kokiConfigMap.Labels
kubeConfigMap.Annotations = kokiConfigMap.Annotations

kubeConfigMap.Data = kokiConfigMap.Data

return kubeConfigMap, nil
}
66 changes: 66 additions & 0 deletions converter/converters/koki_storageclass_to_kube_storageclass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package converters

import (
storagev1 "k8s.io/api/storage/v1"
storagev1beta1 "k8s.io/api/storage/v1beta1"

"github.com/ghodss/yaml"

"github.com/koki/short/parser"
"github.com/koki/short/types"
"github.com/koki/short/util"
)

func Convert_Koki_StorageClass_to_Kube_StorageClass(storageClass *types.StorageClassWrapper) (interface{}, error) {
// Perform version-agnostic conversion into storage/v1 StorageClass.
kubeStorageClass := Convert_Koki_StorageClass_to_Kube_storage_v1_StorageClass(storageClass)

// Serialize the "generic" kube StorageClass.
b, err := yaml.Marshal(kubeStorageClass)
if err != nil {
return nil, util.InvalidValueErrorf(kubeStorageClass, "couldn't serialize 'generic' kube StorageClass: %s", err.Error())
}

// Deserialize a versioned kube StorageClass using its apiVersion.
versionedStorageClass, err := parser.ParseSingleKubeNativeFromBytes(b)
if err != nil {
return nil, err
}

switch versionedStorageClass := versionedStorageClass.(type) {
case *storagev1.StorageClass:
// Perform storage/v1beta1 initialization here.
case *storagev1beta1.StorageClass:
// Perform storage/v1beta1 initialization here.
default:
return nil, util.TypeErrorf(versionedStorageClass, "deserialized the manifest, but not as a supported kube StorageClass")
}

return versionedStorageClass, nil
}

func Convert_Koki_StorageClass_to_Kube_storage_v1_StorageClass(storageClass *types.StorageClassWrapper) *storagev1.StorageClass {
kubeStorageClass := &storagev1.StorageClass{}
kokiStorageClass := &storageClass.StorageClass

kubeStorageClass.Name = kokiStorageClass.Name
kubeStorageClass.Namespace = kokiStorageClass.Namespace
kubeStorageClass.APIVersion = kokiStorageClass.Version
kubeStorageClass.Kind = "StorageClass"
kubeStorageClass.ClusterName = kokiStorageClass.Cluster
kubeStorageClass.Labels = kokiStorageClass.Labels
kubeStorageClass.Annotations = kokiStorageClass.Annotations

kubeStorageClass.Provisioner = kokiStorageClass.Provisioner
kubeStorageClass.Parameters = kokiStorageClass.Parameters

kubeStorageClass.MountOptions = kokiStorageClass.MountOptions
kubeStorageClass.AllowVolumeExpansion = kokiStorageClass.AllowVolumeExpansion

if kokiStorageClass.Reclaim != nil {
reclaimPolicy := revertReclaimPolicy(*kokiStorageClass.Reclaim)
kubeStorageClass.ReclaimPolicy = &reclaimPolicy
}

return kubeStorageClass
}
72 changes: 72 additions & 0 deletions converter/converters/kube_storageclass_to_koki.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package converters

import (
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/runtime"

"github.com/ghodss/yaml"

"github.com/koki/short/parser"
"github.com/koki/short/types"
"github.com/koki/short/util"
)

func Convert_Kube_StorageClass_to_Koki_StorageClass(kubeStorageClass runtime.Object) (*types.StorageClassWrapper, error) {
groupVersionKind := kubeStorageClass.GetObjectKind().GroupVersionKind()
groupVersionString := groupVersionKind.GroupVersion().String()
groupVersionKind.Version = "v1"
groupVersionKind.Group = "storage.k8s.io"
kubeStorageClass.GetObjectKind().SetGroupVersionKind(groupVersionKind)

// Serialize as v1
b, err := yaml.Marshal(kubeStorageClass)
if err != nil {
return nil, util.InvalidInstanceErrorf(kubeStorageClass, "couldn't serialize kube StorageClass after setting apiVersion to storage/v1: %s", err.Error())
}

// Deserialize the "generic" kube StorageClass
genericStorageClass, err := parser.ParseSingleKubeNativeFromBytes(b)
if err != nil {
return nil, util.InvalidInstanceErrorf(string(b), "couldn't deserialize 'generic' kube StorageClass: %s", err.Error())
}

if genericStorageClass, ok := genericStorageClass.(*storagev1.StorageClass); ok {
kokiWrapper, err := Convert_Kube_storage_v1_StorageClass_to_Koki_StorageClass(genericStorageClass)
if err != nil {
return nil, err
}

kokiStorageClass := &kokiWrapper.StorageClass

kokiStorageClass.Version = groupVersionString

return kokiWrapper, nil
}

return nil, util.InvalidInstanceErrorf(genericStorageClass, "didn't deserialize 'generic' kube Deployment as storage/v1.StorageClass")
}

func Convert_Kube_storage_v1_StorageClass_to_Koki_StorageClass(kubeStorageClass *storagev1.StorageClass) (*types.StorageClassWrapper, error) {
kokiStorageClass := &types.StorageClass{}

kokiStorageClass.Name = kubeStorageClass.Name
kokiStorageClass.Namespace = kubeStorageClass.Namespace
kokiStorageClass.Version = kubeStorageClass.APIVersion
kokiStorageClass.Cluster = kubeStorageClass.ClusterName
kokiStorageClass.Labels = kubeStorageClass.Labels
kokiStorageClass.Annotations = kubeStorageClass.Annotations

kokiStorageClass.Provisioner = kubeStorageClass.Provisioner
kokiStorageClass.Parameters = kubeStorageClass.Parameters
kokiStorageClass.MountOptions = kubeStorageClass.MountOptions
kokiStorageClass.AllowVolumeExpansion = kubeStorageClass.AllowVolumeExpansion

if kubeStorageClass.ReclaimPolicy != nil {
reclaimPolicy := convertReclaimPolicy(*kubeStorageClass.ReclaimPolicy)
kokiStorageClass.Reclaim = &reclaimPolicy
}

return &types.StorageClassWrapper{
StorageClass: *kokiStorageClass,
}, nil
}
103 changes: 103 additions & 0 deletions converter/converters/secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package converters

import (
"k8s.io/api/core/v1"

"github.com/koki/short/types"
"github.com/koki/short/util"
)

func Convert_Kube_v1_Secret_to_Koki_Secret(kubeSecret *v1.Secret) (*types.SecretWrapper, error) {
kokiSecretWrapper := &types.SecretWrapper{}
kokiSecret := types.Secret{}

kokiSecret.Name = kubeSecret.Name
kokiSecret.Namespace = kubeSecret.Namespace
kokiSecret.Version = kubeSecret.APIVersion
kokiSecret.Cluster = kubeSecret.ClusterName
kokiSecret.Labels = kubeSecret.Labels
kokiSecret.Annotations = kubeSecret.Annotations
kokiSecret.Data = kubeSecret.Data
kokiSecret.StringData = kubeSecret.StringData

t, err := convertSecretType(kubeSecret.Type)
if err != nil {
return nil, err
}
kokiSecret.SecretType = t

kokiSecretWrapper.Secret = kokiSecret
return kokiSecretWrapper, nil
}

func convertSecretType(secret v1.SecretType) (types.SecretType, error) {
if secret == "" {
return "", nil
}
switch secret {
case v1.SecretTypeOpaque:
return types.SecretTypeOpaque, nil
case v1.SecretTypeServiceAccountToken:
return types.SecretTypeServiceAccountToken, nil
case v1.SecretTypeDockercfg:
return types.SecretTypeDockercfg, nil
case v1.SecretTypeDockerConfigJson:
return types.SecretTypeDockerConfigJson, nil
case v1.SecretTypeBasicAuth:
return types.SecretTypeBasicAuth, nil
case v1.SecretTypeSSHAuth:
return types.SecretTypeSSHAuth, nil
case v1.SecretTypeTLS:
return types.SecretTypeTLS, nil
default:
return "", util.InvalidValueErrorf(secret, "unrecognized Secret type")
}
}

func Convert_Koki_Secret_to_Kube_v1_Secret(kokiSecretWrapper *types.SecretWrapper) (*v1.Secret, error) {
kubeSecret := &v1.Secret{}
kokiSecret := kokiSecretWrapper.Secret

kubeSecret.Name = kokiSecret.Name
kubeSecret.Namespace = kokiSecret.Namespace
kubeSecret.APIVersion = kokiSecret.Version
kubeSecret.ClusterName = kokiSecret.Cluster
kubeSecret.Kind = "Secret"
kubeSecret.Labels = kokiSecret.Labels
kubeSecret.Annotations = kokiSecret.Annotations
kubeSecret.Data = kokiSecret.Data
kubeSecret.StringData = kubeSecret.StringData

t, err := revertSecretType(kokiSecret.SecretType)
if err != nil {
return nil, err
}
kubeSecret.Type = t

return kubeSecret, nil
}

func revertSecretType(secret types.SecretType) (v1.SecretType, error) {
if secret == "" {
return "", nil
}

switch secret {
case types.SecretTypeOpaque:
return v1.SecretTypeOpaque, nil
case types.SecretTypeServiceAccountToken:
return v1.SecretTypeServiceAccountToken, nil
case types.SecretTypeDockercfg:
return v1.SecretTypeDockercfg, nil
case types.SecretTypeDockerConfigJson:
return v1.SecretTypeDockerConfigJson, nil
case types.SecretTypeBasicAuth:
return v1.SecretTypeBasicAuth, nil
case types.SecretTypeSSHAuth:
return v1.SecretTypeSSHAuth, nil
case types.SecretTypeTLS:
return v1.SecretTypeTLS, nil
default:
return "", util.InvalidValueErrorf(secret, "unrecognized Secret type")
}
}
16 changes: 16 additions & 0 deletions converter/koki_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ import (
batchv2alpha1 "k8s.io/api/batch/v2alpha1"
"k8s.io/api/core/v1"
exts "k8s.io/api/extensions/v1beta1"
storagev1 "k8s.io/api/storage/v1"
storagev1beta1 "k8s.io/api/storage/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
)

func DetectAndConvertFromKokiObj(kokiObj interface{}) (interface{}, error) {
switch kokiObj := kokiObj.(type) {
case *types.SecretWrapper:
return converters.Convert_Koki_Secret_to_Kube_v1_Secret(kokiObj)
case *types.ConfigMapWrapper:
return converters.Convert_Koki_ConfigMap_to_Kube_v1_ConfigMap(kokiObj)
case *types.StorageClassWrapper:
return converters.Convert_Koki_StorageClass_to_Kube_StorageClass(kokiObj)
case *types.StatefulSetWrapper:
return converters.Convert_Koki_StatefulSet_to_Kube_StatefulSet(kokiObj)
case *types.PersistentVolumeClaimWrapper:
Expand Down Expand Up @@ -48,6 +56,14 @@ func DetectAndConvertFromKokiObj(kokiObj interface{}) (interface{}, error) {

func DetectAndConvertFromKubeObj(kubeObj runtime.Object) (interface{}, error) {
switch kubeObj := kubeObj.(type) {
case *v1.Secret:
return converters.Convert_Kube_v1_Secret_to_Koki_Secret(kubeObj)
case *v1.ConfigMap:
return converters.Convert_Kube_v1_ConfigMap_to_Koki_ConfigMap(kubeObj)
case *storagev1.StorageClass:
return converters.Convert_Kube_StorageClass_to_Koki_StorageClass(kubeObj)
case *storagev1beta1.StorageClass:
return converters.Convert_Kube_StorageClass_to_Koki_StorageClass(kubeObj)
case *appsv1beta1.StatefulSet:
return converters.Convert_Kube_StatefulSet_to_Koki_StatefulSet(kubeObj)
case *appsv1beta2.StatefulSet:
Expand Down
22 changes: 21 additions & 1 deletion parser/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,30 @@ func ParseKokiNativeObject(obj interface{}) (interface{}, error) {
return nil, util.InvalidValueForTypeErrorf(objMap, statefulSet, err.Error())
}
return statefulSet, nil
case "storage_class":
storageClass := &types.StorageClassWrapper{}
err := json.Unmarshal(bytes, storageClass)
if err != nil {
return nil, util.InvalidValueForTypeErrorf(objMap, storageClass, err.Error())
}
return storageClass, nil
case "config_map":
configMap := &types.ConfigMapWrapper{}
err := json.Unmarshal(bytes, configMap)
if err != nil {
return nil, util.InvalidValueForTypeErrorf(objMap, configMap, err.Error())
}
return configMap, nil
case "secret":
secret := &types.SecretWrapper{}
err := json.Unmarshal(bytes, secret)
if err != nil {
return nil, util.InvalidValueForTypeErrorf(objMap, secret, err.Error())
}
return secret, nil
}
return nil, util.TypeErrorf(objMap, "Unexpected key (%s)", k)
}

return nil, nil
}

Expand Down
18 changes: 18 additions & 0 deletions testdata/config_maps/config_map.short.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
config_map:
data:
game.properties: |
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
name: game-config
namespace: default
version: v1
23 changes: 23 additions & 0 deletions testdata/config_maps/config_map.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: v1
data:
game.properties: |
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
creationTimestamp: 2016-02-18T18:52:05Z
name: game-config
namespace: default
resourceVersion: "516"
selfLink: /api/v1/namespaces/default/configmaps/game-config-2
uid: b4952dc3-d670-11e5-8cd0-68f728db1985
Loading

0 comments on commit 284c80e

Please sign in to comment.