Skip to content

Commit

Permalink
Merge pull request #184 from wlan0/master
Browse files Browse the repository at this point in the history
add support for binding and namespace
  • Loading branch information
ublubu authored Dec 27, 2017
2 parents 883739d + 2704689 commit 07e2c1a
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 2 deletions.
34 changes: 34 additions & 0 deletions converter/converters/koki_binding_to_kube_binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package converters

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

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

func Convert_Koki_Binding_to_Kube_Binding(kokiWrapper *types.BindingWrapper) (*v1.Binding, error) {
var err error
kubeBinding := &v1.Binding{}
kokiBinding := kokiWrapper.Binding

kubeBinding.Name = kokiBinding.Name
kubeBinding.Namespace = kokiBinding.Namespace
if len(kokiBinding.Version) == 0 {
kubeBinding.APIVersion = "v1"
} else {
kubeBinding.APIVersion = kokiBinding.Version
}
kubeBinding.Kind = "Binding"
kubeBinding.ClusterName = kokiBinding.Cluster
kubeBinding.Labels = kokiBinding.Labels
kubeBinding.Annotations = kokiBinding.Annotations

target, err := revertTarget(&kokiBinding.Target)
if err != nil {
return nil, serrors.ContextualizeErrorf(err, "binding target")
}
kubeBinding.Target = *target

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

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

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

func Convert_Koki_Namespace_to_Kube_Namespace(kokiWrapper *types.NamespaceWrapper) (*v1.Namespace, error) {
var err error
kubeNamespace := &v1.Namespace{}
kokiNamespace := kokiWrapper.Namespace

kubeNamespace.Name = kokiNamespace.Name
kubeNamespace.Namespace = kokiNamespace.Namespace
if len(kokiNamespace.Version) == 0 {
kubeNamespace.APIVersion = "v1"
} else {
kubeNamespace.APIVersion = kokiNamespace.Version
}
kubeNamespace.Kind = "Namespace"
kubeNamespace.ClusterName = kokiNamespace.Cluster
kubeNamespace.Labels = kokiNamespace.Labels
kubeNamespace.Annotations = kokiNamespace.Annotations

spec, err := revertNamespaceSpec(kokiNamespace)
if err != nil {
return nil, serrors.ContextualizeErrorf(err, "Namespace spec")
}
kubeNamespace.Spec = spec

status, err := revertNamespaceStatus(kokiNamespace)
if err != nil {
return nil, serrors.ContextualizeErrorf(err, "Namespace status")
}
kubeNamespace.Status = status

return kubeNamespace, nil
}

func revertNamespaceStatus(kokiNamespace types.Namespace) (v1.NamespaceStatus, error) {
var kubeStatus v1.NamespaceStatus

if kokiNamespace.Phase == "" {
return kubeStatus, nil
}

var phase v1.NamespacePhase
switch kokiNamespace.Phase {
case types.NamespaceActive:
phase = v1.NamespaceActive
case types.NamespaceTerminating:
phase = v1.NamespaceTerminating
default:
return kubeStatus, serrors.InvalidValueErrorf(kokiNamespace.Phase, "Invalid namespace phase")
}
kubeStatus.Phase = phase

return kubeStatus, nil
}

func revertNamespaceSpec(kokiNamespace types.Namespace) (v1.NamespaceSpec, error) {
var kubeSpec v1.NamespaceSpec
var kubeFinalizers []v1.FinalizerName

for i := range kokiNamespace.Finalizers {
kokiFinalizer := kokiNamespace.Finalizers[i]

kubeFinalizer, err := revertFinalizer(kokiFinalizer)
if err != nil {
return kubeSpec, err
}

kubeFinalizers = append(kubeFinalizers, kubeFinalizer)
}
kubeSpec.Finalizers = kubeFinalizers

return kubeSpec, nil
}

func revertFinalizer(kokiFinalizer types.FinalizerName) (v1.FinalizerName, error) {
if kokiFinalizer == "" {
return "", nil
}

switch kokiFinalizer {
case types.FinalizerKubernetes:
return v1.FinalizerKubernetes, nil
}

return "", serrors.InvalidValueErrorf(kokiFinalizer, "unrecognized value")
}
28 changes: 28 additions & 0 deletions converter/converters/kube_binding_to_koki_binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package converters

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

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

func Convert_Kube_Binding_to_Koki_Binding(kubeBinding *v1.Binding) (*types.BindingWrapper, error) {
kokiWrapper := &types.BindingWrapper{}
kokiBinding := &kokiWrapper.Binding

kokiBinding.Name = kubeBinding.Name
kokiBinding.Namespace = kubeBinding.Namespace
kokiBinding.Version = kubeBinding.APIVersion
kokiBinding.Cluster = kubeBinding.ClusterName
kokiBinding.Labels = kubeBinding.Labels
kokiBinding.Annotations = kubeBinding.Annotations

target, err := convertTarget(&kubeBinding.Target)
if err != nil {
return nil, serrors.ContextualizeErrorf(err, "Binding Target")
}
kokiBinding.Target = *target

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

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

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

func Convert_Kube_Namespace_to_Koki_Namespace(kubeNamespace *v1.Namespace) (*types.NamespaceWrapper, error) {
kokiWrapper := &types.NamespaceWrapper{}
kokiNamespace := &kokiWrapper.Namespace

kokiNamespace.Name = kubeNamespace.Name
kokiNamespace.Namespace = kubeNamespace.Namespace
kokiNamespace.Version = kubeNamespace.APIVersion
kokiNamespace.Cluster = kubeNamespace.ClusterName
kokiNamespace.Labels = kubeNamespace.Labels
kokiNamespace.Annotations = kubeNamespace.Annotations

finalizers, err := convertNamespaceSpec(kubeNamespace.Spec)
if err != nil {
return nil, err
}
kokiNamespace.Finalizers = finalizers

phase, err := convertNamespaceStatus(kubeNamespace.Status)
if err != nil {
return nil, err
}
kokiNamespace.Phase = phase

return kokiWrapper, nil
}

func convertNamespaceSpec(kubeSpec v1.NamespaceSpec) ([]types.FinalizerName, error) {
var kokiFinalizers []types.FinalizerName

for i := range kubeSpec.Finalizers {
kubeFinalizer := kubeSpec.Finalizers[i]

var kokiFinalizer types.FinalizerName
switch kubeFinalizer {
case v1.FinalizerKubernetes:
kokiFinalizer = types.FinalizerKubernetes
default:
return nil, serrors.InvalidValueErrorf(kubeFinalizer, "unrecognized finalizer")
}

kokiFinalizers = append(kokiFinalizers, kokiFinalizer)
}

return kokiFinalizers, nil
}

func convertNamespaceStatus(kubeStatus v1.NamespaceStatus) (types.NamespacePhase, error) {
switch kubeStatus.Phase {
case v1.NamespaceActive:
return types.NamespaceActive, nil
case v1.NamespaceTerminating:
return types.NamespaceTerminating, nil
case "":
return "", nil
}

return "", serrors.InvalidValueErrorf(kubeStatus.Phase, "invalid phase")
}
8 changes: 8 additions & 0 deletions converter/koki_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func DetectAndConvertFromKokiObj(kokiObj interface{}) (interface{}, error) {
switch kokiObj := kokiObj.(type) {
case *types.APIServiceWrapper:
return converters.Convert_Koki_APIService_to_Kube_APIService(kokiObj)
case *types.BindingWrapper:
return converters.Convert_Koki_Binding_to_Kube_Binding(kokiObj)
case *types.ConfigMapWrapper:
return converters.Convert_Koki_ConfigMap_to_Kube_v1_ConfigMap(kokiObj)
case *types.ControllerRevisionWrapper:
Expand All @@ -51,6 +53,8 @@ func DetectAndConvertFromKokiObj(kokiObj interface{}) (interface{}, error) {
return converters.Convert_Koki_InitializerConfig_to_Kube_InitializerConfig(kokiObj)
case *types.JobWrapper:
return converters.Convert_Koki_Job_to_Kube_Job(kokiObj)
case *types.NamespaceWrapper:
return converters.Convert_Koki_Namespace_to_Kube_Namespace(kokiObj)
case *types.PersistentVolumeClaimWrapper:
return converters.Convert_Koki_PVC_to_Kube_PVC(kokiObj)
case *types.PersistentVolumeWrapper:
Expand Down Expand Up @@ -88,6 +92,8 @@ func DetectAndConvertFromKubeObj(kubeObj runtime.Object) (interface{}, error) {
switch kubeObj := kubeObj.(type) {
case *apiregistrationv1beta1.APIService:
return converters.Convert_Kube_APIService_to_Koki_APIService(kubeObj)
case *v1.Binding:
return converters.Convert_Kube_Binding_to_Koki_Binding(kubeObj)
case *v1.ConfigMap:
return converters.Convert_Kube_v1_ConfigMap_to_Koki_ConfigMap(kubeObj)
case *apps.ControllerRevision, *appsv1beta1.ControllerRevision, *appsv1beta2.ControllerRevision:
Expand All @@ -110,6 +116,8 @@ func DetectAndConvertFromKubeObj(kubeObj runtime.Object) (interface{}, error) {
return converters.Convert_Kube_InitializerConfig_to_Koki_InitializerConfig(kubeObj)
case *batchv1.Job:
return converters.Convert_Kube_Job_to_Koki_Job(kubeObj)
case *v1.Namespace:
return converters.Convert_Kube_Namespace_to_Koki_Namespace(kubeObj)
case *v1.PersistentVolume:
return converters.Convert_Kube_v1_PersistentVolume_to_Koki_PersistentVolume(kubeObj)
case *v1.PersistentVolumeClaim:
Expand Down
14 changes: 14 additions & 0 deletions parser/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func ParseKokiNativeObject(obj interface{}) (interface{}, error) {
return nil, serrors.InvalidValueForTypeContextError(err, objMap, apiService)
}
return apiService, nil
case "binding":
binding := &types.BindingWrapper{}
err := json.Unmarshal(bytes, binding)
if err != nil {
return nil, serrors.InvalidValueForTypeContextError(err, objMap, binding)
}
return binding, nil
case "config_map":
configMap := &types.ConfigMapWrapper{}
err := json.Unmarshal(bytes, configMap)
Expand Down Expand Up @@ -109,6 +116,13 @@ func ParseKokiNativeObject(obj interface{}) (interface{}, error) {
return nil, serrors.InvalidValueForTypeContextError(err, objMap, job)
}
return job, nil
case "namespace":
namespace := &types.NamespaceWrapper{}
err := json.Unmarshal(bytes, namespace)
if err != nil {
return nil, serrors.InvalidValueForTypeContextError(err, objMap, namespace)
}
return namespace, nil
case "pdb":
pdb := &types.PodDisruptionBudgetWrapper{}
err := json.Unmarshal(bytes, pdb)
Expand Down
16 changes: 16 additions & 0 deletions types/binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package types

type BindingWrapper struct {
Binding `json:"binding"`
}

type Binding struct {
Version string `json:"version,omitempty"`
Cluster string `json:"cluster,omitempty"`
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`

Target ObjectReference `json:"target"`
}
4 changes: 2 additions & 2 deletions types/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Event struct {
Annotations map[string]string `json:"annotations,omitempty"`

// The object that this event is about.
InvolvedObject ObjectReference `json:"involved" protobuf:"bytes,2,opt,name=involvedObject"`
InvolvedObject ObjectReference `json:"involved"`

Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
Expand All @@ -33,7 +33,7 @@ type Event struct {
Count int32 `json:"count,omitempty"`
Type string `json:"type,omitempty"`
// first observed
EventTime metav1.MicroTime `json:"first_observed,omitempty" protobuf:"bytes,10,opt,name=eventTime"`
EventTime metav1.MicroTime `json:"first_observed,omitempty"`

// Series::*EventSeries
// Number of occurrences in this series up to the last heartbeat time
Expand Down
31 changes: 31 additions & 0 deletions types/namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package types

type NamespaceWrapper struct {
Namespace `json:"namespace"`
}

type Namespace struct {
Version string `json:"version,omitempty"`
Cluster string `json:"cluster,omitempty"`
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`

Finalizers []FinalizerName `json:"finalizers,omitempty"`

Phase NamespacePhase `json:"phase,omitempty"`
}

type NamespacePhase string

const (
NamespaceActive NamespacePhase = "Active"
NamespaceTerminating NamespacePhase = "Terminating"
)

type FinalizerName string

const (
FinalizerKubernetes FinalizerName = "kubernetes"
)

0 comments on commit 07e2c1a

Please sign in to comment.